11import unittest
22import json
3- import os
4- from pathlib import Path
5- import tempfile
63
74from bcasl .Loader import _load_workspace_config
5+ from .workspace_support import get_shared_workspace
6+
87
98class TestBCASLWorkspaceConfigExtended (unittest .TestCase ):
109 def setUp (self ):
11- self .orig_cwd = Path .cwd ()
12- self .tmpdir = Path (tempfile .mkdtemp (prefix = "bcasl_ws_" ))
13- os .chdir (self .tmpdir )
10+ self .ws = get_shared_workspace ()
11+ # Clean artifacts possibly left by other tests (but keep baseline ARK_Main_Config.yml)
12+ for name in ("bcasl.yaml" , "bcasl.yml" , "bcasl.json" , ".bcasl.json" ):
13+ p = self .ws / name
14+ try :
15+ if p .exists ():
16+ p .unlink ()
17+ except Exception :
18+ pass
1419
1520 def tearDown (self ):
16- os .chdir (self .orig_cwd )
17- # Best-effort cleanup
18- try :
19- for p in sorted (self .tmpdir .rglob ("*" ), reverse = True ):
20- try :
21- if p .is_file () or p .is_symlink ():
22- p .unlink ()
23- elif p .is_dir ():
24- p .rmdir ()
25- except Exception :
26- pass
27- self .tmpdir .rmdir ()
28- except Exception :
29- pass
21+ # Remove only files created by this test (keep baseline ARK_Main_Config.yml)
22+ for name in ("bcasl.yaml" , "bcasl.yml" , "bcasl.json" , ".bcasl.json" ):
23+ p = self .ws / name
24+ try :
25+ if p .exists ():
26+ p .unlink ()
27+ except Exception :
28+ pass
3029
3130 def write_yaml (self , name : str , content : str ):
32- (self .tmpdir / name ).write_text (content , encoding = "utf-8" )
31+ (self .ws / name ).write_text (content , encoding = "utf-8" )
3332
3433 def write_json (self , name : str , obj ):
35- (self .tmpdir / name ).write_text (json .dumps (obj , ensure_ascii = False , indent = 2 ), encoding = "utf-8" )
34+ (self .ws / name ).write_text (json .dumps (obj , ensure_ascii = False , indent = 2 ), encoding = "utf-8" )
3635
3736 def test_default_generation_without_configs (self ):
38- cfg = _load_workspace_config (self .tmpdir )
37+ # Ensure no pre-existing bcasl configs
38+ for n in ("bcasl.yaml" , "bcasl.yml" , "bcasl.json" , ".bcasl.json" ):
39+ p = self .ws / n
40+ if p .exists ():
41+ p .unlink ()
42+ cfg = _load_workspace_config (self .ws )
3943 self .assertIsInstance (cfg , dict )
4044 # Should write a bcasl.json best-effort
41- self .assertTrue ((self .tmpdir / "bcasl.json" ).exists ())
45+ self .assertTrue ((self .ws / "bcasl.json" ).exists ())
4246 self .assertIn ("file_patterns" , cfg )
4347 self .assertIn ("exclude_patterns" , cfg )
4448 self .assertIn ("options" , cfg )
@@ -50,52 +54,71 @@ def test_default_generation_without_configs(self):
5054 def test_yaml_has_priority_over_json (self ):
5155 self .write_json ("bcasl.json" , {"options" : {"enabled" : False }})
5256 self .write_yaml ("bcasl.yaml" , "options:\n enabled: true\n " )
53- cfg = _load_workspace_config (self .tmpdir )
57+ cfg = _load_workspace_config (self .ws )
5458 self .assertTrue (cfg ["options" ]["enabled" ]) # YAML wins
5559
5660 def test_json_is_used_when_no_yaml (self ):
5761 self .write_json (".bcasl.json" , {"options" : {"enabled" : False }})
58- cfg = _load_workspace_config (self .tmpdir )
62+ cfg = _load_workspace_config (self .ws )
5963 self .assertFalse (cfg ["options" ]["enabled" ]) # JSON honored
6064
6165 def test_merge_with_ark_config_patterns (self ):
62- # Create ARK_Main_Config.yml to add patterns
63- ark = (
64- "inclusion_patterns:\n - 'src/**/*.py'\n "
65- "exclusion_patterns:\n - '.venv/**'\n - 'build/**'\n "
66- "plugins:\n bcasl_enabled: true\n plugin_timeout: 1.5\n "
67- )
68- self .write_yaml ("ARK_Main_Config.yml" , ark )
69- # Create minimal bcasl.yaml to be merged
70- self .write_yaml ("bcasl.yaml" , "file_patterns:\n - '**/*.py'\n exclude_patterns:\n - '.git/**'\n " )
71- cfg = _load_workspace_config (self .tmpdir )
72- # inclusion_patterns from ARK should override into file_patterns
73- self .assertIn ("src/**/*.py" , cfg .get ("file_patterns" , []))
74- # exclusion merged should contain both
75- ex = cfg .get ("exclude_patterns" , [])
76- self .assertIn (".venv/**" , ex )
77- self .assertIn ("build/**" , ex )
78- self .assertIn (".git/**" , ex )
79- # plugin options merged
80- self .assertTrue (cfg .get ("options" , {}).get ("enabled" , True ))
81- self .assertEqual (cfg .get ("options" , {}).get ("plugin_timeout_s" ), 1.5 )
66+ # Backup existing ARK and restore after
67+ ark_path = self .ws / "ARK_Main_Config.yml"
68+ backup = None
69+ try :
70+ if ark_path .exists ():
71+ backup = ark_path .read_text (encoding = "utf-8" )
72+ except Exception :
73+ backup = None
74+
75+ try :
76+ # Write a custom ARK for this test
77+ ark = (
78+ "inclusion_patterns:\n - 'src/**/*.py'\n "
79+ "exclusion_patterns:\n - '.venv/**'\n - 'build/**'\n "
80+ "plugins:\n bcasl_enabled: true\n plugin_timeout: 1.5\n "
81+ )
82+ self .write_yaml ("ARK_Main_Config.yml" , ark )
83+ # Create minimal bcasl.yaml to be merged
84+ self .write_yaml ("bcasl.yaml" , "file_patterns:\n - '**/*.py'\n exclude_patterns:\n - '.git/**'\n " )
85+ cfg = _load_workspace_config (self .ws )
86+ # inclusion_patterns from ARK should override into file_patterns
87+ self .assertIn ("src/**/*.py" , cfg .get ("file_patterns" , []))
88+ # exclusion merged should contain both
89+ ex = cfg .get ("exclude_patterns" , [])
90+ self .assertIn (".venv/**" , ex )
91+ self .assertIn ("build/**" , ex )
92+ self .assertIn (".git/**" , ex )
93+ # plugin options merged
94+ self .assertTrue (cfg .get ("options" , {}).get ("enabled" , True ))
95+ self .assertEqual (cfg .get ("options" , {}).get ("plugin_timeout_s" ), 1.5 )
96+ finally :
97+ try :
98+ if backup is not None :
99+ ark_path .write_text (backup , encoding = "utf-8" )
100+ except Exception :
101+ pass
82102
83103 def test_required_files_detection (self ):
84- # Ensure detection when generating default
85- (self .tmpdir / "main.py" ).write_text ("print('x')" , encoding = "utf-8" )
86- (self .tmpdir / "requirements.txt" ).write_text ("" , encoding = "utf-8" )
87- cfg = _load_workspace_config (self .tmpdir )
104+ # Remove any pre-existing bcasl configs to force generation
105+ for n in ("bcasl.yaml" , "bcasl.yml" , "bcasl.json" , ".bcasl.json" ):
106+ p = self .ws / n
107+ if p .exists ():
108+ p .unlink ()
109+ cfg = _load_workspace_config (self .ws )
88110 req = cfg .get ("required_files" , [])
89111 self .assertIn ("main.py" , req )
90112 self .assertIn ("requirements.txt" , req )
91113
92114 def test_options_defaults_and_types (self ):
93- cfg = _load_workspace_config (self .tmpdir )
115+ cfg = _load_workspace_config (self .ws )
94116 opts = cfg .get ("options" , {})
95117 self .assertIsInstance (opts .get ("enabled" , True ), bool )
96118 self .assertIsInstance (opts .get ("plugin_timeout_s" , 0.0 ), float )
97119 self .assertIn ("sandbox" , opts )
98120 self .assertIn ("iter_files_cache" , opts )
99121
122+
100123if __name__ == "__main__" :
101124 unittest .main ()
0 commit comments