template_test.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 TestWriteTemplate(t *testing.T) {
  10. tests := []struct {
  11. findings []Finding
  12. testReportName string
  13. expected string
  14. wantEmpty bool
  15. }{
  16. {
  17. testReportName: "markdown",
  18. expected: filepath.Join(expectPath, "report", "template_markdown.md"),
  19. findings: []Finding{
  20. {
  21. RuleID: "test-rule",
  22. Description: "A 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{"tag1", "tag2", "tag3"},
  36. },
  37. },
  38. },
  39. {
  40. testReportName: "jsonextra",
  41. expected: filepath.Join(expectPath, "report", "template_jsonextra.json"),
  42. findings: []Finding{
  43. {
  44. RuleID: "test-rule",
  45. Description: "A test rule",
  46. Line: "whole line containing secret",
  47. Match: "line containing secret",
  48. Secret: "a secret",
  49. StartLine: 1,
  50. EndLine: 2,
  51. StartColumn: 1,
  52. EndColumn: 2,
  53. Message: "opps",
  54. File: "auth.py",
  55. Commit: "0000000000000000",
  56. Author: "John Doe",
  57. Email: "johndoe@gmail.com",
  58. Date: "10-19-2003",
  59. Tags: []string{"tag1", "tag2", "tag3"},
  60. },
  61. },
  62. },
  63. }
  64. for _, test := range tests {
  65. t.Run(test.testReportName, func(t *testing.T) {
  66. reporter, err := NewTemplateReporter(templatePath + test.testReportName + ".tmpl")
  67. require.NoError(t, err)
  68. tmpfile, err := os.Create(filepath.Join(t.TempDir(), test.testReportName+filepath.Ext(test.expected)))
  69. require.NoError(t, err)
  70. defer tmpfile.Close()
  71. err = reporter.Write(tmpfile, test.findings)
  72. require.NoError(t, err)
  73. assert.FileExists(t, tmpfile.Name())
  74. got, err := os.ReadFile(tmpfile.Name())
  75. require.NoError(t, err)
  76. if test.wantEmpty {
  77. assert.Empty(t, got)
  78. return
  79. }
  80. want, err := os.ReadFile(test.expected)
  81. require.NoError(t, err)
  82. wantStr := lineEndingReplacer.Replace(string(want))
  83. gotStr := lineEndingReplacer.Replace(string(got))
  84. assert.Equal(t, wantStr, gotStr)
  85. })
  86. }
  87. }