refactor: clean up nidusctl CLI
This commit is contained in:
+18
-38
@@ -12,25 +12,6 @@ import (
|
||||
"git.arnef.de/arnef/nidus/internal/db"
|
||||
)
|
||||
|
||||
// writeTestConfig creates a minimal config.yaml in dir and returns its path.
|
||||
func writeTestConfig(t *testing.T, dir string) string {
|
||||
t.Helper()
|
||||
cfgPath := filepath.Join(dir, "config.yaml")
|
||||
dataDir := filepath.Join(dir, "data")
|
||||
content := "storage:\n data_dir: " + dataDir + "\n" +
|
||||
"users:\n" +
|
||||
" alice:\n" +
|
||||
" password: \"$2a$10$9.WEs0uz5TaNJLQbSTLrX.Te.BIe8XTTykVRzZbSHQMEkyFb8Sq/O\"\n" +
|
||||
" bob:\n" +
|
||||
" password: \"$2a$10$9.WEs0uz5TaNJLQbSTLrX.Te.BIe8XTTykVRzZbSHQMEkyFb8Sq/O\"\n"
|
||||
if err := os.WriteFile(cfgPath, []byte(content), 0o644); err != nil {
|
||||
t.Fatalf("writing test config: %v", err)
|
||||
}
|
||||
return cfgPath
|
||||
}
|
||||
|
||||
// runCLI runs the CLI's run() function, capturing stdout/stderr, and
|
||||
// returns (exit code, combined stdout+stderr).
|
||||
func runCLI(t *testing.T, args ...string) (int, string) {
|
||||
t.Helper()
|
||||
|
||||
@@ -56,9 +37,9 @@ func runCLI(t *testing.T, args ...string) (int, string) {
|
||||
|
||||
func TestCalendarShareUnshareLifecycle(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
cfgPath := writeTestConfig(t, dir)
|
||||
t.Setenv("NIDUS_DATA_DIR", filepath.Join(dir, "data"))
|
||||
|
||||
code, out := runCLI(t, "-config", cfgPath, "calendar", "share", "alice", "work", "bob", "write")
|
||||
code, out := runCLI(t, "calendar", "share", "alice", "work", "bob", "write")
|
||||
if code != 0 {
|
||||
t.Fatalf("share exit code = %d, output: %s", code, out)
|
||||
}
|
||||
@@ -66,7 +47,7 @@ func TestCalendarShareUnshareLifecycle(t *testing.T) {
|
||||
t.Errorf("output = %q, want to contain 'shared'", out)
|
||||
}
|
||||
|
||||
code, out = runCLI(t, "-config", cfgPath, "calendar", "shares", "alice", "work")
|
||||
code, out = runCLI(t, "calendar", "shares", "alice", "work")
|
||||
if code != 0 {
|
||||
t.Fatalf("shares exit code = %d, output: %s", code, out)
|
||||
}
|
||||
@@ -74,12 +55,12 @@ func TestCalendarShareUnshareLifecycle(t *testing.T) {
|
||||
t.Errorf("output = %q, want to contain bob/write", out)
|
||||
}
|
||||
|
||||
code, out = runCLI(t, "-config", cfgPath, "calendar", "unshare", "alice", "work", "bob")
|
||||
code, out = runCLI(t, "calendar", "unshare", "alice", "work", "bob")
|
||||
if code != 0 {
|
||||
t.Fatalf("unshare exit code = %d, output: %s", code, out)
|
||||
}
|
||||
|
||||
code, out = runCLI(t, "-config", cfgPath, "calendar", "shares", "alice", "work")
|
||||
code, out = runCLI(t, "calendar", "shares", "alice", "work")
|
||||
if code != 0 {
|
||||
t.Fatalf("shares (after unshare) exit code = %d, output: %s", code, out)
|
||||
}
|
||||
@@ -90,9 +71,9 @@ func TestCalendarShareUnshareLifecycle(t *testing.T) {
|
||||
|
||||
func TestCalendarShareInvalidPermission(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
cfgPath := writeTestConfig(t, dir)
|
||||
t.Setenv("NIDUS_DATA_DIR", filepath.Join(dir, "data"))
|
||||
|
||||
code, out := runCLI(t, "-config", cfgPath, "calendar", "share", "alice", "work", "bob", "admin")
|
||||
code, out := runCLI(t, "calendar", "share", "alice", "work", "bob", "admin")
|
||||
if code != 2 {
|
||||
t.Errorf("exit code = %d, want 2; output: %s", code, out)
|
||||
}
|
||||
@@ -103,9 +84,9 @@ func TestCalendarShareInvalidPermission(t *testing.T) {
|
||||
|
||||
func TestCalendarUnshareNotFound(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
cfgPath := writeTestConfig(t, dir)
|
||||
t.Setenv("NIDUS_DATA_DIR", filepath.Join(dir, "data"))
|
||||
|
||||
code, out := runCLI(t, "-config", cfgPath, "calendar", "unshare", "alice", "ghost", "bob")
|
||||
code, out := runCLI(t, "calendar", "unshare", "alice", "ghost", "bob")
|
||||
if code != 1 {
|
||||
t.Errorf("exit code = %d, want 1; output: %s", code, out)
|
||||
}
|
||||
@@ -116,14 +97,14 @@ func TestCalendarUnshareNotFound(t *testing.T) {
|
||||
|
||||
func TestAddressBookShareUnshareLifecycle(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
cfgPath := writeTestConfig(t, dir)
|
||||
t.Setenv("NIDUS_DATA_DIR", filepath.Join(dir, "data"))
|
||||
|
||||
code, out := runCLI(t, "-config", cfgPath, "addressbook", "share", "alice", "contacts", "bob", "read")
|
||||
code, out := runCLI(t, "addressbook", "share", "alice", "contacts", "bob", "read")
|
||||
if code != 0 {
|
||||
t.Fatalf("share exit code = %d, output: %s", code, out)
|
||||
}
|
||||
|
||||
code, out = runCLI(t, "-config", cfgPath, "addressbook", "shares", "alice", "contacts")
|
||||
code, out = runCLI(t, "addressbook", "shares", "alice", "contacts")
|
||||
if code != 0 {
|
||||
t.Fatalf("shares exit code = %d, output: %s", code, out)
|
||||
}
|
||||
@@ -131,7 +112,7 @@ func TestAddressBookShareUnshareLifecycle(t *testing.T) {
|
||||
t.Errorf("output = %q, want to contain bob/read", out)
|
||||
}
|
||||
|
||||
code, out = runCLI(t, "-config", cfgPath, "addressbook", "unshare", "alice", "contacts", "bob")
|
||||
code, out = runCLI(t, "addressbook", "unshare", "alice", "contacts", "bob")
|
||||
if code != 0 {
|
||||
t.Fatalf("unshare exit code = %d, output: %s", code, out)
|
||||
}
|
||||
@@ -139,9 +120,9 @@ func TestAddressBookShareUnshareLifecycle(t *testing.T) {
|
||||
|
||||
func TestUnknownUserWarningDoesNotBlockShare(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
cfgPath := writeTestConfig(t, dir)
|
||||
t.Setenv("NIDUS_DATA_DIR", filepath.Join(dir, "data"))
|
||||
|
||||
code, out := runCLI(t, "-config", cfgPath, "calendar", "share", "alice", "work", "carol", "read")
|
||||
code, out := runCLI(t, "calendar", "share", "alice", "work", "carol", "read")
|
||||
if code != 0 {
|
||||
t.Fatalf("exit code = %d, output: %s", code, out)
|
||||
}
|
||||
@@ -154,10 +135,9 @@ func TestUnknownUserWarningDoesNotBlockShare(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestNoArgsShowsUsage(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
// No config needed since usage() is printed before config.Load for
|
||||
// missing subcommands.
|
||||
code, out := runCLI(t, "-config", filepath.Join(dir, "missing.yaml"))
|
||||
code, out := runCLI(t)
|
||||
if code != 2 {
|
||||
t.Errorf("exit code = %d, want 2", code)
|
||||
}
|
||||
@@ -168,9 +148,9 @@ func TestNoArgsShowsUsage(t *testing.T) {
|
||||
|
||||
func TestUnknownCommand(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
cfgPath := writeTestConfig(t, dir)
|
||||
t.Setenv("NIDUS_DATA_DIR", filepath.Join(dir, "data"))
|
||||
|
||||
code, out := runCLI(t, "-config", cfgPath, "bogus")
|
||||
code, out := runCLI(t, "bogus")
|
||||
if code != 2 {
|
||||
t.Errorf("exit code = %d, want 2", code)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user