@@ -453,31 +453,50 @@ def test_scan_file_for_imports(self):
453
453
454
454
def test_write_requirements_file_new (self ):
455
455
"""Test writing a new requirements.txt file."""
456
+ # Create a temporary directory structure
457
+ temp_dir = tempfile .mkdtemp ()
458
+ script_dir = os .path .join (temp_dir , "script_dir" )
459
+ os .makedirs (script_dir )
460
+
456
461
content = textwrap .dedent (
457
462
"""
458
463
import pandas as pd
459
464
import numpy as np
460
465
"""
461
466
)
462
- temp_path = create_test_script (content )
467
+ temp_path = os .path .join (script_dir , "test_script.py" )
468
+ with open (temp_path , "w" ) as f :
469
+ f .write (content )
470
+
471
+ requirements_path = None
463
472
try :
464
473
requirements_path = write_requirements_file (temp_path )
465
474
assert os .path .exists (requirements_path )
475
+ assert (
476
+ os .path .dirname (requirements_path ) == temp_dir
477
+ ) # Should be in parent directory
466
478
467
479
with open (requirements_path , "r" ) as f :
468
480
requirements = {line .strip () for line in f }
469
481
470
482
assert "pandas" in requirements
471
483
assert "numpy" in requirements
472
484
finally :
473
- os .unlink (temp_path )
474
- if os .path .exists (requirements_path ):
485
+ if os .path .exists (temp_path ):
486
+ os .unlink (temp_path )
487
+ if requirements_path and os .path .exists (requirements_path ):
475
488
os .unlink (requirements_path )
489
+ os .rmdir (script_dir )
490
+ os .rmdir (temp_dir )
476
491
477
492
def test_write_requirements_file_merge (self ):
478
493
"""Test merging with existing requirements.txt file."""
479
- # First create an existing requirements.txt
494
+ # Create a temporary directory structure
480
495
temp_dir = tempfile .mkdtemp ()
496
+ script_dir = os .path .join (temp_dir , "script_dir" )
497
+ os .makedirs (script_dir )
498
+
499
+ # Create existing requirements.txt in parent directory
481
500
existing_requirements = os .path .join (temp_dir , "requirements.txt" )
482
501
with open (existing_requirements , "w" ) as f :
483
502
f .write ("pandas\n numpy\n " )
@@ -491,10 +510,17 @@ def test_write_requirements_file_merge(self):
491
510
import matplotlib
492
511
"""
493
512
)
494
- temp_path = create_test_script (content )
513
+ temp_path = os .path .join (script_dir , "test_script.py" )
514
+ with open (temp_path , "w" ) as f :
515
+ f .write (content )
516
+
517
+ requirements_path = None
495
518
try :
496
519
requirements_path = write_requirements_file (temp_path )
497
520
assert os .path .exists (requirements_path )
521
+ assert (
522
+ os .path .dirname (requirements_path ) == temp_dir
523
+ ) # Should be in parent directory
498
524
499
525
with open (requirements_path , "r" ) as f :
500
526
requirements = {line .strip () for line in f }
@@ -505,11 +531,13 @@ def test_write_requirements_file_merge(self):
505
531
assert "scipy" in requirements
506
532
assert "matplotlib" in requirements
507
533
finally :
508
- os .unlink (temp_path )
509
- if os .path .exists (requirements_path ):
534
+ if os .path .exists (temp_path ):
535
+ os .unlink (temp_path )
536
+ if requirements_path and os .path .exists (requirements_path ):
510
537
os .unlink (requirements_path )
511
538
if os .path .exists (existing_requirements ):
512
539
os .unlink (existing_requirements )
540
+ os .rmdir (script_dir )
513
541
os .rmdir (temp_dir )
514
542
515
543
def test_standard_library_exclusion (self ):
0 commit comments