1
2
3
4
5 package vcscmd
6
7 import (
8 "path/filepath"
9 "slices"
10 "testing"
11 )
12
13 func TestBuild(t *testing.T) {
14 repo := filepath.Join(t.TempDir(), "repo")
15 for _, tt := range []struct {
16 cmd *Command
17 want []string
18 }{
19 {New("hg").Option("--config", "x=y").Add("lookup").Operands("-ref"), []string{"hg", "--config=x=y", "lookup", "--", "-ref"}},
20 {New("fossil", "cat").Option("-R", repo).Option("-r", "trunk").Operands("file"), []string{"fossil", "cat", "-R", repo, "-r=trunk", "--", "file"}},
21 {New("git", "log").GitRevision("main", "--"), []string{"git", "log", "--end-of-options", "main", "--", "--"}},
22 {New("git", "fetch").Operands("origin"), []string{"git", "fetch", "--end-of-options", "origin"}},
23 } {
24 got, err := tt.cmd.Build()
25 if err != nil || !slices.Equal(got, tt.want) {
26 t.Fatalf("Build() = %q, %v; want %q, nil", got, err, tt.want)
27 }
28 got[0] = "changed"
29 if got, _ := tt.cmd.Build(); !slices.Equal(got, tt.want) {
30 t.Fatalf("Build returned mutable command storage: %q", got)
31 }
32 }
33 }
34
35 func TestInvalidCommand(t *testing.T) {
36 repo := filepath.Join(t.TempDir(), "repo")
37 for _, cmd := range []*Command{
38 {}, new(Command).Operands("file"), New(""), New("vcs", "--"), New("vcs").Add("--end-of-options"),
39 New("vcs").Option("", "v"), New("vcs").Option("--x=y", "v"),
40 New("vcs").Option("--", "v"), New("vcs").Option("--end-of-options", "v"),
41 New("fossil").Option("-R", ""), New("fossil").Option("-R", "relative"), New("fossil").Option("-x", "v"),
42 New("git").Option("-R", repo), New("git").Option("-r", "v"),
43 New("git").GitRevision("--"), New("hg").GitRevision("tip"),
44 New("git").Operands().Operands(), New("hg").Operands().Add("cat"),
45 New("hg").Operands().Option("--rev", "tip"),
46 } {
47 partial, err := cmd.Build()
48 if err == nil {
49 t.Fatalf("Build() = %q, nil; want error", partial)
50 }
51 if len(partial) != 0 {
52 got, nextErr := cmd.Option("-R", "").Add("later").Build()
53 if nextErr != err || !slices.Equal(got, partial) {
54 t.Fatalf("after error (%q, %v), got (%q, %v)", partial, err, got, nextErr)
55 }
56 }
57 }
58 }
59
View as plain text