refactor: simplify test asserts (#1271)

This commit is contained in:
Oleksandr Redko authored and GitHub committed 2023-10-03 10:38:08 -05:00
1 parent d37b38f2ee
commit 2959fc072c
10 files changed
+151 -297

No files matched your search

+16 -22
View File
@@ -6,6 +6,9 @@ import (
"strconv"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/zricethezav/gitleaks/v8/config"
)
@@ -95,28 +98,19 @@ func TestReport(t *testing.T) {
}
for i, test := range tests {
tmpfile, err := os.Create(filepath.Join(t.TempDir(), strconv.Itoa(i)+test.ext))
if err != nil {
t.Error(err)
}
err = Write(test.findings, config.Config{}, test.ext, tmpfile.Name())
if err != nil {
t.Error(err)
}
got, err := os.ReadFile(tmpfile.Name())
if err != nil {
t.Error(err)
}
if len(got) == 0 && !test.wantEmpty {
t.Errorf("got empty file with extension " + test.ext)
}
if test.wantEmpty {
if len(got) > 0 {
t.Errorf("Expected empty file, got %s", got)
t.Run(test.ext, func(t *testing.T) {
tmpfile, err := os.Create(filepath.Join(t.TempDir(), strconv.Itoa(i)+test.ext))
require.NoError(t, err)
err = Write(test.findings, config.Config{}, test.ext, tmpfile.Name())
require.NoError(t, err)
got, err := os.ReadFile(tmpfile.Name())
require.NoError(t, err)
assert.FileExists(t, tmpfile.Name())
if test.wantEmpty {
assert.Empty(t, got)
return
}
continue
}
assert.NotEmpty(t, got)
})
}
}