| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- package main
- import (
- "os"
- "path/filepath"
- "testing"
- )
- func clearEnv(t *testing.T) {
- t.Helper()
- t.Chdir(t.TempDir())
- for _, key := range []string{
- "WIKIJS_URL", "WIKIJS_API_KEY", "WIKIJS_ENABLE_EDIT", "WIKIJS_TIMEOUT",
- "MCP_TRANSPORT", "MCP_ADDR", "MCP_PATH", "MCP_STATELESS",
- "MCP_AUTH_TOKEN", "LOG_LEVEL", "ENV_FILE",
- } {
- t.Setenv(key, "")
- }
- }
- func TestVersionFlagNeedsNoCredentials(t *testing.T) {
- clearEnv(t)
- if err := run([]string{"--version"}); err != nil {
- t.Fatalf("run(--version) error = %v", err)
- }
- }
- func TestHelpNeedsNoCredentials(t *testing.T) {
- clearEnv(t)
- // Silence the usage text; flag.Parse writes it to the flag set output.
- stdout, stderr := os.Stdout, os.Stderr
- devNull, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0)
- if err != nil {
- t.Fatalf("open %s: %v", os.DevNull, err)
- }
- defer devNull.Close()
- os.Stdout, os.Stderr = devNull, devNull
- defer func() {
- os.Stdout, os.Stderr = stdout, stderr
- }()
- if err := run([]string{"--help"}); err != nil {
- t.Fatalf("run(--help) error = %v", err)
- }
- }
- func TestRunRequiresCredentials(t *testing.T) {
- clearEnv(t)
- if err := run(nil); err == nil {
- t.Fatal("run() error = nil, want missing credentials error")
- }
- }
- func TestFlagsOverrideTransport(t *testing.T) {
- clearEnv(t)
- t.Setenv("WIKIJS_URL", "https://wiki.example.com")
- t.Setenv("WIKIJS_API_KEY", "secret")
- t.Setenv("MCP_TRANSPORT", "http")
- if err := run([]string{"--transport", "sse"}); err == nil {
- t.Fatal("run() error = nil, want invalid transport error")
- }
- }
- func TestEnvFileFromArgs(t *testing.T) {
- tests := []struct {
- name string
- args []string
- wantPath string
- wantFound bool
- wantErr bool
- }{
- {name: "none"},
- {name: "separate", args: []string{"--env-file", "custom.env"}, wantPath: "custom.env", wantFound: true},
- {name: "equals", args: []string{"--env-file=custom.env"}, wantPath: "custom.env", wantFound: true},
- {name: "single dash", args: []string{"-env-file", "custom.env"}, wantPath: "custom.env", wantFound: true},
- {name: "single dash equals", args: []string{"-env-file=custom.env"}, wantPath: "custom.env", wantFound: true},
- {name: "last wins", args: []string{"--env-file=a.env", "--env-file", "b.env"}, wantPath: "b.env", wantFound: true},
- {name: "missing value", args: []string{"--env-file"}, wantFound: true, wantErr: true},
- {name: "empty value", args: []string{"--env-file="}, wantFound: true, wantErr: true},
- {name: "after terminator", args: []string{"--", "--env-file", "custom.env"}},
- {name: "other flags", args: []string{"--transport", "http"}},
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- path, found, err := envFileFromArgs(tt.args)
- if gotErr := err != nil; gotErr != tt.wantErr {
- t.Fatalf("envFileFromArgs() error = %v, wantErr %v", err, tt.wantErr)
- }
- if path != tt.wantPath {
- t.Errorf("path = %q, want %q", path, tt.wantPath)
- }
- if found != tt.wantFound {
- t.Errorf("found = %v, want %v", found, tt.wantFound)
- }
- })
- }
- }
- func TestEnvFileFlagOverridesEnvFile(t *testing.T) {
- clearEnv(t)
- t.Setenv("ENV_FILE", filepath.Join(t.TempDir(), "does-not-exist.env"))
- good := filepath.Join(t.TempDir(), "good.env")
- if err := os.WriteFile(good, []byte("# empty\n"), 0o600); err != nil {
- t.Fatalf("write env file: %v", err)
- }
- if err := run([]string{"--env-file", good, "--version"}); err != nil {
- t.Fatalf("run(--env-file ... --version) error = %v", err)
- }
- }
|