|
| 1 | +import json |
| 2 | +import tempfile |
| 3 | +from pathlib import Path |
| 4 | +from unittest.mock import patch |
| 5 | + |
| 6 | +from arm_cli.config import Config, get_config_dir, get_config_file, load_config, save_config |
| 7 | + |
| 8 | + |
| 9 | +class TestConfig: |
| 10 | + def test_config_default_values(self): |
| 11 | + """Test that Config has correct default values.""" |
| 12 | + config = Config() |
| 13 | + assert config.active_project == "" |
| 14 | + |
| 15 | + def test_config_with_values(self): |
| 16 | + """Test that Config can be created with custom values.""" |
| 17 | + config = Config(active_project="test-project") |
| 18 | + assert config.active_project == "test-project" |
| 19 | + |
| 20 | + def test_config_model_dump(self): |
| 21 | + """Test that Config can be serialized to dict.""" |
| 22 | + config = Config(active_project="test-project") |
| 23 | + data = config.model_dump() |
| 24 | + assert data == {"active_project": "test-project"} |
| 25 | + |
| 26 | + |
| 27 | +class TestConfigFunctions: |
| 28 | + def test_get_config_dir(self): |
| 29 | + """Test that config directory is created correctly.""" |
| 30 | + with patch("arm_cli.config.appdirs.user_config_dir") as mock_user_config_dir: |
| 31 | + mock_user_config_dir.return_value = "/tmp/test_config" |
| 32 | + |
| 33 | + config_dir = get_config_dir() |
| 34 | + |
| 35 | + assert config_dir == Path("/tmp/test_config") |
| 36 | + mock_user_config_dir.assert_called_once_with("mycli") |
| 37 | + |
| 38 | + def test_get_config_file(self): |
| 39 | + """Test that config file path is correct.""" |
| 40 | + with patch("arm_cli.config.get_config_dir") as mock_get_config_dir: |
| 41 | + mock_get_config_dir.return_value = Path("/tmp/test_config") |
| 42 | + |
| 43 | + config_file = get_config_file() |
| 44 | + |
| 45 | + assert config_file == Path("/tmp/test_config/config.json") |
| 46 | + |
| 47 | + def test_save_config(self): |
| 48 | + """Test that config can be saved to file.""" |
| 49 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 50 | + with patch("arm_cli.config.get_config_file") as mock_get_config_file: |
| 51 | + config_file = Path(temp_dir) / "config.json" |
| 52 | + mock_get_config_file.return_value = config_file |
| 53 | + |
| 54 | + config = Config(active_project="test-project") |
| 55 | + save_config(config) |
| 56 | + |
| 57 | + assert config_file.exists() |
| 58 | + with open(config_file, "r") as f: |
| 59 | + data = json.load(f) |
| 60 | + |
| 61 | + assert data == {"active_project": "test-project"} |
| 62 | + |
| 63 | + def test_load_config_new_file(self): |
| 64 | + """Test that new config file is created when it doesn't exist.""" |
| 65 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 66 | + with patch("arm_cli.config.get_config_file") as mock_get_config_file: |
| 67 | + config_file = Path(temp_dir) / "config.json" |
| 68 | + mock_get_config_file.return_value = config_file |
| 69 | + |
| 70 | + # File doesn't exist initially |
| 71 | + assert not config_file.exists() |
| 72 | + |
| 73 | + config = load_config() |
| 74 | + |
| 75 | + # File should be created with default values |
| 76 | + assert config_file.exists() |
| 77 | + assert config.active_project == "" |
| 78 | + |
| 79 | + # Verify file contents |
| 80 | + with open(config_file, "r") as f: |
| 81 | + data = json.load(f) |
| 82 | + assert data == {"active_project": ""} |
| 83 | + |
| 84 | + def test_load_config_existing_file(self): |
| 85 | + """Test that existing config file is loaded correctly.""" |
| 86 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 87 | + with patch("arm_cli.config.get_config_file") as mock_get_config_file: |
| 88 | + config_file = Path(temp_dir) / "config.json" |
| 89 | + mock_get_config_file.return_value = config_file |
| 90 | + |
| 91 | + # Create existing config file |
| 92 | + existing_data = {"active_project": "existing-project"} |
| 93 | + with open(config_file, "w") as f: |
| 94 | + json.dump(existing_data, f) |
| 95 | + |
| 96 | + config = load_config() |
| 97 | + |
| 98 | + assert config.active_project == "existing-project" |
| 99 | + |
| 100 | + def test_load_config_corrupted_file(self): |
| 101 | + """Test that corrupted config file is handled gracefully.""" |
| 102 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 103 | + with patch("arm_cli.config.get_config_file") as mock_get_config_file: |
| 104 | + config_file = Path(temp_dir) / "config.json" |
| 105 | + mock_get_config_file.return_value = config_file |
| 106 | + |
| 107 | + # Create corrupted config file |
| 108 | + with open(config_file, "w") as f: |
| 109 | + f.write("invalid json content") |
| 110 | + |
| 111 | + config = load_config() |
| 112 | + |
| 113 | + # Should create new default config |
| 114 | + assert config.active_project == "" |
| 115 | + |
| 116 | + # Verify file was overwritten with valid JSON |
| 117 | + with open(config_file, "r") as f: |
| 118 | + data = json.load(f) |
| 119 | + assert data == {"active_project": ""} |
| 120 | + |
| 121 | + def test_load_config_missing_fields(self): |
| 122 | + """Test that config with missing fields is handled gracefully.""" |
| 123 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 124 | + with patch("arm_cli.config.get_config_file") as mock_get_config_file: |
| 125 | + config_file = Path(temp_dir) / "config.json" |
| 126 | + mock_get_config_file.return_value = config_file |
| 127 | + |
| 128 | + # Create config file with missing fields |
| 129 | + with open(config_file, "w") as f: |
| 130 | + json.dump({}, f) |
| 131 | + |
| 132 | + config = load_config() |
| 133 | + |
| 134 | + # Should use default values for missing fields |
| 135 | + assert config.active_project == "" |
0 commit comments