66"""
77
88import os
9- import sys
109import subprocess
10+ import sys
1111import tempfile
12- from pathlib import Path
1312
1413
1514def test_should_use_uv ():
1615 """Test the should_use_uv function."""
1716 # Import the module
1817 from auto_uv import should_use_uv
19-
18+
2019 # Save original environment
2120 original_env = os .environ .copy ()
22-
21+
2322 try :
24- # Test 1: Should return False if UV_RUN_ACTIVE is set
25- os .environ ["UV_RUN_ACTIVE" ] = "1"
26- assert should_use_uv () is False , "Should not use uv when UV_RUN_ACTIVE is set"
27-
28- # Test 2: Should return False if AUTO_UV_DISABLE is set
29- del os .environ ["UV_RUN_ACTIVE" ]
30- os .environ ["AUTO_UV_DISABLE" ] = "1"
31- assert should_use_uv () is False , "Should not use uv when AUTO_UV_DISABLE is set"
32-
33- # Test 3: Check if uv is available (this depends on the system)
34- del os .environ ["AUTO_UV_DISABLE" ]
23+ _should_use_uv_test (
24+ "UV_RUN_ACTIVE" ,
25+ should_use_uv ,
26+ "Should not use uv when UV_RUN_ACTIVE is set" ,
27+ )
28+ _should_use_uv_test (
29+ "AUTO_UV_DISABLE" ,
30+ should_use_uv ,
31+ "Should not use uv when AUTO_UV_DISABLE is set" ,
32+ )
3533 result = should_use_uv ()
3634 print (f"UV available on system: { result } " )
37-
35+
3836 finally :
3937 # Restore original environment
4038 os .environ .clear ()
4139 os .environ .update (original_env )
4240
4341
42+ def _should_use_uv_test (arg0 , should_use_uv , arg2 ):
43+ # Test 1: Should return False if UV_RUN_ACTIVE is set
44+ os .environ [arg0 ] = "1"
45+ assert should_use_uv () is False , arg2
46+
47+ # Test 2: Should return False if AUTO_UV_DISABLE is set
48+ del os .environ [arg0 ]
49+
50+
4451def test_script_execution ():
4552 """Test that a script can be executed with auto-uv."""
4653 # Create a temporary test script
47- with tempfile .NamedTemporaryFile (mode = 'w' , suffix = ' .py' , delete = False ) as f :
54+ with tempfile .NamedTemporaryFile (mode = "w" , suffix = " .py" , delete = False ) as f :
4855 f .write ("""
4956import os
5057import sys
@@ -58,22 +65,22 @@ def test_script_execution():
5865sys.exit(0)
5966""" )
6067 script_path = f .name
61-
68+
6269 try :
6370 # Run the script
6471 result = subprocess .run (
65- [sys .executable , script_path ],
66- capture_output = True ,
67- text = True
72+ [sys .executable , script_path ], capture_output = True , text = True , check = False
6873 )
69-
74+
7075 print (f"Script output: { result .stdout .strip ()} " )
7176 print (f"Script stderr: { result .stderr .strip ()} " )
7277 print (f"Return code: { result .returncode } " )
73-
78+
7479 # Verify it ran successfully
75- assert result .returncode == 0 , f"Script failed with return code { result .returncode } "
76-
80+ assert result .returncode == 0 , (
81+ f"Script failed with return code { result .returncode } "
82+ )
83+
7784 finally :
7885 # Clean up
7986 os .unlink (script_path )
@@ -82,61 +89,57 @@ def test_script_execution():
8289def test_disable_flag ():
8390 """Test that AUTO_UV_DISABLE flag works."""
8491 # Create a temporary test script
85- with tempfile .NamedTemporaryFile (mode = 'w' , suffix = ' .py' , delete = False ) as f :
92+ with tempfile .NamedTemporaryFile (mode = "w" , suffix = " .py" , delete = False ) as f :
8693 f .write ("""
87- import os
88- if os.environ.get('UV_RUN_ACTIVE'):
89- print('WITH_UV')
90- else:
91- print('WITHOUT_UV')
92- """ )
94+ import os
95+ if os.environ.get('UV_RUN_ACTIVE'):
96+ print('WITH_UV')
97+ else:
98+ print('WITHOUT_UV')
99+ """ )
93100 script_path = f .name
94-
101+
95102 try :
96103 # Run with AUTO_UV_DISABLE
97104 env = os .environ .copy ()
98- env [' AUTO_UV_DISABLE' ] = '1'
99-
105+ env [" AUTO_UV_DISABLE" ] = "1"
106+
100107 result = subprocess .run (
101108 [sys .executable , script_path ],
102109 capture_output = True ,
103110 text = True ,
104- env = env
111+ env = env ,
112+ check = False ,
105113 )
106-
114+
107115 print (f"Output with AUTO_UV_DISABLE: { result .stdout .strip ()} " )
108-
116+
109117 finally :
110118 os .unlink (script_path )
111119
112120
113121if __name__ == "__main__" :
114122 print ("Running auto-uv tests...\n " )
115-
123+
116124 print ("Test 1: should_use_uv()" )
117- print ("-" * 50 )
118125 try :
119126 test_should_use_uv ()
120- print ("✅ PASSED\n " )
127+ print ("PASSED\n " )
121128 except Exception as e :
122- print (f"❌ FAILED: { e } \n " )
123-
129+ print (f"FAILED: { e } \n " )
130+
124131 print ("Test 2: Script execution" )
125- print ("-" * 50 )
126132 try :
127133 test_script_execution ()
128- print ("✅ PASSED\n " )
134+ print ("PASSED\n " )
129135 except Exception as e :
130- print (f"❌ FAILED: { e } \n " )
131-
136+ print (f"FAILED: { e } \n " )
137+
132138 print ("Test 3: AUTO_UV_DISABLE flag" )
133- print ("-" * 50 )
134139 try :
135140 test_disable_flag ()
136- print ("✅ PASSED\n " )
141+ print ("PASSED\n " )
137142 except Exception as e :
138- print (f"❌ FAILED: { e } \n " )
139-
140- print ("=" * 50 )
141- print ("Tests complete!" )
143+ print (f"FAILED: { e } \n " )
142144
145+ print ("Tests complete!" )
0 commit comments