33from modelgauge .config import (
44 DEFAULT_SECRETS ,
55 MissingSecretsFromConfig ,
6+ find_config_dir ,
67 load_secrets_from_config ,
78 raise_if_missing_from_config ,
89 write_default_config ,
910)
1011from modelgauge .secret_values import MissingSecretValues , SecretDescription
1112
1213
14+ def test_find_config_dir (tmpdir ):
15+ config_dir = tmpdir .join ("config" )
16+ os .makedirs (config_dir )
17+ found_dir = find_config_dir (str (tmpdir ))
18+ assert found_dir == config_dir
19+
20+
21+ def test_find_config_dir_searches_up_tree (tmpdir ):
22+ config_dir = tmpdir .join ("config" )
23+ os .makedirs (config_dir )
24+ sub_dir = tmpdir .join ("subdir" )
25+ os .makedirs (sub_dir )
26+ found_dir = find_config_dir (str (sub_dir ))
27+ assert found_dir == config_dir
28+
29+
30+ def test_find_config_dir_no_config (tmpdir ):
31+ with pytest .raises (FileNotFoundError ):
32+ find_config_dir (str (tmpdir ))
33+
34+
1335def test_write_default_config_writes_files (tmpdir ):
36+ write_default_config (tmpdir )
1437 config_dir = tmpdir .join ("config" )
15- write_default_config (config_dir )
1638 files = [f .basename for f in config_dir .listdir ()]
1739 assert files == ["secrets.toml" ]
1840
1941
2042def test_write_default_config_skips_existing_dir (tmpdir ):
2143 config_dir = tmpdir .join ("config" )
2244 os .makedirs (config_dir )
23- write_default_config (config_dir )
45+ write_default_config (tmpdir )
2446 files = [f .basename for f in config_dir .listdir ()]
2547 # No files created
2648 assert files == []
2749
2850
29- def test_load_secrets_from_config_loads_default (tmpdir ):
51+ def test_write_default_config_searches_up_tree (tmpdir ):
3052 config_dir = tmpdir .join ("config" )
31- write_default_config (config_dir )
32- secrets_file = config_dir .join (DEFAULT_SECRETS )
53+ os .makedirs (config_dir )
54+ sub_dir = tmpdir .join ("subdir" )
55+ os .makedirs (sub_dir )
56+ write_default_config (sub_dir )
57+ # Nothing created in subdir
58+ assert not os .path .exists (sub_dir .join ("config" ))
3359
34- assert load_secrets_from_config (secrets_file ) == {"demo" : {"api_key" : "12345" }}
60+
61+ def test_load_secrets_from_config_loads_default (tmpdir ):
62+ write_default_config (tmpdir )
63+ assert load_secrets_from_config (tmpdir ) == {"demo" : {"api_key" : "12345" }}
64+
65+
66+ def test_load_secrets_works_with_file_path (tmpdir ):
67+ """Test that you can also pass in a file path to load_secrets_from_config."""
68+ config_dir = tmpdir .join ("subdir" , "config" )
69+ os .makedirs (config_dir )
70+ secrets_file = config_dir .join ("secrets.toml" )
71+ with open (secrets_file , "w" ) as f :
72+ f .write (
73+ """\
74+ [scope]
75+ api_key = "12345"
76+ """
77+ )
78+ secrets = load_secrets_from_config (secrets_file )
79+ assert secrets == {"scope" : {"api_key" : "12345" }}
3580
3681
3782def test_load_secrets_from_config_no_file (tmpdir ):
3883 config_dir = tmpdir .join ("config" )
39- secrets_file = config_dir . join ( DEFAULT_SECRETS )
84+ os . makedirs ( config_dir )
4085
4186 with pytest .raises (FileNotFoundError ):
42- load_secrets_from_config (secrets_file )
87+ load_secrets_from_config (tmpdir )
4388
4489
4590def test_load_secrets_from_config_bad_format (tmpdir ):
@@ -49,7 +94,7 @@ def test_load_secrets_from_config_bad_format(tmpdir):
4994 with open (secrets_file , "w" ) as f :
5095 f .write ("""not_scoped = "some-value"\n """ )
5196 with pytest .raises (AssertionError ) as err_info :
52- load_secrets_from_config (secrets_file )
97+ load_secrets_from_config (tmpdir )
5398 err_text = str (err_info .value )
5499 assert err_text == "All keys should be in a [scope]."
55100
0 commit comments