TestInitCmd aims to test the init command, with output validation
(t *testing.T)
| 312 | |
| 313 | // TestInitCmd aims to test the init command, with output validation |
| 314 | func TestInitCmd(t *testing.T) { |
| 315 | tests := []struct { |
| 316 | name string |
| 317 | args []string |
| 318 | expectedCode int |
| 319 | outputCheck func(string, string) error |
| 320 | }{ |
| 321 | { |
| 322 | name: "Ensure init warns on template without required_plugin blocks", |
| 323 | args: []string{ |
| 324 | testFixture("hcl", "build-var-in-pp.pkr.hcl"), |
| 325 | }, |
| 326 | expectedCode: 0, |
| 327 | outputCheck: func(stdout, stderr string) error { |
| 328 | if !strings.Contains(stdout, "No plugins requirement found") { |
| 329 | return fmt.Errorf("command should warn about plugin requirements not found, but did not") |
| 330 | } |
| 331 | return nil |
| 332 | }, |
| 333 | }, |
| 334 | } |
| 335 | |
| 336 | for _, tt := range tests { |
| 337 | t.Run(tt.name, func(t *testing.T) { |
| 338 | c := &InitCommand{ |
| 339 | Meta: TestMetaFile(t), |
| 340 | } |
| 341 | |
| 342 | exitCode := c.Run(tt.args) |
| 343 | if exitCode != tt.expectedCode { |
| 344 | t.Errorf("process exit code mismatch: expected %d, got %d", |
| 345 | tt.expectedCode, |
| 346 | exitCode) |
| 347 | } |
| 348 | |
| 349 | out, stderr := GetStdoutAndErrFromTestMeta(t, c.Meta) |
| 350 | err := tt.outputCheck(out, stderr) |
| 351 | if err != nil { |
| 352 | if len(out) != 0 { |
| 353 | t.Logf("command stdout: %q", out) |
| 354 | } |
| 355 | |
| 356 | if len(stderr) != 0 { |
| 357 | t.Logf("command stderr: %q", stderr) |
| 358 | } |
| 359 | t.Error(err.Error()) |
| 360 | } |
| 361 | }) |
| 362 | } |
| 363 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…