-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.dang
More file actions
91 lines (76 loc) · 2.95 KB
/
main.dang
File metadata and controls
91 lines (76 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
pub description = "A toolchain for testing Python applications with automatic OpenTelemetry tracing"
type Pytest {
"""
The uv binary extracted from the bundled Docker image
"""
let uvBinary = currentModule().source().directory("images/uv").dockerBuild().rootfs()
"""
The alpine base image
"""
let alpine = currentModule().source().directory("images/alpine").dockerBuild()
"""
The pytest_otel source directory bundled with this module
"""
let pytestOtelSource = currentModule().source().directory("pytest_otel")
"""
Optional: The source directory containing your Python project
"""
pub source: Directory! @defaultPath(path: "/")
"""
Optional: A custom container with Python and uv installed.
If not provided, defaults to Alpine Linux with uv.
"""
pub container: Container = null
let base: Container! {
if (container != null) {
container
} else {
alpine.
withExec(["apk", "add", "libgcc"]).
withEnvVariable("PYTHONUNBUFFERED", "1").
withEnvVariable("PATH", "/root/.local/bin:/usr/local/bin:$PATH", expand: true).
withEnvVariable("UV_LINK_MODE", "copy").
withEnvVariable("UV_PROJECT_ENVIRONMENT", "/opt/venv").
withMountedCache("/root/.cache/uv", cacheVolume("pytest-toolchain-uv")).
withDirectory("/usr/local/bin", uvBinary, include: ["uv*"])
}
}
"""
Test a Python project with pytest and OpenTelemetry tracing.
This function automatically injects pytest_otel for test tracing visibility
in Dagger TUI and Dagger Cloud. No configuration required.
The function will:
- Use the provided container or default to Alpine Linux with uv
- Install your project dependencies using uv sync
- Inject pytest_otel for automatic test tracing
- Run pytest with the specified Python version
"""
pub test(
"""
Additional arguments to pass to pytest (e.g., ["-x", "--tb=short"])
"""
args: [String!]! = ["-v"],
"""
Python version to use (e.g., "3.12", "3.11", "3.10")
"""
version: String! = "3.13",
): Void @check {
base.
withDirectory("/app", source).
withWorkdir("/app").
# Install user dependencies using uv with specified Python version
# This ensures Python is installed in the virtual environment
withExec([
"sh", "-c",
"if [ -f pyproject.toml ]; then uv sync -p " + version + "; elif [ -f requirements.txt ]; then uv venv -p " + version + " && uv pip install -r requirements.txt; fi"
]).
# Bundle and install pytest_otel into the project's virtual environment
# Using "uv pip install" with explicit python path (after uv sync creates the venv)
# This avoids modifying pyproject.toml which could cause resolution conflicts
withDirectory("/opt/pytest_otel", pytestOtelSource).
withExec(["uv", "pip", "install", "--python", "/opt/venv/bin/python", "/opt/pytest_otel"]).
withExec(["uv", "run", "-p", version, "pytest"] + args).
sync
null
}
}