package buildstep import ( "context" "strings" "testing" "git.novox.be/novox/mesh-sdk/go/forgekey" ) // 1674/AC3: no base is refused before anything runs; so is a base or a commit that is not a full sha, a workspace that // is not at the commit, and a remote that is not ssh://. func TestMergeIntoRefusesWhatIsNotAMerge(t *testing.T) { f := &fakeGit{} for name, c := range map[string]struct{ remote, base, commit string }{ "no base": {"ssh://mesh-forge/o/r.git", "", sha}, "a short base": {"ssh://mesh-forge/o/r.git", "abc123", sha}, "a branch as base": {"ssh://mesh-forge/o/r.git", "main", sha}, "a short commit": {"ssh://mesh-forge/o/r.git", sha, "abc"}, "an https remote": {"https://forge.example/o/r.git", sha, sha}, } { _, err := MergeInto(context.Background(), f.run, "/w", c.remote, c.base, c.commit) if err == nil { t.Errorf("%s accepted", name) } } if len(f.calls) != 0 { t.Errorf("git ran for a refused merge: %v", f.calls) } g := &fakeGit{rule: func(l string) forgekey.Ran { if strings.Contains(l, "rev-parse HEAD") { return forgekey.Ran{Stdout: strings.Repeat("f", 40) + "\n"} } return forgekey.Ran{} }} _, err := MergeInto(context.Background(), g.run, "/w", "ssh://mesh-forge/o/r.git", strings.Repeat("a", 40), sha) if err == nil || !strings.Contains(err.Error(), "not at the commit") || len(g.calls) != 1 { t.Errorf("a workspace at another commit: %v after %d commands", err, len(g.calls)) } } // 1674/AC4: the merge is made with the check merge's identity and flags, hooks off, and fetches with no credential. func TestMergeIntoRunsTheCheckMergesCommands(t *testing.T) { base := strings.Repeat("b", 40) f := &fakeGit{rule: func(l string) forgekey.Ran { switch { case strings.HasSuffix(l, "rev-parse HEAD"): return forgekey.Ran{Stdout: sha + "\n"} case strings.Contains(l, "--is-shallow-repository"): return forgekey.Ran{Stdout: "true\n"} } return forgekey.Ran{} }} if _, err := MergeInto(context.Background(), f.run, "/w", "ssh://mesh-forge/o/r.git", base, sha); err != nil { t.Fatal(err) } all := strings.Join(f.calls, "\n") for _, want := range []string{ pre + "fetch --no-tags --unshallow origin " + base, pre + "checkout -q --detach " + base, "user.name=mesh merge check", "user.email=merge-check@mesh.invalid", "commit.gpgsign=false", "core.hooksPath=/dev/null", "merge --no-ff --no-edit --no-verify --quiet -m", pre + "diff --name-only --no-renames " + base, } { if !strings.Contains(all, want) { t.Errorf("the commands lack %q:\n%s", want, all) } } for _, c := range f.calls { if strings.Contains(c, "https://") || !strings.Contains(c, "credential.helper= ") { t.Errorf("command %q carries a credential or https", c) } } }