def project_remote(request, tmp_path): """Project with a remote style (loaded from a URL).""" from tests.helpers import ProjectMock, tomlstring remote_url = "https://example.com/remote-style.toml" remote_style = """ ["pyproject.toml".tool.black] line-length = 100 """ # https://docs.pytest.org/en/stable/fixture.html#using-markers-to-pass-data-to-fixtures marker = request.node.get_closest_marker("tool_nitpick") tool_nitpick = marker.args[0] if marker else "" with RequestsMock() as mocked_response: mocked_response.add(mocked_response.GET, remote_url, dedent(remote_style), status=200) project = ProjectMock(tmp_path) project.pyproject_toml(f""" [tool.nitpick] style = {tomlstring(remote_url)} {tool_nitpick} [tool.black] line-length = 100 """).remote(mocked_response, remote_url) yield project
def test_relative_style_on_urls(tmp_path): """Read styles from relative paths on URLs.""" base_url = "http://www.example.com/sub/folder" mapping = { "main": """ [nitpick.styles] include = "presets/python.toml" """, "presets/python": """ [nitpick.styles] include = [ "../styles/pytest.toml", "../styles/black.toml", ] """, "styles/pytest": """ ["pyproject.toml".tool.pytest] some-option = 123 """, "styles/black": """ ["pyproject.toml".tool.black] line-length = 99 missing = "value" """, } for filename, body in mapping.items(): responses.add(responses.GET, f"{base_url}/{filename}.toml", dedent(body), status=200) project = ProjectMock(tmp_path) common_pyproject = """ [tool.black] line-length = 99 [tool.pytest] some-option = 123 """ # Use full path on initial styles project.pyproject_toml(f""" [tool.nitpick] style = ["{base_url}/main"] {common_pyproject} """).api_check().assert_violations( Fuss( False, PYPROJECT_TOML, 318, " has missing values:", """ [tool.black] missing = "value" """, ))
def test_init_empty_pyproject_toml(tmp_path): """If no config file is found, create a basic .nitpick.toml.""" project = ProjectMock(tmp_path, pyproject_toml=False, setup_py=True) url = StyleManager.get_default_style_url() project.pyproject_toml("").cli_init( f"A [{TOOL_NITPICK_KEY}] section was created in the config file: {PYPROJECT_TOML}" ).assert_file_contents( PYPROJECT_TOML, f""" [{TOOL_NITPICK_KEY}] # Generated by the 'nitpick init' command # More info at {READ_THE_DOCS_URL}configuration.html style = ['{url}'] """, ) assert url.scheme == Scheme.PY
def test_invalid_tool_nitpick_on_pyproject_toml(request): """Test invalid [tool.nitpick] on pyproject.toml.""" project = ProjectMock(request) for style, error_message in [ ( 'style = [""]\nextra_values = "also raise warnings"', "extra_values: Unknown field.\nstyle[0]: Shorter than minimum length 1.", ), ('style = ""', "style: Shorter than minimum length 1."), ("style = 1", "style: Not a valid string."), ( 'style = ["some_file",""," "]', "style[1]: Shorter than minimum length 1.\nstyle[2]: Shorter than minimum length 1.", ), ]: project.pyproject_toml( "[tool.nitpick]\n{}".format(style)).lint().assert_errors_contain( "NIP001 File pyproject.toml has an incorrect style." + " Invalid data in [tool.nitpick]:\x1b[92m\n{}\x1b[0m".format( error_message), 1, )
def test_invalid_tool_nitpick_on_pyproject_toml(offline, request): """Test invalid [tool.nitpick] on pyproject.toml.""" project = ProjectMock(request) for style, error_message in [ ( 'style = [""]\nextra_values = "also raise warnings"', "extra_values: Unknown configuration. See https://nitpick.rtfd.io/en/latest/tool_nitpick_section.html." + "\nstyle.0: Shorter than minimum length 1.", ), ('style = ""', "style: Shorter than minimum length 1."), ("style = 1", "style: Not a valid string."), ( 'style = ["some_file",""," "]', "style.1: Shorter than minimum length 1.\nstyle.2: Shorter than minimum length 1.", ), ]: project.pyproject_toml("[tool.nitpick]\n{}".format(style)).flake8( offline=offline).assert_errors_contain( "NIP001 File pyproject.toml has an incorrect style." + " Invalid data in [tool.nitpick]:\x1b[32m\n{}\x1b[0m".format( error_message), 1, )
def test_invalid_tool_nitpick_on_pyproject_toml(offline, tmp_path): """Test invalid [tool.nitpick] on pyproject.toml.""" project = ProjectMock(tmp_path) for style, error_message in [ ( 'style = [""]\nextra_values = "also raise warnings"', f"extra_values: Unknown configuration. See {READ_THE_DOCS_URL}configuration.html." + "\nstyle.0: Shorter than minimum length 1.", ), ('style = ""', "style: Shorter than minimum length 1."), ("style = 1", "style: Not a valid string."), ( 'style = ["some_file",""," "]', "style.1: Shorter than minimum length 1.\nstyle.2: Shorter than minimum length 1.", ), ]: project.pyproject_toml(f"[tool.nitpick]\n{style}").flake8( offline=offline ).assert_errors_contain( "NIP001 File pyproject.toml has an incorrect style." + f" Invalid data in [tool.nitpick]:{SUGGESTION_BEGIN}\n{error_message}{SUGGESTION_END}", 1, )
def test_relative_style_on_urls(request): """Read styles from relative paths on URLs.""" base_url = "http://www.example.com/sub/folder" mapping = { "main": """ [nitpick.styles] include = "styles/pytest.toml" """, "styles/pytest": """ ["pyproject.toml".tool.pytest] some-option = 123 """, "styles/black": """ ["pyproject.toml".tool.black] line-length = 99 missing = "value" """, "poetry": """ ["pyproject.toml".tool.poetry] version = "1.0" """, } for file_name, body in mapping.items(): responses.add(responses.GET, "{}/{}.toml".format(base_url, file_name), dedent(body), status=200) project = ProjectMock(request) common_pyproject = """ [tool.black] line-length = 99 [tool.pytest] some-option = 123 """ # Use full path on initial styles project.pyproject_toml(""" [tool.nitpick] style = ["{base_url}/main", "{base_url}/styles/black.toml"] {common_pyproject} """.format( base_url=base_url, common_pyproject=common_pyproject)).flake8().assert_single_error(""" NIP318 File pyproject.toml has missing values:\x1b[32m [tool.black] missing = "value"\x1b[0m """) # Reuse the first full path that appears project.pyproject_toml(""" [tool.nitpick] style = ["{}/main.toml", "styles/black"] {} """.format(base_url, common_pyproject)).flake8().assert_single_error(""" NIP318 File pyproject.toml has missing values:\x1b[32m [tool.black] missing = "value"\x1b[0m """) # Allow relative paths project.pyproject_toml(""" [tool.nitpick] style = ["{}/styles/black.toml", "../poetry"] {} """.format(base_url, common_pyproject)).flake8().assert_single_error(""" NIP318 File pyproject.toml has missing values:\x1b[32m [tool.black] missing = "value" [tool.poetry] version = "1.0"\x1b[0m """)