Skip to content

Commit 1c4e376

Browse files
committed
Fix test_defaults_cfg_creation Windows cleanup issue
On Windows, TemporaryDirectory cannot be deleted if it's the current working directory. The test was doing os.chdir(tmpdir) but never restoring the original directory, causing cleanup to fail with: PermissionError: The process cannot access the file because it is being used by another process Fix: Save original cwd and restore it in finally block before TemporaryDirectory context manager exits. Fixes Windows CI test failure.
1 parent 6ab6cd8 commit 1c4e376

1 file changed

Lines changed: 27 additions & 23 deletions

File tree

tests/test_edge_cases.py

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -215,32 +215,36 @@ class TestUncrustifyDefaults:
215215

216216
def test_defaults_cfg_creation(self):
217217
"""Test that defaults.cfg is created if missing."""
218+
original_cwd = os.getcwd()
218219
with tempfile.TemporaryDirectory() as tmpdir:
219-
os.chdir(tmpdir)
220-
with tempfile.NamedTemporaryFile(
221-
mode="w", suffix=".c", delete=False, dir=tmpdir
222-
) as f:
223-
f.write("int main() { return 0; }\n")
224-
temp_file = f.name
225-
226220
try:
227-
# Mock sys.argv for get_added_files()
228-
original_argv = sys.argv
229-
sys.argv = ["uncrustify-hook", temp_file]
230-
cmd = UncrustifyCmd(["uncrustify-hook", temp_file])
231-
sys.argv = original_argv
232-
# If no -c arg provided, defaults.cfg should be created
233-
if "-c" in cmd.args:
234-
idx = cmd.args.index("-c")
235-
config_file = cmd.args[idx + 1]
236-
# Check that config file exists or is defaults.cfg
237-
assert config_file == "defaults.cfg" or os.path.exists(config_file)
221+
os.chdir(tmpdir)
222+
with tempfile.NamedTemporaryFile(
223+
mode="w", suffix=".c", delete=False, dir=tmpdir
224+
) as f:
225+
f.write("int main() { return 0; }\n")
226+
temp_file = f.name
227+
228+
try:
229+
# Mock sys.argv for get_added_files()
230+
original_argv = sys.argv
231+
sys.argv = ["uncrustify-hook", temp_file]
232+
cmd = UncrustifyCmd(["uncrustify-hook", temp_file])
233+
sys.argv = original_argv
234+
# If no -c arg provided, defaults.cfg should be created
235+
if "-c" in cmd.args:
236+
idx = cmd.args.index("-c")
237+
config_file = cmd.args[idx + 1]
238+
# Check that config file exists or is defaults.cfg
239+
assert config_file == "defaults.cfg" or os.path.exists(config_file)
240+
finally:
241+
if os.path.exists(temp_file):
242+
os.unlink(temp_file)
243+
defaults_path = os.path.join(tmpdir, "defaults.cfg")
244+
if os.path.exists(defaults_path):
245+
os.unlink(defaults_path)
238246
finally:
239-
if os.path.exists(temp_file):
240-
os.unlink(temp_file)
241-
defaults_path = os.path.join(tmpdir, "defaults.cfg")
242-
if os.path.exists(defaults_path):
243-
os.unlink(defaults_path)
247+
os.chdir(original_cwd)
244248

245249

246250
class TestOCLintVersionHandling:

0 commit comments

Comments
 (0)