|
| 1 | +"""Tests for custom config path functionality. |
| 2 | +
|
| 3 | +This module tests the --config option that allows specifying a custom config file |
| 4 | +instead of the default config.yaml in the truss directory. |
| 5 | +""" |
| 6 | + |
| 7 | +import shutil |
| 8 | +import tempfile |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | +import pytest |
| 12 | +import yaml |
| 13 | + |
| 14 | +from truss.base.truss_spec import TrussSpec |
| 15 | +from truss.truss_handle.build import load |
| 16 | +from truss.truss_handle.truss_handle import TrussHandle |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture |
| 20 | +def truss_dir_with_multiple_configs(test_data_path: Path): |
| 21 | + """Create a truss directory with multiple config files for testing.""" |
| 22 | + source_dir = test_data_path / "test_basic_truss" |
| 23 | + with tempfile.TemporaryDirectory() as tmp_dir: |
| 24 | + tmp_path = Path(tmp_dir) |
| 25 | + truss_path = tmp_path / "test_truss" |
| 26 | + shutil.copytree(source_dir, truss_path) |
| 27 | + |
| 28 | + # Read the original config |
| 29 | + original_config_path = truss_path / "config.yaml" |
| 30 | + with open(original_config_path) as f: |
| 31 | + original_config = yaml.safe_load(f) |
| 32 | + |
| 33 | + # Create an alternate config with different model_name |
| 34 | + alt_config = original_config.copy() |
| 35 | + alt_config["model_name"] = "alternate-model" |
| 36 | + alt_config_path = truss_path / "config.dev.yaml" |
| 37 | + with open(alt_config_path, "w") as f: |
| 38 | + yaml.dump(alt_config, f) |
| 39 | + |
| 40 | + # Create another alternate config outside the truss directory |
| 41 | + external_config = original_config.copy() |
| 42 | + external_config["model_name"] = "external-model" |
| 43 | + external_config_path = tmp_path / "external_config.yaml" |
| 44 | + with open(external_config_path, "w") as f: |
| 45 | + yaml.dump(external_config, f) |
| 46 | + |
| 47 | + yield { |
| 48 | + "truss_dir": truss_path, |
| 49 | + "default_config": original_config_path, |
| 50 | + "alt_config": alt_config_path, |
| 51 | + "external_config": external_config_path, |
| 52 | + "original_model_name": original_config.get("model_name"), |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | +class TestTrussSpecCustomConfigPath: |
| 57 | + """Tests for TrussSpec with custom config path.""" |
| 58 | + |
| 59 | + def test_default_config_path_backwards_compatibility( |
| 60 | + self, truss_dir_with_multiple_configs |
| 61 | + ): |
| 62 | + """Test that TrussSpec without config_path uses default config.yaml.""" |
| 63 | + data = truss_dir_with_multiple_configs |
| 64 | + spec = TrussSpec(data["truss_dir"]) |
| 65 | + |
| 66 | + # Should load from default config.yaml |
| 67 | + assert spec.config_path == data["default_config"] |
| 68 | + assert spec.config.model_name == data["original_model_name"] |
| 69 | + |
| 70 | + def test_custom_config_path_in_truss_dir(self, truss_dir_with_multiple_configs): |
| 71 | + """Test that TrussSpec with config_path loads from that file.""" |
| 72 | + data = truss_dir_with_multiple_configs |
| 73 | + spec = TrussSpec(data["truss_dir"], config_path=data["alt_config"]) |
| 74 | + |
| 75 | + # Should load from alternate config |
| 76 | + assert spec.config_path == data["alt_config"] |
| 77 | + assert spec.config.model_name == "alternate-model" |
| 78 | + |
| 79 | + def test_custom_config_path_external(self, truss_dir_with_multiple_configs): |
| 80 | + """Test that TrussSpec with external config_path works.""" |
| 81 | + data = truss_dir_with_multiple_configs |
| 82 | + spec = TrussSpec(data["truss_dir"], config_path=data["external_config"]) |
| 83 | + |
| 84 | + # Should load from external config |
| 85 | + assert spec.config_path == data["external_config"] |
| 86 | + assert spec.config.model_name == "external-model" |
| 87 | + |
| 88 | + |
| 89 | +class TestTrussHandleCustomConfigPath: |
| 90 | + """Tests for TrussHandle with custom config path.""" |
| 91 | + |
| 92 | + def test_default_config_path_backwards_compatibility( |
| 93 | + self, truss_dir_with_multiple_configs |
| 94 | + ): |
| 95 | + """Test that TrussHandle without config_path uses default config.yaml.""" |
| 96 | + data = truss_dir_with_multiple_configs |
| 97 | + handle = TrussHandle(data["truss_dir"]) |
| 98 | + |
| 99 | + # Should load from default config.yaml |
| 100 | + assert handle.spec.config_path == data["default_config"] |
| 101 | + assert handle.spec.config.model_name == data["original_model_name"] |
| 102 | + |
| 103 | + def test_custom_config_path(self, truss_dir_with_multiple_configs): |
| 104 | + """Test that TrussHandle with config_path loads from that file.""" |
| 105 | + data = truss_dir_with_multiple_configs |
| 106 | + handle = TrussHandle(data["truss_dir"], config_path=data["alt_config"]) |
| 107 | + |
| 108 | + # Should load from alternate config |
| 109 | + assert handle.spec.config_path == data["alt_config"] |
| 110 | + assert handle.spec.config.model_name == "alternate-model" |
| 111 | + |
| 112 | + def test_update_config_preserves_custom_config_path( |
| 113 | + self, truss_dir_with_multiple_configs |
| 114 | + ): |
| 115 | + """Test that _update_config preserves the custom config path after reload.""" |
| 116 | + data = truss_dir_with_multiple_configs |
| 117 | + handle = TrussHandle(data["truss_dir"], config_path=data["alt_config"]) |
| 118 | + |
| 119 | + # Update the config (this triggers a reload) |
| 120 | + handle._update_config(description="Test description") |
| 121 | + |
| 122 | + # After reload, should still use the custom config path |
| 123 | + assert handle.spec.config_path == data["alt_config"] |
| 124 | + assert handle.spec.config.model_name == "alternate-model" |
| 125 | + assert handle.spec.config.description == "Test description" |
| 126 | + |
| 127 | + |
| 128 | +class TestLoadFunctionCustomConfigPath: |
| 129 | + """Tests for the load() function with custom config path.""" |
| 130 | + |
| 131 | + def test_load_default_config_path_backwards_compatibility( |
| 132 | + self, truss_dir_with_multiple_configs |
| 133 | + ): |
| 134 | + """Test that load() without config_path uses default config.yaml.""" |
| 135 | + data = truss_dir_with_multiple_configs |
| 136 | + handle = load(data["truss_dir"]) |
| 137 | + |
| 138 | + # Should load from default config.yaml |
| 139 | + assert handle.spec.config_path == data["default_config"] |
| 140 | + assert handle.spec.config.model_name == data["original_model_name"] |
| 141 | + |
| 142 | + def test_load_with_custom_config_path(self, truss_dir_with_multiple_configs): |
| 143 | + """Test that load() with config_path loads from that file.""" |
| 144 | + data = truss_dir_with_multiple_configs |
| 145 | + handle = load(data["truss_dir"], config_path=data["alt_config"]) |
| 146 | + |
| 147 | + # Should load from alternate config |
| 148 | + assert handle.spec.config_path == data["alt_config"] |
| 149 | + assert handle.spec.config.model_name == "alternate-model" |
| 150 | + |
| 151 | + def test_load_with_external_config_path(self, truss_dir_with_multiple_configs): |
| 152 | + """Test that load() with external config_path works.""" |
| 153 | + data = truss_dir_with_multiple_configs |
| 154 | + handle = load(data["truss_dir"], config_path=data["external_config"]) |
| 155 | + |
| 156 | + # Should load from external config |
| 157 | + assert handle.spec.config_path == data["external_config"] |
| 158 | + assert handle.spec.config.model_name == "external-model" |
0 commit comments