|
1 | 1 | package cmd |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "bytes" |
5 | 4 | "errors" |
6 | 5 | "os" |
7 | 6 | "strings" |
@@ -86,31 +85,20 @@ data: |
86 | 85 | chart1 := t.TempDir() |
87 | 86 | chart2 := t.TempDir() |
88 | 87 |
|
89 | | - oldStdout := os.Stdout |
90 | | - r, w, err := os.Pipe() |
91 | | - if err != nil { |
92 | | - t.Fatalf("Failed to create pipe: %v", err) |
93 | | - } |
94 | | - os.Stdout = w |
95 | | - |
96 | | - cmd := localCmd() |
97 | | - cmd.SetArgs([]string{chart1, chart2}) |
| 88 | + output, err := captureStdout(func() { |
| 89 | + cmd := localCmd() |
| 90 | + cmd.SetArgs([]string{chart1, chart2}) |
98 | 91 |
|
99 | | - err = cmd.Execute() |
100 | | - w.Close() |
101 | | - os.Stdout = oldStdout |
| 92 | + if execErr := cmd.Execute(); execErr != nil { |
| 93 | + t.Errorf("Expected no error but got: %v", execErr) |
| 94 | + } |
| 95 | + }) |
102 | 96 |
|
103 | 97 | if err != nil { |
104 | | - t.Fatalf("Expected no error but got: %v", err) |
105 | | - } |
106 | | - |
107 | | - var buf bytes.Buffer |
108 | | - if _, err := buf.ReadFrom(r); err != nil { |
109 | | - t.Fatalf("Failed to read from pipe: %v", err) |
| 98 | + t.Fatalf("Failed to capture stdout: %v", err) |
110 | 99 | } |
111 | | - r.Close() |
112 | | - if buf.String() != "" { |
113 | | - t.Errorf("Expected no output when charts are identical, got: %q", buf.String()) |
| 100 | + if output != "" { |
| 101 | + t.Errorf("Expected no output when charts are identical, got: %q", output) |
114 | 102 | } |
115 | 103 | } |
116 | 104 |
|
@@ -138,30 +126,18 @@ data: |
138 | 126 | chart1 := t.TempDir() |
139 | 127 | chart2 := t.TempDir() |
140 | 128 |
|
141 | | - oldStdout := os.Stdout |
142 | | - r, w, err := os.Pipe() |
143 | | - if err != nil { |
144 | | - t.Fatalf("Failed to create pipe: %v", err) |
145 | | - } |
146 | | - os.Stdout = w |
| 129 | + output, err := captureStdout(func() { |
| 130 | + cmd := localCmd() |
| 131 | + cmd.SetArgs([]string{chart1, chart2}) |
147 | 132 |
|
148 | | - cmd := localCmd() |
149 | | - cmd.SetArgs([]string{chart1, chart2}) |
150 | | - |
151 | | - err = cmd.Execute() |
152 | | - w.Close() |
153 | | - os.Stdout = oldStdout |
| 133 | + if execErr := cmd.Execute(); execErr != nil { |
| 134 | + t.Errorf("Expected no error but got: %v", execErr) |
| 135 | + } |
| 136 | + }) |
154 | 137 |
|
155 | 138 | if err != nil { |
156 | | - t.Fatalf("Expected no error but got: %v", err) |
157 | | - } |
158 | | - |
159 | | - var buf bytes.Buffer |
160 | | - if _, err := buf.ReadFrom(r); err != nil { |
161 | | - t.Fatalf("Failed to read from pipe: %v", err) |
| 139 | + t.Fatalf("Failed to capture stdout: %v", err) |
162 | 140 | } |
163 | | - r.Close() |
164 | | - output := buf.String() |
165 | 141 |
|
166 | 142 | if !strings.Contains(output, "value1") || !strings.Contains(output, "value2") { |
167 | 143 | t.Errorf("Expected diff output containing value1 and value2, got: %q", output) |
@@ -280,6 +256,92 @@ func TestLocalCmdHelmTemplateError(t *testing.T) { |
280 | 256 | } |
281 | 257 | } |
282 | 258 |
|
| 259 | +func TestPrepareStdinValues(t *testing.T) { |
| 260 | + l := &local{ |
| 261 | + valueFiles: valueFiles{"-", "-", "real-values.yaml"}, |
| 262 | + } |
| 263 | + |
| 264 | + stdinContent := `key: stdin-value |
| 265 | +` |
| 266 | + tmpStdin, err := os.CreateTemp("", "fake-stdin") |
| 267 | + if err != nil { |
| 268 | + t.Fatal(err) |
| 269 | + } |
| 270 | + defer os.Remove(tmpStdin.Name()) |
| 271 | + if _, err := tmpStdin.WriteString(stdinContent); err != nil { |
| 272 | + t.Fatal(err) |
| 273 | + } |
| 274 | + if err := tmpStdin.Close(); err != nil { |
| 275 | + t.Fatal(err) |
| 276 | + } |
| 277 | + |
| 278 | + oldStdin := os.Stdin |
| 279 | + f, err := os.Open(tmpStdin.Name()) |
| 280 | + if err != nil { |
| 281 | + t.Fatal(err) |
| 282 | + } |
| 283 | + os.Stdin = f |
| 284 | + defer func() { |
| 285 | + os.Stdin = oldStdin |
| 286 | + f.Close() |
| 287 | + }() |
| 288 | + |
| 289 | + cleanup, err := l.prepareStdinValues() |
| 290 | + if err != nil { |
| 291 | + t.Fatalf("Expected no error but got: %v", err) |
| 292 | + } |
| 293 | + if cleanup == nil { |
| 294 | + t.Fatal("Expected cleanup function but got nil") |
| 295 | + } |
| 296 | + |
| 297 | + if l.valueFiles[0] == "-" { |
| 298 | + t.Error("Expected first valueFile to be replaced with temp file path") |
| 299 | + } |
| 300 | + if l.valueFiles[1] == "-" { |
| 301 | + t.Error("Expected second valueFile to be replaced with temp file path") |
| 302 | + } |
| 303 | + if l.valueFiles[0] != l.valueFiles[1] { |
| 304 | + t.Errorf("Expected both '-' entries to point to the same temp file, got %q and %q", l.valueFiles[0], l.valueFiles[1]) |
| 305 | + } |
| 306 | + if _, err := os.Stat(l.valueFiles[0]); os.IsNotExist(err) { |
| 307 | + t.Errorf("Expected temp file %q to exist", l.valueFiles[0]) |
| 308 | + } |
| 309 | + if l.valueFiles[2] != "real-values.yaml" { |
| 310 | + t.Errorf("Expected third valueFile to be unchanged, got %q", l.valueFiles[2]) |
| 311 | + } |
| 312 | + |
| 313 | + data, err := os.ReadFile(l.valueFiles[0]) |
| 314 | + if err != nil { |
| 315 | + t.Fatalf("Failed to read temp file: %v", err) |
| 316 | + } |
| 317 | + if string(data) != stdinContent { |
| 318 | + t.Errorf("Expected temp file to contain stdin content %q, got %q", stdinContent, string(data)) |
| 319 | + } |
| 320 | + |
| 321 | + cleanup() |
| 322 | + |
| 323 | + if _, err := os.Stat(l.valueFiles[0]); !os.IsNotExist(err) { |
| 324 | + t.Errorf("Expected temp file %q to be removed after cleanup", l.valueFiles[0]) |
| 325 | + } |
| 326 | +} |
| 327 | + |
| 328 | +func TestPrepareStdinValuesNoStdin(t *testing.T) { |
| 329 | + l := &local{ |
| 330 | + valueFiles: valueFiles{"values1.yaml", "values2.yaml"}, |
| 331 | + } |
| 332 | + |
| 333 | + cleanup, err := l.prepareStdinValues() |
| 334 | + if err != nil { |
| 335 | + t.Fatalf("Expected no error but got: %v", err) |
| 336 | + } |
| 337 | + if cleanup != nil { |
| 338 | + t.Error("Expected nil cleanup function when no stdin values") |
| 339 | + } |
| 340 | + if l.valueFiles[0] != "values1.yaml" || l.valueFiles[1] != "values2.yaml" { |
| 341 | + t.Errorf("Expected valueFiles to be unchanged, got %v", l.valueFiles) |
| 342 | + } |
| 343 | +} |
| 344 | + |
283 | 345 | func setupFakeHelm(t *testing.T, mode, output, argsFile, countFile string) { |
284 | 346 | t.Helper() |
285 | 347 | t.Setenv("HELM_DIFF_FAKE_HELM", "1") |
|
0 commit comments