Skip to content

Commit

Permalink
embed zsh completions as stringdata
Browse files Browse the repository at this point in the history
  • Loading branch information
kraashen committed Nov 15, 2023
1 parent b38a4f5 commit 647edcc
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 100 deletions.
22 changes: 0 additions & 22 deletions completions/completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
}
Expand All @@ -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
}
8 changes: 1 addition & 7 deletions completions/completions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package completions
import (
"bytes"
"io"
"io/ioutil"
"testing"
)

Expand Down Expand Up @@ -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
Expand All @@ -72,7 +66,7 @@ func TestGenerateCompletions(t *testing.T) {
{
name: "zsh",
cmpl: &Zsh{},
expected: string(zshTestData),
expected: string(ZshCompletions),
err: false,
},
}
Expand Down
67 changes: 0 additions & 67 deletions completions/mage.zsh

This file was deleted.

6 changes: 2 additions & 4 deletions mage/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"testing"
"time"

"github.com/magefile/mage/completions"
"github.com/magefile/mage/internal"
"github.com/magefile/mage/mg"
)
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 647edcc

Please sign in to comment.