4
0

junit_test.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 TestWriteJunit(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", "junit_simple.xml"),
  19. findings: []Finding{
  20. {
  21. Description: "Test Rule",
  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. Commit: "0000000000000000",
  32. Author: "John Doe",
  33. Email: "johndoe@gmail.com",
  34. Date: "10-19-2003",
  35. Tags: []string{},
  36. },
  37. {
  38. Description: "Test Rule",
  39. RuleID: "test-rule",
  40. Match: "line containing secret",
  41. Secret: "a secret",
  42. StartLine: 2,
  43. EndLine: 3,
  44. StartColumn: 1,
  45. EndColumn: 2,
  46. Message: "",
  47. File: "auth.py",
  48. Commit: "",
  49. Author: "",
  50. Email: "",
  51. Date: "",
  52. Tags: []string{},
  53. },
  54. },
  55. },
  56. {
  57. testReportName: "empty",
  58. expected: filepath.Join(expectPath, "report", "junit_empty.xml"),
  59. findings: []Finding{},
  60. },
  61. }
  62. reporter := JunitReporter{}
  63. for _, test := range tests {
  64. tmpfile, err := os.Create(filepath.Join(t.TempDir(), test.testReportName+".xml"))
  65. require.NoError(t, err)
  66. defer tmpfile.Close()
  67. err = reporter.Write(tmpfile, test.findings)
  68. require.NoError(t, err)
  69. assert.FileExists(t, tmpfile.Name())
  70. got, err := os.ReadFile(tmpfile.Name())
  71. require.NoError(t, err)
  72. if test.wantEmpty {
  73. assert.Empty(t, got)
  74. return
  75. }
  76. want, err := os.ReadFile(test.expected)
  77. require.NoError(t, err)
  78. wantStr := lineEndingReplacer.Replace(string(want))
  79. gotStr := lineEndingReplacer.Replace(string(got))
  80. assert.Equal(t, wantStr, gotStr)
  81. }
  82. }