diff --git a/internal/cmdregistry/branches_test.go b/internal/cmdregistry/branches_test.go new file mode 100644 index 000000000..8fe3952b2 --- /dev/null +++ b/internal/cmdregistry/branches_test.go @@ -0,0 +1,183 @@ +package cmdregistry + +import ( + "io" + "os" + "reflect" + "strings" + "testing" + + "github.com/nself-org/cli/internal/canon" + "github.com/spf13/cobra" +) + +func TestEffectiveJSON(t *testing.T) { + c := &Command{JSON: canon.JSONEnvelope, Flags: []Flag{ + {Name: "plain"}, + {Name: "raw", JSON: strPtr(canon.JSONLegacy)}, + {Name: "quiet", JSON: strPtr(canon.JSONNone)}, + }} + for _, tc := range []struct { + name string + set map[string]bool + want string + }{ + {"no flag set keeps the command value", nil, canon.JSONEnvelope}, + {"flag without override keeps the command value", map[string]bool{"plain": true}, canon.JSONEnvelope}, + {"unset override flag is ignored", map[string]bool{"plain": true, "raw": false}, canon.JSONEnvelope}, + {"legacy override replaces envelope", map[string]bool{"raw": true}, canon.JSONLegacy}, + {"none override replaces envelope", map[string]bool{"quiet": true}, canon.JSONNone}, + {"none wins over legacy", map[string]bool{"raw": true, "quiet": true}, canon.JSONNone}, + } { + t.Run(tc.name, func(t *testing.T) { + if got := EffectiveJSON(c, tc.set); got != tc.want { + t.Fatalf("EffectiveJSON = %q, want %q", got, tc.want) + } + }) + } + if got := EffectiveJSON(nil, map[string]bool{"raw": true}); got != "" { + t.Fatalf("nil command has effective json %q", got) + } +} + +func TestBracketedUnclosed(t *testing.T) { + for _, tc := range []struct { + in, wantTok, wantRest string + }{ + {"') + if open == '[' { + closeCh = ']' + } + tok, rest := bracketed(tc.in, open, closeCh) + if tok != tc.wantTok || rest != tc.wantRest { + t.Errorf("bracketed(%q) = (%q, %q), want (%q, %q)", tc.in, tok, rest, tc.wantTok, tc.wantRest) + } + } + want := []Arg{{Name: "file", Required: true}} + if got := parseArgs("get