From 245bf1082acf6a207272c8c69ade60f3e11e0763 Mon Sep 17 00:00:00 2001 From: Stephen Rosen Date: Fri, 5 Jul 2024 08:52:40 -0500 Subject: [PATCH 1/3] Add simple non-string examples to format tests Simply add `object()` instances as non-string test data. This improves test coverage by exercising the failure path for non-string data. --- tests/unit/formats/test_rfc3339.py | 1 + tests/unit/formats/test_time.py | 1 + 2 files changed, 2 insertions(+) diff --git a/tests/unit/formats/test_rfc3339.py b/tests/unit/formats/test_rfc3339.py index 84c096b5c..87f61580e 100644 --- a/tests/unit/formats/test_rfc3339.py +++ b/tests/unit/formats/test_rfc3339.py @@ -22,6 +22,7 @@ def test_simple_positive_cases(datestr): @pytest.mark.parametrize( "datestr", ( + object(), "2018-12-31T23:59:59", "2018-12-31T23:59:59+00:00Z", "2018-12-31 23:59:59", diff --git a/tests/unit/formats/test_time.py b/tests/unit/formats/test_time.py index 76231b438..e30f20995 100644 --- a/tests/unit/formats/test_time.py +++ b/tests/unit/formats/test_time.py @@ -21,6 +21,7 @@ def test_simple_positive_cases(timestr): @pytest.mark.parametrize( "timestr", ( + object(), "12:34:56", "23:59:60Z", "23:59:59+24:00", From b016212d75bf8f4b88eb88b42c44b55969dd3492 Mon Sep 17 00:00:00 2001 From: Stephen Rosen Date: Fri, 5 Jul 2024 09:48:09 -0500 Subject: [PATCH 2/3] Add a missing test case for '--validator-class' --- .../acceptance/test_custom_validator_class.py | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/acceptance/test_custom_validator_class.py b/tests/acceptance/test_custom_validator_class.py index 018718cec..170f4524b 100644 --- a/tests/acceptance/test_custom_validator_class.py +++ b/tests/acceptance/test_custom_validator_class.py @@ -147,3 +147,28 @@ def test_custom_validator_class_can_pass_when_valid(run_line, tmp_path): ], ) assert result.exit_code == 0 # pass + + +@pytest.mark.parametrize( + "add_opts", + ( + ["--builtin-schema", "vendor.github-workflows"], + ["--check-metaschema"], + ), +) +def test_custom_validator_class_is_incompatible_with_schema_opts( + run_line, tmp_path, add_opts +): + doc = tmp_path / "instance.json" + doc.write_text("{}") + result = run_line( + [ + "check-jsonschema", + "--validator-class", + "foo:MyValidator", + str(doc), + ] + + add_opts + ) + assert result.exit_code == 2 + assert "--validator-class can only be used with --schemafile" in result.stderr From 08d04a7ea3f1cc3a3a5015f6effca091234caa5e Mon Sep 17 00:00:00 2001 From: Stephen Rosen Date: Fri, 5 Jul 2024 10:21:36 -0500 Subject: [PATCH 3/3] Refactor CLI unit tests + add deprecation tests - Move CLI unit tests to `tests/unit/cli/` - Add initial test cases for the deprecation callback helper --- tests/unit/cli/conftest.py | 7 +++ .../test_annotations.py} | 0 tests/unit/cli/test_callbacks.py | 51 +++++++++++++++++++ .../{test_cli_parse.py => cli/test_parse.py} | 6 --- 4 files changed, 58 insertions(+), 6 deletions(-) create mode 100644 tests/unit/cli/conftest.py rename tests/unit/{test_cli_annotations.py => cli/test_annotations.py} (100%) create mode 100644 tests/unit/cli/test_callbacks.py rename tests/unit/{test_cli_parse.py => cli/test_parse.py} (98%) diff --git a/tests/unit/cli/conftest.py b/tests/unit/cli/conftest.py new file mode 100644 index 000000000..98a793137 --- /dev/null +++ b/tests/unit/cli/conftest.py @@ -0,0 +1,7 @@ +import pytest +from click.testing import CliRunner + + +@pytest.fixture +def runner() -> CliRunner: + return CliRunner(mix_stderr=False) diff --git a/tests/unit/test_cli_annotations.py b/tests/unit/cli/test_annotations.py similarity index 100% rename from tests/unit/test_cli_annotations.py rename to tests/unit/cli/test_annotations.py diff --git a/tests/unit/cli/test_callbacks.py b/tests/unit/cli/test_callbacks.py new file mode 100644 index 000000000..b2813144d --- /dev/null +++ b/tests/unit/cli/test_callbacks.py @@ -0,0 +1,51 @@ +import click +import pytest + +from check_jsonschema.cli.warnings import deprecation_warning_callback + + +@click.command("foo") +@click.option( + "--bar", + is_flag=True, + callback=deprecation_warning_callback("--bar", is_flag=True), +) +@click.option( + "--baz", + callback=deprecation_warning_callback( + "--baz", append_message="Use --frob instead!" + ), +) +def mycli(bar, baz): + print(bar) + if baz: + print(baz) + + +def test_deprecation_warning_callback_on_missing_opts(runner): + result = runner.invoke(mycli, []) + assert result.exit_code == 0 + assert result.stdout == "False\n" + + +def test_deprecation_warning_callback_on_flag(runner): + with pytest.warns( + UserWarning, + match="'--bar' is deprecated and will be removed in a future release", + ): + result = runner.invoke(mycli, ["--bar"], catch_exceptions=False) + assert result.exit_code == 0, result.stdout + assert result.stdout == "True\n" + + +def test_deprecation_warning_callback_added_message(runner): + with pytest.warns( + UserWarning, + match=( + "'--baz' is deprecated and will be removed in a future release. " + "Use --frob instead!" + ), + ): + result = runner.invoke(mycli, ["--baz", "ok"], catch_exceptions=False) + assert result.exit_code == 0, result.stdout + assert result.stdout == "False\nok\n" diff --git a/tests/unit/test_cli_parse.py b/tests/unit/cli/test_parse.py similarity index 98% rename from tests/unit/test_cli_parse.py rename to tests/unit/cli/test_parse.py index fd376ef32..3b486f1b6 100644 --- a/tests/unit/test_cli_parse.py +++ b/tests/unit/cli/test_parse.py @@ -4,7 +4,6 @@ import click import pytest -from click.testing import CliRunner from check_jsonschema import main as cli_main from check_jsonschema.cli.parse_result import ParseResult, SchemaLoadingMode @@ -43,11 +42,6 @@ def get_ctx(*args): yield m -@pytest.fixture -def runner() -> CliRunner: - return CliRunner(mix_stderr=False) - - @pytest.mark.parametrize( "schemafile,builtin_schema,check_metaschema,expect_mode", [