diff --git a/.dagger/modules/e2e/lib/e2e.rb b/.dagger/modules/e2e/lib/e2e.rb index a49f4a0..fda03e6 100644 --- a/.dagger/modules/e2e/lib/e2e.rb +++ b/.dagger/modules/e2e/lib/e2e.rb @@ -23,3 +23,4 @@ class E2E require_relative "e2e/collection_checks" require_relative "e2e/sources_checks" require_relative "e2e/plain_checks" +require_relative "e2e/git_workspace_checks" diff --git a/.dagger/modules/e2e/lib/e2e/git_workspace_checks.rb b/.dagger/modules/e2e/lib/e2e/git_workspace_checks.rb new file mode 100644 index 0000000..c901ef9 --- /dev/null +++ b/.dagger/modules/e2e/lib/e2e/git_workspace_checks.rb @@ -0,0 +1,47 @@ +class E2E + GIT_WORKSPACE = <<~TOML.freeze + [modules.ruby-sdk] + source = "." + + [sdks.ruby] + module = "ruby-sdk" + + [sdks.ruby.scopes."#{TYPES}"] + is-module = true + name = "types" + clients = ["./#{DEP}"] + TOML + + function "A workspace opened from a git ref generates what its checkout generates. There a client of a module " \ + "of the same repository resolves as a git source at the workspace's commit; generation still binds it " \ + "by path, with the ref's selector and without one.", check: true + #: (ws: Core::Workspace) -> void + def git_workspace_generate_check(ws:) + ground = playground(ws).with_new_file(path: "#{PLAYGROUND}/dagger.toml", contents: GIT_WORKSPACE) + generates_nothing(ground, "", "generating from the checkout") + + server = git_server(ws, ground.directory(path: PLAYGROUND).without_directory(path: ".git")) + # The engine takes a git ref whose host has no dot for a Go import path + # and refuses it: the ref names the server by its full name, in the + # session domain that resolv.conf searches last. + repo = "git://#{server.hostname}.$(awk '/^search/ {print $NF}' /etc/resolv.conf)/ruby-sdk.git" + served = ground.with_service_binding(alias_: server.hostname, service: server) + generates_nothing(served, %(-W "#{repo}@main"), "generating from the git ref at main") + generates_nothing(served, %(-W "#{repo}"), "generating from the git ref without a selector") + end + + private + + # The CLI reports an empty changeset in a log line, which plain progress + # prints, and a changeset it does not apply as a summary near the end of + # its output. + def generates_nothing(container, flags, what) + attempt = container.with_workdir(path: PLAYGROUND) + .with_exec(args: ["sh", "-c", "dagger --progress=plain #{flags} generate --no-apply"], + experimental_privileged_nesting: true, expect: Core::ReturnType::ANY) + output = attempt.stderr + check(attempt.exit_code.zero? && output.include?("no changes to apply") && + !output.include?("Generated changes were not applied"), + "#{what} changed the files generated from the checkout:\n#{output.lines.last(80).join}") + end +end diff --git a/.dagger/modules/e2e/lib/e2e/install_checks.rb b/.dagger/modules/e2e/lib/e2e/install_checks.rb index 28f6602..5071a25 100644 --- a/.dagger/modules/e2e/lib/e2e/install_checks.rb +++ b/.dagger/modules/e2e/lib/e2e/install_checks.rb @@ -32,11 +32,11 @@ def sdk_installed_by_git_ref_check(ws:) # This repository, committed whole, as github.com/dagger/ruby-sdk would # serve it. The engine fetches by the service's own hostname: a binding's # alias, or a name set with withHostname, does not resolve for it here. - def git_server(ws) + def git_server(ws, source = tree(ws)) commit = "git init --quiet --initial-branch main && git add --all && " \ "git -c user.name=e2e -c user.email=e2e@dagger.io commit --quiet --no-gpg-sign --message sdk && " \ "git clone --quiet --bare /repo /srv/ruby-sdk.git" - development_image(ws).with_directory(path: "/repo", source: tree(ws)) + development_image(ws).with_directory(path: "/repo", source:) .with_workdir(path: "/repo") .with_exec(args: ["sh", "-c", commit]) .with_exposed_port(port: 9418) diff --git a/.dagger/modules/e2e/sdk/entrypoint/signatures.json b/.dagger/modules/e2e/sdk/entrypoint/signatures.json index f72ec0d..fa73eaf 100644 --- a/.dagger/modules/e2e/sdk/entrypoint/signatures.json +++ b/.dagger/modules/e2e/sdk/entrypoint/signatures.json @@ -159,6 +159,26 @@ } } ], + "lib/e2e/git_workspace_checks.rb": [ + { + "class": "E2E", + "nesting": [ + "E2E" + ], + "methods": { + "git_workspace_generate_check": { + "line": 19, + "params": [ + { + "name": "ws", + "type": "Core::Workspace" + } + ], + "returns": "void" + } + } + } + ], "lib/e2e/install_checks.rb": [ { "class": "E2E", diff --git a/.dagger/modules/e2e/sdk/entrypoint/types.dang b/.dagger/modules/e2e/sdk/entrypoint/types.dang index f4fd3fb..26610c5 100644 --- a/.dagger/modules/e2e/sdk/entrypoint/types.dang +++ b/.dagger/modules/e2e/sdk/entrypoint/types.dang @@ -262,6 +262,12 @@ type ModuleTypes { .withCheck .withArg("ws", typeDef.withObject("Workspace")) ) + .withFunction( + function("gitWorkspaceGenerateCheck", typeDef.withOptional(true).withKind(TypeDefKind.VOID_KIND)) + .withDescription("A workspace opened from a git ref generates what its checkout generates. There a client of a module of the same repository resolves as a git source at the workspace's commit; generation still binds it by path, with the ref's selector and without one.") + .withCheck + .withArg("ws", typeDef.withObject("Workspace")) + ) .withConstructor( function("", typeDef.withObject("E2E")) ), diff --git a/README.md b/README.md index 6b418fb..8b4308d 100644 --- a/README.md +++ b/README.md @@ -393,8 +393,9 @@ dagger module client add ../lib ``` This writes `clients/lib/`, a line of `dagger.gemfile` and the method `lib` of -`clients/roots/`, and relocks. No client reaches the manifest. A plain program -uses the long form, and a module the method: +`clients/roots/`, and relocks. No client reaches the manifest. A client loads +a module of the workspace by its path, also in a workspace opened from a git +ref. A plain program uses the long form, and a module the method: ```ruby # main.rb, a plain program diff --git a/lib/ruby_sdk/git_workspace.rb b/lib/ruby_sdk/git_workspace.rb new file mode 100644 index 0000000..669ac8c --- /dev/null +++ b/lib/ruby_sdk/git_workspace.rb @@ -0,0 +1,49 @@ +require "dagger" + +class RubySdk + # A workspace opened from git resolves its own modules as git sources at + # its commit: bound by that ref, a client would change with every commit + # and never match what the checkout generates, so it is bound by path. The + # Go and Python SDKs bind by the same rule (dagger/go-sdk#73, #78, #79; + # dagger/python-sdk#64, #65). + module GitWorkspace + module_function + + def includes?(ws, client) + matches?(address: ws.address, commit: commit(ws), clone_ref: client.clone_ref, ref: client.as_string, + pin: client.pin) + end + + # An unknown commit matches no client, which keeps its git binding. + def commit(ws) + ws.git.head.commit + rescue Dagger::QueryError + "" + end + + # A workspace without a selector resolves its modules at the default + # branch, which its address does not name. Selectors compare as written, + # so an alias, HEAD for main say, keeps the git binding. + def matches?(address:, commit:, clone_ref:, ref:, pin:) + return false if clone_ref.empty? || pin.empty? || pin != commit + + workspace_selector = selector(address, clone_ref) or return false + module_selector = selector(ref, clone_ref) or return false + workspace_selector.empty? || workspace_selector == module_selector + end + + # A workspace's address reads /@; from engine + # v1.0.0-beta.20 a module's ref reads #[:]. A GitHub + # address has no URL scheme, while its clone ref has https://. + def selector(ref, clone_ref) + repository = clone_ref.sub(%r{\A.*?://}, "") + path = ref.sub(%r{\A.*?://}, "") + return nil unless path.start_with?(repository) + + rest = path.delete_prefix(repository) + return rest.delete_prefix("#").partition(":").first if rest.start_with?("#") + + rest.empty? || rest.start_with?("/", "@") ? rest.partition("@").last : nil + end + end +end diff --git a/lib/ruby_sdk/scope.rb b/lib/ruby_sdk/scope.rb index 8711bd0..f752cb8 100644 --- a/lib/ruby_sdk/scope.rb +++ b/lib/ruby_sdk/scope.rb @@ -8,6 +8,7 @@ require_relative "codegen/stand_in" require_relative "signatures" require_relative "resources" +require_relative "git_workspace" class RubySdk # One directory the SDK manages, module or plain project. Its members are @@ -96,7 +97,7 @@ def client_members members = @clients.map do |client| name = client.module_name next Member.new(name, Codegen::Naming.client_file(name), local_ref(client.source_root_subpath), nil, client) \ - unless client.kind == Core::ModuleSourceKind::GIT_SOURCE + if client.kind != Core::ModuleSourceKind::GIT_SOURCE || GitWorkspace.includes?(@ws, client) Member.new(name, Codegen::Naming.client_file(name), client.as_string, git_pin(client), client) end diff --git a/sig/ruby_sdk/git_workspace.rbs b/sig/ruby_sdk/git_workspace.rbs new file mode 100644 index 0000000..6b2b120 --- /dev/null +++ b/sig/ruby_sdk/git_workspace.rbs @@ -0,0 +1,8 @@ +class RubySdk + module GitWorkspace + def self?.includes?: (Dagger::Clients::Core::Workspace ws, Dagger::Clients::Core::ModuleSource client) -> bool + def self?.commit: (Dagger::Clients::Core::Workspace ws) -> String + def self?.matches?: (address: String, commit: String, clone_ref: String, ref: String, pin: String) -> bool + def self?.selector: (String ref, String clone_ref) -> String? + end +end diff --git a/test/module/git_workspace_test.rb b/test/module/git_workspace_test.rb new file mode 100644 index 0000000..5962f7c --- /dev/null +++ b/test/module/git_workspace_test.rb @@ -0,0 +1,43 @@ +require_relative "../test_helper" +$LOAD_PATH.unshift File.expand_path("../../lib", __dir__) +require "ruby_sdk/git_workspace" + +class GitWorkspaceTest < Minitest::Test + SHA = "419c4afea78603265c40e273c455623a0291922e".freeze + OTHER = ("0" * 40).freeze + CLONE = "http://h/r.git".freeze + GITHUB = "https://github.com/o/r".freeze + + # The cases of dagger/python-sdk's workspaceRefMatchCheck: address, commit, + # clone ref, ref, pin, and whether the module is the workspace's. + CASES = { + "the workspace at the commit" => ["#{CLONE}@#{SHA}", SHA, CLONE, "#{CLONE}/app@#{SHA}", SHA, true], + "a workspace without a selector" => [CLONE, SHA, CLONE, "#{CLONE}/app@main", SHA, true], + "a workspace in a subdirectory" => ["#{CLONE}/ci@main", SHA, CLONE, "#{CLONE}/app@main", SHA, true], + "another selector at the same commit" => ["#{CLONE}@main", SHA, CLONE, "#{CLONE}/app@release", SHA, false], + "another commit" => ["#{CLONE}@main", OTHER, CLONE, "#{CLONE}/app@main", SHA, false], + "another repository" => ["#{CLONE}-fork@#{SHA}", SHA, CLONE, "#{CLONE}/app@#{SHA}", SHA, false], + "an unknown workspace commit" => ["#{CLONE}@#{SHA}", "", CLONE, "#{CLONE}/app@#{SHA}", SHA, false], + "a module without a pin" => ["#{CLONE}@#{SHA}", SHA, CLONE, "#{CLONE}/app@#{SHA}", "", false], + "a GitHub address without a scheme" => ["github.com/o/r@#{SHA}", SHA, GITHUB, "#{GITHUB}/app@#{SHA}", SHA, true], + "another host without a scheme" => ["gitlab.com/o/r@#{SHA}", SHA, GITHUB, "#{GITHUB}/app@#{SHA}", SHA, false], + "a repository the clone ref is a prefix of" => + ["github.com/o/r2@#{SHA}", SHA, GITHUB, "#{GITHUB}2/app@#{SHA}", SHA, false], + "the explicit form at the commit" => ["#{CLONE}@#{SHA}", SHA, CLONE, "#{CLONE}##{SHA}:app", SHA, true], + "the explicit form on GitHub" => ["github.com/o/r@#{SHA}", SHA, GITHUB, "#{GITHUB}##{SHA}:app", SHA, true], + "the explicit form at the repository root" => + ["github.com/o/r@#{SHA}", SHA, GITHUB, "#{GITHUB}##{SHA}", SHA, true], + "the explicit form at a branch" => ["#{CLONE}@main", SHA, CLONE, "#{CLONE}#main:app", SHA, true], + "the explicit form at another selector" => ["#{CLONE}@main", SHA, CLONE, "#{CLONE}#release:app", SHA, false], + "the explicit form in another repository" => + ["github.com/o/r@#{SHA}", SHA, "#{GITHUB}-fork", "#{GITHUB}-fork##{SHA}:app", SHA, false], + "the explicit form in a repository the clone ref is a prefix of" => + ["github.com/o/r@#{SHA}", SHA, GITHUB, "#{GITHUB}2##{SHA}:app", SHA, false] + }.freeze + + def test_a_module_of_the_workspace_is_in_its_repository_at_its_commit_and_selector + CASES.each do |name, (address, commit, clone_ref, ref, pin, want)| + assert_equal want, RubySdk::GitWorkspace.matches?(address:, commit:, clone_ref:, ref:, pin:), name + end + end +end diff --git a/test/module/scope_helper.rb b/test/module/scope_helper.rb index dd0201b..5483311 100644 --- a/test/module/scope_helper.rb +++ b/test/module/scope_helper.rb @@ -32,14 +32,28 @@ def with_new_file(path:, contents:) = FakeTree.new(files.merge(path => contents) FakeFile = Data.define(:contents) +FakeRef = Data.define(:commit) +FakeGit = Data.define(:head) + class FakeWorkspace - attr_reader :tree, :cwd + attr_reader :tree, :cwd, :address + + def initialize(tree, cwd, address: "", commit: nil) + @tree = tree + @cwd = cwd + @address = address + @commit = commit + end - def initialize(tree, cwd) = (@tree = tree) && (@cwd = cwd) + def git + raise Dagger::QueryError.new([{ "message" => "not in a git repository" }], nil) unless @commit + + FakeGit.new(FakeRef.new(@commit)) + end def directory(path:, **) = path == "/" ? tree : tree.directory(path: path.delete_prefix("/")) - def with_workdir(path:) = FakeWorkspace.new(tree, path == "." ? "/" : "/#{path}") + def with_workdir(path:) = copy(cwd: path == "." ? "/" : "/#{path}") def find_up(name:, from: nil) dir = File.join(cwd.delete_prefix("/"), from.to_s).delete_prefix("/").delete_suffix("/").delete_prefix("./") @@ -66,15 +80,17 @@ def with_new_directory(path:, source:) def without_directory(path:) prefix = "#{path.delete_prefix("/")}/" - FakeWorkspace.new(FakeTree.new(tree.files.reject { |p, _| p.start_with?(prefix) }), cwd) + copy(tree: FakeTree.new(tree.files.reject { |p, _| p.start_with?(prefix) })) end - def write(path, contents) - FakeWorkspace.new(FakeTree.new(tree.files.merge(path.delete_prefix("/") => contents)), cwd) - end + def write(path, contents) = copy(tree: FakeTree.new(tree.files.merge(path.delete_prefix("/") => contents))) + + private + + def copy(tree: @tree, cwd: @cwd) = FakeWorkspace.new(tree, cwd, address:, commit: @commit) end -FakeClient = Data.define(:module_name, :kind, :as_string, :pin, :source_root_subpath) +FakeClient = Data.define(:module_name, :kind, :as_string, :pin, :source_root_subpath, :clone_ref) # Scope with the engine's work replaced: members come out as marked trees. class OfflineScope < RubySdk::Scope diff --git a/test/module/scope_test.rb b/test/module/scope_test.rb index d69effe..d8af02e 100644 --- a/test/module/scope_test.rb +++ b/test/module/scope_test.rb @@ -3,7 +3,10 @@ class ScopeTest < Minitest::Test LOCAL = Dagger::Clients::Core::ModuleSourceKind::LOCAL_SOURCE GIT = Dagger::Clients::Core::ModuleSourceKind::GIT_SOURCE - LIB = FakeClient.new("lib", LOCAL, "", "", ".dagger/modules/lib") + LIB = FakeClient.new("lib", LOCAL, "", "", ".dagger/modules/lib", "") + SHA = "419c4afea78603265c40e273c455623a0291922e".freeze + OTHER = ("0" * 40).freeze + CLONE = "http://h/r.git".freeze def generate(files, cwd: "/app", is_module: true, clients: [], name: "app", **) ws = FakeWorkspace.new(FakeTree.new(files), cwd) @@ -53,7 +56,7 @@ def test_clients_that_become_one_member_are_refused end def test_a_git_client_is_pinned_and_a_local_one_is_rooted - git = FakeClient.new("dep", GIT, "github.com/acme/dep@v1", "abc123", "dep") + git = FakeClient.new("dep", GIT, "github.com/acme/dep@v1", "abc123", "dep", "https://github.com/acme/dep") scope = OfflineScope.new(ws: FakeWorkspace.new(FakeTree.new({}), "/"), is_module: false, name: "", clients: [git, LIB.with(source_root_subpath: "./lib/")], sdk: nil, image: "ruby") @@ -62,8 +65,26 @@ def test_a_git_client_is_pinned_and_a_local_one_is_rooted assert_equal "/", RubySdk::Scope.local_ref(".") end + def test_a_git_client_of_the_git_workspace_is_rooted_and_another_commit_pinned + ws = FakeWorkspace.new(FakeTree.new({}), "/", address: "#{CLONE}@main", commit: SHA) + clients = [FakeClient.new("dep", GIT, "#{CLONE}#main:dep", SHA, "dep", CLONE), + FakeClient.new("lib", GIT, "#{CLONE}#v1:lib", OTHER, "lib", CLONE)] + scope = OfflineScope.new(ws:, is_module: false, name: "", clients:, sdk: nil, image: "ruby") + + assert_equal([["dep", "/dep", nil], ["lib", "#{CLONE}#v1:lib", OTHER]], + scope.client_members.map { [it.dir, it.ref, it.pin] }) + end + + def test_a_git_fake_workspace_keeps_its_address_and_commit_through_each_copy + ws = FakeWorkspace.new(FakeTree.new("a/x" => ""), "/", address: "#{CLONE}@main", commit: SHA) + + [ws.with_workdir(path: "a"), ws.without_directory(path: "a"), ws.write("b", "")].each do |copy| + assert_equal ["#{CLONE}@main", SHA], [copy.address, copy.git.head.commit] + end + end + def test_a_git_client_without_a_pin_is_refused - git = FakeClient.new("dep", GIT, "github.com/acme/dep@v1", "", "dep") + git = FakeClient.new("dep", GIT, "github.com/acme/dep@v1", "", "dep", "https://github.com/acme/dep") scope = OfflineScope.new(ws: FakeWorkspace.new(FakeTree.new({}), "/"), is_module: false, name: "", clients: [git], sdk: nil, image: "ruby")