|
| 1 | +// Copyright 2023 Harald Albrecht. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 4 | +// use this file except in compliance with the License. You may obtain a copy |
| 5 | +// of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | +// License for the specific language governing permissions and limitations |
| 13 | +// under the License. |
| 14 | + |
| 15 | +package enumflag |
| 16 | + |
| 17 | +import ( |
| 18 | + "io" |
| 19 | + "os" |
| 20 | + "os/exec" |
| 21 | + "path/filepath" |
| 22 | + "syscall" |
| 23 | + "time" |
| 24 | + |
| 25 | + "github.com/onsi/gomega/gbytes" |
| 26 | + "github.com/onsi/gomega/gexec" |
| 27 | + "golang.org/x/exp/slices" |
| 28 | + |
| 29 | + . "github.com/onsi/ginkgo/v2" |
| 30 | + . "github.com/onsi/gomega" |
| 31 | + . "github.com/thediveo/success" |
| 32 | +) |
| 33 | + |
| 34 | +const dummyCommandName = "enumflag-testing" |
| 35 | + |
| 36 | +// See: |
| 37 | +// https://serverfault.com/questions/506612/standard-place-for-user-defined-bash-completion-d-scripts/1013395#1013395 |
| 38 | +const bashComplDirEnv = "BASH_COMPLETION_USER_DIR" |
| 39 | + |
| 40 | +type writer struct { |
| 41 | + io.WriteCloser |
| 42 | +} |
| 43 | + |
| 44 | +func (w *writer) WriteString(s string) { |
| 45 | + GinkgoHelper() |
| 46 | + Expect(w.WriteCloser.Write([]byte(s))).Error().NotTo(HaveOccurred()) |
| 47 | +} |
| 48 | + |
| 49 | +var _ = Describe("flag enum completions end-to-end", Ordered, func() { |
| 50 | + |
| 51 | + var enumflagTestingPath string |
| 52 | + var completionsUserDir string |
| 53 | + |
| 54 | + BeforeAll(func() { |
| 55 | + By("building a CLI binary for testing") |
| 56 | + enumflagTestingPath = Successful(gexec.Build("./test/enumflag-testing")) |
| 57 | + DeferCleanup(func() { |
| 58 | + gexec.CleanupBuildArtifacts() |
| 59 | + }) |
| 60 | + |
| 61 | + By("creating a temporary directory for storing completion scripts") |
| 62 | + completionsUserDir = Successful(os.MkdirTemp("", "bash-completions-*")) |
| 63 | + DeferCleanup(func() { |
| 64 | + os.RemoveAll(completionsUserDir) |
| 65 | + }) |
| 66 | + // Notice how the bash-completion FAQ |
| 67 | + // https://github.com/scop/bash-completion/blob/master/README.md#faq |
| 68 | + // says that the completions must be inside a "completions" sub |
| 69 | + // directory of $BASH_COMPLETION_USER_DIR, and not inside |
| 70 | + // $BASH_COMPLETION_USER_DIR itself ... yeah, 🤷 |
| 71 | + Expect(os.Mkdir(filepath.Join(completionsUserDir, "completions"), 0770)).To(Succeed()) |
| 72 | + |
| 73 | + By("telling the CLI binary to give us a completion script that we then store away") |
| 74 | + session := Successful( |
| 75 | + gexec.Start(exec.Command(enumflagTestingPath, "completion", "bash"), |
| 76 | + GinkgoWriter, GinkgoWriter)) |
| 77 | + Eventually(session).Within(5 * time.Second).ProbeEvery(100 * time.Millisecond). |
| 78 | + Should(gexec.Exit(0)) |
| 79 | + completionScript := session.Out.Contents() |
| 80 | + Expect(completionScript).To(MatchRegexp(`^# bash completion V2 for`)) |
| 81 | + Expect(os.WriteFile(filepath.Join(completionsUserDir, "completions", "enumflag-testing"), |
| 82 | + completionScript, 0770)). |
| 83 | + To(Succeed()) |
| 84 | + }) |
| 85 | + |
| 86 | + Bash := func() (*gexec.Session, *writer) { |
| 87 | + GinkgoHelper() |
| 88 | + By("creating a new test bash session") |
| 89 | + bashCmd := exec.Command("/bin/bash", "--rcfile", "/etc/profile", "-i") |
| 90 | + // Run the silly interactive subshell in its own session so we don't get |
| 91 | + // funny surprises such as the subshell getting suspended by its parent |
| 92 | + // shell... |
| 93 | + bashCmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true} |
| 94 | + bashCmd.Env = append(slices.Clone(os.Environ()), |
| 95 | + bashComplDirEnv+"="+completionsUserDir, |
| 96 | + "PATH="+filepath.Dir(enumflagTestingPath)+":"+os.Getenv("PATH"), |
| 97 | + ) |
| 98 | + stdin := &writer{Successful(bashCmd.StdinPipe())} |
| 99 | + session := Successful( |
| 100 | + gexec.Start(bashCmd, GinkgoWriter, GinkgoWriter)) |
| 101 | + DeferCleanup(func() { |
| 102 | + By("killing the test bash session") |
| 103 | + stdin.Close() |
| 104 | + session.Kill().Wait(2 * time.Second) |
| 105 | + }) |
| 106 | + return session, stdin |
| 107 | + } |
| 108 | + |
| 109 | + var bash *gexec.Session |
| 110 | + var bashin *writer |
| 111 | + |
| 112 | + BeforeEach(func() { |
| 113 | + bash, bashin = Bash() |
| 114 | + }) |
| 115 | + |
| 116 | + It("tab-completes the canary's name in $PATH", func() { |
| 117 | + By("checking BASH_COMPLETION_USER_DIR") |
| 118 | + bashin.WriteString("echo $" + bashComplDirEnv + "\n") |
| 119 | + Eventually(bash.Out).Should(gbytes.Say(completionsUserDir)) |
| 120 | + |
| 121 | + By("listing the canary in the first search PATH directory") |
| 122 | + bashin.WriteString("ls -l ${PATH%%:*}\n") |
| 123 | + Eventually(bash.Out).Should(gbytes.Say(dummyCommandName)) |
| 124 | + |
| 125 | + By("ensuring the canary is in the PATH and gets completed") |
| 126 | + bashin.WriteString(dummyCommandName[:len(dummyCommandName)-4] + "\t") |
| 127 | + Eventually(bash.Err).Should(gbytes.Say(dummyCommandName)) |
| 128 | + }) |
| 129 | + |
| 130 | + It("completes canary's test subcommand", func() { |
| 131 | + bashin.WriteString(dummyCommandName + " t\t") |
| 132 | + Eventually(bash.Err).Should(gbytes.Say(dummyCommandName + " test")) |
| 133 | + }) |
| 134 | + |
| 135 | + It("completes canary's \"mode\" enum flag name", func() { |
| 136 | + bashin.WriteString(dummyCommandName + " test --\t\t") |
| 137 | + Eventually(bash.Err).Should(gbytes.Say( |
| 138 | + `--help\s+\(help for test\)\s+--mode\s+\(sets foo mode\)`)) |
| 139 | + }) |
| 140 | + |
| 141 | + It("lists enum flag's values", func() { |
| 142 | + bashin.WriteString(dummyCommandName + " test --mode \t\t") |
| 143 | + Eventually(bash.Err).Should(gbytes.Say( |
| 144 | + `bar\s+\(bars the output\)\s+baz\s+\(bazs the output\)\s+foo\s+\(foos the output\)`)) |
| 145 | + bashin.WriteString("\b=\t\t") |
| 146 | + Eventually(bash.Err).Should(gbytes.Say( |
| 147 | + `bar\s+\(bars the output\)\s+baz\s+\(bazs the output\)\s+foo\s+\(foos the output\)`)) |
| 148 | + }) |
| 149 | + |
| 150 | + It("completes enum flag's values", func() { |
| 151 | + bashin.WriteString(dummyCommandName + " test --mode ba\t\t") |
| 152 | + Eventually(bash.Err).Should(gbytes.Say( |
| 153 | + `bar\s+\(bars the output\)\s+baz\s+\(bazs the output\)`)) |
| 154 | + bashin.WriteString("\b\bf\t") |
| 155 | + Eventually(bash.Err).Should(gbytes.Say( |
| 156 | + `oo `)) |
| 157 | + }) |
| 158 | + |
| 159 | +}) |
0 commit comments