diff --git a/ni_python_styleguide/_cli.py b/ni_python_styleguide/_cli.py index 1ec7596b..55bba043 100644 --- a/ni_python_styleguide/_cli.py +++ b/ni_python_styleguide/_cli.py @@ -39,14 +39,23 @@ def _read_pyproject_toml(ctx, param, value): def _get_application_import_names(pyproject): """Returns the application package name the config.""" - # Otherwise override with what was specified + # Use the tool-specific import names if specified app_name = ( pyproject.get("tool", {}) .get("ni-python-styleguide", {}) .get("application-import-names", "") ) - # Allow the poetry name as a fallback + # Next fall back to project.import-names + if not app_name: + import_names = pyproject.get("project", {}).get("import-names", []) + app_name = ",".join(import_names) + + # Next fall back to project.name (replace hyphens) + if not app_name: + app_name = pyproject.get("project", {}).get("name", "").replace("-", "_") + + # Next fall back to tool.poetry.name (replace hyphens) if not app_name: app_name = pyproject.get("tool", {}).get("poetry", {}).get("name", "").replace("-", "_") diff --git a/tests/test_cli/test_application_import_names.py b/tests/test_cli/test_application_import_names.py new file mode 100644 index 00000000..819cd57b --- /dev/null +++ b/tests/test_cli/test_application_import_names.py @@ -0,0 +1,44 @@ +"""Tests for the "_get_application_import_names" method of _cli.py.""" + +import pytest + +from ni_python_styleguide._cli import _get_application_import_names + + +@pytest.mark.parametrize( + "pyproject_obj, expected_names", + [ + ( + { + "tool": { + "ni-python-styleguide": {"application-import-names": "ain1,ain2"}, + "poetry": {"name": "tool.poetry.name"}, + }, + "project": { + "import-names": ["import-names1", "import-names2"], + "name": "project.name", + }, + }, + "ain1,ain2,tests", + ), + ( + { + "tool": {"poetry": {"name": "tool.poetry.name"}}, + "project": { + "import-names": ["import-names1", "import-names2"], + "name": "project.name", + }, + }, + "import-names1,import-names2,tests", + ), + ( + {"tool": {"poetry": {"name": "tool.poetry.name"}}, "project": {"name": "project.name"}}, + "project.name,tests", + ), + ({"tool": {"poetry": {"name": "tool.poetry.name"}}}, "tool.poetry.name,tests"), + ], +) +def test_get_application_import_names_returns_valid_data(pyproject_obj, expected_names): + """Tests that _get_application_import_names returns valid data.""" + result = _get_application_import_names(pyproject_obj) + assert result == expected_names