Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions cmd/qbo/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import (
"bytes"
"errors"
"os"
"os/exec"
"strings"
"testing"

"github.com/voska/qbo-cli/internal/errfmt"
)

func TestSchemaUnknownCommandExit(t *testing.T) {
if os.Getenv("QBO_TEST_SCHEMA_PROCESS") == "1" {
os.Args = append([]string{"qbo"}, os.Args[3:]...)
main()
return
}

for _, flag := range []string{"", "--json", "--plain"} {
t.Run("mode="+flag, func(t *testing.T) {
args := []string{"-test.run=^TestSchemaUnknownCommandExit$", "--", "schema", "nosuch"}
if flag != "" {
args = append(args, flag)
}
process := exec.Command(os.Args[0], args...)
process.Env = append(os.Environ(), "QBO_TEST_SCHEMA_PROCESS=1", "QBO_CONFIG_DIR="+t.TempDir(), "QBO_AUTO_JSON=0")
var stdout, stderr bytes.Buffer
process.Stdout, process.Stderr = &stdout, &stderr
err := process.Run()
var exitErr *exec.ExitError
if !errors.As(err, &exitErr) || exitErr.ExitCode() != errfmt.ExitUsage {
t.Fatalf("exit = %v, want %d; stdout = %q, stderr = %q", err, errfmt.ExitUsage, stdout.String(), stderr.String())
}
if stdout.Len() != 0 {
t.Errorf("stdout = %q, want no success output", stdout.String())
}
if !strings.Contains(stderr.String(), "command not found: nosuch") {
t.Errorf("stderr = %q, want unknown-command error", stderr.String())
}
})
}
}
15 changes: 10 additions & 5 deletions internal/cmd/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"sort"

"github.com/voska/qbo-cli/internal/api"
"github.com/voska/qbo-cli/internal/errfmt"
)

type SchemaCmd struct {
Expand All @@ -12,7 +13,11 @@ type SchemaCmd struct {

func (c *SchemaCmd) Run(g *Globals) error {
if c.Command != "" {
return WriteOutput(g.Ctx, commandSchema(c.Command, g.Version))
schema, err := commandSchema(c.Command, g.Version)
if err != nil {
return err
}
return WriteOutput(g.Ctx, schema)
}
return WriteOutput(g.Ctx, fullSchema(g.Version))
}
Expand Down Expand Up @@ -164,16 +169,16 @@ func fullSchema(version string) map[string]any {
}
}

func commandSchema(name, version string) map[string]any {
func commandSchema(name, version string) (map[string]any, error) {
schema := fullSchema(version)
commands, ok := schema["commands"].([]map[string]any)
if !ok {
return map[string]any{"error": "command not found: " + name}
return nil, errfmt.Usage("command not found: " + name)
}
for _, cmd := range commands {
if cmd["name"] == name {
return cmd
return cmd, nil
}
}
return map[string]any{"error": "command not found: " + name}
return nil, errfmt.Usage("command not found: " + name)
}
5 changes: 4 additions & 1 deletion internal/cmd/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ func TestPDFSchemaMatchesCommand(t *testing.T) {
if command == nil {
t.Fatal("pdf is missing from the command model")
}
schema := commandSchema("pdf", "test")
schema, err := commandSchema("pdf", "test")
if err != nil {
t.Fatal(err)
}
if schema["name"] != command.Name || schema["help"] != command.Help {
t.Fatalf("pdf schema does not match the command: %v", schema)
}
Expand Down
5 changes: 4 additions & 1 deletion internal/cmd/void_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,10 @@ func TestVoidCommandOutput(t *testing.T) {
}

func TestVoidSchema(t *testing.T) {
schema := commandSchema("void", "test")
schema, err := commandSchema("void", "test")
if err != nil {
t.Fatal(err)
}
if schema["name"] != "void" {
t.Fatalf("void is missing from schema: %v", schema)
}
Expand Down
Loading