Skip to content

Commit 7223e2d

Browse files
author
PyCompiler ARK++
committed
revue des test
1 parent e776ed6 commit 7223e2d

2 files changed

Lines changed: 118 additions & 79 deletions

File tree

Lines changed: 74 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,48 @@
11
import unittest
22
import json
3-
import os
4-
from pathlib import Path
5-
import tempfile
63

74
from bcasl.Loader import _load_workspace_config
5+
from .workspace_support import get_shared_workspace
6+
87

98
class 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'\nexclude_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'\nexclude_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+
100123
if __name__ == "__main__":
101124
unittest.main()
Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,74 @@
11
import os
22
import json
3-
import tempfile
43
import unittest
5-
from pathlib import Path
64

75
from bcasl.Loader import resolve_bcasl_timeout, run_pre_compile
6+
from .workspace_support import get_shared_workspace
7+
88

99
class Dummy:
1010
def __init__(self, workspace_dir=None):
1111
self.workspace_dir = workspace_dir
12+
1213
class L:
1314
def __init__(self):
1415
self._data = []
16+
1517
def append(self, s):
1618
self._data.append(s)
19+
1720
self.log = L()
1821

22+
1923
class TestLoaderTimeoutAndRunPaths(unittest.TestCase):
2024
def setUp(self):
21-
self.tmp = Path(tempfile.mkdtemp(prefix='bcasl_ws_'))
25+
self.ws = get_shared_workspace()
26+
# Clean artifacts between runs (keep baseline ARK_Main_Config.yml)
27+
for name in ("bcasl.yaml", "bcasl.yml", "bcasl.json", ".bcasl.json"):
28+
try:
29+
p = self.ws / name
30+
if p.exists():
31+
p.unlink()
32+
except Exception:
33+
pass
34+
os.environ.pop("PYCOMPILER_BCASL_PLUGIN_TIMEOUT", None)
2235

2336
def tearDown(self):
24-
try:
25-
for p in sorted(self.tmp.rglob('*'), reverse=True):
26-
try:
27-
if p.is_file() or p.is_symlink():
28-
p.unlink()
29-
else:
30-
p.rmdir()
31-
except Exception:
32-
pass
33-
self.tmp.rmdir()
34-
except Exception:
35-
pass
36-
os.environ.pop('PYCOMPILER_BCASL_PLUGIN_TIMEOUT', None)
37+
# Only remove artifacts we created
38+
for name in ("bcasl.yaml", "bcasl.yml", "bcasl.json", ".bcasl.json"):
39+
try:
40+
p = self.ws / name
41+
if p.exists():
42+
p.unlink()
43+
except Exception:
44+
pass
45+
os.environ.pop("PYCOMPILER_BCASL_PLUGIN_TIMEOUT", None)
3746

3847
def test_resolve_bcasl_timeout_config_over_env(self):
39-
ws = self.tmp
4048
# Config timeout should win over env
41-
(ws / 'bcasl.json').write_text(json.dumps({"options": {"plugin_timeout_s": 2.5}}, indent=2), encoding='utf-8')
42-
os.environ['PYCOMPILER_BCASL_PLUGIN_TIMEOUT'] = '10'
43-
d = Dummy(str(ws))
49+
(self.ws / "bcasl.json").write_text(
50+
json.dumps({"options": {"plugin_timeout_s": 2.5}}, indent=2),
51+
encoding="utf-8",
52+
)
53+
os.environ["PYCOMPILER_BCASL_PLUGIN_TIMEOUT"] = "10"
54+
d = Dummy(str(self.ws))
4455
self.assertEqual(resolve_bcasl_timeout(d), 2.5)
4556

4657
def test_resolve_bcasl_timeout_env_used_when_config_zero(self):
47-
ws = self.tmp
48-
(ws / 'bcasl.json').write_text(json.dumps({"options": {"plugin_timeout_s": 0.0}}, indent=2), encoding='utf-8')
49-
os.environ['PYCOMPILER_BCASL_PLUGIN_TIMEOUT'] = '3.3'
50-
d = Dummy(str(ws))
58+
(self.ws / "bcasl.json").write_text(
59+
json.dumps({"options": {"plugin_timeout_s": 0.0}}, indent=2),
60+
encoding="utf-8",
61+
)
62+
os.environ["PYCOMPILER_BCASL_PLUGIN_TIMEOUT"] = "3.3"
63+
d = Dummy(str(self.ws))
5164
self.assertEqual(resolve_bcasl_timeout(d), 3.3)
5265

5366
def test_run_pre_compile_skips_when_disabled(self):
54-
ws = self.tmp
55-
(ws / 'bcasl.json').write_text(json.dumps({"options": {"enabled": False}}, indent=2), encoding='utf-8')
56-
d = Dummy(str(ws))
67+
(self.ws / "bcasl.json").write_text(
68+
json.dumps({"options": {"enabled": False}}, indent=2),
69+
encoding="utf-8",
70+
)
71+
d = Dummy(str(self.ws))
5772
rep = run_pre_compile(d)
5873
self.assertIsNone(rep)
5974

@@ -62,5 +77,6 @@ def test_run_pre_compile_no_workspace(self):
6277
rep = run_pre_compile(d)
6378
self.assertIsNone(rep)
6479

65-
if __name__ == '__main__':
80+
81+
if __name__ == "__main__":
6682
unittest.main()

0 commit comments

Comments
 (0)