| 14 | ) |
| 15 | |
| 16 | func TestJSONFormatter(t *testing.T) { |
| 17 | out := new(bytes.Buffer) |
| 18 | formatter := JSONFormatter{out: out} |
| 19 | |
| 20 | makefile, _ := parser.Parse("../fixtures/missing_phony.make") |
| 21 | |
| 22 | violations := validator.Validate(makefile, &config.Config{}) |
| 23 | formatter.Format(violations) |
| 24 | |
| 25 | // Verify JSON output |
| 26 | var violationsJSON []struct { |
| 27 | Rule string `json:"rule"` |
| 28 | Violation string `json:"violation"` |
| 29 | FileName string `json:"file_name"` |
| 30 | LineNumber int `json:"line_number"` |
| 31 | } |
| 32 | |
| 33 | err := json.Unmarshal(out.Bytes(), &violationsJSON) |
| 34 | require.NoError(t, err, "output should be valid JSON") |
| 35 | |
| 36 | // Verify we have violations |
| 37 | assert.Greater(t, len(violationsJSON), 0, "should have at least one violation") |
| 38 | |
| 39 | // Verify structure |
| 40 | for _, v := range violationsJSON { |
| 41 | assert.NotEmpty(t, v.Rule, "rule should not be empty") |
| 42 | assert.NotEmpty(t, v.Violation, "violation should not be empty") |
| 43 | assert.NotEmpty(t, v.FileName, "file_name should not be empty") |
| 44 | assert.Greater(t, v.LineNumber, 0, "line_number should be greater than 0") |
| 45 | } |
| 46 | |
| 47 | // Verify specific violations are present |
| 48 | ruleNames := make(map[string]bool) |
| 49 | for _, v := range violationsJSON { |
| 50 | ruleNames[v.Rule] = true |
| 51 | } |
| 52 | assert.Contains(t, ruleNames, "phonydeclared", "should contain phonydeclared violation") |
| 53 | } |
| 54 | |
| 55 | func TestJSONFormatter_EmptyViolations(t *testing.T) { |
| 56 | out := new(bytes.Buffer) |