diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index dd5fec92..736a2770 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -130,7 +130,7 @@ repos: # https://docs.openstack.org/bashate/latest/man/bashate.html#options args: [-i, E006] - repo: https://github.com/commitizen-tools/commitizen - rev: v4.13.0 + rev: v4.12.1 hooks: - id: commitizen stages: [commit-msg] diff --git a/tests/conftest.py b/tests/conftest.py index 8de8867a..881e5ac9 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -26,12 +26,10 @@ def project_default(tmp_path): from tests.helpers import ProjectMock, tomlstring # noqa: PLC0415 nitpick_style = Path(__file__).parent.parent / NITPICK_STYLE_TOML - return ProjectMock(tmp_path).pyproject_toml( - f""" + return ProjectMock(tmp_path).pyproject_toml(f""" [tool.nitpick] style = {tomlstring(nitpick_style)} - """ - ) + """) @pytest.fixture @@ -52,16 +50,14 @@ def project_remote(request, tmp_path): mocked_response.add(mocked_response.GET, remote_url, dedent(remote_style), status=200) project = ProjectMock(tmp_path) - project.pyproject_toml( - f""" + project.pyproject_toml(f""" [tool.nitpick] style = {tomlstring(remote_url)} {tool_nitpick} [tool.black] line-length = 100 - """ - ).remote(mocked_response, remote_url) + """).remote(mocked_response, remote_url) yield project diff --git a/tests/test_cli.py b/tests/test_cli.py index 91107f9e..e415da26 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -27,27 +27,21 @@ def test_simple_error(tmp_path: Path) -> None: """A simple error on the CLI.""" project = ( ProjectMock(tmp_path) - .style( - """ + .style(""" ["pyproject.toml".tool.black] line-length = 100 - """ - ) - .pyproject_toml( - """ + """) + .pyproject_toml(""" [tool.blabla] something = 22 - """ - ) + """) ) - project.cli_run( - f""" + project.cli_run(f""" {project.root_dir / "pyproject.toml"!s}:1: NIP318 has missing values: [tool.black] line-length = 100 - """ - ) + """) def test_missing_style_and_suggest_option(tmp_path: Path) -> None: diff --git a/tests/test_ini.py b/tests/test_ini.py index 13dfdeea..35e32355 100644 --- a/tests/test_ini.py +++ b/tests/test_ini.py @@ -18,16 +18,14 @@ def test_setup_cfg_has_no_configuration(tmp_path): def test_comma_separated_keys_on_style_file(tmp_path): """Comma separated keys on the style file.""" - ProjectMock(tmp_path).style( - f""" + ProjectMock(tmp_path).style(f""" [nitpick.files."{PYTHON_SETUP_CFG}"] comma_separated_values = ["food.eat", "food.drink"] ["{PYTHON_SETUP_CFG}".food] eat = "salt,ham,eggs" drink = "water,bier,wine" - """ - ).setup_cfg( + """).setup_cfg( """ [food] eat = spam,eggs,cheese @@ -76,8 +74,7 @@ def test_suggest_initial_contents(tmp_path): end_of_line = lf insert_final_newline = True """ - ProjectMock(tmp_path).style( - f""" + ProjectMock(tmp_path).style(f""" ["{PYTHON_SETUP_CFG}".mypy] ignore_missing_imports = true @@ -94,8 +91,7 @@ def test_suggest_initial_contents(tmp_path): ["{EDITOR_CONFIG}"."*"] end_of_line = "lf" insert_final_newline = true - """ - ).api_check_then_fix( + """).api_check_then_fix( Fuss( True, PYTHON_SETUP_CFG, @@ -124,12 +120,10 @@ def test_suggest_initial_contents(tmp_path): def test_missing_sections(tmp_path): """Test missing sections.""" - ProjectMock(tmp_path).setup_cfg( - """ + ProjectMock(tmp_path).setup_cfg(""" [mypy] ignore_missing_imports = true - """ - ).style( + """).style( f""" ["{PYTHON_SETUP_CFG}".mypy] ignore_missing_imports = true @@ -171,8 +165,7 @@ def test_missing_sections(tmp_path): def test_missing_different_values(tmp_path): """Test different and missing keys/values.""" - ProjectMock(tmp_path).setup_cfg( - """ + ProjectMock(tmp_path).setup_cfg(""" [mypy] # Line comment with hash (inline comments are not supported) ignore_missing_imports = true @@ -183,8 +176,7 @@ def test_missing_different_values(tmp_path): [flake8] ; Line comment with semicolon xxx = "aaa" - """ - ).style( + """).style( f""" ["{PYTHON_SETUP_CFG}".mypy] ignore_missing_imports = true @@ -308,8 +300,7 @@ def test_missing_different_values_editorconfig_with_root(tmp_path, datadir): def test_invalid_configuration_comma_separated_values(tmp_path): """Test an invalid configuration for comma_separated_values.""" - ProjectMock(tmp_path).style( - f""" + ProjectMock(tmp_path).style(f""" ["{PYTHON_SETUP_CFG}".flake8] max-line-length = 85 max-complexity = 12 @@ -318,8 +309,7 @@ def test_invalid_configuration_comma_separated_values(tmp_path): [nitpick.files."{PYTHON_SETUP_CFG}"] comma_separated_values = ["flake8.ignore", "flake8.exclude"] - """ - ).api_check().assert_violations( + """).api_check().assert_violations( Fuss( False, PYTHON_SETUP_CFG, @@ -338,12 +328,10 @@ def test_invalid_configuration_comma_separated_values(tmp_path): def test_invalid_section_dot_fields(tmp_path): """Test invalid section/field pairs.""" - ProjectMock(tmp_path).style( - f""" + ProjectMock(tmp_path).style(f""" [nitpick.files."{PYTHON_SETUP_CFG}"] comma_separated_values = ["no_dot", "multiple.dots.here", ".filed_only", "section_only."] - """ - ).setup_cfg("").api_check().assert_violations( + """).setup_cfg("").api_check().assert_violations( Fuss( False, "nitpick-style.toml", @@ -361,8 +349,7 @@ def test_invalid_section_dot_fields(tmp_path): def test_invalid_sections_comma_separated_values(tmp_path): """Test invalid sections on comma_separated_values.""" - ProjectMock(tmp_path).style( - f""" + ProjectMock(tmp_path).style(f""" ["{PYTHON_SETUP_CFG}".flake8] ignore = "W503,E203,FI58,PT003,C408" exclude = "venv*,**/migrations/" @@ -370,8 +357,7 @@ def test_invalid_sections_comma_separated_values(tmp_path): [nitpick.files."{PYTHON_SETUP_CFG}"] comma_separated_values = ["flake8.ignore", "flake8.exclude", "falek8.per-file-ignores", "aaa.invalid-section"] - """ - ).setup_cfg( + """).setup_cfg( """ [flake8] exclude = venv*,**/migrations/ @@ -385,12 +371,10 @@ def test_invalid_sections_comma_separated_values(tmp_path): def test_multiline_comment(tmp_path, datadir): """Test file with multiline comments should not raise a configparser.ParsingError.""" - ProjectMock(tmp_path).style( - f""" + ProjectMock(tmp_path).style(f""" ["{PYTHON_SETUP_CFG}".flake8] new = "value" - """ - ).setup_cfg(datadir / "3-actual-setup.cfg").api_fix().assert_violations( + """).setup_cfg(datadir / "3-actual-setup.cfg").api_fix().assert_violations( Fuss( True, PYTHON_SETUP_CFG, @@ -401,9 +385,7 @@ def test_multiline_comment(tmp_path, datadir): new = value """, ) - ).assert_file_contents( - PYTHON_SETUP_CFG, datadir / "3-expected-setup.cfg" - ) + ).assert_file_contents(PYTHON_SETUP_CFG, datadir / "3-expected-setup.cfg") def test_duplicated_option(tmp_path): @@ -414,12 +396,10 @@ def test_duplicated_option(tmp_path): easy = as sunday morning """ project = ProjectMock(tmp_path) - project.style( - f""" + project.style(f""" ["{PYTHON_SETUP_CFG}".abc] hard = "as a rock" - """ - ).setup_cfg(original_file).api_fix().assert_violations( + """).setup_cfg(original_file).api_fix().assert_violations( Fuss( False, PYTHON_SETUP_CFG, @@ -427,9 +407,7 @@ def test_duplicated_option(tmp_path): f": parsing error (DuplicateOptionError): While reading from {project.path_for(PYTHON_SETUP_CFG)!r} " f"[line 3]: option 'easy' in section 'abc' already exists", ) - ).assert_file_contents( - PYTHON_SETUP_CFG, original_file - ) + ).assert_file_contents(PYTHON_SETUP_CFG, original_file) @mock.patch.object(ConfigUpdater, "update_file") @@ -441,12 +419,10 @@ def test_simulate_parsing_error_when_saving(update_file, tmp_path): [flake8] existing = value """ - ProjectMock(tmp_path).style( - f""" + ProjectMock(tmp_path).style(f""" ["{PYTHON_SETUP_CFG}".flake8] new = "value" - """ - ).setup_cfg(original_file).api_fix().assert_violations( + """).setup_cfg(original_file).api_fix().assert_violations( Fuss( True, PYTHON_SETUP_CFG, @@ -463,9 +439,7 @@ def test_simulate_parsing_error_when_saving(update_file, tmp_path): Violations.PARSING_ERROR.code, ": parsing error (ParsingError): Source contains parsing errors: 'simulating a captured error'", ), - ).assert_file_contents( - PYTHON_SETUP_CFG, original_file - ) + ).assert_file_contents(PYTHON_SETUP_CFG, original_file) def test_generic_ini_with_missing_header(tmp_path): @@ -478,13 +452,11 @@ def test_generic_ini_with_missing_header(tmp_path): your_string = value """ project = ProjectMock(tmp_path) - project.save_file("generic.ini", expected_generic_ini).style( - """ + project.save_file("generic.ini", expected_generic_ini).style(""" ["generic.ini".your-section] your_string = "value" your_number = 100 - """ - ).api_check_then_fix( + """).api_check_then_fix( Fuss( False, "generic.ini", @@ -493,9 +465,7 @@ def test_generic_ini_with_missing_header(tmp_path): f"file: {project.path_for('generic.ini')!r}, line: 1\n" "'this_key_is_invalid = for a generic .ini (it should always have a section)\\n'", ) - ).assert_file_contents( - "generic.ini", expected_generic_ini - ) + ).assert_file_contents("generic.ini", expected_generic_ini) def test_add_options_with_multiple_trailing_spaces(tmp_path): @@ -504,14 +474,12 @@ def test_add_options_with_multiple_trailing_spaces(tmp_path): This is a regression test for the NotAttachedError bug that occurred when trying to detach multiple consecutive Space blocks. """ - ProjectMock(tmp_path).setup_cfg( - """ + ProjectMock(tmp_path).setup_cfg(""" [flake8] max-line-length = 100 - """ - ).style( + """).style( f""" ["{PYTHON_SETUP_CFG}".flake8] max-line-length = 100 diff --git a/tests/test_json.py b/tests/test_json.py index daddb064..ba52234e 100644 --- a/tests/test_json.py +++ b/tests/test_json.py @@ -11,12 +11,10 @@ def test_suggest_initial_contents(tmp_path, datadir): """Suggest initial contents for missing JSON file.""" expected_package_json = (datadir / "1-expected-package.json").read_text() - ProjectMock(tmp_path).named_style("package-json", datadir / "package-json-style.toml").pyproject_toml( - """ + ProjectMock(tmp_path).named_style("package-json", datadir / "package-json-style.toml").pyproject_toml(""" [tool.nitpick] style = ["package-json"] - """ - ).api_check_then_fix( + """).api_check_then_fix( Fuss( True, JAVASCRIPT_PACKAGE_JSON, @@ -24,20 +22,16 @@ def test_suggest_initial_contents(tmp_path, datadir): " was not found. Create it with this content:", expected_package_json, ) - ).assert_file_contents( - JAVASCRIPT_PACKAGE_JSON, expected_package_json - ).api_check_then_fix() + ).assert_file_contents(JAVASCRIPT_PACKAGE_JSON, expected_package_json).api_check_then_fix() def test_missing_different_values_with_contains_json_with_contains_keys(tmp_path, datadir): """Test missing and different values with "contains_json" and "contains_keys".""" expected_package_json = (datadir / "2-expected-package.json").read_text() - ProjectMock(tmp_path).named_style("package-json", datadir / "package-json-style.toml").pyproject_toml( - """ + ProjectMock(tmp_path).named_style("package-json", datadir / "package-json-style.toml").pyproject_toml(""" [tool.nitpick] style = ["package-json"] - """ - ).save_file(JAVASCRIPT_PACKAGE_JSON, datadir / "2-actual-package.json").api_check_then_fix( + """).save_file(JAVASCRIPT_PACKAGE_JSON, datadir / "2-actual-package.json").api_check_then_fix( Fuss( True, JAVASCRIPT_PACKAGE_JSON, @@ -70,9 +64,7 @@ def test_missing_different_values_with_contains_json_with_contains_keys(tmp_path } """, ), - ).assert_file_contents( - JAVASCRIPT_PACKAGE_JSON, expected_package_json - ).api_check_then_fix() + ).assert_file_contents(JAVASCRIPT_PACKAGE_JSON, expected_package_json).api_check_then_fix() def test_missing_different_values_with_contains_json_without_contains_keys(tmp_path, datadir): @@ -148,15 +140,13 @@ def test_invalid_json(tmp_path, datadir): def test_json_configuration(tmp_path): """Test configuration for JSON files.""" - ProjectMock(tmp_path).style( - """ + ProjectMock(tmp_path).style(""" ["your.json".has] an_extra = "key" ["their.json"] x = 1 - """ - ).api_check_then_fix( + """).api_check_then_fix( Fuss( False, "nitpick-style.toml", @@ -176,15 +166,13 @@ def test_jsonfile_deprecated(tmp_path): # Cause all warnings to always be triggered. warnings.simplefilter("always") - ProjectMock(tmp_path).style( - """ + ProjectMock(tmp_path).style(""" [nitpick.JSONFile] file_names = ["my.json"] ["my.json"] contains_keys = ["x"] - """ - ).save_file("my.json", '{"x":1}').flake8().assert_no_errors() + """).save_file("my.json", '{"x":1}').flake8().assert_no_errors() filtered = filter_desired_warning( captured, "The [nitpick.JSONFile] section is not needed anymore; just declare your JSON files directly" diff --git a/tests/test_plugin.py b/tests/test_plugin.py index 645c12c3..11936812 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -42,13 +42,11 @@ def _call_main(argv, retv=0): def test_absent_files(tmp_path): """Test absent files from the style configuration.""" - ProjectMock(tmp_path).style( - """ + ProjectMock(tmp_path).style(""" [nitpick.files.absent] xxx = "Remove this" yyy = "Remove that" - """ - ).touch_file("xxx").touch_file("yyy").api_check_then_fix( + """).touch_file("xxx").touch_file("yyy").api_check_then_fix( Fuss(False, "xxx", 104, " should be deleted: Remove this"), Fuss(False, "yyy", 104, " should be deleted: Remove that"), ) @@ -56,12 +54,10 @@ def test_absent_files(tmp_path): def test_missing_message(tmp_path): """Test if the breaking style change "missing_message" key points to the correct help page.""" - ProjectMock(tmp_path).style( - """ + ProjectMock(tmp_path).style(""" [nitpick.files."pyproject.toml"] missing_message = "Install poetry and run 'poetry init' to create it" - """ - ).api_check_then_fix( + """).api_check_then_fix( # pylint: disable=line-too-long Fuss( False, @@ -75,14 +71,12 @@ def test_missing_message(tmp_path): def test_present_files(tmp_path): """Test present files from the style configuration.""" - ProjectMock(tmp_path).style( - """ + ProjectMock(tmp_path).style(""" [nitpick.files.present] ".editorconfig" = "Create this file" ".env" = "" "another-file.txt" = "" - """ - ).api_check_then_fix( + """).api_check_then_fix( Fuss(False, ".editorconfig", 103, " should exist: Create this file"), Fuss(False, ".env", 103, " should exist"), Fuss(False, "another-file.txt", 103, " should exist"), @@ -122,12 +116,10 @@ def test_offline_flag_env_variable(tmpdir): def project_github(tmp_path): """Project using a style from the Nitpick GitHub repo.""" github_url = StyleManager.get_default_style_url(True) - return ProjectMock(tmp_path).pyproject_toml( - f""" + return ProjectMock(tmp_path).pyproject_toml(f""" [tool.nitpick] style = "{github_url}" - """ - ) + """) @responses.activate diff --git a/tests/test_project.py b/tests/test_project.py index 551794d7..7cd1188d 100644 --- a/tests/test_project.py +++ b/tests/test_project.py @@ -78,18 +78,14 @@ def test_at_least_one_python_file(python_file, error, tmp_path): """At least one Python file on the root dir, even if it's not a main file.""" project = ( ProjectMock(tmp_path, setup_py=False) - .style( - """ + .style(""" ["pyproject.toml".tool.black] lines = 100 - """ - ) - .pyproject_toml( - """ + """) + .pyproject_toml(""" [tool.black] lines = 100 - """ - ) + """) .save_file(python_file, "", lint=True) .flake8() ) @@ -103,19 +99,15 @@ def test_at_least_one_python_file(python_file, error, tmp_path): def test_django_project_structure(tmp_path): """Django project with pyproject.toml in the parent dir of manage.py's dir.""" - ProjectMock(tmp_path, setup_py=False).pyproject_toml( - """ + ProjectMock(tmp_path, setup_py=False).pyproject_toml(""" [tool.black] lines = 100 - """ - ).setup_cfg( + """).setup_cfg( """ [flake8] some = thing """ - ).touch_file( - "my_django_project/manage.py" - ).style( + ).touch_file("my_django_project/manage.py").style( f""" ["{PYTHON_PYPROJECT_TOML}".tool.black] lines = 100 diff --git a/tests/test_style.py b/tests/test_style.py index 1763a66c..0b83aab1 100644 --- a/tests/test_style.py +++ b/tests/test_style.py @@ -237,13 +237,11 @@ def test_relative_and_other_root_dirs(offline, tmp_path): """ # Use full path on initial styles - project.pyproject_toml( - f""" + project.pyproject_toml(f""" [tool.nitpick] style = [{tomlstring(another_dir / "main")}, {tomlstring(another_dir / "styles/black")}] {common_pyproject} - """ - ).flake8(offline=offline).assert_single_error( + """).flake8(offline=offline).assert_single_error( f""" NIP318 File pyproject.toml has missing values:{SUGGESTION_BEGIN} [tool.black] @@ -252,13 +250,11 @@ def test_relative_and_other_root_dirs(offline, tmp_path): ) # Allow relative paths - project.pyproject_toml( - f""" + project.pyproject_toml(f""" [tool.nitpick] style = [{tomlstring(another_dir / "styles/black")}, "./another_dir/poetry"] {common_pyproject} - """ - ).flake8(offline=offline).assert_single_error( + """).flake8(offline=offline).assert_single_error( f""" NIP318 File pyproject.toml has missing values:{SUGGESTION_BEGIN} [tool.black] @@ -342,13 +338,11 @@ def test_relative_style_on_urls(tmp_path): some-option = 123 """ # Use full path on initial styles - project.pyproject_toml( - f""" + project.pyproject_toml(f""" [tool.nitpick] style = ["{base_url}/main"] {common_pyproject} - """ - ).api_check().assert_violations( + """).api_check().assert_violations( Fuss( False, PYTHON_PYPROJECT_TOML, @@ -378,8 +372,7 @@ def test_local_style_should_override_settings(tmp_path): line-length = 120 """ - ProjectMock(tmp_path).pyproject_toml( - f""" + ProjectMock(tmp_path).pyproject_toml(f""" [tool.nitpick] style = [ "{remote_url}", @@ -388,8 +381,7 @@ def test_local_style_should_override_settings(tmp_path): [tool.black] line-length = 80 - """ - ).named_style(local_file, local_style).api_check().assert_violations( + """).named_style(local_file, local_style).api_check().assert_violations( Fuss( False, PYTHON_PYPROJECT_TOML, @@ -415,19 +407,15 @@ def test_fetch_private_github_urls(tmp_path): """ responses.add(responses.GET, full_raw_url, dedent(body), status=200) - project = ProjectMock(tmp_path).pyproject_toml( - f""" + project = ProjectMock(tmp_path).pyproject_toml(f""" [tool.nitpick] style = "{gh_url}?token={file_token}" - """ - ) - project.flake8(offline=False).assert_single_error( - f""" + """) + project.flake8(offline=False).assert_single_error(f""" NIP318 File pyproject.toml has missing values:{SUGGESTION_BEGIN} [tool.black] missing = "thing"{SUGGESTION_END} - """ - ) + """) assert responses.calls[0].request.headers["Authorization"] == f"token {file_token}" project.flake8(offline=True).assert_no_errors() @@ -447,19 +435,15 @@ def test_fetch_private_github_urls_no_branch(tmp_path): responses.add(responses.GET, api_url, api_response, status=200) responses.add(responses.GET, full_raw_url, dedent(body), status=200) - project = ProjectMock(tmp_path).pyproject_toml( - f""" + project = ProjectMock(tmp_path).pyproject_toml(f""" [tool.nitpick] style = "{gh_url}" - """ - ) - project.flake8(offline=False).assert_single_error( - f""" + """) + project.flake8(offline=False).assert_single_error(f""" NIP318 File pyproject.toml has missing values:{SUGGESTION_BEGIN} [tool.black] missing = "thing"{SUGGESTION_END} - """ - ) + """) assert responses.calls[0].request.headers["Authorization"] == f"token {file_token}" assert responses.calls[1].request.headers["Authorization"] == f"token {file_token}" project.flake8(offline=True).assert_no_errors() @@ -582,14 +566,12 @@ def test_include_remote_style_from_local_style(tmp_path): """ responses.add(responses.GET, url_with_extension, dedent(body), status=200) - project = ProjectMock(tmp_path).style( - f""" + project = ProjectMock(tmp_path).style(f""" [nitpick.styles] include = [ "{remote_style}" ] - """ - ) + """) project.assert_file_contents(PYTHON_TOX_INI, None).api_check_then_fix( Fuss(True, PYTHON_TOX_INI, 321, " was not found. Create it with this content:", "[section]\nkey = value") ).assert_file_contents( @@ -738,12 +720,10 @@ def test_invalid_tool_nitpick_on_pyproject_toml(offline, tmp_path): def test_invalid_toml(tmp_path): """Invalid TOML should emit a NIP warning, not raise TomlDecodeError.""" - ProjectMock(tmp_path).style( - f""" + ProjectMock(tmp_path).style(f""" ["{PYTHON_SETUP_CFG}".flake8] ignore = D100,D104,D202,E203,W503 - """ - ).api_check_then_fix( + """).api_check_then_fix( Fuss( False, "nitpick-style.toml", @@ -830,12 +810,10 @@ def test_always_fetch_github_raw_url(style_url, tmp_path): responses.add(responses.GET, "https://api.github.com/repos/andreoliwa/nitpick", '{"default_branch": "develop"}') - ProjectMock(tmp_path).pyproject_toml( - f""" + ProjectMock(tmp_path).pyproject_toml(f""" [tool.nitpick] style = ["{style_url}"] - """ - ).api_check().assert_violations( + """).api_check().assert_violations( Fuss( False, PYTHON_PYPROJECT_TOML, @@ -952,12 +930,10 @@ def test_raw_content_url_of_python_package(original_url, expected_content_path_s def test_protocol_not_supported(tmp_path): """Test unsupported protocols.""" - project = ProjectMock(tmp_path).pyproject_toml( - """ + project = ProjectMock(tmp_path).pyproject_toml(""" [tool.nitpick] style = ["abc://www.example.com/style.toml"] - """ - ) + """) with pytest.raises(RuntimeError) as exc_info: project.api_check() assert str(exc_info.value) == "URL protocol 'abc' is not supported" diff --git a/tests/test_text.py b/tests/test_text.py index cf807497..746e67a1 100644 --- a/tests/test_text.py +++ b/tests/test_text.py @@ -7,16 +7,14 @@ def test_suggest_initial_contents(tmp_path): """Suggest initial contents for a text file.""" - ProjectMock(tmp_path).style( - """ + ProjectMock(tmp_path).style(""" [["requirements.txt".contains]] # File contains this exact line anywhere line = "sphinx>=1.3.0" [["requirements.txt".contains]] line = "some-package==1.0.0" - """ - ).api_check_then_fix( + """).api_check_then_fix( Fuss( False, "requirements.txt", @@ -33,8 +31,7 @@ def test_suggest_initial_contents(tmp_path): def test_text_configuration(tmp_path): """Test configuration for text files.""" # pylint: disable=line-too-long - ProjectMock(tmp_path).style( - """ + ProjectMock(tmp_path).style(""" [["abc.txt".contains]] invalid = "key" line = ["it", "should", "be", "a", "string"] @@ -44,8 +41,7 @@ def test_text_configuration(tmp_path): ["ghi.txt".whatever] wrong = "everything" - """ - ).flake8().assert_errors_contain( + """).flake8().assert_errors_contain( f""" NIP001 File nitpick-style.toml has an incorrect style. Invalid config:{SUGGESTION_BEGIN} "abc.txt".contains.0.invalid: Unknown configuration. See {READ_THE_DOCS_URL}plugins.html#text-files. @@ -59,16 +55,14 @@ def test_text_configuration(tmp_path): def test_text_file_contains_line(tmp_path): """Test if the text file contains a line.""" - ProjectMock(tmp_path).style( - """ + ProjectMock(tmp_path).style(""" [["my.txt".contains]] line = "qqq" [["my.txt".contains]] line = "abc" [["my.txt".contains]] line = "www" - """ - ).save_file("my.txt", "def\nghi\nwww").api_check_then_fix( + """).save_file("my.txt", "def\nghi\nwww").api_check_then_fix( Fuss( False, "my.txt", @@ -84,12 +78,10 @@ def test_text_file_contains_line(tmp_path): def test_yaml_file_as_text(tmp_path): """A YAML file is also a text file, so it could be checked with the text plugin.""" - ProjectMock(tmp_path).style( - """ + ProjectMock(tmp_path).style(""" [[".gitlab-ci.yml".contains]] line = " - mypy -p ims --junit-xml report-mypy.xml" - """ - ).save_file(".gitlab-ci.yml", "def\nghi\nwww").api_check_then_fix( + """).save_file(".gitlab-ci.yml", "def\nghi\nwww").api_check_then_fix( Fuss( False, ".gitlab-ci.yml", 352, " has missing lines:", f"{NBSP * 4}- mypy -p ims --junit-xml report-mypy.xml" ) diff --git a/tests/test_toml.py b/tests/test_toml.py index 48f0e0d3..90cf29ce 100644 --- a/tests/test_toml.py +++ b/tests/test_toml.py @@ -13,12 +13,10 @@ def test_pyproject_has_no_configuration(tmp_path): def test_pyproject_toml_file_present(tmp_path): """Suggest poetry init when pyproject.toml does not exist.""" - ProjectMock(tmp_path, pyproject_toml=False).style( - """ + ProjectMock(tmp_path, pyproject_toml=False).style(""" [nitpick.files.present] "pyproject.toml" = "Do something" - """ - ).api_check_then_fix(Fuss(False, PYTHON_PYPROJECT_TOML, 103, " should exist: Do something")).cli_run( + """).api_check_then_fix(Fuss(False, PYTHON_PYPROJECT_TOML, 103, " should exist: Do something")).cli_run( f"{PYTHON_PYPROJECT_TOML}:1: NIP103 should exist: Do something", violations=1 ) @@ -32,14 +30,12 @@ def test_suggest_initial_contents(tmp_path): number = 10 list = [ "a", "b", "c",] """ - ProjectMock(tmp_path).style( - f""" + ProjectMock(tmp_path).style(f""" ["{filename}".section] key = "value" number = 10 list = ["a", "b", "c"] - """ - ).api_check_then_fix( + """).api_check_then_fix( Fuss( True, filename, @@ -47,15 +43,12 @@ def test_suggest_initial_contents(tmp_path): " was not found. Create it with this content:", expected_toml, ) - ).assert_file_contents( - filename, expected_toml - ) + ).assert_file_contents(filename, expected_toml) def test_missing_different_values_pyproject_toml(tmp_path): """Test missing and different values on pyproject.toml.""" - ProjectMock(tmp_path).style( - """ + ProjectMock(tmp_path).style(""" ["pyproject.toml".something] yada = "after" @@ -64,8 +57,7 @@ def test_missing_different_values_pyproject_toml(tmp_path): ["pyproject.toml".config] list = ["a", "b", "c"] - """ - ).pyproject_toml( + """).pyproject_toml( """ [something] x = 1 # comment for x diff --git a/tests/test_yaml.py b/tests/test_yaml.py index d1a97c27..9d6de1e4 100644 --- a/tests/test_yaml.py +++ b/tests/test_yaml.py @@ -80,14 +80,12 @@ def test_repos_yaml_key_deprecated(tmp_path, shared_datadir): # Cause all warnings to always be triggered. warnings.simplefilter("always") - ProjectMock(tmp_path).style(shared_datadir / "pre-commit-config-with-old-repos-yaml-key.toml").pre_commit( - """ + ProjectMock(tmp_path).style(shared_datadir / "pre-commit-config-with-old-repos-yaml-key.toml").pre_commit(""" repos: - repo: not checked yet hooks: - id: my-hook - """ - ).api_check().assert_violations() + """).api_check().assert_violations() filtered = filter_desired_warning( captured, diff --git a/tests/test_yaml_old_pre_commit.py b/tests/test_yaml_old_pre_commit.py index 813c6c5f..15eed4e1 100644 --- a/tests/test_yaml_old_pre_commit.py +++ b/tests/test_yaml_old_pre_commit.py @@ -24,12 +24,10 @@ def test_pre_commit_has_no_configuration(tmp_path): def test_pre_commit_referenced_in_style(tmp_path): """Only check files if they have configured styles.""" - ProjectMock(tmp_path).style( - """ + ProjectMock(tmp_path).style(""" [".pre-commit-config.yaml"] fail_fast = true - """ - ).pre_commit("").api_check_then_fix( + """).pre_commit("").api_check_then_fix( Fuss(True, PRE_COMMIT_CONFIG_YAML, 368, " has missing values:", "fail_fast: true") ) @@ -68,8 +66,7 @@ def test_no_yaml_key(tmp_path): Read the warning on :py:class:`nitpick.plugins.yaml.YamlPlugin`. """ - ProjectMock(tmp_path).style( - ''' + ProjectMock(tmp_path).style(''' [[".pre-commit-config.yaml".repos]] missing_yaml_key = """ - repo: https://github.com/PyCQA/isort @@ -77,8 +74,7 @@ def test_no_yaml_key(tmp_path): hooks: - id: isort """ - ''' - ).api_check_then_fix( + ''').api_check_then_fix( Fuss( True, PRE_COMMIT_CONFIG_YAML, @@ -94,14 +90,12 @@ def test_no_yaml_key(tmp_path): def test_root_values_on_missing_file(tmp_path): """Test values on the root of the config file when it's missing.""" - ProjectMock(tmp_path).style( - """ + ProjectMock(tmp_path).style(""" [".pre-commit-config.yaml"] bla_bla = "oh yeah" fail_fast = true whatever = "1" - """ - ).api_check_then_fix( + """).api_check_then_fix( Fuss( True, PRE_COMMIT_CONFIG_YAML, @@ -118,15 +112,13 @@ def test_root_values_on_missing_file(tmp_path): def test_root_values_on_existing_file(tmp_path): """Test values on the root of the config file when there is a file.""" - ProjectMock(tmp_path).style( - """ + ProjectMock(tmp_path).style(""" [".pre-commit-config.yaml"] fail_fast = true blabla = "what" something = true another_thing = "yep" - """ - ).pre_commit( + """).pre_commit( """ repos: - hooks: @@ -160,30 +152,24 @@ def test_root_values_on_existing_file(tmp_path): def test_missing_repos(tmp_path): """Test missing repos on file.""" - ProjectMock(tmp_path).style( - """ + ProjectMock(tmp_path).style(""" [".pre-commit-config.yaml"] fail_fast = true - """ - ).pre_commit( + """).pre_commit( """ grepos: - hooks: - id: whatever """ - ).api_check_then_fix( - Fuss(True, PRE_COMMIT_CONFIG_YAML, 368, " has missing values:", "fail_fast: true") - ) + ).api_check_then_fix(Fuss(True, PRE_COMMIT_CONFIG_YAML, 368, " has missing values:", "fail_fast: true")) def test_missing_repo_key(tmp_path): """Test missing repo key on the style file.""" - ProjectMock(tmp_path).style( - """ + ProjectMock(tmp_path).style(""" [[".pre-commit-config.yaml".repos]] grepo = "glocal" - """ - ).pre_commit( + """).pre_commit( """ repos: - hooks: @@ -213,12 +199,10 @@ def test_missing_repo_key(tmp_path): def test_repo_does_not_exist(tmp_path): """Test repo does not exist on the pre-commit file.""" - ProjectMock(tmp_path).style( - """ + ProjectMock(tmp_path).style(""" [[".pre-commit-config.yaml".repos]] repo = "local" - """ - ).pre_commit( + """).pre_commit( """ repos: - hooks: @@ -243,12 +227,10 @@ def test_missing_hooks_in_repo(tmp_path): Read the warning on :py:class:`nitpick.plugins.yaml.YamlPlugin`. """ - ProjectMock(tmp_path).style( - """ + ProjectMock(tmp_path).style(""" [[".pre-commit-config.yaml".repos]] repo = "whatever" - """ - ).pre_commit( + """).pre_commit( """ repos: - repo: whatever @@ -258,12 +240,10 @@ def test_missing_hooks_in_repo(tmp_path): def test_style_missing_hooks_in_repo(tmp_path): """Test style file is missing hooks in repo.""" - ProjectMock(tmp_path).style( - """ + ProjectMock(tmp_path).style(""" [[".pre-commit-config.yaml".repos]] repo = "another" - """ - ).pre_commit( + """).pre_commit( """ repos: - repo: another @@ -289,16 +269,14 @@ def test_style_missing_id_in_hook(tmp_path): Read the warning on :py:class:`nitpick.plugins.yaml.YamlPlugin`. """ - ProjectMock(tmp_path).style( - f''' + ProjectMock(tmp_path).style(f''' [[".pre-commit-config.yaml".repos]] repo = "another" hooks = """ - name: isort entry: isort -sp {PYTHON_SETUP_CFG} """ - ''' - ).pre_commit( + ''').pre_commit( """ repos: - repo: another @@ -328,8 +306,7 @@ def test_style_missing_id_in_hook(tmp_path): def test_missing_hook_with_id(tmp_path): """Test missing hook with specific id.""" - ProjectMock(tmp_path).style( - ''' + ProjectMock(tmp_path).style(''' [[".pre-commit-config.yaml".repos]] repo = "other" hooks = """ @@ -337,8 +314,7 @@ def test_missing_hook_with_id(tmp_path): name: black entry: black """ - ''' - ).pre_commit( + ''').pre_commit( """ repos: - repo: other @@ -447,16 +423,10 @@ def test_missing_different_values(tmp_path, datadir, shared_datadir): def test_pre_commit_section_without_dot_deprecated(tmp_path): """A pre-commit section without dot is deprecated.""" - project = ( - ProjectMock(tmp_path) - .style( - """ + project = ProjectMock(tmp_path).style(""" ["pre-commit-config.yaml"] fail_fast = true - """ - ) - .pre_commit("fail_fast: true") - ) + """).pre_commit("fail_fast: true") with pytest.deprecated_call() as warning_list: project.flake8().assert_no_errors()