65 lines
1.8 KiB
Go
65 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestStartupBannerCommandBoundary(testContext *testing.T) {
|
|
for _, command := range []string{"", "version", "--version", "data-path", "inspect-database", "restore-database", "export-ca", "reset-password", "migrate-key", "migrate-data", "invalid-command"} {
|
|
name := command
|
|
if name == "" {
|
|
name = "serve"
|
|
}
|
|
testContext.Run(name, func(testContext *testing.T) {
|
|
blocker := filepath.Join(testContext.TempDir(), "not-a-directory")
|
|
if err := os.WriteFile(blocker, []byte("fixture"), 0600); err != nil {
|
|
testContext.Fatal(err)
|
|
}
|
|
testContext.Setenv("LINKPORT_DATA", filepath.Join(blocker, "test.sqlite"))
|
|
previousArgs, previousStdout, previousLog := os.Args, os.Stdout, log.Writer()
|
|
reader, writer, err := os.Pipe()
|
|
if err != nil {
|
|
testContext.Fatal(err)
|
|
}
|
|
defer reader.Close()
|
|
defer writer.Close()
|
|
testContext.Cleanup(func() {
|
|
os.Args, os.Stdout = previousArgs, previousStdout
|
|
log.SetOutput(previousLog)
|
|
})
|
|
var messages bytes.Buffer
|
|
log.SetOutput(&messages)
|
|
os.Stdout = writer
|
|
os.Args = []string{"linkport"}
|
|
if command != "" {
|
|
os.Args = append(os.Args, command)
|
|
}
|
|
runErr := run()
|
|
if command == "" && runErr == nil {
|
|
testContext.Fatal("expected isolated data path to stop initialization")
|
|
}
|
|
writer.Close()
|
|
output, err := io.ReadAll(reader)
|
|
if err != nil {
|
|
testContext.Fatal(err)
|
|
}
|
|
expected := 0
|
|
if command == "" {
|
|
expected = 1
|
|
}
|
|
if count := strings.Count(messages.String(), startupBanner); count != expected {
|
|
testContext.Fatalf("banner count = %d, want %d", count, expected)
|
|
}
|
|
if bytes.Contains(output, []byte(startupBanner)) {
|
|
testContext.Fatal("banner must not be written to command stdout")
|
|
}
|
|
})
|
|
}
|
|
}
|