package bonfire import ( "bytes" "errors" "os" "path/filepath" "strings" "testing" ) // TestWrongDirectoryHintSmoke: an application binary failing below its // application root (no config/ here, summer.yaml in a parent) gets the // "cd ; bin/ " hint; summer, a directory with config/ and // a usage error do not. func TestWrongDirectoryHintSmoke(t *testing.T) { root := t.TempDir() if err := os.WriteFile(filepath.Join(root, "summer.yaml"), []byte("binary: acme\n"), 0o644); err != nil { t.Fatal(err) } sub := filepath.Join(root, "plugins", "acme") if err := os.MkdirAll(sub, 0o755); err != nil { t.Fatal(err) } if err := os.MkdirAll(filepath.Join(root, "config"), 0o755); err != nil { t.Fatal(err) } fail := errors.New("lagoon: database.dsn is empty") got := wrongDirectory("acme", fail, []string{"migrate", "--plugin", "acme blog"}, sub) var hint *HintError if !errors.As(got, &hint) || hint.Explanation != wrongDirectoryExplanation || hint.Try != "cd ../..; {bin} migrate --plugin 'acme blog'" || !errors.Is(got, fail) { t.Fatalf("hint = %#v", got) } var buf bytes.Buffer RenderError(&buf, "acme", got, Globals{Color: ColorNever}) if !strings.Contains(buf.String(), "cd ../..; bin/acme migrate --plugin 'acme blog'") { t.Fatalf("rendered block lacks the Try line:\n%s", buf.String()) } for name, tc := range map[string]struct { name string err error dir string }{ "summer": {"summer", fail, sub}, "app root": {"acme", fail, root}, "usage error": {"acme", Usage(fail), sub}, "reported": {"acme", Reported(fail), sub}, "no app above": {"acme", fail, t.TempDir()}, } { if got := wrongDirectory(tc.name, tc.err, []string{"migrate"}, tc.dir); errors.As(got, &hint) && hint.Explanation == wrongDirectoryExplanation { t.Errorf("%s: unexpected hint %#v", name, got) } } }