-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit_test.go
More file actions
100 lines (87 loc) · 2.28 KB
/
init_test.go
File metadata and controls
100 lines (87 loc) · 2.28 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
97
98
99
100
package cli
import (
"io"
"os"
"path/filepath"
"testing"
"github.com/LAA-Software-Engineering/agentic-control-plane/internal/spec"
)
func TestInit_thenValidateSucceeds(t *testing.T) {
parent := t.TempDir()
name := "starter"
ResetGlobalsForTest()
icmd := NewRootCmd()
icmd.SetOut(io.Discard)
icmd.SetErr(io.Discard)
icmd.SetArgs([]string{"init", name, "--parent-dir", parent})
if err := icmd.Execute(); err != nil {
t.Fatal(err)
}
proj := filepath.Join(parent, name)
if _, err := os.Stat(filepath.Join(proj, "project.yaml")); err != nil {
t.Fatal(err)
}
ResetGlobalsForTest()
v := NewRootCmd()
v.SetOut(io.Discard)
v.SetErr(io.Discard)
v.SetArgs([]string{"validate", "--project", proj})
if err := v.Execute(); err != nil {
t.Fatal(err)
}
}
func TestInit_defaultPolicyExpandsShellSafePreset(t *testing.T) {
parent := t.TempDir()
name := "shellsafe"
ResetGlobalsForTest()
cmd := NewRootCmd()
cmd.SetOut(io.Discard)
cmd.SetErr(io.Discard)
cmd.SetArgs([]string{"init", name, "--parent-dir", parent})
if err := cmd.Execute(); err != nil {
t.Fatal(err)
}
ResetGlobalsForTest()
g := &Global{ProjectRoot: filepath.Join(parent, name)}
graph, _, err := prepareProjectGraph(g.ProjectRoot, g)
if err != nil {
t.Fatal(err)
}
pr, ok := graph.Policies["default"]
if !ok || pr == nil {
t.Fatal("expected default policy")
}
if pr.Spec.ResolvedPreset != spec.PresetShellSafe {
t.Fatalf("default policy ResolvedPreset = %q want %s", pr.Spec.ResolvedPreset, spec.PresetShellSafe)
}
}
func TestInit_rejectsExistingDir(t *testing.T) {
parent := t.TempDir()
name := "dup"
if err := os.MkdirAll(filepath.Join(parent, name), 0o755); err != nil {
t.Fatal(err)
}
ResetGlobalsForTest()
cmd := NewRootCmd()
cmd.SetOut(io.Discard)
cmd.SetErr(io.Discard)
cmd.SetArgs([]string{"init", name, "--parent-dir", parent})
if err := cmd.Execute(); err == nil {
t.Fatal("expected error")
}
}
func TestInit_rejectsBadName(t *testing.T) {
parent := t.TempDir()
ResetGlobalsForTest()
cmd := NewRootCmd()
cmd.SetOut(io.Discard)
cmd.SetErr(io.Discard)
cmd.SetArgs([]string{"init", "../nope", "--parent-dir", parent})
err := cmd.Execute()
if err == nil {
t.Fatal("expected error")
}
if ExitCodeOf(err) != ExitValidationError {
t.Fatalf("exit=%d err=%v", ExitCodeOf(err), err)
}
}