-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlifecycle_test.go
More file actions
96 lines (83 loc) · 2.54 KB
/
lifecycle_test.go
File metadata and controls
96 lines (83 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright 2025-2026 : Nawa Manusitthipol
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package lifecycle
import (
"strings"
"testing"
)
func TestReadConfirmation(t *testing.T) {
tests := []struct {
input string
expected bool
}{
{input: "y\n", expected: true},
{input: "YES\n", expected: true},
{input: "n\n", expected: false},
{input: "\n", expected: false},
}
for _, test := range tests {
got, err := readConfirmation(strings.NewReader(test.input))
if err != nil {
t.Fatalf("readConfirmation(%q) returned error: %v", test.input, err)
}
if got != test.expected {
t.Fatalf("readConfirmation(%q) = %t, want %t", test.input, got, test.expected)
}
}
}
func TestExitCode(t *testing.T) {
if code := ExitCode(nil); code != 0 {
t.Fatalf("ExitCode(nil) = %d, want 0", code)
}
err := commandExit(2, "bad input")
if code := ExitCode(err); code != 2 {
t.Fatalf("ExitCode(commandExit(2,...)) = %d, want 2", code)
}
}
func TestPruneArgValidation(t *testing.T) {
err := Prune([]string{"--unknown"}, strings.NewReader(""), &strings.Builder{}, &strings.Builder{})
if err == nil {
t.Fatal("expected argument parsing error")
}
if code := ExitCode(err); code != 2 {
t.Fatalf("ExitCode(err) = %d, want 2", code)
}
}
func TestSanitizeProjectName(t *testing.T) {
cases := map[string]string{
"my-project": "my-project",
"my project": "my-project",
"my@project#123": "my-project-123",
"": "booth",
}
for in, want := range cases {
got := sanitizeProjectName(in)
if got != want {
t.Fatalf("sanitizeProjectName(%q) = %q, want %q", in, got, want)
}
}
}
func TestResolveSingleContainerByCodePathAmbiguous(t *testing.T) {
containers := []managedContainer{
{Name: "a", CodePath: normalizeCodePath("."), State: "exited"},
{Name: "b", CodePath: normalizeCodePath("."), State: "exited"},
}
_, err := resolveSingleContainer(containers, "", ".", nil, stateStopped)
if err == nil {
t.Fatal("expected ambiguous code-path error")
}
if !strings.Contains(err.Error(), "multiple booths match code path") {
t.Fatalf("unexpected error: %v", err)
}
}
func TestResolveSingleContainerStateValidation(t *testing.T) {
containers := []managedContainer{{Name: "demo", State: "running"}}
_, err := resolveSingleContainer(containers, "demo", "", nil, stateStopped)
if err == nil {
t.Fatal("expected already running error")
}
if !strings.Contains(err.Error(), "already running") {
t.Fatalf("unexpected error: %v", err)
}
}