csv_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 TestWriteCSV(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", "csv_simple.csv"),
  19. findings: []Finding{
  20. {
  21. RuleID: "test-rule",
  22. Match: "line containing secret",
  23. Secret: "a secret",
  24. StartLine: 1,
  25. EndLine: 2,
  26. StartColumn: 1,
  27. EndColumn: 2,
  28. Message: "opps",
  29. File: "auth.py",
  30. SymlinkFile: "",
  31. Commit: "0000000000000000",
  32. Author: "John Doe",
  33. Email: "johndoe@gmail.com",
  34. Date: "10-19-2003",
  35. Fingerprint: "fingerprint",
  36. Tags: []string{"tag1", "tag2", "tag3"},
  37. },
  38. }},
  39. {
  40. wantEmpty: true,
  41. testReportName: "empty",
  42. expected: filepath.Join(expectPath, "report", "this_should_not_exist.csv"),
  43. findings: []Finding{},
  44. },
  45. }
  46. reporter := CsvReporter{}
  47. for _, test := range tests {
  48. t.Run(test.testReportName, func(t *testing.T) {
  49. tmpfile, err := os.Create(filepath.Join(t.TempDir(), test.testReportName+".csv"))
  50. require.NoError(t, err)
  51. err = reporter.Write(tmpfile, test.findings)
  52. require.NoError(t, err)
  53. assert.FileExists(t, tmpfile.Name())
  54. got, err := os.ReadFile(tmpfile.Name())
  55. require.NoError(t, err)
  56. if test.wantEmpty {
  57. assert.Empty(t, got)
  58. return
  59. }
  60. want, err := os.ReadFile(test.expected)
  61. require.NoError(t, err)
  62. assert.Equal(t, string(want), string(got))
  63. })
  64. }
  65. }