72 lines
2.7 KiB
Go
72 lines
2.7 KiB
Go
package cli
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
commandkit "go.maximumdirect.net/commandkit"
|
|
)
|
|
|
|
func TestPreflightBindingsAndRunAlias(t *testing.T) {
|
|
dir := t.TempDir()
|
|
for name, contents := range map[string]string{
|
|
"config.yml": "version: 3\npipeline: {stages: []}\nreporting: {enabled: false}\n",
|
|
"transcript.json": `[{"speaker":"Alice","start":0,"end":1,"text":"hello"}]`,
|
|
"glossary.yml": "party: {entries: [{id: alice, name: Alice, category: person}]}\n",
|
|
"overlays/local.yml": "context: {description: Local example}\n",
|
|
} {
|
|
path := filepath.Join(dir, name)
|
|
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(path, []byte(contents), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
app, err := buildApplication()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cfg, transcript, output := filepath.Join(dir, "config.yml"), filepath.Join(dir, "transcript.json"), filepath.Join(dir, "out.json")
|
|
if err := os.WriteFile(output, []byte("existing"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
common := []string{"--config", cfg, "--overlay", "local", "--glossary-file", filepath.Join(dir, "glossary.yml"), "--glossaries", `["party"]`, "--output", output, transcript}
|
|
var out, stderr bytes.Buffer
|
|
invoke := func(args ...string) int {
|
|
out.Reset()
|
|
stderr.Reset()
|
|
return app.Execute(context.Background(), args, commandkit.Streams{Out: &out, Err: &stderr}).Status
|
|
}
|
|
args := append([]string{"preflight", "--smoke=false"}, common...)
|
|
if status := invoke(args...); status != 0 {
|
|
t.Fatalf("preflight %d: %s", status, stderr.String())
|
|
}
|
|
if data, err := os.ReadFile(output); err != nil || string(data) != "existing" {
|
|
t.Fatalf("destination changed: %q %v", data, err)
|
|
}
|
|
if status := invoke("preflight", "--help"); status != 0 || !strings.Contains(out.String(), "--smoke") {
|
|
t.Fatalf("help %d: %s %s", status, out.String(), stderr.String())
|
|
}
|
|
if status := invoke("preflight", "--quiet", "--config", filepath.Join(dir, "missing"), transcript); status != 2 || !strings.Contains(stderr.String(), "quiet") {
|
|
t.Fatalf("quiet %d: %s", status, stderr.String())
|
|
}
|
|
if status := invoke("preflight", "--quiet", "--smoke=false", "--config", cfg, "--output", os.DevNull, transcript); status != 0 {
|
|
t.Fatalf("null %d: %s", status, stderr.String())
|
|
}
|
|
args = append([]string{"run"}, common...)
|
|
if status := invoke(args...); status != 0 {
|
|
t.Fatalf("alias %d: %s", status, stderr.String())
|
|
}
|
|
if data, err := os.ReadFile(output); err != nil || string(data) == "existing" || !strings.Contains(string(data), "hello") {
|
|
t.Fatalf("alias output: %q %v", data, err)
|
|
}
|
|
if status := invoke("process", "--smoke=false", transcript); status != 2 {
|
|
t.Fatalf("process accepted smoke flag: %d", status)
|
|
}
|
|
}
|