| 🛟 Updated. 28d5985 k33g 12h ago | 1 | package tools |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func TestACommandWithNoPlaceholdersAsksForNothing(t *testing.T) { |
| 9 | // The common case, and the one that must stay instant: an ordinary tool |
| 10 | // runs without a box opening in front of it. |
| 11 | if got := (Tool{Command: "go test ./..."}).Placeholders(); got != nil { |
| 12 | t.Errorf("Placeholders() = %v, want none", got) |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | func TestAPlaceholderIsAskedForByItsLabel(t *testing.T) { |
| 17 | got := Tool{Command: "go mod init {{module path}}"}.Placeholders() |
| 18 | |
| 19 | if len(got) != 1 { |
| 20 | t.Fatalf("Placeholders() = %v, want one", got) |
| 21 | } |
| 22 | if got[0].Label != "module path" { |
| 23 | t.Errorf("Label = %q, want %q", got[0].Label, "module path") |
| 24 | } |
| 25 | if got[0].Raw { |
| 26 | t.Error("Raw = true; a plain placeholder is shell-quoted") |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | func TestPlaceholdersComeBackInTheOrderTheyAppear(t *testing.T) { |
| 31 | got := Tool{Command: "cp {{source}} {{destination}}"}.Placeholders() |
| 32 | |
| 33 | if len(got) != 2 || got[0].Label != "source" || got[1].Label != "destination" { |
| 34 | t.Errorf("Placeholders() = %v, want source then destination", got) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | func TestALabelUsedTwiceIsAskedForOnce(t *testing.T) { |
| 39 | // One field, and both occurrences get what is typed into it. |
| 40 | got := Tool{Command: "mkdir {{name}} && cd {{name}}"}.Placeholders() |
| 41 | |
| 42 | if len(got) != 1 || got[0].Label != "name" { |
| 43 | t.Fatalf("Placeholders() = %v, want just name", got) |
| 44 | } |
| 45 | if filled := (Tool{Command: "mkdir {{name}} && cd {{name}}"}).Fill(map[string]string{"name": "src"}); filled != "mkdir 'src' && cd 'src'" { |
| 46 | t.Errorf("Fill() = %q", filled) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | func TestATrailingEllipsisAsksForTheValueVerbatim(t *testing.T) { |
| 51 | got := Tool{Command: "go test {{extra flags...}} ./..."}.Placeholders() |
| 52 | |
| 53 | if len(got) != 1 { |
| 54 | t.Fatalf("Placeholders() = %v, want one", got) |
| 55 | } |
| 56 | if got[0].Label != "extra flags" { |
| 57 | t.Errorf("Label = %q, want the ... stripped off", got[0].Label) |
| 58 | } |
| 59 | if !got[0].Raw { |
| 60 | t.Error("Raw = false; a trailing ... asks for the value verbatim") |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | func TestTheEllipsisDoesNotSwallowTheRestOfTheCommand(t *testing.T) { |
| 65 | // `./...` is in nearly every Go tools file, and it must not be read as a |
| 66 | // placeholder's raw marker leaking past the closing braces. |
| 67 | filled := Tool{Command: "go test {{extra flags...}} ./..."}.Fill(map[string]string{"extra flags": "-run TestX -v"}) |
| 68 | |
| 69 | if want := "go test -run TestX -v ./..."; filled != want { |
| 70 | t.Errorf("Fill() = %q, want %q", filled, want) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | func TestSingleBracesAreNotPlaceholders(t *testing.T) { |
| 75 | // Both of these are ordinary things to put in a tools file, and reading |
| 76 | // either as a placeholder would turn a working command into a dialog |
| 77 | // asking for "print $1". |
| 78 | tests := map[string]string{ |
| 79 | "awk": `git log --oneline | awk '{print $1}'`, |
| 80 | "find": `find . -name '*.tmp' -exec rm {} +`, |
| 81 | } |
| 82 | |
| 83 | for name, command := range tests { |
| 84 | t.Run(name, func(t *testing.T) { |
| 85 | if got := (Tool{Command: command}).Placeholders(); got != nil { |
| 86 | t.Errorf("Placeholders() = %v, want none", got) |
| 87 | } |
| 88 | if filled := (Tool{Command: command}).Fill(nil); filled != command { |
| 89 | t.Errorf("Fill() changed the command:\n got %q\nwant %q", filled, command) |
| 90 | } |
| 91 | }) |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | func TestAValueIsShellQuotedSoASpaceStaysOneArgument(t *testing.T) { |
| 96 | filled := Tool{Command: "go build -o {{output}}"}.Fill(map[string]string{"output": "my binary"}) |
| 97 | |
| 98 | if want := "go build -o 'my binary'"; filled != want { |
| 99 | t.Errorf("Fill() = %q, want %q", filled, want) |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | func TestAQuoteInsideAValueDoesNotEscapeTheQuoting(t *testing.T) { |
| 104 | // The one case single-quoting has to handle, and the one that would let a |
| 105 | // typed value run as a command if it were got wrong. |
| 106 | filled := Tool{Command: "echo {{message}}"}.Fill(map[string]string{"message": "it's here"}) |
| 107 | |
| 108 | if want := `echo 'it'\''s here'`; filled != want { |
| 109 | t.Errorf("Fill() = %q, want %q", filled, want) |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | func TestShellQuoteSurvivesWhatAShellWouldOtherwiseRead(t *testing.T) { |
| 114 | for _, value := range []string{"a b", "$HOME", "`whoami`", "a; rm -rf /", "*", "it's", ""} { |
| 115 | quoted := ShellQuote(value) |
| 116 | if !strings.HasPrefix(quoted, "'") || !strings.HasSuffix(quoted, "'") { |
| 117 | t.Errorf("ShellQuote(%q) = %q, want it wrapped in single quotes", value, quoted) |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | func TestARawPlaceholderIsNotQuoted(t *testing.T) { |
| 123 | filled := Tool{Command: "cargo test {{args...}}"}.Fill(map[string]string{"args": "--release --all-features"}) |
| 124 | |
| 125 | if want := "cargo test --release --all-features"; filled != want { |
| 126 | t.Errorf("Fill() = %q, want %q", filled, want) |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | func TestALabelWithNoValueBecomesNothing(t *testing.T) { |
| 131 | // The command then reports its own complaint, which says more than the |
| 132 | // editor guessing what an empty field was supposed to mean. |
| 133 | filled := Tool{Command: "go mod init {{module}}"}.Fill(map[string]string{}) |
| 134 | |
| 135 | if want := "go mod init ''"; filled != want { |
| 136 | t.Errorf("Fill() = %q, want %q", filled, want) |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | func TestFillLeavesACommandWithNoPlaceholdersAlone(t *testing.T) { |
| 141 | const command = "go test ./..." |
| 142 | |
| 143 | if filled := (Tool{Command: command}).Fill(map[string]string{"unused": "x"}); filled != command { |
| 144 | t.Errorf("Fill() = %q, want it unchanged", filled) |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | func TestALabelUsedBothWaysIsSubstitutedPerOccurrence(t *testing.T) { |
| 149 | // Asked for once; each occurrence then honours its own braces. |
| 150 | filled := Tool{Command: "echo {{x}} && echo {{x...}}"}.Fill(map[string]string{"x": "a b"}) |
| 151 | |
| 152 | if want := "echo 'a b' && echo a b"; filled != want { |
| 153 | t.Errorf("Fill() = %q, want %q", filled, want) |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | func TestAHalfTypedPlaceholderIsRefusedRatherThanRun(t *testing.T) { |
| 158 | // Left alone it would reach the shell with its braces still in it, which |
| 159 | // fails in a way that names neither the tool nor the file. |
| 160 | dir := project(t, "[[tool]]\nname = \"Init\"\ncommand = \"go mod init {{module\"\n") |
| 161 | |
| 162 | _, err := Load(testProfile(), dir) |
| 163 | |
| 164 | if err == nil { |
| 165 | t.Fatal("Load() accepted a placeholder that is never closed") |
| 166 | } |
| 167 | for _, want := range []string{"Init", "never closed"} { |
| 168 | if !strings.Contains(err.Error(), want) { |
| 169 | t.Errorf("the error never mentions %q: %v", want, err) |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | func TestAPlaceholderWithNoLabelIsRefused(t *testing.T) { |
| 175 | // It would open a box asking for nothing, under a blank heading. |
| 176 | for _, command := range []string{"go mod init {{}}", "go mod init {{ }}", "go test {{...}}"} { |
| 177 | t.Run(command, func(t *testing.T) { |
| 178 | dir := project(t, "[[tool]]\nname = \"X\"\ncommand = \""+command+"\"\n") |
| 179 | |
| 180 | if _, err := Load(testProfile(), dir); err == nil { |
| 181 | t.Errorf("Load() accepted %q", command) |
| 182 | } |
| 183 | }) |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | func TestAWellFormedPlaceholderLoadsFine(t *testing.T) { |
| 188 | dir := project(t, "[[tool]]\nname = \"Init\"\ncommand = \"go mod init {{module path}}\"\n") |
| 189 | |
| 190 | list, err := Load(testProfile(), dir) |
| 191 | if err != nil { |
| 192 | t.Fatalf("Load() error = %v", err) |
| 193 | } |
| 194 | |
| 195 | got := list.Tools()[0].Placeholders() |
| 196 | if len(got) != 1 || got[0].Label != "module path" { |
| 197 | t.Errorf("Placeholders() = %v, want one called %q", got, "module path") |
| 198 | } |
| 199 | } |