Skip to content

Commit

Permalink
fix: enable thelper linter
Browse files Browse the repository at this point in the history
Signed-off-by: Matthieu MOREL <[email protected]>
  • Loading branch information
mmorel-35 committed Oct 18, 2024
1 parent 35fd018 commit de1432a
Show file tree
Hide file tree
Showing 32 changed files with 96 additions and 5 deletions.
1 change: 1 addition & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ linters:
- revive
- tenv
- testifylint
- thelper
- typecheck
- unconvert
- unused
Expand Down
2 changes: 2 additions & 0 deletions internal/dbtest/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

// InitDB initializes testing database.
func InitDB(t *testing.T, fixtureFiles []string) string {
t.Helper()
// Create a temp dir
cacheDir := t.TempDir()

Expand All @@ -41,6 +42,7 @@ func Close() error {
}

func InitJavaDB(t *testing.T, cacheDir string) {
t.Helper()
dbDir := filepath.Join(cacheDir, "java-db")
javaDB, err := jdb.New(dbDir)
require.NoError(t, err)
Expand Down
3 changes: 3 additions & 0 deletions internal/dbtest/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func (f fakeLayer) MediaType() (types.MediaType, error) {
}

func NewFakeLayer(t *testing.T, input string, mediaType types.MediaType) v1.Layer {
t.Helper()
layer, err := tarball.LayerFromFile(input, tarball.WithMediaType(mediaType))
require.NoError(t, err)

Expand All @@ -39,6 +40,7 @@ type FakeDBOptions struct {
}

func NewFakeDB(t *testing.T, dbPath string, opts FakeDBOptions) *oci.Artifact {
t.Helper()
mediaType := lo.Ternary(opts.MediaType != "", opts.MediaType, defaultMediaType)
img := new(fakei.FakeImage)
img.LayersReturns([]v1.Layer{NewFakeLayer(t, dbPath, mediaType)}, nil)
Expand Down Expand Up @@ -66,6 +68,7 @@ func NewFakeDB(t *testing.T, dbPath string, opts FakeDBOptions) *oci.Artifact {
}

func ArchiveDir(t *testing.T, dir string) string {
t.Helper()
tmpDBPath := filepath.Join(t.TempDir(), "db.tar")
f, err := os.Create(tmpDBPath)
require.NoError(t, err)
Expand Down
6 changes: 6 additions & 0 deletions internal/gittest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var signature = &object.Signature{
}

func NewServer(t *testing.T, repo, dir string) *httptest.Server {
t.Helper()
wtDir := t.TempDir()

// git init
Expand Down Expand Up @@ -60,6 +61,7 @@ func NewServer(t *testing.T, repo, dir string) *httptest.Server {
}

func Clone(t *testing.T, ts *httptest.Server, repo, worktree string) *git.Repository {
t.Helper()
cloneOptions := git.CloneOptions{
URL: ts.URL + "/" + repo + ".git",
}
Expand All @@ -71,6 +73,7 @@ func Clone(t *testing.T, ts *httptest.Server, repo, worktree string) *git.Reposi
}

func CommitAll(t *testing.T, r *git.Repository, msg string) {
t.Helper()
w, err := r.Worktree()
require.NoError(t, err)

Expand All @@ -84,6 +87,7 @@ func CommitAll(t *testing.T, r *git.Repository, msg string) {
}

func SetTag(t *testing.T, r *git.Repository, tag string) {
t.Helper()
h, err := r.Head()
require.NoError(t, err)

Expand All @@ -96,6 +100,7 @@ func SetTag(t *testing.T, r *git.Repository, tag string) {
}

func PushTags(t *testing.T, r *git.Repository) {
t.Helper()
t.Log("git push --tags")
err := r.Push(&git.PushOptions{
RemoteName: "origin",
Expand All @@ -111,6 +116,7 @@ func PushTags(t *testing.T, r *git.Repository) {
}

func CreateRemoteBranch(t *testing.T, r *git.Repository, branchName string) {
t.Helper()
wt, err := r.Worktree()
require.NoError(t, err)

Expand Down
1 change: 1 addition & 0 deletions internal/testutil/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type DockerClient struct {
}

func NewDockerClient(t *testing.T) *DockerClient {
t.Helper()
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
require.NoError(t, err)
return &DockerClient{Client: cli}
Expand Down
6 changes: 6 additions & 0 deletions internal/testutil/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
)

func CopyFile(t *testing.T, src, dst string) {
t.Helper()
MustMkdirAll(t, filepath.Dir(dst))

_, err := fsutils.CopyFile(src, dst)
Expand All @@ -22,6 +23,7 @@ func CopyFile(t *testing.T, src, dst string) {
// CopyDir copies the directory content from src to dst.
// It supports only simple cases for testing.
func CopyDir(t *testing.T, src, dst string) {
t.Helper()
srcInfo, err := os.Stat(src)
require.NoError(t, err)

Expand Down Expand Up @@ -65,25 +67,29 @@ func MustReadYAML(t *testing.T, path string, out any) {
}

func MustMkdirAll(t *testing.T, dir string) {
t.Helper()
err := os.MkdirAll(dir, 0750)
require.NoError(t, err)
}

func MustReadJSON(t *testing.T, filePath string, v any) {
t.Helper()
b, err := os.ReadFile(filePath)
require.NoError(t, err)
err = json.Unmarshal(b, v)
require.NoError(t, err)
}

func MustWriteJSON(t *testing.T, filePath string, v any) {
t.Helper()
data, err := json.Marshal(v)
require.NoError(t, err)

MustWriteFile(t, filePath, data)
}

func MustWriteFile(t *testing.T, filePath string, content []byte) {
t.Helper()
dir := filepath.Dir(filePath)
MustMkdirAll(t, dir)

Expand Down
2 changes: 2 additions & 0 deletions internal/testutil/gzip.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const (
)

func DecompressGzip(t *testing.T, src, dst string) {
t.Helper()
w, err := os.Create(dst)
require.NoError(t, err)
defer w.Close()
Expand All @@ -33,6 +34,7 @@ func DecompressGzip(t *testing.T, src, dst string) {

// DecompressSparseGzip decompresses a sparse gzip file for virtual machine image.
func DecompressSparseGzip(t *testing.T, src, dst string) {
t.Helper()
w, err := os.Create(dst)
require.NoError(t, err)
defer w.Close()
Expand Down
4 changes: 4 additions & 0 deletions internal/testutil/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
)

func AssertRuleFound(t *testing.T, ruleID string, results scan.Results, message string, args ...any) {
t.Helper()
found := ruleIDInResults(ruleID, results.GetFailed())
assert.True(t, found, append([]any{message}, args...)...)
for _, result := range results.GetFailed() {
Expand All @@ -32,6 +33,7 @@ func AssertRuleFound(t *testing.T, ruleID string, results scan.Results, message
}

func AssertRuleNotFound(t *testing.T, ruleID string, results scan.Results, message string, args ...any) {
t.Helper()
found := ruleIDInResults(ruleID, results.GetFailed())
assert.False(t, found, append([]any{message}, args...)...)
}
Expand All @@ -46,6 +48,7 @@ func ruleIDInResults(ruleID string, results scan.Results) bool {
}

func CreateFS(t *testing.T, files map[string]string) fs.FS {
t.Helper()
memfs := memoryfs.New()
for name, content := range files {
name := strings.TrimPrefix(name, "/")
Expand All @@ -58,6 +61,7 @@ func CreateFS(t *testing.T, files map[string]string) fs.FS {
}

func AssertDefsecEqual(t *testing.T, expected, actual any) {
t.Helper()
expectedJson, err := json.MarshalIndent(expected, "", "\t")
require.NoError(t, err)
actualJson, err := json.MarshalIndent(actual, "", "\t")
Expand Down
1 change: 1 addition & 0 deletions pkg/cache/fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
)

func newTempDB(t *testing.T, dbPath string) (string, error) {
t.Helper()
dir := t.TempDir()
if dbPath != "" {
d := filepath.Join(dir, "fanal")
Expand Down
7 changes: 7 additions & 0 deletions pkg/commands/clean/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func TestRun(t *testing.T) {
},
wantErr: false,
checkFunc: func(t *testing.T, dir string) {
t.Helper()
assert.NoDirExists(t, filepath.Join(dir, "fanal"))
assert.NoDirExists(t, filepath.Join(dir, "db"))
assert.NoDirExists(t, filepath.Join(dir, "java-db"))
Expand All @@ -43,6 +44,7 @@ func TestRun(t *testing.T) {
},
wantErr: false,
checkFunc: func(t *testing.T, dir string) {
t.Helper()
assert.NoDirExists(t, filepath.Join(dir, "fanal"))
assert.DirExists(t, filepath.Join(dir, "db"))
assert.DirExists(t, filepath.Join(dir, "java-db"))
Expand All @@ -57,6 +59,7 @@ func TestRun(t *testing.T) {
},
wantErr: false,
checkFunc: func(t *testing.T, dir string) {
t.Helper()
assert.NoDirExists(t, filepath.Join(dir, "db"))
assert.DirExists(t, filepath.Join(dir, "fanal"))
assert.DirExists(t, filepath.Join(dir, "java-db"))
Expand All @@ -71,6 +74,7 @@ func TestRun(t *testing.T) {
},
wantErr: false,
checkFunc: func(t *testing.T, dir string) {
t.Helper()
assert.NoDirExists(t, filepath.Join(dir, "java-db"))
assert.DirExists(t, filepath.Join(dir, "fanal"))
assert.DirExists(t, filepath.Join(dir, "db"))
Expand All @@ -85,6 +89,7 @@ func TestRun(t *testing.T) {
},
wantErr: false,
checkFunc: func(t *testing.T, dir string) {
t.Helper()
assert.NoDirExists(t, filepath.Join(dir, "policy"))
assert.DirExists(t, filepath.Join(dir, "fanal"))
assert.DirExists(t, filepath.Join(dir, "db"))
Expand All @@ -99,6 +104,7 @@ func TestRun(t *testing.T) {
},
wantErr: false,
checkFunc: func(t *testing.T, dir string) {
t.Helper()
assert.DirExists(t, filepath.Join(dir, "policy"))
assert.DirExists(t, filepath.Join(dir, "fanal"))
assert.DirExists(t, filepath.Join(dir, "db"))
Expand Down Expand Up @@ -145,6 +151,7 @@ func TestRun(t *testing.T) {
}

func createTestFiles(t *testing.T, dir string) {
t.Helper()
subdirs := []string{
"fanal",
"db",
Expand Down
1 change: 1 addition & 0 deletions pkg/fanal/artifact/repo/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
)

func setupGitRepository(t *testing.T, repo, dir string) (*httptest.Server, *git.Repository) {
t.Helper()
gs := gittest.NewServer(t, repo, dir)

worktree := t.TempDir()
Expand Down
3 changes: 3 additions & 0 deletions pkg/fanal/image/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
)

func setupEngineAndRegistry(t *testing.T) (*httptest.Server, *httptest.Server) {
t.Helper()
imagePaths := map[string]string{
"alpine:3.10": "../test/testdata/alpine-310.tar.gz",
"alpine:3.11": "../test/testdata/alpine-311.tar.gz",
Expand Down Expand Up @@ -302,6 +303,7 @@ func TestNewDockerImage(t *testing.T) {
}

func setupPrivateRegistry(t *testing.T) *httptest.Server {
t.Helper()
images := map[string]v1.Image{
"v2/library/alpine:3.10": localImage(t),
}
Expand Down Expand Up @@ -557,6 +559,7 @@ func TestDockerPlatformArguments(t *testing.T) {
}

func localImage(t *testing.T) v1.Image {
t.Helper()
img, err := tarfile.ImageFromPath("../test/testdata/alpine-310.tar.gz")
require.NoError(t, err)
return img
Expand Down
1 change: 1 addition & 0 deletions pkg/iac/adapters/cloudformation/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
type adaptFn[T any] func(fctx parser.FileContext) T

func AdaptAndCompare[T any](t *testing.T, source string, expected any, fn adaptFn[T]) {
t.Helper()
fsys := testutil.CreateFS(t, map[string]string{
"main.yaml": source,
})
Expand Down
1 change: 1 addition & 0 deletions pkg/iac/adapters/terraform/tftestutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
)

func CreateModulesFromSource(t *testing.T, source, ext string) terraform.Modules {
t.Helper()
fs := testutil.CreateFS(t, map[string]string{
"source" + ext: source,
})
Expand Down
1 change: 1 addition & 0 deletions pkg/iac/rego/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
)

func CreateFS(t *testing.T, files map[string]string) fs.FS {
t.Helper()
memfs := memoryfs.New()
for name, content := range files {
name := strings.TrimPrefix(name, "/")
Expand Down
2 changes: 2 additions & 0 deletions pkg/iac/scanners/cloudformation/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
)

func parseFile(t *testing.T, source, name string) (FileContexts, error) {
t.Helper()
tmp, err := os.MkdirTemp(os.TempDir(), "defsec")
require.NoError(t, err)
defer func() { _ = os.RemoveAll(tmp) }()
Expand Down Expand Up @@ -174,6 +175,7 @@ Resources:
}

func createTestFileContext(t *testing.T, source string) *FileContext {
t.Helper()
contexts, err := parseFile(t, source, "main.yaml")
require.NoError(t, err)
require.Len(t, contexts, 1)
Expand Down
1 change: 1 addition & 0 deletions pkg/iac/scanners/kubernetes/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ spec:
}

func assertLines(t *testing.T, content string, results scan.Results) {
t.Helper()
lines := strings.Split(content, "\n")
for _, res := range results {
actualCode, err := res.GetCode()
Expand Down
1 change: 1 addition & 0 deletions pkg/iac/scanners/terraform/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1503,6 +1503,7 @@ resource "test_block" "this" {
}

func parse(t *testing.T, files map[string]string) terraform.Modules {
t.Helper()
fs := testutil.CreateFS(t, files)
parser := New(fs, "", OptionStopOnHCLError(true))
require.NoError(t, parser.ParseFS(context.TODO(), "."))
Expand Down
4 changes: 4 additions & 0 deletions pkg/iac/scanners/terraform/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
)

func createModulesFromSource(t *testing.T, source, ext string) terraform.Modules {
t.Helper()
fs := testutil.CreateFS(t, map[string]string{
"source" + ext: source,
})
Expand All @@ -31,10 +32,12 @@ func createModulesFromSource(t *testing.T, source, ext string) terraform.Modules
}

func scanHCLWithWorkspace(t *testing.T, source, workspace string) scan.Results {
t.Helper()
return scanHCL(t, source, ScannerWithWorkspaceName(workspace))
}

func scanHCL(t *testing.T, source string, opts ...options.ScannerOption) scan.Results {
t.Helper()

fs := testutil.CreateFS(t, map[string]string{
"main.tf": source,
Expand All @@ -47,6 +50,7 @@ func scanHCL(t *testing.T, source string, opts ...options.ScannerOption) scan.Re
}

func scanJSON(t *testing.T, source string) scan.Results {
t.Helper()

fs := testutil.CreateFS(t, map[string]string{
"main.tf.json": source,
Expand Down
1 change: 1 addition & 0 deletions pkg/log/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ func TestContext(t *testing.T) {
}

func compareLines(t *testing.T, got string, wantLines []string) {
t.Helper()
// Strip color codes from the output.
got = stripColorCodes(got)

Expand Down
Loading

0 comments on commit de1432a

Please sign in to comment.