(t *testing.T)
| 66 | } |
| 67 | |
| 68 | func Test_ValidateInputCmd_SuccessSingleFile(t *testing.T) { |
| 69 | fs := afero.NewMemMapFs() |
| 70 | // Write a dummy file to simulate input |
| 71 | require.NoError(t, afero.WriteFile(fs, "/input.yaml", []byte("some: data"), 0o644)) |
| 72 | |
| 73 | // Mock validator: returns success with no violations, one success result. |
| 74 | outMock := &output.Output{ |
| 75 | PolicyCheck: []evaluator.Outcome{ |
| 76 | { |
| 77 | Successes: []evaluator.Result{ |
| 78 | {Message: "Everything looks great!"}, |
| 79 | }, |
| 80 | }, |
| 81 | }, |
| 82 | } |
| 83 | |
| 84 | cmd, buf := setUpValidateInputCmd(mockValidate(outMock, nil), fs) |
| 85 | cmd.SetArgs([]string{ |
| 86 | "input", |
| 87 | "--file", "/input.yaml", |
| 88 | "--policy", `{"publicKey": "testkey"}`, |
| 89 | "--output", "json", // Explicitly request JSON output |
| 90 | }) |
| 91 | |
| 92 | utils.SetTestRekorPublicKey(t) |
| 93 | err := cmd.Execute() |
| 94 | assert.NoError(t, err) |
| 95 | |
| 96 | // Parse the JSON output |
| 97 | var outJSON map[string]interface{} |
| 98 | err = json.Unmarshal(buf.Bytes(), &outJSON) |
| 99 | assert.NoError(t, err) |
| 100 | |
| 101 | // Ensure success is true |
| 102 | assert.True(t, outJSON["success"].(bool)) |
| 103 | |
| 104 | inputFiles, ok := outJSON["filepaths"].([]interface{}) |
| 105 | if !ok { |
| 106 | t.Fatal("expected 'filepaths' key in output") |
| 107 | } |
| 108 | assert.Len(t, inputFiles, 1) |
| 109 | inputObj := inputFiles[0].(map[string]interface{}) |
| 110 | assert.Equal(t, "/input.yaml", inputObj["filepath"]) |
| 111 | assert.True(t, inputObj["success"].(bool)) |
| 112 | } |
| 113 | |
| 114 | func Test_ValidateInputCmd_SuccessMultipleFiles(t *testing.T) { |
| 115 | fs := afero.NewMemMapFs() |
nothing calls this directly
no test coverage detected