-
Notifications
You must be signed in to change notification settings - Fork 7
Configuration normalization #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| import tempfile | ||
| import os | ||
| import sys | ||
| from pathlib import Path | ||
| from unittest.mock import patch | ||
|
|
||
| from nextmeeting.cli import _load_config, parse_args | ||
|
|
||
|
|
||
| def test_load_config_normalizes_hyphens_to_underscores(): | ||
| """Test that configuration keys with hyphens are normalized to underscores.""" | ||
| with tempfile.NamedTemporaryFile(mode="w", suffix=".toml", delete=False) as f: | ||
| f.write("""[nextmeeting] | ||
| caldav-url = "https://example.com/calendar" | ||
| caldav-username = "user" | ||
| caldav-password = "pass" | ||
| max-title-length = 30 | ||
| today-only = true | ||
| """) | ||
| config_path = Path(f.name) | ||
|
|
||
| try: | ||
| config = _load_config(config_path) | ||
|
|
||
| # All keys should be normalized to underscores | ||
| assert config["caldav_url"] == "https://example.com/calendar" | ||
| assert config["caldav_username"] == "user" | ||
| assert config["caldav_password"] == "pass" | ||
| assert config["max_title_length"] == 30 | ||
| assert config["today_only"] is True | ||
|
|
||
| # Hyphens should not exist in the keys | ||
| assert "caldav-url" not in config | ||
| assert "caldav-username" not in config | ||
| assert "caldav-password" not in config | ||
| assert "max-title-length" not in config | ||
| assert "today-only" not in config | ||
| finally: | ||
| os.unlink(config_path) | ||
|
Comment on lines
+10
to
+39
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For cleaner and more idiomatic pytest tests, consider using the def test_load_config_normalizes_hyphens_to_underscores(tmp_path: Path):
"""Test that configuration keys with hyphens are normalized to underscores."""
config_path = tmp_path / "config.toml"
config_path.write_text("""[nextmeeting]
caldav-url = "https://example.com/calendar"
caldav-username = "user"
caldav-password = "pass"
max-title-length = 30
today-only = true
""")
config = _load_config(config_path)
# All keys should be normalized to underscores
assert config["caldav_url"] == "https://example.com/calendar"
assert config["caldav_username"] == "user"
assert config["caldav_password"] == "pass"
assert config["max_title_length"] == 30
assert config["today_only"] is True
# Hyphens should not exist in the keys
assert "caldav-url" not in config
assert "caldav-username" not in config
assert "caldav-password" not in config
assert "max-title-length" not in config
assert "today-only" not in config |
||
|
|
||
|
|
||
| def test_load_config_works_with_both_formats(): | ||
| """Test that both hyphen and underscore formats work in config files.""" | ||
| # Test with hyphens (README format) | ||
| with tempfile.NamedTemporaryFile(mode="w", suffix=".toml", delete=False) as f: | ||
| f.write("""[nextmeeting] | ||
| caldav-url = "https://example.com/hyphens" | ||
| caldav-username = "user-hyphens" | ||
| """) | ||
| config_path_hyphens = Path(f.name) | ||
|
|
||
| # Test with underscores (internal format) | ||
| with tempfile.NamedTemporaryFile(mode="w", suffix=".toml", delete=False) as f: | ||
| f.write("""[nextmeeting] | ||
| caldav_url = "https://example.com/underscores" | ||
| caldav_username = "user_underscores" | ||
| """) | ||
| config_path_underscores = Path(f.name) | ||
|
|
||
| try: | ||
| # Both should work and produce the same key format | ||
| config_hyphens = _load_config(config_path_hyphens) | ||
| config_underscores = _load_config(config_path_underscores) | ||
|
|
||
| # Both should have normalized underscore keys | ||
| assert "caldav_url" in config_hyphens | ||
| assert "caldav_username" in config_hyphens | ||
| assert "caldav_url" in config_underscores | ||
| assert "caldav_username" in config_underscores | ||
|
|
||
| # Values should be preserved | ||
| assert config_hyphens["caldav_url"] == "https://example.com/hyphens" | ||
| assert config_hyphens["caldav_username"] == "user-hyphens" | ||
| assert config_underscores["caldav_url"] == "https://example.com/underscores" | ||
| assert config_underscores["caldav_username"] == "user_underscores" | ||
|
|
||
| finally: | ||
| os.unlink(config_path_hyphens) | ||
| os.unlink(config_path_underscores) | ||
|
Comment on lines
+42
to
+79
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to the previous test, this can be simplified using the def test_load_config_works_with_both_formats(tmp_path: Path):
"""Test that both hyphen and underscore formats work in config files."""
# Test with hyphens (README format)
config_content_hyphens = """[nextmeeting]
caldav-url = "https://example.com/hyphens"
caldav-username = "user-hyphens"
"""
config_path_hyphens = tmp_path / "config_hyphens.toml"
config_path_hyphens.write_text(config_content_hyphens)
# Test with underscores (internal format)
config_content_underscores = """[nextmeeting]
caldav_url = "https://example.com/underscores"
caldav_username = "user_underscores"
"""
config_path_underscores = tmp_path / "config_underscores.toml"
config_path_underscores.write_text(config_content_underscores)
# Both should work and produce the same key format
config_hyphens = _load_config(config_path_hyphens)
config_underscores = _load_config(config_path_underscores)
# Both should have normalized underscore keys
assert "caldav_url" in config_hyphens
assert "caldav_username" in config_hyphens
assert "caldav_url" in config_underscores
assert "caldav_username" in config_underscores
# Values should be preserved
assert config_hyphens["caldav_url"] == "https://example.com/hyphens"
assert config_hyphens["caldav_username"] == "user-hyphens"
assert config_underscores["caldav_url"] == "https://example.com/underscores"
assert config_underscores["caldav_username"] == "user_underscores"
Comment on lines
+42
to
+79
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test can be made more concise and maintainable by using @pytest.mark.parametrize(
("config_content", "expected_values"),
[
(
"""[nextmeeting]
caldav-url = "https://example.com/hyphens"
caldav-username = "user-hyphens"
""",
{
"caldav_url": "https://example.com/hyphens",
"caldav_username": "user-hyphens",
},
),
(
"""[nextmeeting]
caldav_url = "https://example.com/underscores"
caldav_username = "user_underscores"
""",
{
"caldav_url": "https://example.com/underscores",
"caldav_username": "user_underscores",
},
),
],
)
def test_load_config_works_with_both_formats(tmp_path: Path, config_content: str, expected_values: dict):
"""Test that both hyphen and underscore formats work in config files."""
config_path = tmp_path / "config.toml"
config_path.write_text(config_content)
config = _load_config(config_path)
assert config == expected_values |
||
|
|
||
|
|
||
| def test_parse_args_accepts_config_with_hyphens(): | ||
| """Test that parse_args properly handles config files with hyphenated keys.""" | ||
| with tempfile.NamedTemporaryFile(mode="w", suffix=".toml", delete=False) as f: | ||
| f.write("""[nextmeeting] | ||
| caldav-url = "https://example.com/config-test" | ||
| caldav-username = "config-user" | ||
| max-title-length = 25 | ||
| """) | ||
| config_path = f.name | ||
|
|
||
| # Clear environment variables that might interfere | ||
| with patch.dict(os.environ, {}, clear=True): | ||
| original_argv = sys.argv | ||
| try: | ||
| sys.argv = ["nextmeeting", "--config", config_path] | ||
| args = parse_args() | ||
|
|
||
| # The arguments should be accessible with underscores | ||
| assert getattr(args, "caldav_url") == "https://example.com/config-test" | ||
| assert getattr(args, "caldav_username") == "config-user" | ||
| assert getattr(args, "max_title_length") == 25 | ||
|
|
||
| finally: | ||
| sys.argv = original_argv | ||
| os.unlink(config_path) | ||
|
Comment on lines
+82
to
+106
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test can also be refactored to use the def test_parse_args_accepts_config_with_hyphens(tmp_path: Path):
"""Test that parse_args properly handles config files with hyphenated keys."""
config_content = """[nextmeeting]
caldav-url = "https://example.com/config-test"
caldav-username = "config-user"
max-title-length = 25
"""
config_path = tmp_path / "config.toml"
config_path.write_text(config_content)
# Clear environment variables that might interfere
with patch.dict(os.environ, {}, clear=True):
original_argv = sys.argv
try:
sys.argv = ["nextmeeting", "--config", str(config_path)]
args = parse_args()
# The arguments should be accessible with underscores
assert getattr(args, "caldav_url") == "https://example.com/config-test"
assert getattr(args, "caldav_username") == "config-user"
assert getattr(args, "max_title_length") == 25
finally:
sys.argv = original_argv
Comment on lines
+82
to
+106
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly to the other tests in this file, using the def test_parse_args_accepts_config_with_hyphens(tmp_path: Path):
"""Test that parse_args properly handles config files with hyphenated keys."""
config_path = tmp_path / "config.toml"
config_path.write_text("""[nextmeeting]
caldav-url = "https://example.com/config-test"
caldav-username = "config-user"
max-title-length = 25
""")
# Clear environment variables that might interfere
with patch.dict(os.environ, {}, clear=True):
original_argv = sys.argv
try:
sys.argv = ["nextmeeting", "--config", str(config_path)]
args = parse_args()
# The arguments should be accessible with underscores
assert args.caldav_url == "https://example.com/config-test"
assert args.caldav_username == "config-user"
assert args.max_title_length == 25
finally:
sys.argv = original_argv |
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test can be simplified by using pytest's built-in
tmp_pathfixture. It automatically creates and cleans up a temporary directory for the test, removing the need fortempfile.NamedTemporaryFilewithdelete=Falseand manualos.unlinkin atry...finallyblock. This makes the test cleaner, more readable, and less prone to errors if cleanup fails.