test(cabana,flare,dev,summer): json, reported errors and dev serve

- Docker: admin:token:create and list --json, -q keeps the token
- flare generate and test-push failures print once and are Reported
- summer dev starts serve with the forwarded globals, reports rebuilds,
  failures and restarts, and stops serve with SIGTERM returning nil
- delegates carry two examples and say the global options pass through
This commit is contained in:
Jakub Zych
2026-10-21 19:38:19 +02:00
parent 6720caebd3
commit 2baec56420
4 changed files with 390 additions and 0 deletions

View File

@@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"os"
@@ -366,3 +367,58 @@ func TestTestPushCommand(t *testing.T) {
noSecret(t, out)
})
}
// TestFlareReported: a failing generate or test-push prints its own error
// line once and returns an error matching bonfire.ErrReported, so the
// binary exits 1 without a second error block.
func TestFlareReported(t *testing.T) {
keys, err := GenerateVAPIDKeys()
if err != nil {
t.Fatal(err)
}
withKeys, _ := newCommandApp(t, map[string]string{"push.yaml": pushYAML(true, keys)})
withSource, _ := newCommandApp(t, map[string]string{"push.yaml": pushYAML(true, keys)})
publishSource(t, withSource, &fakeSource{})
cases := []struct {
name string
app *backpack.App
args []string
line string
}{
{"generate --update without a config", backpack.New(nil), []string{GenerateVAPIDKeysCommandName, "--update"}, "Failed to save the keys: no configuration loaded"},
{"test-push with a bad user id", withKeys, []string{TestPushCommandName, "abc"}, "User abc not found"},
{"test-push without a source", withKeys, []string{TestPushCommandName, "5"}, "no subscription source registered"},
{"test-push for an unknown user", withSource, []string{TestPushCommandName, "9"}, "User 9 not found"},
{"test-push without subscriptions", withSource, []string{TestPushCommandName, "5"}, "No push subscriptions found for this user"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
var out, errW bytes.Buffer
root, err := bonfire.NewRootIO("acme", Commands(tc.app), strings.NewReader(""), &out, &errW)
if err != nil {
t.Fatal(err)
}
if code := bonfire.Execute(context.Background(), root, tc.args); code != bonfire.ExitFailure {
t.Fatalf("exit %d\n%s%s", code, out.String(), errW.String())
}
if n := strings.Count(errW.String(), tc.line); n != 1 {
t.Errorf("error line %q printed %d times:\n%s", tc.line, n, errW.String())
}
if strings.Contains(errW.String(), "ERROR") || strings.Contains(out.String(), "ERROR") {
t.Errorf("a reported failure printed an error block:\n%s%s", out.String(), errW.String())
}
var direct bytes.Buffer
var cmd bonfire.Command
for _, c := range Commands(tc.app) {
if c.Name == tc.args[0] {
cmd = c
}
}
err = bonfire.Call(context.Background(), []bonfire.Command{cmd}, cmd.Name, tc.args[1:], &direct)
if !errors.Is(err, bonfire.ErrReported) {
t.Errorf("error %v does not match bonfire.ErrReported", err)
}
})
}
}