json_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package report
  2. import (
  3. "os"
  4. "path/filepath"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. )
  9. func TestWriteJSON(t *testing.T) {
  10. tests := []struct {
  11. findings []Finding
  12. testReportName string
  13. expected string
  14. wantEmpty bool
  15. }{
  16. {
  17. testReportName: "simple",
  18. expected: filepath.Join(expectPath, "report", "json_simple.json"),
  19. findings: []Finding{
  20. {
  21. Description: "",
  22. RuleID: "test-rule",
  23. Match: "line containing secret",
  24. Secret: "a secret",
  25. StartLine: 1,
  26. EndLine: 2,
  27. StartColumn: 1,
  28. EndColumn: 2,
  29. Message: "opps",
  30. File: "auth.py",
  31. SymlinkFile: "",
  32. Commit: "0000000000000000",
  33. Author: "John Doe",
  34. Email: "johndoe@gmail.com",
  35. Date: "10-19-2003",
  36. Tags: []string{},
  37. },
  38. }},
  39. {
  40. testReportName: "empty",
  41. expected: filepath.Join(expectPath, "report", "empty.json"),
  42. findings: []Finding{}},
  43. }
  44. for _, test := range tests {
  45. t.Run(test.testReportName, func(t *testing.T) {
  46. tmpfile, err := os.Create(filepath.Join(t.TempDir(), test.testReportName+".json"))
  47. require.NoError(t, err)
  48. err = writeJson(test.findings, tmpfile)
  49. require.NoError(t, err)
  50. assert.FileExists(t, tmpfile.Name())
  51. got, err := os.ReadFile(tmpfile.Name())
  52. require.NoError(t, err)
  53. if test.wantEmpty {
  54. assert.Empty(t, got)
  55. return
  56. }
  57. want, err := os.ReadFile(test.expected)
  58. require.NoError(t, err)
  59. assert.Equal(t, want, got)
  60. })
  61. }
  62. }