Skip to content

Commit

Permalink
Fix TOML booleans (#104)
Browse files Browse the repository at this point in the history
Fix TOML booleans

The main application logic assumes, that booleans are passed as strings from the config.
However, TOML supports native booleans which are obviously no strings.
  • Loading branch information
kasium committed Sep 2, 2021
1 parent a7bb1c8 commit 92ceeae
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/importlinter/adapters/user_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,14 @@ def _read_config_filename(self, config_filename: str) -> Optional[UserOptions]:
return None

contracts = session_options.pop("contracts", [])

self._normalize_booleans(session_options)
for contract in contracts:
self._normalize_booleans(contract)

return UserOptions(session_options=session_options, contracts_options=contracts)

def _normalize_booleans(self, data: dict) -> None:
for key, value in data.items():
if isinstance(value, bool):
data[key] = str(value)
9 changes: 9 additions & 0 deletions tests/assets/testpackage/.externalkeptcontract.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[tool.importlinter]
root_package = "testpackage"
include_external_packages = true

[[tool.importlinter.contracts]]
name = "External kept contract"
type = "forbidden"
source_modules = ["testpackage.high.blue"]
forbidden_modules = ["sqlalchemy"]
6 changes: 6 additions & 0 deletions tests/functional/test_lint_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
marks=pytest.mark.toml_installed,
),
(testpackage_directory, ".externalkeptcontract.ini", cli.EXIT_STATUS_SUCCESS),
pytest.param(
testpackage_directory,
".externalkeptcontract.toml",
cli.EXIT_STATUS_SUCCESS,
marks=pytest.mark.toml_installed,
),
(testpackage_directory, ".externalbrokencontract.ini", cli.EXIT_STATUS_ERROR),
(multipleroots_directory, ".multiplerootskeptcontract.ini", cli.EXIT_STATUS_SUCCESS),
(multipleroots_directory, ".multiplerootsbrokencontract.ini", cli.EXIT_STATUS_ERROR),
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/adapters/test_user_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ def test_respects_passed_filename(passed_filename, expected_foo_value):
"""
[tool.importlinter]
foo = "hello"
include_external_packages = true
[[tool.importlinter.contracts]]
name = "Contract One"
Expand All @@ -166,7 +167,7 @@ def test_respects_passed_filename(passed_filename, expected_foo_value):
baz = 3
""",
UserOptions(
session_options={"foo": "hello"},
session_options={"foo": "hello", "include_external_packages": "True"},
contracts_options=[
{
"name": "Contract One",
Expand Down

0 comments on commit 92ceeae

Please sign in to comment.