def test_exit(self):
     pipeline = [Exit("exit", [])]
     stdin, stdout, stderr = os.pipe(), os.pipe(), os.pipe()
     with open(stdin[0],
               "r") as sin, open(stdout[1],
                                 "w") as sout, open(stderr[1], "w") as serr:
         sh = Shell(sin, sout, serr, {}, None)
         with self.assertRaises(SystemExit):
             sh._execute(pipeline)
 def test_echo(self):
     hello = "Hello, world!"
     pipeline = [Echo("echo", [hello])]
     stdin, stdout, stderr = os.pipe(), os.pipe(), os.pipe()
     with open(stdin[0],
               "r") as sin, open(stdout[1],
                                 "w") as sout, open(stderr[1], "w") as serr:
         sh = Shell(sin, sout, serr, {}, None)
         sh._execute(pipeline)
     with open(stdout[0], "r") as res:
         self.assertEqual(res.read(), hello + "\n")
 def test_eq(self):
     pipeline = [Eq("=", ["a", "b"])]
     stdin, stdout, stderr = os.pipe(), os.pipe(), os.pipe()
     with open(stdin[0],
               "r") as sin, open(stdout[1],
                                 "w") as sout, open(stderr[1], "w") as serr:
         env = {}
         sh = Shell(sin, sout, serr, env, None)
         sh._execute(pipeline)
         self.assertIn("a", env)
         self.assertEqual("b", env["a"])
    def test_pwd(self):
        pwd = os.getcwd()
        pipeline = [Pwd("pwd", [])]
        stdin, stdout, stderr = os.pipe(), os.pipe(), os.pipe()

        with open(stdin[0],
                  "r") as sin, open(stdout[1],
                                    "w") as sout, open(stderr[1], "w") as serr:
            sh = Shell(sin, sout, serr, {"PWD": pwd}, None)
            sh._execute(pipeline)

        with open(stdout[0], "r") as res:
            self.assertEqual(pwd, res.read())
    def test_wc(self):
        testfile = "./tests/test.txt"
        content = "1 5 21"
        pipeline = [Wc("wc", [testfile])]
        stdin, stdout, stderr = os.pipe(), os.pipe(), os.pipe()

        with open(stdin[0],
                  "r") as sin, open(stdout[1],
                                    "w") as sout, open(stderr[1], "w") as serr:
            sh = Shell(sin, sout, serr, {}, None)
            sh._execute(pipeline)

        with open(stdout[0], "r") as res:
            self.assertEqual(content, res.read())
    def test_system(self):
        hello = "Hello, world!\n"
        pipeline = [Command("./tests/test.sh", [])]
        stdin, stdout, stderr = os.pipe(), os.pipe(), os.pipe()
        with open(stdin[1], "w") as sin:
            sin.write(hello)

        with open(stdin[0],
                  "r") as sin, open(stdout[1],
                                    "w") as sout, open(stderr[1], "w") as serr:
            sh = Shell(sin, sout, serr, {}, None)
            sh._execute(pipeline)
        with open(stdout[0], "r") as res:
            self.assertEqual(res.read(), hello)
    def test_pipes(self):
        hello = "hello world"

        pipe1, pipe2 = os.pipe(), os.pipe()
        err1, err2 = os.pipe(), os.pipe()
        pipeline = [
            Echo("echo", hello.split(" "), outfd=pipe1[1], errfd=err1[1]),
            Cat("cat", [], infd=pipe1[0], outfd=pipe2[1], errfd=err2[1]),
            Cat("cat", [], infd=pipe2[0]),
        ]
        stdin, stdout, stderr = os.pipe(), os.pipe(), os.pipe()
        with open(stdin[0],
                  "r") as sin, open(stdout[1],
                                    "w") as sout, open(stderr[1], "w") as serr:
            sh = Shell(sin, sout, serr, {}, None)
            sh._execute(pipeline)
        with open(stdout[0], "r") as res:
            self.assertEqual(hello + "\n", res.read())