Skip to content

Commit 2f35231

Browse files
mjohanse-emrMichael Johansen
andauthored
Update _get_application_import_names to have fall-backs (#234)
* Update _get_application_import_names to have fall-backs Signed-off-by: Michael Johansen <[email protected]> * Handle import-names as an array Signed-off-by: Michael Johansen <[email protected]> * Add unit tests for _get_application_import_names(). Signed-off-by: Michael Johansen <[email protected]> * Fix linting error. Signed-off-by: Michael Johansen <[email protected]> --------- Signed-off-by: Michael Johansen <[email protected]> Co-authored-by: Michael Johansen <[email protected]>
1 parent 3d83096 commit 2f35231

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

ni_python_styleguide/_cli.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,23 @@ def _read_pyproject_toml(ctx, param, value):
3939

4040
def _get_application_import_names(pyproject):
4141
"""Returns the application package name the config."""
42-
# Otherwise override with what was specified
42+
# Use the tool-specific import names if specified
4343
app_name = (
4444
pyproject.get("tool", {})
4545
.get("ni-python-styleguide", {})
4646
.get("application-import-names", "")
4747
)
4848

49-
# Allow the poetry name as a fallback
49+
# Next fall back to project.import-names
50+
if not app_name:
51+
import_names = pyproject.get("project", {}).get("import-names", [])
52+
app_name = ",".join(import_names)
53+
54+
# Next fall back to project.name (replace hyphens)
55+
if not app_name:
56+
app_name = pyproject.get("project", {}).get("name", "").replace("-", "_")
57+
58+
# Next fall back to tool.poetry.name (replace hyphens)
5059
if not app_name:
5160
app_name = pyproject.get("tool", {}).get("poetry", {}).get("name", "").replace("-", "_")
5261

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""Tests for the "_get_application_import_names" method of _cli.py."""
2+
3+
import pytest
4+
5+
from ni_python_styleguide._cli import _get_application_import_names
6+
7+
8+
@pytest.mark.parametrize(
9+
"pyproject_obj, expected_names",
10+
[
11+
(
12+
{
13+
"tool": {
14+
"ni-python-styleguide": {"application-import-names": "ain1,ain2"},
15+
"poetry": {"name": "tool.poetry.name"},
16+
},
17+
"project": {
18+
"import-names": ["import-names1", "import-names2"],
19+
"name": "project.name",
20+
},
21+
},
22+
"ain1,ain2,tests",
23+
),
24+
(
25+
{
26+
"tool": {"poetry": {"name": "tool.poetry.name"}},
27+
"project": {
28+
"import-names": ["import-names1", "import-names2"],
29+
"name": "project.name",
30+
},
31+
},
32+
"import-names1,import-names2,tests",
33+
),
34+
(
35+
{"tool": {"poetry": {"name": "tool.poetry.name"}}, "project": {"name": "project.name"}},
36+
"project.name,tests",
37+
),
38+
({"tool": {"poetry": {"name": "tool.poetry.name"}}}, "tool.poetry.name,tests"),
39+
],
40+
)
41+
def test_get_application_import_names_returns_valid_data(pyproject_obj, expected_names):
42+
"""Tests that _get_application_import_names returns valid data."""
43+
result = _get_application_import_names(pyproject_obj)
44+
assert result == expected_names

0 commit comments

Comments
 (0)