refactor(commands): separate commands and its core into two folders (#32)
- I think it would be better if one for commands only, one for the spec handling core logic so people won't be confused by the core logic inside the commands folder (cleaner I guess) - Regen docs
This commit is contained in:
+21
-21
@@ -9,7 +9,7 @@ import (
|
||||
"testing"
|
||||
|
||||
_ "github.com/versenilvis/iris/commands"
|
||||
"github.com/versenilvis/iris/commands/core"
|
||||
"github.com/versenilvis/iris/spec"
|
||||
)
|
||||
|
||||
// setupGitRepo creates a real git repo in a temp dir with:
|
||||
@@ -100,14 +100,14 @@ func TestGitSuggestions(t *testing.T) {
|
||||
defer cleanup()
|
||||
|
||||
t.Run("git top-level", func(t *testing.T) {
|
||||
res := core.Lookup("git ")
|
||||
res := spec.Lookup("git ")
|
||||
if len(res) < 10 {
|
||||
t.Errorf("expected many git subcommands, got %d", len(res))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("tag -d shows tags", func(t *testing.T) {
|
||||
res := core.Lookup("git tag -d ")
|
||||
res := spec.Lookup("git tag -d ")
|
||||
found := false
|
||||
for _, r := range res {
|
||||
if strings.Contains(r.Cmd, "v1.0") {
|
||||
@@ -120,7 +120,7 @@ func TestGitSuggestions(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("push HEAD options", func(t *testing.T) {
|
||||
res := core.Lookup("git push origin HEAD --")
|
||||
res := spec.Lookup("git push origin HEAD --")
|
||||
found := false
|
||||
for _, r := range res {
|
||||
if strings.Contains(r.Cmd, "--force") {
|
||||
@@ -133,7 +133,7 @@ func TestGitSuggestions(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("push -u origin suggests branches", func(t *testing.T) {
|
||||
res := core.Lookup("git push -u origin ")
|
||||
res := spec.Lookup("git push -u origin ")
|
||||
found := false
|
||||
for _, r := range res {
|
||||
if strings.Contains(r.Cmd, "dev") {
|
||||
@@ -152,7 +152,7 @@ func TestGitSuggestions(t *testing.T) {
|
||||
t.Skip("can't determine HEAD branch")
|
||||
}
|
||||
activeBranch := strings.TrimSpace(string(out))
|
||||
res := core.Lookup("git push origin ")
|
||||
res := spec.Lookup("git push origin ")
|
||||
found := false
|
||||
for _, r := range res {
|
||||
parts := strings.Fields(r.Cmd)
|
||||
@@ -173,7 +173,7 @@ func TestGitSuggestions(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("push origin no duplicate branches", func(t *testing.T) {
|
||||
res := core.Lookup("git push origin ")
|
||||
res := spec.Lookup("git push origin ")
|
||||
seen := make(map[string]int)
|
||||
for _, r := range res {
|
||||
parts := strings.Fields(r.Cmd)
|
||||
@@ -189,7 +189,7 @@ func TestGitSuggestions(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("branch with slash is suggested correctly", func(t *testing.T) {
|
||||
res := core.Lookup("git checkout ")
|
||||
res := spec.Lookup("git checkout ")
|
||||
found := false
|
||||
for _, r := range res {
|
||||
if strings.Contains(r.Cmd, "feature/login") {
|
||||
@@ -202,7 +202,7 @@ func TestGitSuggestions(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("remote branches suggested for push", func(t *testing.T) {
|
||||
res := core.Lookup("git push origin ")
|
||||
res := spec.Lookup("git push origin ")
|
||||
var cmdStr strings.Builder
|
||||
for _, r := range res {
|
||||
cmdStr.WriteString(r.Cmd)
|
||||
@@ -223,7 +223,7 @@ func TestGitSuggestions(t *testing.T) {
|
||||
}
|
||||
activeBranch := strings.TrimSpace(string(out))
|
||||
|
||||
res := core.Lookup("git checkout ")
|
||||
res := spec.Lookup("git checkout ")
|
||||
for _, r := range res {
|
||||
// the suggestion should not contain the active branch as a standalone word
|
||||
parts := strings.FieldsSeq(r.Cmd)
|
||||
@@ -236,7 +236,7 @@ func TestGitSuggestions(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("checkout -b no suggest", func(t *testing.T) {
|
||||
res := core.Lookup("git checkout -b ")
|
||||
res := spec.Lookup("git checkout -b ")
|
||||
for _, r := range res {
|
||||
if strings.Contains(r.Cmd, "dev") {
|
||||
t.Error("git checkout -b should not suggest existing branches")
|
||||
@@ -245,7 +245,7 @@ func TestGitSuggestions(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("switch -c no suggest", func(t *testing.T) {
|
||||
res := core.Lookup("git switch -c ")
|
||||
res := spec.Lookup("git switch -c ")
|
||||
for _, r := range res {
|
||||
if strings.Contains(r.Cmd, "dev") {
|
||||
t.Error("git switch -c should not suggest existing branches")
|
||||
@@ -255,7 +255,7 @@ func TestGitSuggestions(t *testing.T) {
|
||||
|
||||
t.Run("stash variants suggest entries", func(t *testing.T) {
|
||||
for _, cmd := range []string{"apply", "drop", "pop"} {
|
||||
res := core.Lookup("git stash " + cmd + " ")
|
||||
res := spec.Lookup("git stash " + cmd + " ")
|
||||
found := false
|
||||
for _, r := range res {
|
||||
if strings.Contains(r.Cmd, "stash@{0}") {
|
||||
@@ -270,7 +270,7 @@ func TestGitSuggestions(t *testing.T) {
|
||||
|
||||
t.Run("remote subcommands suggest remotes", func(t *testing.T) {
|
||||
for _, cmd := range []string{"remove", "rename", "set-url"} {
|
||||
res := core.Lookup("git remote " + cmd + " ")
|
||||
res := spec.Lookup("git remote " + cmd + " ")
|
||||
found := false
|
||||
for _, r := range res {
|
||||
// origin is our fake remote
|
||||
@@ -288,12 +288,12 @@ func TestGitSuggestions(t *testing.T) {
|
||||
emptyDir := t.TempDir()
|
||||
_ = os.Chdir(emptyDir)
|
||||
defer func() { _ = os.Chdir(tmp) }()
|
||||
_ = core.Lookup("git status ")
|
||||
_ = spec.Lookup("git status ")
|
||||
})
|
||||
|
||||
t.Run("reset options", func(t *testing.T) {
|
||||
_ = core.Lookup("git reset --soft origin/main ")
|
||||
res := core.Lookup("git reset HEAD ")
|
||||
_ = spec.Lookup("git reset --soft origin/main ")
|
||||
res := spec.Lookup("git reset HEAD ")
|
||||
found := false
|
||||
for _, r := range res {
|
||||
if strings.Contains(r.Cmd, "file.go") {
|
||||
@@ -306,7 +306,7 @@ func TestGitSuggestions(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("show suggests tags and commits", func(t *testing.T) {
|
||||
res := core.Lookup("git show ")
|
||||
res := spec.Lookup("git show ")
|
||||
foundTag := false
|
||||
foundCommit := false
|
||||
for _, r := range res {
|
||||
@@ -331,7 +331,7 @@ func TestGitSuggestions(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("cherry-pick suggests commits", func(t *testing.T) {
|
||||
res := core.Lookup("git cherry-pick ")
|
||||
res := spec.Lookup("git cherry-pick ")
|
||||
if len(res) == 0 {
|
||||
t.Error("git cherry-pick should suggest commits")
|
||||
}
|
||||
@@ -349,7 +349,7 @@ func TestGitSuggestions(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("revert suggests commits", func(t *testing.T) {
|
||||
res := core.Lookup("git revert ")
|
||||
res := spec.Lookup("git revert ")
|
||||
if len(res) == 0 {
|
||||
t.Error("git revert should suggest commits")
|
||||
}
|
||||
@@ -363,7 +363,7 @@ func TestGitSuggestions(t *testing.T) {
|
||||
}
|
||||
activeBranch := strings.TrimSpace(string(out))
|
||||
|
||||
res := core.Lookup("git -c core.pager=cat checkout ")
|
||||
res := spec.Lookup("git -c core.pager=cat checkout ")
|
||||
for _, r := range res {
|
||||
parts := strings.FieldsSeq(r.Cmd)
|
||||
for p := range parts {
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"testing"
|
||||
|
||||
_ "github.com/versenilvis/iris/commands"
|
||||
"github.com/versenilvis/iris/commands/core"
|
||||
"github.com/versenilvis/iris/spec"
|
||||
"github.com/versenilvis/iris/commands/js"
|
||||
)
|
||||
|
||||
@@ -33,7 +33,7 @@ func TestNpmScriptGenerator(t *testing.T) {
|
||||
defer os.Remove(filepath.Join(tmp, "package.json"))
|
||||
|
||||
// ensure CWD is tmp
|
||||
core.ShellPID = 0
|
||||
spec.ShellPID = 0
|
||||
_ = os.Chdir(tmp)
|
||||
|
||||
results := js.NpmScriptGenerator(nil, "", "")
|
||||
@@ -154,7 +154,7 @@ Host !forbidden wildcard-test
|
||||
}
|
||||
|
||||
// sshHostGeneratorFromPath is a helper that reads a specific ssh config path
|
||||
func sshHostGeneratorFromPath(configPath string) []core.Suggestion {
|
||||
func sshHostGeneratorFromPath(configPath string) []spec.Suggestion {
|
||||
import_bufio := func() {
|
||||
// using bufio in the same style as ssh.go
|
||||
}
|
||||
@@ -167,7 +167,7 @@ func sshHostGeneratorFromPath(configPath string) []core.Suggestion {
|
||||
defer func() { _ = f.Close() }()
|
||||
|
||||
seen := make(map[string]bool)
|
||||
var results []core.Suggestion
|
||||
var results []spec.Suggestion
|
||||
|
||||
scanner := strings.NewReader("")
|
||||
_ = scanner
|
||||
@@ -187,7 +187,7 @@ func sshHostGeneratorFromPath(configPath string) []core.Suggestion {
|
||||
continue
|
||||
}
|
||||
seen[host] = true
|
||||
results = append(results, core.Suggestion{Cmd: host, Desc: "ssh host"})
|
||||
results = append(results, spec.Suggestion{Cmd: host, Desc: "ssh host"})
|
||||
}
|
||||
}
|
||||
return results
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/versenilvis/iris/commands/core"
|
||||
"github.com/versenilvis/iris/spec"
|
||||
_ "github.com/versenilvis/iris/commands"
|
||||
)
|
||||
|
||||
@@ -35,7 +35,7 @@ func TestIntegration_ZoxideMultiWord(t *testing.T) {
|
||||
t.Run("z matches multi-word folder without quotes", func(t *testing.T) {
|
||||
// Simulating user typing "z My Awe"
|
||||
input := "z My Awe"
|
||||
results := core.Lookup(input)
|
||||
results := spec.Lookup(input)
|
||||
|
||||
found := false
|
||||
for _, r := range results {
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/versenilvis/iris/commands/core"
|
||||
"github.com/versenilvis/iris/spec"
|
||||
)
|
||||
|
||||
func TestFileGenerator(t *testing.T) {
|
||||
@@ -24,7 +24,7 @@ func TestFileGenerator(t *testing.T) {
|
||||
|
||||
|
||||
t.Run("dirOnly shows only dirs", func(t *testing.T) {
|
||||
gen := core.FileGenerator("/")
|
||||
gen := spec.FileGenerator("/")
|
||||
results := gen([]string{"cd", ""}, "cd ", "")
|
||||
for _, r := range results {
|
||||
if !strings.HasSuffix(r.Cmd, "/") {
|
||||
@@ -35,7 +35,7 @@ func TestFileGenerator(t *testing.T) {
|
||||
|
||||
|
||||
t.Run("Filter extension", func(t *testing.T) {
|
||||
gen := core.FileGenerator(".go")
|
||||
gen := spec.FileGenerator(".go")
|
||||
results := gen([]string{"ls", ""}, "ls ", "")
|
||||
foundMain := false
|
||||
for _, r := range results {
|
||||
@@ -53,7 +53,7 @@ func TestFileGenerator(t *testing.T) {
|
||||
|
||||
|
||||
t.Run("Nested path", func(t *testing.T) {
|
||||
gen := core.FileGenerator()
|
||||
gen := spec.FileGenerator()
|
||||
results := gen([]string{"ls", "src/u"}, "ls src/u", "src/u")
|
||||
foundUtils := false
|
||||
for _, r := range results {
|
||||
@@ -68,7 +68,7 @@ func TestFileGenerator(t *testing.T) {
|
||||
|
||||
|
||||
t.Run("Deep scan 1 level", func(t *testing.T) {
|
||||
gen := core.FileGenerator()
|
||||
gen := spec.FileGenerator()
|
||||
results := gen([]string{"ls", "src/"}, "ls src/", "src/")
|
||||
foundUtils := false
|
||||
for _, r := range results {
|
||||
@@ -85,7 +85,7 @@ func TestFileGenerator(t *testing.T) {
|
||||
|
||||
|
||||
t.Run("Hidden files are skipped", func(t *testing.T) {
|
||||
gen := core.FileGenerator()
|
||||
gen := spec.FileGenerator()
|
||||
results := gen([]string{"ls", ""}, "ls ", "")
|
||||
for _, r := range results {
|
||||
if strings.HasPrefix(r.Cmd, ".") {
|
||||
@@ -5,23 +5,23 @@ import (
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/versenilvis/iris/commands/core"
|
||||
"github.com/versenilvis/iris/spec"
|
||||
)
|
||||
|
||||
func TestLookup(t *testing.T) {
|
||||
// Setup Registry
|
||||
core.Registry = make(map[string]*core.Spec)
|
||||
core.Register(&core.Spec{
|
||||
spec.Registry = make(map[string]*spec.Spec)
|
||||
spec.Register(&spec.Spec{
|
||||
Name: "git",
|
||||
Subcommands: []core.Subcommand{
|
||||
{Name: "commit", Options: []core.Option{{Name: "--message"}}, MaxArgs: 1},
|
||||
{Name: "remote", Subcommands: []core.Subcommand{{Name: "add"}}},
|
||||
Subcommands: []spec.Subcommand{
|
||||
{Name: "commit", Options: []spec.Option{{Name: "--message"}}, MaxArgs: 1},
|
||||
{Name: "remote", Subcommands: []spec.Subcommand{{Name: "add"}}},
|
||||
},
|
||||
Options: []core.Option{{Name: "--verbose"}},
|
||||
Options: []spec.Option{{Name: "--verbose"}},
|
||||
})
|
||||
|
||||
// Setup Aliases
|
||||
core.ShellAliases = map[string]string{
|
||||
spec.ShellAliases = map[string]string{
|
||||
"gca": "git commit -a",
|
||||
"ta": "tmux a -t",
|
||||
}
|
||||
@@ -44,7 +44,7 @@ func TestLookup(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
results := core.Lookup(tt.input)
|
||||
results := spec.Lookup(tt.input)
|
||||
if len(results) < tt.minResults {
|
||||
t.Errorf("Lookup(%q) got %d results; want at least %d", tt.input, len(results), tt.minResults)
|
||||
}
|
||||
@@ -65,15 +65,15 @@ func TestLookup(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestLookupConcurrent(t *testing.T) {
|
||||
core.Registry = make(map[string]*core.Spec)
|
||||
core.Register(&core.Spec{
|
||||
spec.Registry = make(map[string]*spec.Spec)
|
||||
spec.Register(&spec.Spec{
|
||||
Name: "git",
|
||||
Subcommands: []core.Subcommand{
|
||||
{Name: "commit", Options: []core.Option{{Name: "--message"}}, MaxArgs: 1},
|
||||
Subcommands: []spec.Subcommand{
|
||||
{Name: "commit", Options: []spec.Option{{Name: "--message"}}, MaxArgs: 1},
|
||||
},
|
||||
})
|
||||
|
||||
core.ShellAliases = map[string]string{
|
||||
spec.ShellAliases = map[string]string{
|
||||
"gca": "git commit -a",
|
||||
}
|
||||
|
||||
@@ -86,8 +86,8 @@ func TestLookupConcurrent(t *testing.T) {
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for range iterations {
|
||||
_ = core.Lookup("gca")
|
||||
_ = core.Lookup("git ")
|
||||
_ = spec.Lookup("gca")
|
||||
_ = spec.Lookup("git ")
|
||||
}
|
||||
}()
|
||||
}
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/versenilvis/iris/commands/core"
|
||||
"github.com/versenilvis/iris/spec"
|
||||
)
|
||||
|
||||
func TestTokenize(t *testing.T) {
|
||||
@@ -24,7 +24,7 @@ func TestTokenize(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := core.Tokenize(tt.input)
|
||||
got := spec.Tokenize(tt.input)
|
||||
if !reflect.DeepEqual(got, tt.expected) {
|
||||
t.Errorf("Tokenize(%q) = %v; want %v", tt.input, got, tt.expected)
|
||||
}
|
||||
@@ -47,7 +47,7 @@ func TestHasPrefix(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := core.HasPrefix(tt.s, tt.prefix); got != tt.want {
|
||||
if got := spec.HasPrefix(tt.s, tt.prefix); got != tt.want {
|
||||
t.Errorf("HasPrefix(%q, %q) = %v; want %v", tt.s, tt.prefix, got, tt.want)
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user