/
nickolasfox
/
kek
Обзор
Документация
Войти
/
nickolasfox
/
kek
Код
Запросы
0
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
pkg/plugins/complete_test.go
228 строк
5 KB
Nickolas Fox
Alpha version
29 мар 2024, 20:17
29 мар 2024, 20:17
f14be9d
Код
Авторство
О чём код?
package plugins import ( "bytes" "context" "errors" "fmt" "os/exec" "runtime" "slices" "sync" "testing" "github.com/spf13/cobra" ) const ( testScriptFile = "testdata/test.sh" ) var ( equal = slices.Equal[[]string] testPlugins = []*Plugin{ // configuration detected by path (e.g. kek-test) with a "mocked" completion. { Name: "test", Command: []string{testScriptFile}, Version: "runtime", Description: "runtime", Complete: func(cmd *cobra.Command, args []string) (err error) { _, _ = fmt.Fprintf(cmd.OutOrStdout(), "--help\n") return nil }, isValid: true, Completion: PluginCompletion{ Enabled: true, }, }, { Name: "nocomp", Command: []string{testScriptFile}, Version: "runtime", Description: "runtime", isValid: true, // disable completion }, { Name: "demo", Command: []string{"/bin/bash", "-xe", "echo", "test"}, Version: "runtime", Description: "runtime", isValid: true, isManifested: true, }, } ) // helpers var once = sync.Once{} func goos() string { return runtime.GOOS } func WithStubCmd(t testing.TB) *cobra.Command { cmd := &cobra.Command{ Use: "stub", } cmd.SetContext(context.TODO()) return cmd } func WithEcho(t testing.TB) string { app := "echo" if goos() == "windows" { app = "write.exe" } location, err := exec.LookPath(app) if err != nil { t.Errorf("could not find echo: %v", err) t.FailNow() } return location } func WithLoadManifest(t testing.TB, compl *Complete) { for _, plugin := range compl.plugins { plugin.loadManifest() } } func NewTestComplete() *Complete { // initiate plugins once once.Do(func() { for _, plugin := range testPlugins { plugin.loadManifest() } }) return &Complete{ plugins: testPlugins, } } // tests func TestNewComplete(t *testing.T) { if complete := NewComplete(); complete == nil { t.Errorf("NewComplete() expected not to be nil") } } func TestComplete_Complete(t *testing.T) { t.Run("self-complete", func(sub *testing.T) { buf := &bytes.Buffer{} cmd := WithStubCmd(sub) cmd.SetOut(buf) if err := NewTestComplete().Complete(cmd, []string{"test"}); err != nil { sub.Errorf("got error: %v", err) } expected := "test\truntime\n" if result := buf.String(); result != expected { sub.Errorf("\nexp:\n`%v`\ngot:\n`%v`\n", expected, result) } }) t.Run("plugin-complete", func(sub *testing.T) { buf := &bytes.Buffer{} cmd := WithStubCmd(sub) cmd.SetOut(buf) if err := NewTestComplete().Complete(cmd, []string{"test", "--hel"}); err != nil { sub.Errorf("got error: %v", err) sub.FailNow() } expected := "--help\n" if result := buf.String(); result != expected { sub.Errorf("\nexp:\n`%v`\ngot:\n`%v`\n", expected, result) } }) } func TestComplete_pluginComplete(t *testing.T) { t.Run("ok", func(sub *testing.T) { buf := &bytes.Buffer{} cmd := WithStubCmd(sub) cmd.SetOut(buf) if err := NewTestComplete().pluginComplete(cmd, []string{"test", "--hel"}); err != nil { sub.Errorf("got error: %v", err) sub.FailNow() } expected := "--help\n" if result := buf.String(); result != expected { sub.Errorf("\nexp:\n`%v`\ngot:\n`%v`\n", expected, result) } }) t.Run("no-complete", func(sub *testing.T) { buf := &bytes.Buffer{} cmd := WithStubCmd(sub) cmd.SetOut(buf) if err := NewTestComplete().pluginComplete(cmd, []string{"nocompl", "--hel"}); err != nil { sub.Errorf("got error: %v", err) sub.FailNow() } expected := "" if result := buf.String(); result != expected { sub.Errorf("\nexp:\n`%v`\ngot:\n`%v`\n", expected, result) } }) } func TestComplete_lookup(t *testing.T) { t.Run("ok", func(sub *testing.T) { result, found := NewTestComplete().lookup("te") if !found { sub.Errorf("lookup() expected to found something") sub.FailNow() } expected := []string{"test\truntime"} if !equal(result, expected) { sub.Errorf("\nexp:\n`%v`\ngot:\n`%v`\n", expected, result) } }) } func TestComplete_Execute(t *testing.T) { t.Run("ok", func(sub *testing.T) { NewTestComplete().Execute(WithStubCmd(sub), []string{"test", "--hel"}) }) } func TestComplete_ExecuteE(t *testing.T) { t.Run("ok", func(sub *testing.T) { compl := &Complete{ plugins: []*Plugin{ { Name: "echo", Command: []string{WithEcho(sub)}, Version: "runtime", Description: "echo example", isValid: true, }, }, } WithLoadManifest(sub, compl) // first argument -> plugin to execute args := []string{"echo", "this", "is", "a", "test"} if err := compl.ExecuteE(WithStubCmd(sub), args); err != nil { sub.Errorf("got error: %v", err) } }) t.Run("no-plugin-found", func(sub *testing.T) { compl := &Complete{} WithLoadManifest(sub, compl) // first argument -> plugin to execute args := []string{"echo", "this", "is", "a", "test"} if err := compl.ExecuteE(WithStubCmd(sub), args); !errors.Is(err, ErrNoExecutable) { sub.Errorf("expected: %v, got: %v", ErrCommandIsExpected, err) } }) }