package cli //nolint:testpackage // needs access to unexported cleanStartupError import ( "errors" "path/filepath" "testing" "sneak.berlin/go/vaultik/internal/pidlock" ) func TestCleanStartupError(t *testing.T) { t.Parallel() tests := []struct { name string in string want string }{ { name: "real fx error chain", in: `could not build arguments for function ` + `"sneak.berlin/go/vaultik/internal/cli".newSnapshotCreateCommand.func1.1 ` + `(/Users/user/dev/vaultik/internal/cli/snapshot.go:71): ` + `failed to build *vaultik.Vaultik: ` + `could not build arguments for function ` + `"sneak.berlin/go/vaultik/internal/vaultik".New ` + `(/Users/user/dev/vaultik/internal/vaultik/vaultik.go:59): ` + `failed to build storage.Storer: ` + `received non-nil error from function ` + `"sneak.berlin/go/vaultik/internal/storage".NewStorer ` + `(/Users/user/dev/vaultik/internal/storage/module.go:23): ` + `creating base path: mkdir /Volumes/BACKUPS: permission denied`, want: `creating base path: mkdir /Volumes/BACKUPS: permission denied`, }, { name: "no fx wrapping", in: "plain error", want: "plain error", }, { name: "single fx wrapping", in: `received non-nil error from function "foo" (file.go:1): underlying problem`, want: "underlying problem", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { t.Parallel() //nolint:err113 // test constructs errors from table input got := cleanStartupError(errors.New(tt.in)).Error() if got != tt.want { t.Errorf("got %q, want %q", got, tt.want) } }) } } // TestLockScopedToMutatingCommands proves the partition the PID lock now // enforces: a read-only command runs while a mutator holds the lock, and // two mutating commands still mutually exclude. func TestLockScopedToMutatingCommands(t *testing.T) { t.Parallel() lockDir := filepath.Join(t.TempDir(), "vaultik") // A mutating command takes the process-wide lock. releaseMutator, err := acquireLockIfMutating(mutating, lockDir) if err != nil { t.Fatalf("mutating command could not acquire lock: %v", err) } // A read-only command runs to completion even while the lock is held. releaseReader, err := acquireLockIfMutating(readOnly, lockDir) if err != nil { t.Fatalf("read-only command was blocked by held lock: %v", err) } releaseReader() // A second mutating command is refused while the first holds the lock. _, err = acquireLockIfMutating(mutating, lockDir) if !errors.Is(err, pidlock.ErrAlreadyRunning) { t.Fatalf("second mutating command was not excluded, got: %v", err) } // Once the first mutator releases, another mutating command may run. releaseMutator() release, err := acquireLockIfMutating(mutating, lockDir) if err != nil { t.Fatalf("mutating command could not acquire released lock: %v", err) } release() }