78 lines
2.7 KiB
Go
78 lines
2.7 KiB
Go
package cli
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
commandkit "go.maximumdirect.net/commandkit"
|
|
)
|
|
|
|
func TestNativeConfigurationAndResourceCommands(t *testing.T) {
|
|
dir := t.TempDir()
|
|
file := filepath.Join(dir, "config.yml")
|
|
if err := os.WriteFile(file, []byte("version: 3\ncontext:\n description: example\n"), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
app, err := buildApplication()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, tc := range []struct {
|
|
args []string
|
|
contains string
|
|
}{
|
|
{[]string{"--help"}, "audita"},
|
|
{[]string{"version"}, "audita"},
|
|
{[]string{"process", "--help"}, "transcript"},
|
|
{[]string{"config", "show", "--scope", "app", "--config", file}, "example"},
|
|
{[]string{"promptkit", "list", "prompts", "--config", file}, "audita/modules.grammar.proposal"},
|
|
} {
|
|
var out, stderr bytes.Buffer
|
|
result := app.Execute(context.Background(), tc.args, commandkit.Streams{Out: &out, Err: &stderr})
|
|
if result.Status != 0 || !strings.Contains(out.String(), tc.contains) {
|
|
t.Errorf("%v: status=%d out=%q err=%q", tc.args, result.Status, out.String(), stderr.String())
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestNativeConfigurationSelectionsAndComparison(t *testing.T) {
|
|
dir := t.TempDir()
|
|
first, second := filepath.Join(dir, "first.yml"), filepath.Join(dir, "second.yml")
|
|
for name, data := range map[string]string{first: "context:\n description: before\n", second: "context:\n description: after\n"} {
|
|
if err := os.WriteFile(name, []byte(data), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
app, err := buildApplication()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var out, stderr bytes.Buffer
|
|
outcome := app.Execute(context.Background(), []string{"config", "diff", "--details", "--check", "--scope=app", "--config", first, "--against-config", second, "--set=/context/description=\"left\"", "--against-set=/context/description=\"right\""}, commandkit.Streams{Out: &out, Err: &stderr})
|
|
if outcome.Status != 3 || !strings.Contains(out.String(), "left") || !strings.Contains(out.String(), "right") {
|
|
t.Fatalf("status=%d output=%q err=%q", outcome.Status, out.String(), stderr.String())
|
|
}
|
|
out.Reset()
|
|
stderr.Reset()
|
|
outcome = app.Execute(context.Background(), []string{"config", "show", "--scope=app", "--config", filepath.Join(dir, "missing.yml")}, commandkit.Streams{Out: &out, Err: &stderr})
|
|
if outcome.Status == 0 {
|
|
t.Fatalf("missing file accepted: %s", out.String())
|
|
}
|
|
}
|
|
|
|
func TestExplicitEmptyConfigurationPathFails(t *testing.T) {
|
|
app, err := buildApplication()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var out, stderr bytes.Buffer
|
|
result := app.Execute(context.Background(), []string{"config", "show", "--scope=app", "--config="}, commandkit.Streams{Out: &out, Err: &stderr})
|
|
if result.Status == 0 {
|
|
t.Fatal("explicit empty path accepted")
|
|
}
|
|
}
|