Skip to content

Commit c21b049

Browse files
refactor: Extract UV utility functions in test file
Co-Authored-By: Aaron <AJ> Steers <aj@airbyte.io>
1 parent 1d23bed commit c21b049

File tree

1 file changed

+86
-36
lines changed

1 file changed

+86
-36
lines changed

tests/integration_tests/test_uv_functionality.py

+86-36
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,89 @@
99
import pytest
1010

1111

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],
1626
capture_output=True,
1727
text=True,
28+
env=env,
29+
check=check,
1830
)
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"])
1995
assert result.returncode == 0
2096
assert "uv" in result.stdout.lower()
2197

@@ -32,52 +108,26 @@ def temp_venv(tmp_path: Path) -> Path:
32108

33109
def test_uv_venv_creation(temp_venv: Path) -> None:
34110
"""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)])
40112
assert result.returncode == 0
41113
assert temp_venv.exists()
42114
assert (temp_venv / "bin" / "python").exists()
43115

44116

45117
def test_uv_package_install(temp_venv: Path) -> None:
46118
"""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
71122

72123
# 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")
75125
assert black_path.exists()
76126

77127
# Try running the package
78128
result = subprocess.run(
79129
[str(black_path), "--version"],
80-
env=venv_env,
130+
env=get_venv_env(temp_venv),
81131
capture_output=True,
82132
text=True,
83133
)

0 commit comments

Comments
 (0)