4
0

csv_test.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. defer tmpfile.Close()
  52. err = reporter.Write(tmpfile, test.findings)
  53. require.NoError(t, err)
  54. assert.FileExists(t, tmpfile.Name())
  55. got, err := os.ReadFile(tmpfile.Name())
  56. require.NoError(t, err)
  57. if test.wantEmpty {
  58. assert.Empty(t, got)
  59. return
  60. }
  61. want, err := os.ReadFile(test.expected)
  62. require.NoError(t, err)
  63. wantStr := lineEndingReplacer.Replace(string(want))
  64. gotStr := lineEndingReplacer.Replace(string(got))
  65. assert.Equal(t, wantStr, gotStr)
  66. })
  67. }
  68. }