Example #1
0
 def test_parse_standard_stream(self):
     stdin = BytesIO(b"foobar")
     cli = TestCLI(
         options=[("foo", Option("-o", Resource()))],
         stdin=stdin
     )
     opener = cli.run(["-o", "-"])[1]["foo"]
     with opener as stream:
         self.assert_equal(stream.read(), b"foobar")
Example #2
0
 def test_parse_http(self):
     cli = TestCLI(options=[("foo", Option("-o", Resource()))])
     opener = cli.run(["-o", "http://httpbin.org/user-agent"])[1]["foo"]
     try:
         with opener as response:
             self.assert_equal(
                 json.loads(response.content),
                 {u("user-agent"): u("Awwparse/0.1-dev")}
             )
     except requests.ConnectionError:
         self.skip_test("missing internet connection")
Example #3
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")
Example #4
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")
Example #5
0
 def test_parse_http_fails(self):
     cli = TestCLI(options=[("foo", Option("-o", Resource()))])
     with self.assert_raises(RuntimeError):
         cli.run(["-o", "http://httpbin.org"])
Example #6
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")
Example #7
0
    def test_error_handling(self):
        stringio = StringIO()
        def exit(code):
            assert code != 1
        cli = TestCLI(
            application_name=u("app"),
            stdout=stringio,
            stderr=stringio,
            exit=exit,
            width=40,
            options=[("foo", Option("-o", Integer()))]
        )
        with self.assert_raises(AssertionError) as error:
            cli.run(["-o", "foo"])
        self.assert_equal(
            error.exception.args[0], "exit should have aborted execution"
        )
        self.assert_equal(
            stringio.getvalue(),
            u(
                "Error: 'foo' is not an integer\n"
                "Usage: app [-h] [-o foo]\n"
                "\n"
                "Options\n"
                "  -h, --help   Show this message\n"
                "  -o foo\n"
            )
        )
        with self.assert_raises(UserTypeError):
            cli.run(["-o", "foo"], passthrough_errors=True)

        stringio = StringIO()
        cli = TestCLI(
            application_name=u("app"),
            stdout=stringio,
            stderr=stringio,
            exit=exit,
            width=40,
            commands={
                "spam": Command(
                    options=[("foo", Option("-o", String()))]
                )
            }
        )
        with self.assert_raises(AssertionError) as error:
            cli.run(["spam", "-o"])
        self.assert_equal(
            error.exception.args[0],
            "exit should have aborted execution"
        )
        self.assert_equal(
            stringio.getvalue(),
            u(
                "Error: foo\n"
                "Usage: app spam [-h] [-o foo]\n"
                "\n"
                "Options\n"
                "  -h, --help   Show this message\n"
                "  -o foo\n"
            )
        )

        stringio = StringIO()
        cli = TestCLI(
            application_name=u("app"),
            stdout=stringio,
            stderr=stringio,
            exit=exit,
            width=40,
            commands={
                "foo": Command(
                    positionals=[String(metavar=u("a")), String(metavar=u("b"))]
                )
            }
        )
        with self.assert_raises(AssertionError):
            cli.run(["foo", "bar"])
        self.assert_equal(
            stringio.getvalue(),
            u(
                "Error: expected b\n"
                "Usage: app foo [-h] a b\n"
                "\n"
                "Positional Arguments\n"
                "  a\n"
                "  b\n"
                "\n"
                "Options\n"
                "  -h, --help   Show this message\n"
            )
        )