2323
2424
2525# Module-level helper function
26- def create_dynaconf_with_custom_loader (temp_dir , secrets_file , fresh_vars = None ):
26+ def create_dynaconf_with_custom_loader (temp_dir , secrets_file ):
2727 """
2828 Create a Dynaconf instance matching the production configuration.
2929
@@ -32,28 +32,25 @@ def create_dynaconf_with_custom_loader(temp_dir, secrets_file, fresh_vars=None):
3232 - custom_merge_loader and env_loader enabled
3333 - merge_enabled = True
3434
35+ Note: fresh_vars should be configured via FRESH_VARS_FOR_DYNACONF environment variable,
36+ which is the only way to configure it in pr-agent.
37+
3538 Args:
3639 temp_dir: Temporary directory path
3740 secrets_file: Path to secrets file
38- fresh_vars: List of section names to mark as fresh (e.g., ["GITLAB"])
3941
4042 Returns:
4143 Dynaconf instance configured like production
4244 """
43- kwargs = {
44- "core_loaders" : [],
45- "loaders" : ["pr_agent.custom_merge_loader" , "dynaconf.loaders.env_loader" ],
46- "root_path" : temp_dir ,
47- "merge_enabled" : True ,
48- "envvar_prefix" : False ,
49- "load_dotenv" : False ,
50- "settings_files" : [str (secrets_file )],
51- }
52-
53- if fresh_vars :
54- kwargs ["fresh_vars" ] = fresh_vars
55-
56- return Dynaconf (** kwargs )
45+ return Dynaconf (
46+ core_loaders = [],
47+ loaders = ["pr_agent.custom_merge_loader" , "dynaconf.loaders.env_loader" ],
48+ root_path = temp_dir ,
49+ merge_enabled = True ,
50+ envvar_prefix = False ,
51+ load_dotenv = False ,
52+ settings_files = [str (secrets_file )],
53+ )
5754
5855
5956class TestFreshVarsGitLabScenario :
@@ -104,11 +101,13 @@ def test_gitlab_personal_access_token_reload(self):
104101 # Create initial secrets file
105102 self .create_secrets_toml (personal_access_token = "token_v1" , shared_secret = "secret_v1" )
106103
107- # Create Dynaconf with GITLAB marked as fresh
108- settings = create_dynaconf_with_custom_loader (self .temp_dir , self .secrets_file , fresh_vars = ["GITLAB" ])
104+ # Set FRESH_VARS_FOR_DYNACONF environment variable (the only way to configure fresh_vars in pr-agent)
105+ with patch .dict (os .environ , {"FRESH_VARS_FOR_DYNACONF" : '["GITLAB"]' }):
106+ # Create Dynaconf with GITLAB marked as fresh via env var
107+ settings = create_dynaconf_with_custom_loader (self .temp_dir , self .secrets_file )
109108
110- # First access - should return initial value
111- first_token = settings .GITLAB .PERSONAL_ACCESS_TOKEN
109+ # First access - should return initial value
110+ first_token = settings .GITLAB .PERSONAL_ACCESS_TOKEN
112111 assert first_token == "token_v1" , "Initial personal_access_token should be 'token_v1'"
113112
114113 # Modify the secrets file
@@ -123,35 +122,6 @@ def test_gitlab_personal_access_token_reload(self):
123122 # Verify the values are different (fresh_vars working)
124123 assert first_token != second_token , "fresh_vars should cause values to be reloaded, not cached"
125124
126- def test_gitlab_shared_secret_reload (self ):
127- """
128- Test that gitlab.shared_secret is reloaded when GITLAB is marked as fresh.
129-
130- Verifies that fresh_vars applies to all fields in the GITLAB section,
131- not just personal_access_token.
132- """
133- # Create initial secrets file
134- self .create_secrets_toml (personal_access_token = "token_v1" , shared_secret = "secret_v1" )
135-
136- # Create Dynaconf with GITLAB marked as fresh
137- settings = create_dynaconf_with_custom_loader (self .temp_dir , self .secrets_file , fresh_vars = ["GITLAB" ])
138-
139- # First access
140- first_secret = settings .GITLAB .SHARED_SECRET
141- assert first_secret == "secret_v1" , "Initial shared_secret should be 'secret_v1'"
142-
143- # Modify the secrets file
144- self .create_secrets_toml (personal_access_token = "token_v1" , shared_secret = "secret_v2_updated" )
145-
146- # Second access - should return NEW value
147- second_secret = settings .GITLAB .SHARED_SECRET
148- assert second_secret == "secret_v2_updated" , (
149- "After file modification, shared_secret should be reloaded to 'secret_v2_updated'"
150- )
151-
152- # Verify fresh_vars is working
153- assert first_secret != second_secret , "fresh_vars should cause shared_secret to be reloaded"
154-
155125 def test_gitlab_multiple_fields_reload (self ):
156126 """
157127 Test that both gitlab fields reload together when GITLAB is marked as fresh.
@@ -162,28 +132,32 @@ def test_gitlab_multiple_fields_reload(self):
162132 # Create initial secrets file
163133 self .create_secrets_toml (personal_access_token = "token_v1" , shared_secret = "secret_v1" )
164134
165- # Create Dynaconf with GITLAB marked as fresh
166- settings = create_dynaconf_with_custom_loader (self .temp_dir , self .secrets_file , fresh_vars = ["GITLAB" ])
135+ # Set FRESH_VARS_FOR_DYNACONF environment variable
136+ with patch .dict (os .environ , {"FRESH_VARS_FOR_DYNACONF" : '["GITLAB"]' }):
137+ # Create Dynaconf with GITLAB marked as fresh via env var
138+ settings = create_dynaconf_with_custom_loader (self .temp_dir , self .secrets_file )
167139
168- # First access - both fields
169- first_token = settings .GITLAB .PERSONAL_ACCESS_TOKEN
170- first_secret = settings .GITLAB .SHARED_SECRET
171- assert first_token == "token_v1"
172- assert first_secret == "secret_v1"
140+ # First access - both fields
141+ first_token = settings .GITLAB .PERSONAL_ACCESS_TOKEN
142+ first_secret = settings .GITLAB .SHARED_SECRET
143+ assert first_token == "token_v1"
144+ assert first_secret == "secret_v1"
173145
174- # Modify both fields in the secrets file
175- self .create_secrets_toml (personal_access_token = "token_v2_both_updated" , shared_secret = "secret_v2_both_updated" )
146+ # Modify both fields in the secrets file
147+ self .create_secrets_toml (
148+ personal_access_token = "token_v2_both_updated" , shared_secret = "secret_v2_both_updated"
149+ )
176150
177- # Second access - both fields should be updated
178- second_token = settings .GITLAB .PERSONAL_ACCESS_TOKEN
179- second_secret = settings .GITLAB .SHARED_SECRET
151+ # Second access - both fields should be updated
152+ second_token = settings .GITLAB .PERSONAL_ACCESS_TOKEN
153+ second_secret = settings .GITLAB .SHARED_SECRET
180154
181- assert second_token == "token_v2_both_updated" , "personal_access_token should be reloaded"
182- assert second_secret == "secret_v2_both_updated" , "shared_secret should be reloaded"
155+ assert second_token == "token_v2_both_updated" , "personal_access_token should be reloaded"
156+ assert second_secret == "secret_v2_both_updated" , "shared_secret should be reloaded"
183157
184- # Verify both fields were reloaded
185- assert first_token != second_token , "personal_access_token should not be cached"
186- assert first_secret != second_secret , "shared_secret should not be cached"
158+ # Verify both fields were reloaded
159+ assert first_token != second_token , "personal_access_token should not be cached"
160+ assert first_secret != second_secret , "shared_secret should not be cached"
187161
188162
189163class TestFreshVarsCustomLoaderIntegration :
@@ -229,11 +203,13 @@ def test_fresh_vars_without_core_loaders(self):
229203 # Create initial secrets file
230204 self .create_secrets_toml (personal_access_token = "token_before_bug_test" )
231205
232- # Create Dynaconf WITHOUT core loaders but WITH fresh_vars
233- settings = create_dynaconf_with_custom_loader (self .temp_dir , self .secrets_file , fresh_vars = ["GITLAB" ])
206+ # Set FRESH_VARS_FOR_DYNACONF environment variable
207+ with patch .dict (os .environ , {"FRESH_VARS_FOR_DYNACONF" : '["GITLAB"]' }):
208+ # Create Dynaconf WITHOUT core loaders but WITH fresh_vars via env var
209+ settings = create_dynaconf_with_custom_loader (self .temp_dir , self .secrets_file )
234210
235- # First access
236- first_value = settings .GITLAB .PERSONAL_ACCESS_TOKEN
211+ # First access
212+ first_value = settings .GITLAB .PERSONAL_ACCESS_TOKEN
237213 assert first_value == "token_before_bug_test" , "Initial value should be loaded correctly"
238214
239215 # Modify the file
@@ -265,33 +241,35 @@ def test_custom_loader_respects_fresh_vars(self):
265241"""
266242 self .secrets_file .write_text (content )
267243
268- # Create Dynaconf with only GITLAB marked as fresh
269- settings = create_dynaconf_with_custom_loader (self .temp_dir , self .secrets_file , fresh_vars = ["GITLAB" ])
244+ # Set FRESH_VARS_FOR_DYNACONF environment variable (only GITLAB)
245+ with patch .dict (os .environ , {"FRESH_VARS_FOR_DYNACONF" : '["GITLAB"]' }):
246+ # Create Dynaconf with only GITLAB marked as fresh via env var
247+ settings = create_dynaconf_with_custom_loader (self .temp_dir , self .secrets_file )
270248
271- # Access both sections
272- gitlab_token_1 = settings .GITLAB .PERSONAL_ACCESS_TOKEN
273- github_token_1 = settings .GITHUB .USER_TOKEN
249+ # Access both sections
250+ gitlab_token_1 = settings .GITLAB .PERSONAL_ACCESS_TOKEN
251+ github_token_1 = settings .GITHUB .USER_TOKEN
274252
275- # Modify both sections
276- content = """[gitlab]
253+ # Modify both sections
254+ content = """[gitlab]
277255personal_access_token = "gitlab_token_v2"
278256
279257[github]
280258user_token = "github_token_v2"
281259"""
282- self .secrets_file .write_text (content )
260+ self .secrets_file .write_text (content )
283261
284- # Access again
285- gitlab_token_2 = settings .GITLAB .PERSONAL_ACCESS_TOKEN
286- github_token_2 = settings .GITHUB .USER_TOKEN
262+ # Access again
263+ gitlab_token_2 = settings .GITLAB .PERSONAL_ACCESS_TOKEN
264+ github_token_2 = settings .GITHUB .USER_TOKEN
287265
288- # GITLAB should be reloaded (marked as fresh)
289- assert gitlab_token_2 == "gitlab_token_v2" , "GITLAB section should be reloaded (marked as fresh)"
290- assert gitlab_token_1 != gitlab_token_2 , "GITLAB values should not be cached"
266+ # GITLAB should be reloaded (marked as fresh)
267+ assert gitlab_token_2 == "gitlab_token_v2" , "GITLAB section should be reloaded (marked as fresh)"
268+ assert gitlab_token_1 != gitlab_token_2 , "GITLAB values should not be cached"
291269
292- # GITHUB should be cached (not marked as fresh)
293- assert github_token_2 == "github_token_v1" , "GITHUB section should be cached (not marked as fresh)"
294- assert github_token_1 == github_token_2 , "GITHUB values should be cached"
270+ # GITHUB should be cached (not marked as fresh)
271+ assert github_token_2 == "github_token_v1" , "GITHUB section should be cached (not marked as fresh)"
272+ assert github_token_1 == github_token_2 , "GITHUB values should be cached"
295273
296274
297275class TestFreshVarsBasicFunctionality :
@@ -321,34 +299,6 @@ def create_secrets_toml(self, personal_access_token="initial_token"):
321299"""
322300 self .secrets_file .write_text (content )
323301
324- def test_fresh_vars_from_environment_variable (self ):
325- """
326- Test that fresh_vars can be set via FRESH_VARS_FOR_DYNACONF environment variable.
327-
328- This tests the common use case where fresh_vars is configured through
329- an environment variable rather than in code.
330- """
331- # Create initial secrets file
332- self .create_secrets_toml (personal_access_token = "env_token_v1" )
333-
334- # Set FRESH_VARS_FOR_DYNACONF environment variable
335- with patch .dict (os .environ , {"FRESH_VARS_FOR_DYNACONF" : '["GITLAB"]' }):
336- # Create Dynaconf (should pick up fresh_vars from env)
337- settings = create_dynaconf_with_custom_loader (self .temp_dir , self .secrets_file )
338-
339- # First access
340- first_value = settings .GITLAB .PERSONAL_ACCESS_TOKEN
341- assert first_value == "env_token_v1"
342-
343- # Modify file
344- self .create_secrets_toml (personal_access_token = "env_token_v2" )
345-
346- # Second access - should be reloaded
347- second_value = settings .GITLAB .PERSONAL_ACCESS_TOKEN
348- assert second_value == "env_token_v2" , "fresh_vars from environment variable should cause reload"
349-
350- assert first_value != second_value , "Values should be different when fresh_vars is set via env var"
351-
352302 def test_gitlab_credentials_not_cached_when_fresh (self ):
353303 """
354304 Test that GitLab credentials are not cached when marked as fresh.
@@ -360,13 +310,15 @@ def test_gitlab_credentials_not_cached_when_fresh(self):
360310 # Create initial secrets file
361311 self .create_secrets_toml (personal_access_token = "no_cache_v1" )
362312
363- # Create Dynaconf with GITLAB marked as fresh
364- settings = create_dynaconf_with_custom_loader (self .temp_dir , self .secrets_file , fresh_vars = ["GITLAB" ])
313+ # Set FRESH_VARS_FOR_DYNACONF environment variable
314+ with patch .dict (os .environ , {"FRESH_VARS_FOR_DYNACONF" : '["GITLAB"]' }):
315+ # Create Dynaconf with GITLAB marked as fresh via env var
316+ settings = create_dynaconf_with_custom_loader (self .temp_dir , self .secrets_file )
365317
366- # Access the token multiple times before modification
367- access_1 = settings .GITLAB .PERSONAL_ACCESS_TOKEN
368- access_2 = settings .GITLAB .PERSONAL_ACCESS_TOKEN
369- access_3 = settings .GITLAB .PERSONAL_ACCESS_TOKEN
318+ # Access the token multiple times before modification
319+ access_1 = settings .GITLAB .PERSONAL_ACCESS_TOKEN
320+ access_2 = settings .GITLAB .PERSONAL_ACCESS_TOKEN
321+ access_3 = settings .GITLAB .PERSONAL_ACCESS_TOKEN
370322
371323 # All should return the same value (file hasn't changed)
372324 assert access_1 == access_2 == access_3 == "no_cache_v1" , (
0 commit comments