Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions .dagger/modules/e2e/main.dang
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,7 @@ type E2e {
.withNewFile("/" + scope + "/tools/mine/pyproject.toml", "[project]\nname = \"mine\"\n")
.withNewFile("/" + scope + "/clients/tools/pyproject.toml", "[project]\nname = \"tools\"\n")
.withNewFile("/" + scope + "/clients/notes/README.md", "not a member\n")
.withNewFile("/" + scope + "/clients/README.md", "not a member either\n")
.withNewFile("/" + scope + "/clients/handmade/pyproject.toml", "[project]\nname = \"handmade\"\ndescription = \"\"\"\n[tool.dagger]\ngenerated = \"client\"\n\"\"\"\n")
.withNewFile("/" + scope + "/clients/handmade/secret.txt", "mine\n")
# A marker that cannot be read never licenses a delete.
Expand Down Expand Up @@ -865,6 +866,9 @@ type E2e {
null
}
assert(removed.directory("/" + scope).exists(clientDepMember) == false, "a removed client's member was left behind")
let aliased = generated.withDirectory("/" + scope + "/clients", directory.withSymlink("client-dep", "alias")).withWorkdir(scope)
let aliasKept = pythonSdk.generateScope(aliased, isModule: false, name: "user-content", clients: []).withWorkdir(".")
assert(aliasKept.directory("/" + scope).exists("clients/alias", expectedType: ExistsType.SYMLINK_TYPE, doNotFollowSymlinks: true), "a symlink under clients/ was taken for the generated client it points to")
let removedFile = removed.file("/" + scope + "/pyproject.toml").contents
assertNotContains(removedFile, "\"" + clientDepMember + "\"", "a removed client stayed a workspace member")
assertContains(removedFile, "\"clients/tools\"", "removing a client dropped the user's member under clients/")
Expand Down Expand Up @@ -1045,6 +1049,79 @@ type E2e {
null
}

"""
The Python floor refusal case by case: each version form generation reads,
from .python-version or requires-python. A user-owned sdk/ ends generation
right after the refusal check, so a case that is not refused costs no
generation.
"""
pub pythonRefusalCasesCheck(scope: Workspace!): Void @check {
let ws = withScope(scope, tomlGenerateModulePath, "toml-generate-app")
.withNewFile("/" + tomlGenerateModulePath + "/sdk/mine.txt", "mine\n")
let pyprojectPath = "/" + tomlGenerateModulePath + "/pyproject.toml"
let project = "[project]\nname = \"app\"\nrequires-python = \""
let newer = project + ">=3.14\"\n"
let ownImage = "\n[tool.dagger]\nbase-image = \"python:3.14-slim\"\n"
let floorNeeded = "this Python SDK needs Python 3.11 or later, and "
let pinRefusal = floorNeeded + ".python-version pins Python "
let pinFix = ". Set it to Python 3.11 or later"
let requiresRefusal = floorNeeded + "requires-python = \""
let requiresFix = ". Ask for Python 3.11 or later"
let lastReleaseOffer = ", or pin the last release of this SDK that runs on it: dagger module install github.com/dagger/python-sdk@v1.2.0"
# file "" is no pyproject.toml, pin "" no .python-version, want "" no refusal.
[
{{file: newer, pin: "3.10", want: pinRefusal + "3.10" + pinFix + lastReleaseOffer}},
{{file: newer, pin: "3.10.9", want: pinRefusal + "3.10.9" + pinFix + lastReleaseOffer}},
{{file: newer, pin: "2.7", want: pinRefusal + "2.7" + pinFix}},
{{file: newer, pin: "2", want: pinRefusal + "2" + pinFix}},
{{file: newer, pin: "3.11.0rc1", want: pinRefusal + "3.11.0rc1" + pinFix + lastReleaseOffer}},
{{file: newer, pin: "3.11rc1", want: pinRefusal + "3.11rc1" + pinFix + lastReleaseOffer}},
{{file: newer, pin: "3.11.dev0", want: pinRefusal + "3.11.dev0" + pinFix + lastReleaseOffer}},
{{file: newer, pin: "3.11", want: ""}},
{{file: newer, pin: "3.11.0", want: ""}},
{{file: newer, pin: "3.11.1rc1", want: ""}},
{{file: newer, pin: "3.12", want: ""}},
{{file: newer, pin: "3.13.0b2", want: ""}},
{{file: newer, pin: "3", want: ""}},
{{file: newer, pin: "4", want: ""}},
{{file: newer, pin: " ", want: ""}},
{{file: "", pin: "pypy3.10", want: ""}},
{{file: project + ">=3.10\"\n", pin: "3.13", want: ""}},
{{file: ownImage, pin: "3.10", want: ""}},
{{file: project + ">=3.11\"\n", pin: "", want: ""}},
{{file: newer, pin: "", want: ""}},
{{file: project + ">=3.10\"\n", pin: "", want: requiresRefusal + ">=3.10\" asks for Python 3.10" + requiresFix + lastReleaseOffer}},
{{file: project + ">=3.10,<4\"\n", pin: "", want: requiresRefusal + ">=3.10,<4\" asks for Python 3.10" + requiresFix + lastReleaseOffer}},
{{file: project + "==3.10.4\"\n", pin: "", want: requiresRefusal + "==3.10.4\" asks for Python 3.10.4" + requiresFix + lastReleaseOffer}},
{{file: project + ">=3.9\"\n", pin: "", want: requiresRefusal + ">=3.9\" asks for Python 3.9" + requiresFix}},
{{file: project + ">=2.7\"\n", pin: "", want: requiresRefusal + ">=2.7\" asks for Python 2.7" + requiresFix}},
{{file: project + ">=2\"\n", pin: "", want: requiresRefusal + ">=2\" asks for Python 2" + requiresFix}},
{{file: project + "~=3.10\"\n", pin: "", want: ""}},
{{file: project + ">=3.10\"\n" + ownImage, pin: "", want: ""}},
{{file: "[project]\nname = \"app\"\n", pin: "", want: ""}},
{{file: "", pin: "", want: ""}},
].each { row =>
let files = if (row.file == "") { ws.withoutFile(pyprojectPath) } else { ws.withNewFile(pyprojectPath, row.file) }
let module = if (row.pin == "") { files } else { files.withNewFile("/" + tomlGenerateModulePath + "/.python-version", row.pin + "\n") }
# checkPython runs before generation, and a user-owned sdk/ makes the next
# step raise (scope.dang checkMembers), so a case that passes the refusal
# ends there without generating.
let got = pythonSdk.mod(module, path: tomlGenerateModulePath, findUp: false).generated.cwd rescue {
err: Error => err.message
}
let pinLabel = if (row.pin == "") { "no .python-version" } else { ".python-version " + row.pin }
let fileLabel = if (row.file == "") { "no pyproject.toml" } else { "pyproject.toml " + row.file }
let label = pinLabel + ", " + fileLabel + ": "
if (row.want == "") {
assertContains(got, "sdk/ exists in " + tomlGenerateModulePath, label + "refused or failed otherwise: " + got)
} else {
assertContains(got, "the module at " + tomlGenerateModulePath + ": " + row.want + ", then run dagger generate again", label + "got " + got)
}
null
}
null
}

"""
A module on this repository's runtime builds and runs, driven through the
floor release's CLI. The fixture points `[runtime] source` at `runtime/` by relative
Expand Down Expand Up @@ -2219,6 +2296,11 @@ type E2e {

let pyproj = configModulePath + "/pyproject.toml"

let mistypedToml = "[project]\nrequires-python = \" ~= 3.12 \"\n\n[tool.dagger]\nuse-uv = \"false\"\nbase-image = 3\nglobal-client = [true]\n"
let mistyped = pythonSdk.mod(ws.withNewFile("/" + pyproj, mistypedToml), path: configModulePath).config.get
assert(mistyped.pythonVersion == "3.12", "pythonVersion should read without its operator, got " + (mistyped.pythonVersion ?? "null"))
assert(mistyped.useUv == null and mistyped.baseImage == null and mistyped.globalClient == null, "a mistyped setting should read as unset")

let py = app.set(pythonVersion: "3.13")
assert(contains(py.modifiedPaths, pyproj), "set should modify pyproject.toml")
assert(py.modifiedPaths.length == 1, "set modified more than pyproject.toml")
Expand Down
2 changes: 2 additions & 0 deletions .dagger/modules/engine-e2e/main.dang
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ type EngineE2e {

pub pythonFloorRefusalCheck(ws: Workspace!): Void @check { onFloor(ws, "python-floor-refusal-check") }

pub pythonRefusalCasesCheck(ws: Workspace!): Void @check { onFloor(ws, "python-refusal-cases-check") }

pub runtimeCallCheck(ws: Workspace!): Void @check { onFloor(ws, "runtime-call-check") }

pub runtimeRequiresGeneratedFilesCheck(ws: Workspace!): Void @check { onFloor(ws, "runtime-requires-generated-files-check") }
Expand Down
60 changes: 3 additions & 57 deletions helpers/pyproject/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,21 @@ func main() {
}
}

// run dispatches a subcommand. get-* commands print to stdout (no trailing
// newline). set-* commands edit the file in place and re-emit it.
// edit-scope and set-global-client edit the file in place and keep its
// formatting: they touch a scope file that generation also owns.
//
// get-member-kinds takes a scope directory instead of a file, and
// python-refusal a module directory.
// run dispatches a subcommand. set-* commands edit the file in place and
// re-emit it. edit-scope and set-global-client edit the file in place and keep
// its formatting: they touch a scope file that generation also owns.
//
// derive-resources takes a directory of pyproject.toml files at their
// workspace paths and a module directory in it, and prints as JSON the paths
// the module's build reads outside the module directory, each relative to the
// module directory ("pattern") and to the workspace root ("rooted").
//
// get-generated prints nothing for a file that does not parse: such a file is
// no member of the SDK's, which is all its callers ask.
//
// usage: pyproject <command> <file> [value | flags]
func run(args []string) error {
if len(args) < 2 {
return fmt.Errorf("usage: pyproject <command> <file> [value | flags]")
}
cmd, path := args[0], args[1]
if cmd == "get-member-kinds" {
out, err := memberKinds(path)
if err != nil {
return err
}
fmt.Print(out)
return nil
}
if cmd == "derive-resources" {
module, err := value(args)
if err != nil {
Expand All @@ -61,14 +46,6 @@ func run(args []string) error {
fmt.Print(string(out))
return nil
}
if cmd == "python-refusal" {
out, err := pythonRefusal(path)
if err != nil {
return err
}
fmt.Print(out)
return nil
}

data, err := os.ReadFile(path)
if err != nil {
Expand All @@ -79,37 +56,13 @@ func run(args []string) error {
return runEditScope(path, data, args[2:])
case "set-global-client":
return runSetGlobalClient(path, data, args)
case "get-generated":
fmt.Print(generatedKind(data))
return nil
}
doc, err := load(data)
if err != nil {
return err
}

switch cmd {
case "get-python-version":
fmt.Print(getPythonVersion(doc))
return nil
case "get-use-uv":
// Print nothing when unset so callers can tell an absent setting from
// an explicit value, instead of guessing the true default.
if v, ok := getUseUv(doc); ok {
fmt.Print(boolStr(v))
}
return nil
case "get-base-image":
fmt.Print(getBaseImage(doc))
return nil
case "get-members":
fmt.Print(getMembers(doc))
return nil
case "get-global-client":
if v, ok := getGlobalClient(doc); ok {
fmt.Print(boolStr(v))
}
return nil
case "set-python-version":
v, err := value(args)
if err != nil {
Expand Down Expand Up @@ -215,10 +168,3 @@ func value(args []string) (string, error) {
}
return args[2], nil
}

func boolStr(b bool) string {
if b {
return "true"
}
return "false"
}
65 changes: 0 additions & 65 deletions helpers/pyproject/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,6 @@ func TestRunSetThenGet(t *testing.T) {
}
}

func TestRunGetDoesNotWrite(t *testing.T) {
p := writeTemp(t, sample)
before, err := os.ReadFile(p)
if err != nil {
t.Fatalf("read: %v", err)
}
if err := run([]string{"get-python-version", p}); err != nil {
t.Fatalf("get: %v", err)
}
after, err := os.ReadFile(p)
if err != nil {
t.Fatalf("read: %v", err)
}
if string(before) != string(after) {
t.Errorf("get-* must not modify the file:\nbefore:\n%s\nafter:\n%s", before, after)
}
}

func TestRunUnknownCommand(t *testing.T) {
p := writeTemp(t, sample)
if err := run([]string{"bogus", p}); err == nil {
Expand Down Expand Up @@ -215,50 +197,3 @@ func printed(t *testing.T, args ...string) string {
}
return string(out)
}

func TestRunGetGenerated(t *testing.T) {
for name, tc := range map[string]struct {
src string
want string
}{
"marked": {"[project]\nname = \"dagger-clients-linter\"\n\n[tool.dagger]\ngenerated = \"client\"\n", "client"},
"not marked": {sample, ""},
"inside a multiline string": {`[project]
name = "notes"
description = """
[tool.dagger]
generated = "client"
"""
`, ""},
"unparsable": {"[tool.dagger]\ngenerated = \"client\"\n[tool.dagger]\n", ""},
} {
t.Run(name, func(t *testing.T) {
if got := printed(t, "get-generated", writeTemp(t, tc.src)); got != tc.want {
t.Errorf("got %q, want %q", got, tc.want)
}
})
}
}

func TestRunGetGlobalClientReadsTheFlagAsTOML(t *testing.T) {
for name, tc := range map[string]struct {
src string
want string
}{
"set": {"[tool.dagger]\nglobal-client = true\n", "true"},
"unset": {sample, ""},
"inside a multiline string": {`[project]
name = "notes"
description = """
[tool.dagger]
global-client = true
"""
`, ""},
} {
t.Run(name, func(t *testing.T) {
if got := printed(t, "get-global-client", writeTemp(t, tc.src)); got != tc.want {
t.Errorf("got %q, want %q", got, tc.want)
}
})
}
}
59 changes: 0 additions & 59 deletions helpers/pyproject/members.go

This file was deleted.

Loading