4
0

junit_test.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. err = reporter.Write(tmpfile, test.findings)
  67. require.NoError(t, err)
  68. assert.FileExists(t, tmpfile.Name())
  69. got, err := os.ReadFile(tmpfile.Name())
  70. require.NoError(t, err)
  71. if test.wantEmpty {
  72. assert.Empty(t, got)
  73. return
  74. }
  75. want, err := os.ReadFile(test.expected)
  76. require.NoError(t, err)
  77. assert.Equal(t, string(want), string(got))
  78. }
  79. }