-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsources_test.go
More file actions
142 lines (125 loc) · 3.57 KB
/
Copy pathsources_test.go
File metadata and controls
142 lines (125 loc) · 3.57 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package huhx
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/spf13/cobra"
)
func TestRunner_AnswerPairMalformed(t *testing.T) {
var name string
form := NewForm(NewGroup(
NewInput().Key("name").Value(&name),
))
cmd := &cobra.Command{Use: "t"}
cmd.Flags().StringArray("answer", nil, "")
if err := cmd.ParseFlags([]string{"--answer", "name-only-no-equals"}); err != nil {
t.Fatal(err)
}
r := New(form,
WithNonInteractive(Always),
WithCobraFlags(cmd),
)
err := r.Run()
if err == nil {
t.Fatal("expected error for malformed --answer pair")
}
msg := err.Error()
if !strings.Contains(msg, `invalid --answer "name-only-no-equals"`) {
t.Errorf("expected invalid-pair message, got %q", msg)
}
if !strings.Contains(msg, "expected key=val") {
t.Errorf("expected key=val hint, got %q", msg)
}
}
func TestRunner_AnswerFileNotFound(t *testing.T) {
var name string
form := NewForm(NewGroup(
NewInput().Key("name").Value(&name),
))
bogus := "/nonexistent/path/that/should/not/exist.yaml"
r := New(form,
WithNonInteractive(Always),
WithAnswerFile(bogus),
)
err := r.Run()
if err == nil {
t.Fatal("expected error for missing answer file")
}
msg := err.Error()
if !strings.Contains(msg, "read answer file") {
t.Errorf("expected read-answer-file prefix, got %q", msg)
}
if !strings.Contains(msg, bogus) {
t.Errorf("expected bogus path in error, got %q", msg)
}
}
// TestRunner_AnswerFileMalformedYAML exercises loadAnswerFile's parse
// failure path. The file exists and is readable but contains a YAML
// document that yaml.v3 cannot decode into a map.
func TestRunner_AnswerFileMalformedYAML(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "bad.yaml")
// "name: : :" is well-formed text but produces a mapping value that
// is itself a mapping with a nil key, which yaml.v3 rejects.
body := "name: : :\n"
if err := os.WriteFile(path, []byte(body), 0o600); err != nil {
t.Fatal(err)
}
var name string
form := NewForm(NewGroup(
NewInput().Key("name").Value(&name),
))
r := New(form,
WithNonInteractive(Always),
WithAnswerFile(path),
)
err := r.Run()
if err == nil {
t.Fatal("expected error for malformed YAML")
}
msg := err.Error()
if !strings.Contains(msg, "parse answer file") {
t.Errorf("expected parse-answer-file prefix, got %q", msg)
}
if !strings.Contains(msg, path) {
t.Errorf("expected file path in error, got %q", msg)
}
}
// TestLoadAnswerFile_CacheAndInvalidate verifies the answer file is memoized
// across calls, that each call returns an independent copy, and that the
// cache is invalidated when the file changes.
func TestLoadAnswerFile_CacheAndInvalidate(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "answers.yaml")
if err := os.WriteFile(path, []byte("name: alice\n"), 0o600); err != nil {
t.Fatal(err)
}
first, err := loadAnswerFile(path)
if err != nil {
t.Fatal(err)
}
if first["name"] != "alice" {
t.Fatalf("got %q, want alice", first["name"])
}
// Mutating the returned copy must not poison the cache.
first["name"] = "mutated"
second, err := loadAnswerFile(path)
if err != nil {
t.Fatal(err)
}
if second["name"] != "alice" {
t.Fatalf("cache returned a shared/mutated map: got %q, want alice", second["name"])
}
// Rewriting the file (different size) invalidates the cache.
if err := os.WriteFile(path, []byte("name: bob-the-builder\n"), 0o600); err != nil {
t.Fatal(err)
}
third, err := loadAnswerFile(path)
if err != nil {
t.Fatal(err)
}
if third["name"] != "bob-the-builder" {
t.Fatalf("cache not invalidated after rewrite: got %q", third["name"])
}
}