Beispiel #1
0
 def test_draft3_schema_draft4_validator(self):
     stdout, stderr = NativeIO(), NativeIO()
     with self.assertRaises(SchemaError):
         cli.run(
             {
                 "validator": Draft4Validator,
                 "schema": {
                     "anyOf": [
                         {
                             "minimum": 20
                         },
                         {
                             "type": "string"
                         },
                         {
                             "required": True
                         },
                     ],
                 },
                 "instances": [1],
                 "error_format": "{error.message}",
             },
             stdout=stdout,
             stderr=stderr,
         )
Beispiel #2
0
def captured_output():
    new_out, new_err = NativeIO(), NativeIO()
    old_out, old_err = sys.stdout, sys.stderr
    try:
        sys.stdout, sys.stderr = new_out, new_err
        yield sys.stdout, sys.stderr
    finally:
        sys.stdout, sys.stderr = old_out, old_err
Beispiel #3
0
 def test_successful_validation(self):
     stdout, stderr = NativeIO(), NativeIO()
     exit_code = cli.run(
         {
             "validator": fake_validator(),
             "schema": {},
             "instances": [1],
             "error_format": "{error.message}",
         },
         stdout=stdout,
         stderr=stderr,
     )
     self.assertFalse(stdout.getvalue())
     self.assertFalse(stderr.getvalue())
     self.assertEqual(exit_code, 0)
Beispiel #4
0
 def test_unsuccessful_validation(self):
     error = ValidationError("I am an error!", instance=1)
     stdout, stderr = NativeIO(), NativeIO()
     exit_code = cli.run(
         {
             "validator": fake_validator([error]),
             "schema": {},
             "instances": [1],
             "error_format": "{error.instance} - {error.message}",
         },
         stdout=stdout,
         stderr=stderr,
     )
     self.assertFalse(stdout.getvalue())
     self.assertEqual(stderr.getvalue(), "1 - I am an error!")
     self.assertEqual(exit_code, 1)
Beispiel #5
0
 def fake_open(self, path):
     if path == self.instance_file:
         contents = ""
     elif path == self.schema_file:
         contents = {}
     else:  # pragma: no cover
         self.fail("What is {!r}".format(path))
     return NativeIO(json.dumps(contents))
 def test_successful_validation_of_stdin_pretty_output(self):
     self.assertOutputs(
         files=dict(some_schema="{}"),
         stdin=NativeIO("{}"),
         argv=["--output", "pretty", "some_schema"],
         stdout="===[SUCCESS]===(<stdin>)===\n",
         stderr="",
     )
 def test_successful_validation_of_stdin(self):
     self.assertOutputs(
         files=dict(some_schema="{}"),
         stdin=NativeIO("{}"),
         argv=["some_schema"],
         stdout="",
         stderr="",
     )
Beispiel #8
0
 def test_unsuccessful_validation_multiple_instances(self):
     first_errors = [
         ValidationError("9", instance=1),
         ValidationError("8", instance=1),
     ]
     second_errors = [ValidationError("7", instance=2)]
     stdout, stderr = NativeIO(), NativeIO()
     exit_code = cli.run(
         {
             "validator": fake_validator(first_errors, second_errors),
             "schema": {},
             "instances": [1, 2],
             "error_format": "{error.instance} - {error.message}\t",
         },
         stdout=stdout,
         stderr=stderr,
     )
     self.assertFalse(stdout.getvalue())
     self.assertEqual(stderr.getvalue(), "1 - 9\t1 - 8\t2 - 7\t")
     self.assertEqual(exit_code, 1)
    def test_instance_is_invalid_JSON_on_stdin(self):
        instance = "not valid JSON!"

        self.assertOutputs(
            files=dict(some_schema="{}"),
            stdin=NativeIO(instance),
            argv=["some_schema"],
            exit_code=1,
            stderr="""\
                Failed to parse <stdin>. Got the following error: {}
            """.format(_message_for(instance)),
        )
    def run_cli(self,
                argv,
                files={},
                stdin=NativeIO(),
                exit_code=0,
                **override):
        arguments = cli.parse_args(argv)
        arguments.update(override)

        self.assertFalse(hasattr(cli, "open"))
        cli.open = fake_open(files)
        try:
            stdout, stderr = NativeIO(), NativeIO()
            actual_exit_code = cli.run(
                arguments,
                stdin=stdin,
                stdout=stdout,
                stderr=stderr,
            )
        finally:
            del cli.open

        self.assertEqual(
            actual_exit_code,
            exit_code,
            msg=dedent(
                """
                    Expected an exit code of {} != {}.

                    stdout: {}

                    stderr: {}
                """.format(
                    exit_code,
                    actual_exit_code,
                    stdout.getvalue(),
                    stderr.getvalue(),
                ), ),
        )
        return stdout.getvalue(), stderr.getvalue()
 def test_instance_is_invalid_JSON_on_stdin_pretty_output(self):
     stdout, stderr = self.run_cli(
         files=dict(some_schema="{}"),
         stdin=NativeIO("not valid JSON!"),
         argv=["--output", "pretty", "some_schema"],
         exit_code=1,
     )
     self.assertFalse(stdout)
     self.assertIn(
         "(<stdin>)===\n\nTraceback (most recent call last):\n",
         stderr,
     )
     self.assertNotIn("some_schema", stderr)
 def open(path):
     contents = all_contents.get(path)
     if contents is None:
         raise OSError(errno.ENOENT, os.strerror(errno.ENOENT), path)
     return NativeIO(contents)