Run shellcheck on bash completion

This commit is contained in:
Dr. Stefan Schimanski
2016-04-02 22:45:01 +02:00
parent 60267ae24d
commit defeccc04d
3 changed files with 51 additions and 15 deletions

View File

@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"os"
"os/exec"
"strings"
"testing"
)
@ -23,6 +24,26 @@ func check(t *testing.T, found, expected string) {
}
}
func runShellCheck(s string) error {
excluded := []string{
"SC2034", // PREFIX appears unused. Verify it or export it.
}
cmd := exec.Command("shellcheck", "-s", "bash", "-", "-e", strings.Join(excluded, ","))
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
stdin, err := cmd.StdinPipe()
if err != nil {
return err
}
go func() {
defer stdin.Close()
stdin.Write([]byte(s))
}()
return cmd.Run()
}
// World worst custom function, just keep telling you to enter hello!
const (
bashCompletionFunc = `__custom_func() {
@ -107,4 +128,13 @@ func TestBashCompletions(t *testing.T) {
check(t, str, `flags_completion+=("__handle_subdirs_in_dir_flag themes")`)
checkOmit(t, str, cmdDeprecated.Name())
// if available, run shellcheck against the script
if err := exec.Command("which", "shellcheck").Run(); err != nil {
return
}
err := runShellCheck(str)
if err != nil {
t.Fatalf("shellcheck failed: %v", err)
}
}