|
7 | 7 | import fabric_cli.core.fab_state_config as cfg |
8 | 8 |
|
9 | 9 |
|
10 | | -def test_read_config(): |
11 | | - with tempfile.TemporaryDirectory() as tmpdir: |
12 | | - tmp_file = os.path.join(tmpdir, "tmp_test.txt") |
13 | | - with open(tmp_file, "w") as file: |
14 | | - file.write('{"key": "value"}') |
15 | | - file.flush() # flush the buffer to write the data to the file before reading it |
16 | | - with open(tmp_file, "r") as file: |
17 | | - data = cfg.read_config(file.name) |
| 10 | +class TestStateConfig: |
| 11 | + """Test suite for state config read/write operations.""" |
| 12 | + |
| 13 | + def test_read_config(self): |
| 14 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 15 | + tmp_file = os.path.join(tmpdir, "tmp_test.txt") |
| 16 | + with open(tmp_file, "w") as file: |
| 17 | + file.write('{"key": "value"}') |
| 18 | + file.flush() # flush the buffer to write the data to the file before reading it |
| 19 | + with open(tmp_file, "r") as file: |
| 20 | + data = cfg.read_config(file.name) |
| 21 | + assert data == {"key": "value"} |
| 22 | + |
| 23 | + def test_read_config_missing_file(self): |
| 24 | + with tempfile.NamedTemporaryFile("w") as fp: |
| 25 | + fp.close() # close the file to delete it |
| 26 | + data = cfg.read_config(fp.name) |
| 27 | + assert data == {} |
| 28 | + |
| 29 | + def test_read_config_bad_json(self): |
| 30 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 31 | + tmp_file = os.path.join(tmpdir, "tmp_test.txt") |
| 32 | + with open(tmp_file, "w") as file: |
| 33 | + file.write('{"key": "value"') |
| 34 | + file.flush() |
| 35 | + data = cfg.read_config(tmp_file) |
| 36 | + assert data == {} |
| 37 | + |
| 38 | + def test_write_config(self, monkeypatch): |
| 39 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 40 | + tmp_file = os.path.join(tmpdir, "tmp_test.txt") |
| 41 | + with open(tmp_file, "w") as file: |
| 42 | + file.flush() |
| 43 | + monkeypatch.setattr(cfg, "config_file", tmp_file) |
| 44 | + cfg.write_config({"key": "value"}) |
| 45 | + data = cfg.read_config(tmp_file) |
18 | 46 | assert data == {"key": "value"} |
19 | 47 |
|
| 48 | + def test_get_set_config(self, monkeypatch): |
| 49 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 50 | + cfg_file = os.path.join(tmpdir, "tmp_cfg.txt") |
| 51 | + with open(cfg_file, "w") as cfg_fp: |
| 52 | + cfg_fp.write('{"key": "value"}') |
| 53 | + cfg_fp.flush() |
| 54 | + monkeypatch.setattr(cfg, "config_file", cfg_file) |
| 55 | + cfg.set_config("key2", "value2") |
| 56 | + assert cfg.get_config("key") == "value" |
| 57 | + assert cfg.get_config("key2") == "value2" |
| 58 | + |
| 59 | + def test_list_configs(self, monkeypatch): |
| 60 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 61 | + tmp_file = os.path.join(tmpdir, "tmp_test.txt") |
| 62 | + with open(tmp_file, "w") as file: |
| 63 | + file.write('{"key": "value"}') |
| 64 | + file.flush() |
| 65 | + monkeypatch.setattr(cfg, "config_file", tmp_file) |
| 66 | + cfg.set_config("key2", "value2") |
| 67 | + assert cfg.list_configs() == {"key": "value", "key2": "value2"} |
| 68 | + |
| 69 | + |
| 70 | +class TestInitDefaults: |
| 71 | + """Test suite for config initialization optimization.""" |
| 72 | + |
| 73 | + def test_init_defaults__no_write_when_unchanged(self, tmp_path, monkeypatch): |
| 74 | + """Test that init_defaults skips writing when config already has all defaults.""" |
| 75 | + import json |
| 76 | + |
| 77 | + from fabric_cli.core import fab_constant |
| 78 | + |
| 79 | + # Create a config file with all defaults already set |
| 80 | + config_data = dict(fab_constant.CONFIG_DEFAULT_VALUES) |
| 81 | + config_file = tmp_path / "config.json" |
| 82 | + config_file.write_text(json.dumps(config_data)) |
| 83 | + |
| 84 | + monkeypatch.setattr(cfg, "config_file", str(config_file)) |
| 85 | + |
| 86 | + # Track write calls |
| 87 | + original_write = cfg.write_config |
| 88 | + write_calls = [] |
| 89 | + |
| 90 | + def tracking_write(data): |
| 91 | + write_calls.append(data) |
| 92 | + original_write(data) |
| 93 | + |
| 94 | + monkeypatch.setattr(cfg, "write_config", tracking_write) |
| 95 | + |
| 96 | + cfg.init_defaults() |
20 | 97 |
|
21 | | -def test_read_config_missing_file(): |
22 | | - with tempfile.NamedTemporaryFile("w") as fp: |
23 | | - fp.close() # close the file to delete it |
24 | | - data = cfg.read_config(fp.name) |
25 | | - assert data == {} |
26 | | - |
27 | | - |
28 | | -def test_read_config_bad_json(): |
29 | | - with tempfile.TemporaryDirectory() as tmpdir: |
30 | | - tmp_file = os.path.join(tmpdir, "tmp_test.txt") |
31 | | - with open(tmp_file, "w") as file: |
32 | | - file.write('{"key": "value"') |
33 | | - file.flush() |
34 | | - data = cfg.read_config(tmp_file) |
35 | | - assert data == {} |
36 | | - |
37 | | - |
38 | | -def test_write_config(monkeypatch): |
39 | | - with tempfile.TemporaryDirectory() as tmpdir: |
40 | | - tmp_file = os.path.join(tmpdir, "tmp_test.txt") |
41 | | - with open(tmp_file, "w") as file: |
42 | | - file.flush() |
43 | | - monkeypatch.setattr(cfg, "config_file", tmp_file) |
44 | | - cfg.write_config({"key": "value"}) |
45 | | - data = cfg.read_config(tmp_file) |
46 | | - assert data == {"key": "value"} |
47 | | - |
48 | | - |
49 | | -def test_get_set_config(monkeypatch): |
50 | | - with tempfile.TemporaryDirectory() as tmpdir: |
51 | | - cfg_file = os.path.join(tmpdir, "tmp_cfg.txt") |
52 | | - with open(cfg_file, "w") as cfg_fp: |
53 | | - cfg_fp.write('{"key": "value"}') |
54 | | - cfg_fp.flush() |
55 | | - monkeypatch.setattr(cfg, "config_file", cfg_file) |
56 | | - cfg.set_config("key2", "value2") |
57 | | - assert cfg.get_config("key") == "value" |
58 | | - assert cfg.get_config("key2") == "value2" |
59 | | - |
60 | | - |
61 | | -def test_list_configs(monkeypatch): |
62 | | - with tempfile.TemporaryDirectory() as tmpdir: |
63 | | - tmp_file = os.path.join(tmpdir, "tmp_test.txt") |
64 | | - with open(tmp_file, "w") as file: |
65 | | - file.write('{"key": "value"}') |
66 | | - file.flush() |
67 | | - monkeypatch.setattr(cfg, "config_file", tmp_file) |
68 | | - cfg.set_config("key2", "value2") |
69 | | - assert cfg.list_configs() == {"key": "value", "key2": "value2"} |
| 98 | + # Should NOT have written since nothing changed |
| 99 | + assert len(write_calls) == 0, "Should skip write when config unchanged" |
0 commit comments