From 027c89256d2fd332f55f57576508faede6bb503a Mon Sep 17 00:00:00 2001 From: Tomi Juntunen Date: Wed, 15 Nov 2023 12:04:37 +0200 Subject: [PATCH] embed zsh completions as stringdata --- completions/completions.go | 22 ---------------------- completions/completions_test.go | 8 +------- completions/{mage.zsh => zsh.go} | 21 ++++++++++++++++++++- mage/main_test.go | 6 ++---- 4 files changed, 23 insertions(+), 34 deletions(-) rename completions/{mage.zsh => zsh.go} (89%) diff --git a/completions/completions.go b/completions/completions.go index e98f852..a8c9478 100644 --- a/completions/completions.go +++ b/completions/completions.go @@ -3,17 +3,12 @@ package completions import ( "fmt" "io" - "io/ioutil" - "path/filepath" - "runtime" ) type Completion interface { GenerateCompletions(w io.Writer) error } -type Zsh struct{} - var completions = map[string]Completion{ "zsh": &Zsh{}, } @@ -27,20 +22,3 @@ func GetCompletions(shell string) (Completion, error) { return completions, nil } - -func (z *Zsh) GenerateCompletions(w io.Writer) error { - _, filename, _, _ := runtime.Caller(0) - dir := filepath.Dir(filename) - filePath := filepath.Join(dir, "mage.zsh") - - data, err := ioutil.ReadFile(filePath) - if err != nil { - return err - } - - _, err = fmt.Fprint(w, string(data)) - if err != nil { - return err - } - return nil -} diff --git a/completions/completions_test.go b/completions/completions_test.go index 71e0314..e49364d 100644 --- a/completions/completions_test.go +++ b/completions/completions_test.go @@ -3,7 +3,6 @@ package completions import ( "bytes" "io" - "io/ioutil" "testing" ) @@ -58,11 +57,6 @@ func TestGenerateCompletionsFails(t *testing.T) { } func TestGenerateCompletions(t *testing.T) { - zshTestData, err := ioutil.ReadFile("mage.zsh") - if err != nil { - t.Fatalf("expected to get test data, but got error: %v", err) - } - testCases := []struct { name string cmpl Completion @@ -72,7 +66,7 @@ func TestGenerateCompletions(t *testing.T) { { name: "zsh", cmpl: &Zsh{}, - expected: string(zshTestData), + expected: string(ZshCompletions), err: false, }, } diff --git a/completions/mage.zsh b/completions/zsh.go similarity index 89% rename from completions/mage.zsh rename to completions/zsh.go index 4f3a0dd..94d1afc 100644 --- a/completions/mage.zsh +++ b/completions/zsh.go @@ -1,4 +1,11 @@ -#compdef mage +package completions + +import ( + "fmt" + "io" +) + +var ZshCompletions string = `#compdef mage local curcontext="$curcontext" state line ret=1 typeset -A opt_args @@ -65,3 +72,15 @@ return ret # sh-basic-offset: 2 # End: # vim: ft=zsh sw=2 ts=2 et +` + +type Zsh struct{} + +func (z *Zsh) GenerateCompletions(w io.Writer) error { + _, err := fmt.Fprint(w, string(ZshCompletions)) + + if err != nil { + return err + } + return nil +} diff --git a/mage/main_test.go b/mage/main_test.go index 0c4d2c7..49354a9 100644 --- a/mage/main_test.go +++ b/mage/main_test.go @@ -26,6 +26,7 @@ import ( "testing" "time" + "github.com/magefile/mage/completions" "github.com/magefile/mage/internal" "github.com/magefile/mage/mg" ) @@ -902,10 +903,7 @@ func TestCompletionFlag(t *testing.T) { t.Fatalf("expected code 0, but got %v, %s", code, stderr) } actual := stdout.String() - expectedData, err := ioutil.ReadFile("../completions/mage.zsh") - if err != nil { - t.Fatal(err) - } + expectedData := completions.ZshCompletions expected := string(expectedData) if actual != expected {