15
15
from sed .core .config import read_env_var
16
16
from sed .core .config import save_config
17
17
from sed .core .config import save_env_var
18
- from sed .core .config import USER_CONFIG_PATH
19
18
20
19
test_dir = os .path .dirname (__file__ )
21
20
test_config_dir = Path (f"{ test_dir } /data/loader/" )
@@ -235,11 +234,15 @@ def test_invalid_config_wrong_values():
235
234
assert "Invalid value 9999 for gid. Group not found." in str (e .value )
236
235
237
236
238
- def test_env_var_read_write ( tmp_path , monkeypatch ):
239
- """Test reading and writing environment variables."""
240
- # Mock USER_CONFIG_PATH to use a temporary directory
237
+ @ pytest . fixture
238
+ def mock_env_file ( tmp_path , monkeypatch ):
239
+ """ Mock the .env file for testing"""
241
240
monkeypatch .setattr ("sed.core.config.USER_CONFIG_PATH" , tmp_path )
241
+ yield tmp_path
242
+
242
243
244
+ def test_env_var_read_write (mock_env_file ): # noqa: ARG001
245
+ """Test reading and writing environment variables."""
243
246
# Test writing a new variable
244
247
save_env_var ("TEST_VAR" , "test_value" )
245
248
assert read_env_var ("TEST_VAR" ) == "test_value"
@@ -258,16 +261,13 @@ def test_env_var_read_write(tmp_path, monkeypatch):
258
261
assert read_env_var ("NON_EXISTENT_VAR" ) is None
259
262
260
263
261
- def test_env_var_read_no_file (tmp_path , monkeypatch ):
264
+ def test_env_var_read_no_file (mock_env_file ): # noqa: ARG001
262
265
"""Test reading environment variables when .env file doesn't exist."""
263
- # Mock USER_CONFIG_PATH to use an empty temporary directory
264
- monkeypatch .setattr ("sed.core.config.USER_CONFIG_PATH" , tmp_path )
265
-
266
266
# Test reading from non-existent file
267
267
assert read_env_var ("TEST_VAR" ) is None
268
268
269
269
270
- def test_env_var_special_characters ():
270
+ def test_env_var_special_characters (mock_env_file ): # noqa: ARG001
271
271
"""Test reading and writing environment variables with special characters."""
272
272
test_cases = {
273
273
"TEST_URL" : "http://example.com/path?query=value" ,
@@ -280,50 +280,42 @@ def test_env_var_special_characters():
280
280
assert read_env_var (var_name ) == value
281
281
282
282
283
- @pytest .fixture
284
- def cleanup_env_files ():
285
- """Cleanup any .env files before and after tests"""
286
- # Clean up any existing .env files
287
- for path in [Path (".env" ), USER_CONFIG_PATH / ".env" ]:
288
- if path .exists ():
289
- path .unlink ()
290
-
291
- yield
292
-
293
- # Clean up after tests
294
- for path in [Path (".env" ), USER_CONFIG_PATH / ".env" ]:
295
- if path .exists ():
296
- path .unlink ()
297
-
298
-
299
- def test_env_var_precedence (cleanup_env_files ): # noqa: ARG001
283
+ def test_env_var_precedence (mock_env_file , tmp_path , monkeypatch ): # noqa: ARG001
300
284
"""Test that environment variables are read in correct order of precedence"""
285
+ # Create local .env directory if it doesn't exist
286
+ local_env_dir = tmp_path / "local"
287
+ local_env_dir .mkdir (exist_ok = True )
288
+ monkeypatch .setattr ("sed.core.config.ENV_DIR" , local_env_dir / ".env" )
289
+
301
290
# Set up test values in different locations
302
291
os .environ ["TEST_VAR" ] = "os_value"
303
292
304
- with open (".env" , "w" ) as f :
305
- f .write ("TEST_VAR=local_value\n " )
306
-
307
- save_env_var ("TEST_VAR" , "user_value" ) # Saves to USER_CONFIG_PATH
293
+ # Save to user config first (lowest precedence)
294
+ save_env_var ("TEST_VAR" , "user_value" )
308
295
309
- # Should get OS value first
310
- assert read_env_var ("TEST_VAR" ) == "os_value"
296
+ # Create local .env file (medium precedence)
297
+ with open (local_env_dir / ".env" , "w" ) as f :
298
+ f .write ("TEST_VAR=local_value\n " )
311
299
312
- # Remove from OS env and should get local value
313
- del os . environ [ "TEST_VAR" ]
300
+ # Remove from OS env to test other precedence levels
301
+ monkeypatch . delenv ( "TEST_VAR" , raising = False )
314
302
assert read_env_var ("TEST_VAR" ) == "local_value"
315
303
316
304
# Remove local .env and should get user config value
317
- Path ( ".env" ).unlink ()
305
+ ( local_env_dir / ".env" ).unlink ()
318
306
assert read_env_var ("TEST_VAR" ) == "user_value"
319
307
320
308
# Remove user config and should get None
321
- (USER_CONFIG_PATH / ".env" ).unlink ()
309
+ (mock_env_file / ".env" ).unlink ()
322
310
assert read_env_var ("TEST_VAR" ) is None
323
311
324
312
325
- def test_env_var_save_and_load (cleanup_env_files ): # noqa: ARG001
313
+ def test_env_var_save_and_load (mock_env_file , monkeypatch ): # noqa: ARG001
326
314
"""Test saving and loading environment variables"""
315
+ # Clear any existing OS environment variables
316
+ monkeypatch .delenv ("TEST_VAR" , raising = False )
317
+ monkeypatch .delenv ("OTHER_VAR" , raising = False )
318
+
327
319
# Save a variable
328
320
save_env_var ("TEST_VAR" , "test_value" )
329
321
@@ -336,14 +328,20 @@ def test_env_var_save_and_load(cleanup_env_files): # noqa: ARG001
336
328
assert read_env_var ("OTHER_VAR" ) == "other_value"
337
329
338
330
339
- def test_env_var_not_found (cleanup_env_files ): # noqa: ARG001
331
+ def test_env_var_not_found (mock_env_file ): # noqa: ARG001
340
332
"""Test behavior when environment variable is not found"""
341
333
assert read_env_var ("NONEXISTENT_VAR" ) is None
342
334
343
335
344
- def test_env_file_format (cleanup_env_files ): # noqa: ARG001
336
+ def test_env_file_format (mock_env_file , monkeypatch ): # noqa: ARG001
345
337
"""Test that .env file parsing handles different formats correctly"""
346
- with open (".env" , "w" ) as f :
338
+ # Clear any existing OS environment variables
339
+ monkeypatch .delenv ("TEST_VAR" , raising = False )
340
+ monkeypatch .delenv ("SPACES_VAR" , raising = False )
341
+ monkeypatch .delenv ("EMPTY_VAR" , raising = False )
342
+ monkeypatch .delenv ("COMMENT" , raising = False )
343
+
344
+ with open (mock_env_file / ".env" , "w" ) as f :
347
345
f .write (
348
346
"""
349
347
TEST_VAR=value1
0 commit comments