9
9
import pytest
10
10
11
11
12
- def test_uv_available () -> None :
13
- """Test that UV is available on the command line."""
14
- result = subprocess .run (
15
- ["uv" , "--version" ],
12
+ # TODO: Move these utility functions to a dedicated module under "utils"
13
+ def run_uv_command (args : list [str ], env : dict | None = None , check : bool = False ) -> subprocess .CompletedProcess :
14
+ """Run a UV command and return the result.
15
+
16
+ Args:
17
+ args: Command arguments to pass to UV
18
+ env: Optional environment variables
19
+ check: Whether to raise an exception on non-zero return code
20
+
21
+ Returns:
22
+ CompletedProcess instance with command output
23
+ """
24
+ return subprocess .run (
25
+ ["uv" , * args ],
16
26
capture_output = True ,
17
27
text = True ,
28
+ env = env ,
29
+ check = check ,
18
30
)
31
+
32
+
33
+ def create_venv (venv_path : Path , with_pip : bool = True ) -> None :
34
+ """Create a virtual environment using UV.
35
+
36
+ Args:
37
+ venv_path: Path where the virtual environment should be created
38
+ with_pip: Whether to include pip and other seed packages
39
+ """
40
+ args = ["venv" ]
41
+ if with_pip :
42
+ args .append ("--seed" )
43
+ args .append (str (venv_path ))
44
+ run_uv_command (args , check = True )
45
+
46
+
47
+ def install_package (venv_path : Path , pip_url : str ) -> None :
48
+ """Install a package into a virtual environment using UV.
49
+
50
+ Args:
51
+ venv_path: Path to the virtual environment
52
+ pip_url: Package specification (name, URL, or path) to install
53
+ """
54
+ venv_env = get_venv_env (venv_path )
55
+ run_uv_command (
56
+ ["pip" , "install" , pip_url ],
57
+ env = venv_env ,
58
+ check = True ,
59
+ )
60
+
61
+
62
+ def get_venv_env (venv_path : Path ) -> dict :
63
+ """Get environment variables for a virtual environment.
64
+
65
+ Args:
66
+ venv_path: Path to the virtual environment
67
+
68
+ Returns:
69
+ Dict of environment variables
70
+ """
71
+ env = os .environ .copy ()
72
+ env ["VIRTUAL_ENV" ] = str (venv_path )
73
+ env ["PATH" ] = f"{ venv_path } /bin:{ os .environ ['PATH' ]} "
74
+ return env
75
+
76
+
77
+ def get_executable_path (venv_path : Path , executable : str ) -> Path :
78
+ """Get the path to an executable in a virtual environment.
79
+
80
+ Args:
81
+ venv_path: Path to the virtual environment
82
+ executable: Name of the executable
83
+
84
+ Returns:
85
+ Path to the executable
86
+ """
87
+ bin_dir = "Scripts" if os .name == "nt" else "bin"
88
+ suffix = ".exe" if os .name == "nt" else ""
89
+ return venv_path / bin_dir / f"{ executable } { suffix } "
90
+
91
+
92
+ def test_uv_available () -> None :
93
+ """Test that UV is available on the command line."""
94
+ result = run_uv_command (["--version" ])
19
95
assert result .returncode == 0
20
96
assert "uv" in result .stdout .lower ()
21
97
@@ -32,52 +108,26 @@ def temp_venv(tmp_path: Path) -> Path:
32
108
33
109
def test_uv_venv_creation (temp_venv : Path ) -> None :
34
110
"""Test that UV can create a virtual environment."""
35
- result = subprocess .run (
36
- ["uv" , "venv" , str (temp_venv )],
37
- capture_output = True ,
38
- text = True ,
39
- )
111
+ result = run_uv_command (["venv" , str (temp_venv )])
40
112
assert result .returncode == 0
41
113
assert temp_venv .exists ()
42
114
assert (temp_venv / "bin" / "python" ).exists ()
43
115
44
116
45
117
def test_uv_package_install (temp_venv : Path ) -> None :
46
118
"""Test that UV can install a package and we can execute it."""
47
- # Create venv with seed packages (pip, setuptools, wheel)
48
- subprocess .run (
49
- ["uv" , "venv" , "--seed" , str (temp_venv )],
50
- check = True ,
51
- )
52
-
53
- # Set up environment variables for the virtual environment
54
- venv_env = os .environ .copy ()
55
- venv_env ["VIRTUAL_ENV" ] = str (temp_venv )
56
- venv_env ["PATH" ] = f"{ temp_venv } /bin:{ os .environ ['PATH' ]} "
57
-
58
- # Install package using UV directly
59
- subprocess .run (
60
- [
61
- "uv" ,
62
- "pip" ,
63
- "install" ,
64
- "black" ,
65
- ], # Use black since it's a common tool that creates executables
66
- env = venv_env ,
67
- capture_output = True ,
68
- text = True ,
69
- check = True ,
70
- )
119
+ # Create venv and install black
120
+ create_venv (temp_venv , with_pip = True )
121
+ install_package (temp_venv , "black" ) # Use black since it's a common tool that creates executables
71
122
72
123
# Verify package executable exists and is runnable
73
- bin_dir = "Scripts" if os .name == "nt" else "bin"
74
- black_path = temp_venv / bin_dir / ("black.exe" if os .name == "nt" else "black" )
124
+ black_path = get_executable_path (temp_venv , "black" )
75
125
assert black_path .exists ()
76
126
77
127
# Try running the package
78
128
result = subprocess .run (
79
129
[str (black_path ), "--version" ],
80
- env = venv_env ,
130
+ env = get_venv_env ( temp_venv ) ,
81
131
capture_output = True ,
82
132
text = True ,
83
133
)
0 commit comments