mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-11-30 13:14:22 +01:00
refactor: simplify test asserts (#1271)
This commit is contained in:
1 parent
d37b38f2ee
commit
2959fc072c
10 files changed
+151
-297
No files matched your search
+4
-13
@@ -7,6 +7,7 @@ import (
|
|||||||
|
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
const configPath = "../testdata/config/"
|
const configPath = "../testdata/config/"
|
||||||
@@ -126,23 +127,13 @@ func TestTranslate(t *testing.T) {
|
|||||||
viper.SetConfigName(tt.cfgName)
|
viper.SetConfigName(tt.cfgName)
|
||||||
viper.SetConfigType("toml")
|
viper.SetConfigType("toml")
|
||||||
err := viper.ReadInConfig()
|
err := viper.ReadInConfig()
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
var vc ViperConfig
|
var vc ViperConfig
|
||||||
err = viper.Unmarshal(&vc)
|
err = viper.Unmarshal(&vc)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
cfg, err := vc.Translate()
|
cfg, err := vc.Translate()
|
||||||
if tt.wantError != nil {
|
assert.Equal(t, tt.wantError, err)
|
||||||
if err == nil {
|
|
||||||
t.Errorf("expected error")
|
|
||||||
}
|
|
||||||
assert.Equal(t, tt.wantError, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
assert.Equal(t, cfg.Rules, tt.cfg.Rules)
|
assert.Equal(t, cfg.Rules, tt.cfg.Rules)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
"github.com/zricethezav/gitleaks/v8/report"
|
"github.com/zricethezav/gitleaks/v8/report"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -82,7 +83,7 @@ func TestFileLoadBaseline(t *testing.T) {
|
|||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
_, err := LoadBaseline(test.Filename)
|
_, err := LoadBaseline(test.Filename)
|
||||||
assert.Equal(t, test.ExpectedError.Error(), err.Error())
|
assert.Equal(t, test.ExpectedError, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -132,6 +133,6 @@ func TestIgnoreIssuesInBaseline(t *testing.T) {
|
|||||||
for _, finding := range test.findings {
|
for _, finding := range test.findings {
|
||||||
d.addFinding(finding)
|
d.addFinding(finding)
|
||||||
}
|
}
|
||||||
assert.Equal(t, test.expectCount, len(d.findings))
|
assert.Len(t, d.findings, test.expectCount)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+37
-98
@@ -8,6 +8,7 @@ import (
|
|||||||
|
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/zricethezav/gitleaks/v8/config"
|
"github.com/zricethezav/gitleaks/v8/config"
|
||||||
"github.com/zricethezav/gitleaks/v8/report"
|
"github.com/zricethezav/gitleaks/v8/report"
|
||||||
@@ -336,23 +337,14 @@ func TestDetect(t *testing.T) {
|
|||||||
viper.SetConfigName(tt.cfgName)
|
viper.SetConfigName(tt.cfgName)
|
||||||
viper.SetConfigType("toml")
|
viper.SetConfigType("toml")
|
||||||
err := viper.ReadInConfig()
|
err := viper.ReadInConfig()
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
var vc config.ViperConfig
|
var vc config.ViperConfig
|
||||||
err = viper.Unmarshal(&vc)
|
err = viper.Unmarshal(&vc)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
cfg, err := vc.Translate()
|
cfg, err := vc.Translate()
|
||||||
cfg.Path = filepath.Join(configPath, tt.cfgName+".toml")
|
cfg.Path = filepath.Join(configPath, tt.cfgName+".toml")
|
||||||
if tt.wantError != nil {
|
assert.Equal(t, tt.wantError, err)
|
||||||
if err == nil {
|
|
||||||
t.Errorf("expected error")
|
|
||||||
}
|
|
||||||
assert.Equal(t, tt.wantError, err)
|
|
||||||
}
|
|
||||||
d := NewDetector(cfg)
|
d := NewDetector(cfg)
|
||||||
d.baselinePath = tt.baselinePath
|
d.baselinePath = tt.baselinePath
|
||||||
|
|
||||||
@@ -444,56 +436,38 @@ func TestFromGit(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
err := moveDotGit("dotGit", ".git")
|
moveDotGit(t, "dotGit", ".git")
|
||||||
if err != nil {
|
defer moveDotGit(t, ".git", "dotGit")
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer func() {
|
|
||||||
if err := moveDotGit(".git", "dotGit"); err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
|
|
||||||
viper.AddConfigPath(configPath)
|
viper.AddConfigPath(configPath)
|
||||||
viper.SetConfigName("simple")
|
viper.SetConfigName("simple")
|
||||||
viper.SetConfigType("toml")
|
viper.SetConfigType("toml")
|
||||||
err = viper.ReadInConfig()
|
err := viper.ReadInConfig()
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
var vc config.ViperConfig
|
var vc config.ViperConfig
|
||||||
err = viper.Unmarshal(&vc)
|
err = viper.Unmarshal(&vc)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
cfg, err := vc.Translate()
|
cfg, err := vc.Translate()
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
detector := NewDetector(cfg)
|
detector := NewDetector(cfg)
|
||||||
|
|
||||||
var ignorePath string
|
var ignorePath string
|
||||||
info, err := os.Stat(tt.source)
|
info, err := os.Stat(tt.source)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatalf("could not os.Stat: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if info.IsDir() {
|
if info.IsDir() {
|
||||||
ignorePath = filepath.Join(tt.source, ".gitleaksignore")
|
ignorePath = filepath.Join(tt.source, ".gitleaksignore")
|
||||||
} else {
|
} else {
|
||||||
ignorePath = filepath.Join(filepath.Dir(tt.source), ".gitleaksignore")
|
ignorePath = filepath.Join(filepath.Dir(tt.source), ".gitleaksignore")
|
||||||
}
|
}
|
||||||
if err = detector.AddGitleaksIgnore(ignorePath); err != nil {
|
err = detector.AddGitleaksIgnore(ignorePath)
|
||||||
t.Fatalf("could not call AddGitleaksIgnore: %v", err)
|
require.NoError(t, err)
|
||||||
}
|
|
||||||
|
|
||||||
findings, err := detector.DetectGit(tt.source, tt.logOpts, DetectType)
|
findings, err := detector.DetectGit(tt.source, tt.logOpts, DetectType)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, f := range findings {
|
for _, f := range findings {
|
||||||
f.Match = "" // remove lines cause copying and pasting them has some wack formatting
|
f.Match = "" // remove lines cause copying and pasting them has some wack formatting
|
||||||
@@ -540,43 +514,27 @@ func TestFromGitStaged(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
err := moveDotGit("dotGit", ".git")
|
moveDotGit(t, "dotGit", ".git")
|
||||||
if err != nil {
|
defer moveDotGit(t, ".git", "dotGit")
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer func() {
|
|
||||||
if err := moveDotGit(".git", "dotGit"); err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
|
|
||||||
viper.AddConfigPath(configPath)
|
viper.AddConfigPath(configPath)
|
||||||
viper.SetConfigName("simple")
|
viper.SetConfigName("simple")
|
||||||
viper.SetConfigType("toml")
|
viper.SetConfigType("toml")
|
||||||
err = viper.ReadInConfig()
|
err := viper.ReadInConfig()
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
var vc config.ViperConfig
|
var vc config.ViperConfig
|
||||||
err = viper.Unmarshal(&vc)
|
err = viper.Unmarshal(&vc)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
cfg, err := vc.Translate()
|
cfg, err := vc.Translate()
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
detector := NewDetector(cfg)
|
detector := NewDetector(cfg)
|
||||||
if err = detector.AddGitleaksIgnore(filepath.Join(tt.source, ".gitleaksignore")); err != nil {
|
err = detector.AddGitleaksIgnore(filepath.Join(tt.source, ".gitleaksignore"))
|
||||||
t.Fatalf("could not call AddGitleaksIgnore: %v", err)
|
require.NoError(t, err)
|
||||||
}
|
|
||||||
findings, err := detector.DetectGit(tt.source, tt.logOpts, ProtectStagedType)
|
findings, err := detector.DetectGit(tt.source, tt.logOpts, ProtectStagedType)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, f := range findings {
|
for _, f := range findings {
|
||||||
f.Match = "" // remove lines cause copying and pasting them has some wack formatting
|
f.Match = "" // remove lines cause copying and pasting them has some wack formatting
|
||||||
@@ -647,38 +605,28 @@ func TestFromFiles(t *testing.T) {
|
|||||||
viper.SetConfigName("simple")
|
viper.SetConfigName("simple")
|
||||||
viper.SetConfigType("toml")
|
viper.SetConfigType("toml")
|
||||||
err := viper.ReadInConfig()
|
err := viper.ReadInConfig()
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
var vc config.ViperConfig
|
var vc config.ViperConfig
|
||||||
err = viper.Unmarshal(&vc)
|
err = viper.Unmarshal(&vc)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
cfg, _ := vc.Translate()
|
cfg, _ := vc.Translate()
|
||||||
detector := NewDetector(cfg)
|
detector := NewDetector(cfg)
|
||||||
|
|
||||||
var ignorePath string
|
var ignorePath string
|
||||||
info, err := os.Stat(tt.source)
|
info, err := os.Stat(tt.source)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatalf("could not call os.Stat: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if info.IsDir() {
|
if info.IsDir() {
|
||||||
ignorePath = filepath.Join(tt.source, ".gitleaksignore")
|
ignorePath = filepath.Join(tt.source, ".gitleaksignore")
|
||||||
} else {
|
} else {
|
||||||
ignorePath = filepath.Join(filepath.Dir(tt.source), ".gitleaksignore")
|
ignorePath = filepath.Join(filepath.Dir(tt.source), ".gitleaksignore")
|
||||||
}
|
}
|
||||||
if err = detector.AddGitleaksIgnore(ignorePath); err != nil {
|
err = detector.AddGitleaksIgnore(ignorePath)
|
||||||
t.Fatalf("could not call AddGitleaksIgnore: %v", err)
|
require.NoError(t, err)
|
||||||
}
|
|
||||||
detector.FollowSymlinks = true
|
detector.FollowSymlinks = true
|
||||||
findings, err := detector.DetectFiles(tt.source)
|
findings, err := detector.DetectFiles(tt.source)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
assert.ElementsMatch(t, tt.expectedFindings, findings)
|
assert.ElementsMatch(t, tt.expectedFindings, findings)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -718,31 +666,25 @@ func TestDetectWithSymlinks(t *testing.T) {
|
|||||||
viper.SetConfigName("simple")
|
viper.SetConfigName("simple")
|
||||||
viper.SetConfigType("toml")
|
viper.SetConfigType("toml")
|
||||||
err := viper.ReadInConfig()
|
err := viper.ReadInConfig()
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
var vc config.ViperConfig
|
var vc config.ViperConfig
|
||||||
err = viper.Unmarshal(&vc)
|
err = viper.Unmarshal(&vc)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
cfg, _ := vc.Translate()
|
cfg, _ := vc.Translate()
|
||||||
detector := NewDetector(cfg)
|
detector := NewDetector(cfg)
|
||||||
detector.FollowSymlinks = true
|
detector.FollowSymlinks = true
|
||||||
findings, err := detector.DetectFiles(tt.source)
|
findings, err := detector.DetectFiles(tt.source)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
assert.ElementsMatch(t, tt.expectedFindings, findings)
|
assert.ElementsMatch(t, tt.expectedFindings, findings)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func moveDotGit(from, to string) error {
|
func moveDotGit(t *testing.T, from, to string) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
repoDirs, err := os.ReadDir("../testdata/repos")
|
repoDirs, err := os.ReadDir("../testdata/repos")
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
return err
|
|
||||||
}
|
|
||||||
for _, dir := range repoDirs {
|
for _, dir := range repoDirs {
|
||||||
if to == ".git" {
|
if to == ".git" {
|
||||||
_, err := os.Stat(fmt.Sprintf("%s/%s/%s", repoBasePath, dir.Name(), "dotGit"))
|
_, err := os.Stat(fmt.Sprintf("%s/%s/%s", repoBasePath, dir.Name(), "dotGit"))
|
||||||
@@ -762,9 +704,6 @@ func moveDotGit(from, to string) error {
|
|||||||
|
|
||||||
err = os.Rename(fmt.Sprintf("%s/%s/%s", repoBasePath, dir.Name(), from),
|
err = os.Rename(fmt.Sprintf("%s/%s/%s", repoBasePath, dir.Name(), from),
|
||||||
fmt.Sprintf("%s/%s/%s", repoBasePath, dir.Name(), to))
|
fmt.Sprintf("%s/%s/%s", repoBasePath, dir.Name(), to))
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
@@ -2,6 +2,8 @@ package detect
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestGetLocation tests the getLocation function.
|
// TestGetLocation tests the getLocation function.
|
||||||
@@ -50,11 +52,6 @@ func TestGetLocation(t *testing.T) {
|
|||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
loc := location(Fragment{newlineIndices: test.linePairs}, []int{test.start, test.end})
|
loc := location(Fragment{newlineIndices: test.linePairs}, []int{test.start, test.end})
|
||||||
if loc != test.wantLocation {
|
assert.Equal(t, test.wantLocation, loc)
|
||||||
t.Errorf("\nstartLine %d\nstartColumn: %d\nendLine: %d\nendColumn: %d\nstartLineIndex: %d\nendlineIndex %d",
|
|
||||||
loc.startLine, loc.startColumn, loc.endLine, loc.endColumn, loc.startLineIndex, loc.endLineIndex)
|
|
||||||
|
|
||||||
t.Error("got", loc, "want", test.wantLocation)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+20
-32
@@ -1,11 +1,12 @@
|
|||||||
package report
|
package report
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestWriteCSV(t *testing.T) {
|
func TestWriteCSV(t *testing.T) {
|
||||||
@@ -43,39 +44,26 @@ func TestWriteCSV(t *testing.T) {
|
|||||||
wantEmpty: true,
|
wantEmpty: true,
|
||||||
testReportName: "empty",
|
testReportName: "empty",
|
||||||
expected: filepath.Join(expectPath, "report", "this_should_not_exist.csv"),
|
expected: filepath.Join(expectPath, "report", "this_should_not_exist.csv"),
|
||||||
findings: []Finding{}},
|
findings: []Finding{},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
tmpfile, err := os.Create(filepath.Join(t.TempDir(), test.testReportName+".csv"))
|
t.Run(test.testReportName, func(t *testing.T) {
|
||||||
if err != nil {
|
tmpfile, err := os.Create(filepath.Join(t.TempDir(), test.testReportName+".csv"))
|
||||||
t.Error(err)
|
require.NoError(t, err)
|
||||||
}
|
err = writeCsv(test.findings, tmpfile)
|
||||||
err = writeCsv(test.findings, tmpfile)
|
require.NoError(t, err)
|
||||||
if err != nil {
|
assert.FileExists(t, tmpfile.Name())
|
||||||
t.Error(err)
|
got, err := os.ReadFile(tmpfile.Name())
|
||||||
}
|
require.NoError(t, err)
|
||||||
got, err := os.ReadFile(tmpfile.Name())
|
if test.wantEmpty {
|
||||||
if err != nil {
|
assert.Empty(t, got)
|
||||||
t.Error(err)
|
return
|
||||||
}
|
|
||||||
if test.wantEmpty {
|
|
||||||
if len(got) > 0 {
|
|
||||||
t.Errorf("Expected empty file, got %s", got)
|
|
||||||
}
|
}
|
||||||
continue
|
want, err := os.ReadFile(test.expected)
|
||||||
}
|
require.NoError(t, err)
|
||||||
want, err := os.ReadFile(test.expected)
|
assert.Equal(t, want, got)
|
||||||
if err != nil {
|
})
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !bytes.Equal(got, want) {
|
|
||||||
err = os.WriteFile(strings.Replace(test.expected, ".csv", ".got.csv", 1), got, 0644)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
t.Errorf("got %s, want %s", string(got), string(want))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+10
-16
@@ -1,6 +1,10 @@
|
|||||||
package report
|
package report
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
func TestRedact(t *testing.T) {
|
func TestRedact(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
@@ -19,12 +23,8 @@ func TestRedact(t *testing.T) {
|
|||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
for _, f := range test.findings {
|
for _, f := range test.findings {
|
||||||
f.Redact(100)
|
f.Redact(100)
|
||||||
if f.Secret != "REDACTED" {
|
assert.Equal(t, "REDACTED", f.Secret)
|
||||||
t.Error("redact not redacting: ", f.Secret)
|
assert.Equal(t, "line containing REDACTED", f.Match)
|
||||||
}
|
|
||||||
if f.Match != "line containing REDACTED" {
|
|
||||||
t.Error("redact not redacting: ", f.Secret)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -57,12 +57,8 @@ func TestMask(t *testing.T) {
|
|||||||
f := test.finding
|
f := test.finding
|
||||||
e := test.expect
|
e := test.expect
|
||||||
f.Redact(test.percent)
|
f.Redact(test.percent)
|
||||||
if f.Secret != e.Secret {
|
assert.Equal(t, e.Secret, f.Secret)
|
||||||
t.Error("redact not redacting: ", f.Secret)
|
assert.Equal(t, e.Match, f.Match)
|
||||||
}
|
|
||||||
if f.Match != e.Match {
|
|
||||||
t.Error("redact not redacting: ", f.Match)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -82,9 +78,7 @@ func TestMaskSecret(t *testing.T) {
|
|||||||
for name, test := range tests {
|
for name, test := range tests {
|
||||||
t.Run(name, func(t *testing.T) {
|
t.Run(name, func(t *testing.T) {
|
||||||
got := maskSecret(test.secret, test.percent)
|
got := maskSecret(test.secret, test.percent)
|
||||||
if got != test.expect {
|
assert.Equal(t, test.expect, got)
|
||||||
t.Error("redact not redacting: ", got)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+18
-31
@@ -1,11 +1,12 @@
|
|||||||
package report
|
package report
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestWriteJSON(t *testing.T) {
|
func TestWriteJSON(t *testing.T) {
|
||||||
@@ -47,35 +48,21 @@ func TestWriteJSON(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
tmpfile, err := os.Create(filepath.Join(t.TempDir(), test.testReportName+".json"))
|
t.Run(test.testReportName, func(t *testing.T) {
|
||||||
if err != nil {
|
tmpfile, err := os.Create(filepath.Join(t.TempDir(), test.testReportName+".json"))
|
||||||
t.Error(err)
|
require.NoError(t, err)
|
||||||
}
|
err = writeJson(test.findings, tmpfile)
|
||||||
err = writeJson(test.findings, tmpfile)
|
require.NoError(t, err)
|
||||||
if err != nil {
|
assert.FileExists(t, tmpfile.Name())
|
||||||
t.Error(err)
|
got, err := os.ReadFile(tmpfile.Name())
|
||||||
}
|
require.NoError(t, err)
|
||||||
got, err := os.ReadFile(tmpfile.Name())
|
if test.wantEmpty {
|
||||||
if err != nil {
|
assert.Empty(t, got)
|
||||||
t.Error(err)
|
return
|
||||||
}
|
|
||||||
if test.wantEmpty {
|
|
||||||
if len(got) > 0 {
|
|
||||||
t.Errorf("Expected empty file, got %s", got)
|
|
||||||
}
|
}
|
||||||
continue
|
want, err := os.ReadFile(test.expected)
|
||||||
}
|
require.NoError(t, err)
|
||||||
want, err := os.ReadFile(test.expected)
|
assert.Equal(t, want, got)
|
||||||
if err != nil {
|
})
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !bytes.Equal(got, want) {
|
|
||||||
err = os.WriteFile(strings.Replace(test.expected, ".json", ".got.json", 1), got, 0644)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
t.Errorf("got %s, want %s", string(got), string(want))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+11
-26
@@ -3,8 +3,10 @@ package report
|
|||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestWriteJunit(t *testing.T) {
|
func TestWriteJunit(t *testing.T) {
|
||||||
@@ -64,36 +66,19 @@ func TestWriteJunit(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
// create tmp file using os.TempDir()
|
|
||||||
tmpfile, err := os.Create(filepath.Join(t.TempDir(), test.testReportName+".xml"))
|
tmpfile, err := os.Create(filepath.Join(t.TempDir(), test.testReportName+".xml"))
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
err = writeJunit(test.findings, tmpfile)
|
err = writeJunit(test.findings, tmpfile)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
assert.FileExists(t, tmpfile.Name())
|
||||||
}
|
|
||||||
got, err := os.ReadFile(tmpfile.Name())
|
got, err := os.ReadFile(tmpfile.Name())
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if test.wantEmpty {
|
if test.wantEmpty {
|
||||||
if len(got) > 0 {
|
assert.Empty(t, got)
|
||||||
t.Errorf("Expected empty file, got %s", got)
|
return
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
want, err := os.ReadFile(test.expected)
|
want, err := os.ReadFile(test.expected)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
assert.Equal(t, want, got)
|
||||||
}
|
|
||||||
|
|
||||||
if string(got) != string(want) {
|
|
||||||
err = os.WriteFile(strings.Replace(test.expected, ".xml", ".got.xml", 1), got, 0644)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
t.Errorf("got %s, want %s", string(got), string(want))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+16
-22
@@ -6,6 +6,9 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/zricethezav/gitleaks/v8/config"
|
"github.com/zricethezav/gitleaks/v8/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -95,28 +98,19 @@ func TestReport(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for i, test := range tests {
|
for i, test := range tests {
|
||||||
tmpfile, err := os.Create(filepath.Join(t.TempDir(), strconv.Itoa(i)+test.ext))
|
t.Run(test.ext, func(t *testing.T) {
|
||||||
if err != nil {
|
tmpfile, err := os.Create(filepath.Join(t.TempDir(), strconv.Itoa(i)+test.ext))
|
||||||
t.Error(err)
|
require.NoError(t, err)
|
||||||
}
|
err = Write(test.findings, config.Config{}, test.ext, tmpfile.Name())
|
||||||
err = Write(test.findings, config.Config{}, test.ext, tmpfile.Name())
|
require.NoError(t, err)
|
||||||
if err != nil {
|
got, err := os.ReadFile(tmpfile.Name())
|
||||||
t.Error(err)
|
require.NoError(t, err)
|
||||||
}
|
assert.FileExists(t, tmpfile.Name())
|
||||||
got, err := os.ReadFile(tmpfile.Name())
|
if test.wantEmpty {
|
||||||
if err != nil {
|
assert.Empty(t, got)
|
||||||
t.Error(err)
|
return
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
continue
|
assert.NotEmpty(t, got)
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+29
-51
@@ -1,14 +1,14 @@
|
|||||||
package report
|
package report
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"fmt"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/zricethezav/gitleaks/v8/config"
|
"github.com/zricethezav/gitleaks/v8/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -49,56 +49,34 @@ func TestWriteSarif(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
tmpfile, err := os.Create(filepath.Join(t.TempDir(), test.testReportName+".json"))
|
t.Run(test.cfgName, func(t *testing.T) {
|
||||||
if err != nil {
|
tmpfile, err := os.Create(filepath.Join(t.TempDir(), test.testReportName+".json"))
|
||||||
t.Error(err)
|
require.NoError(t, err)
|
||||||
}
|
viper.Reset()
|
||||||
viper.Reset()
|
viper.AddConfigPath(configPath)
|
||||||
viper.AddConfigPath(configPath)
|
viper.SetConfigName(test.cfgName)
|
||||||
viper.SetConfigName(test.cfgName)
|
viper.SetConfigType("toml")
|
||||||
viper.SetConfigType("toml")
|
err = viper.ReadInConfig()
|
||||||
err = viper.ReadInConfig()
|
require.NoError(t, err)
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
var vc config.ViperConfig
|
var vc config.ViperConfig
|
||||||
err = viper.Unmarshal(&vc)
|
err = viper.Unmarshal(&vc)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
cfg, err := vc.Translate()
|
cfg, err := vc.Translate()
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Error(err)
|
err = writeSarif(cfg, test.findings, tmpfile)
|
||||||
}
|
require.NoError(t, err)
|
||||||
err = writeSarif(cfg, test.findings, tmpfile)
|
assert.FileExists(t, tmpfile.Name())
|
||||||
fmt.Println(cfg)
|
got, err := os.ReadFile(tmpfile.Name())
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Error(err)
|
if test.wantEmpty {
|
||||||
}
|
assert.Empty(t, got)
|
||||||
got, err := os.ReadFile(tmpfile.Name())
|
return
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
if test.wantEmpty {
|
|
||||||
if len(got) > 0 {
|
|
||||||
t.Errorf("Expected empty file, got %s", got)
|
|
||||||
}
|
}
|
||||||
continue
|
want, err := os.ReadFile(test.expected)
|
||||||
}
|
require.NoError(t, err)
|
||||||
want, err := os.ReadFile(test.expected)
|
assert.Equal(t, want, got)
|
||||||
if err != nil {
|
})
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !bytes.Equal(got, want) {
|
|
||||||
err = os.WriteFile(strings.Replace(test.expected, ".sarif", ".got.sarif", 1), got, 0644)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
t.Errorf("got %s, want %s", string(got), string(want))
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user