Пример #1
0
 def test_parse_file(self):
     cli = TestCLI(options=[("foo", Option("-o", Resource()))])
     test_file_path = get_test_file_path(
         "awwparse.testsuite.positionals.ResourceTestCase.test_parse_file"
     )
     with file_cleaner([test_file_path]):
         with open(test_file_path, "wb") as f:
             f.write(b"foobar")
         opener = cli.run(["-o", "file://" + test_file_path])[1]["foo"]
         with opener as f:
             self.assert_equal(f.read(), "foobar")
Пример #2
0
    def test_parse(self):
        test_file_path = get_test_file_path(
            "awwparse.testsuite.positionals.FileTestCase.test_parse"
        )
        with file_cleaner([test_file_path]):
            cli = TestCLI(options=[("foo", Option("-o", File(mode="wb")))])
            opener = cli.run(["-o", test_file_path])[1]["foo"]
            with opener as file:
                file.write(b"foobar")

            cli = TestCLI(options=[("foo", Option("-o", File(mode="rb")))])
            opener = cli.run(["-o", test_file_path])[1]["foo"]
            with opener as file:
                self.assert_equal(file.read(), b"foobar")
Пример #3
0
    def test_parse(self):
        stdin = StringIO("foobar")
        cli = TestCLI(
            options=[("foo", Option("-o", LocalResource()))],
            stdin=stdin
        )
        opener = cli.run(["-o", "-"])[1]["foo"]
        with opener as file:
            self.assert_equal(file.read(), "foobar")

        stdout = BytesIO()
        cli = TestCLI(
            options=[
                ("foo", Option("-o", LocalResource(mode="w")))
            ],
            stdout=stdout
        )
        opener = cli.run(["-o", "-"])[1]["foo"]
        with opener as file:
            file.write(b"foobar")
        self.assert_equal(stdout.getvalue(), b"foobar")

        stdout = BytesIO()
        cli = TestCLI(
            options=[
                ("foo", Option("-o", LocalResource(mode="w", encoding="utf-8")))
            ],
            stdout=stdout
        )
        opener = cli.run(["-o", "-"])[1]["foo"]
        with opener as file:
            file.write(u("äöü"))
        self.assert_equal(stdout.getvalue(), u("äöü").encode("utf-8"))

        with self.assert_raises(ValueError):
            LocalResource(mode="r+")
        # check that it doesn't raise
        LocalResource(mode="r+", allow_std_streams=False)

        test_file_path = get_test_file_path(
            "awwparse.testsuite.positionals.LocalResourceTestCase.test_parse"
        )
        with file_cleaner([test_file_path]):
            cli = TestCLI(
                options=[
                    ("foo", Option(
                        "-o", LocalResource(mode="wb", allow_std_streams=False))
                    )
                ]
            )
            opener = cli.run(["-o", test_file_path])[1]["foo"]
            with opener as file:
                file.write(b"foobar")

            cli = TestCLI(
                options=[
                    ("foo", Option(
                        "-o", LocalResource(mode="rb", allow_std_streams=False))
                    )
                ]
            )
            opener = cli.run(["-o", test_file_path])[1]["foo"]
            with opener as file:
                self.assert_equal(file.read(), b"foobar")

            cli = TestCLI(
                options=[
                    ("foo", Option(
                        "-o", LocalResource(mode="rb", allow_std_streams=False)
                    ))
                ]
            )
            opener = cli.run(["-o", "file://" + test_file_path])[1]["foo"]
            with opener as file:
                self.assert_equal(file.read(), b"foobar")