junit_test.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. for _, test := range tests {
  63. tmpfile, err := os.Create(filepath.Join(t.TempDir(), test.testReportName+".xml"))
  64. require.NoError(t, err)
  65. err = writeJunit(test.findings, tmpfile)
  66. require.NoError(t, err)
  67. assert.FileExists(t, tmpfile.Name())
  68. got, err := os.ReadFile(tmpfile.Name())
  69. require.NoError(t, err)
  70. if test.wantEmpty {
  71. assert.Empty(t, got)
  72. return
  73. }
  74. want, err := os.ReadFile(test.expected)
  75. require.NoError(t, err)
  76. assert.Equal(t, want, got)
  77. }
  78. }