Skip to content

Commit

Permalink
Hide some defers and tests from linter
Browse files Browse the repository at this point in the history
  • Loading branch information
filip-debricked committed Nov 20, 2024
1 parent 26863d8 commit 5de834c
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 26 deletions.
1 change: 1 addition & 0 deletions internal/callgraph/language/java/callgraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func (cg *Callgraph) javaVersion(path string) (string, error) {
if len(match) > 1 {
return match[1], nil
}

return "", err
}

Expand Down
5 changes: 2 additions & 3 deletions internal/callgraph/language/java/cmd_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"github.com/stretchr/testify/assert"
)

const jarPath = "jarpath"

func TestMakeMvnCopyDependenciesCmd(t *testing.T) {
targetDir := "target"
ctx, _ := ctxTestdata.NewContextMock()
Expand All @@ -21,7 +23,6 @@ func TestMakeMvnCopyDependenciesCmd(t *testing.T) {
}

func TestMakeCallGraphGenerationCmd(t *testing.T) {
jarPath := "jarpath"
targetClasses := []string{"targetclasses"}
dependencyClasses := "dependencypath"
outputName := ".outputName"
Expand All @@ -40,7 +41,6 @@ func TestMakeCallGraphGenerationCmd(t *testing.T) {
}

func TestMakeBuildMavenCmd(t *testing.T) {
jarPath := "jarpath"
ctx, _ := ctxTestdata.NewContextMock()
cmd, err := CmdFactory{}.MakeBuildMavenCmd(jarPath, ctx)

Expand All @@ -54,7 +54,6 @@ func TestMakeBuildMavenCmd(t *testing.T) {
}

func TestMakeJavaVersionCmd(t *testing.T) {
jarPath := "jarpath"
ctx, _ := ctxTestdata.NewContextMock()
cmd, err := CmdFactory{}.MakeJavaVersionCmd(jarPath, ctx)

Expand Down
9 changes: 7 additions & 2 deletions internal/callgraph/language/java/soot_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,15 @@ func downloadSootWrapper(arc ioFs.IArchive, fs ioFs.IFileSystem, path string, ve

return err
}

zipPath := dir + "/soot_wrapper.zip"
zipFile, err := fs.Create(zipPath)
defer zipFile.Close()
if err != nil {

return err
}
defer zipFile.Close()

err = downloadCompressedSootWrapper(fs, zipFile, version)
if err != nil {

Expand All @@ -78,6 +80,7 @@ func downloadCompressedSootWrapper(fs ioFs.IFileSystem, zipFile *os.File, versio
client := http.Client{
CheckRedirect: func(r *http.Request, via []*http.Request) error {
r.URL.Opaque = r.URL.Path

return nil
},
}
Expand All @@ -89,8 +92,8 @@ func downloadCompressedSootWrapper(fs ioFs.IFileSystem, zipFile *os.File, versio
defer resp.Body.Close()

_, err = fs.Copy(zipFile, resp.Body)
return err

return err
}

func (sh SootHandler) GetSootWrapper(version string, fs ioFs.IFileSystem, arc ioFs.IArchive) (string, error) {
Expand All @@ -110,6 +113,7 @@ func (sh SootHandler) GetSootWrapper(version string, fs ioFs.IFileSystem, arc io
}
path, err := filepath.Abs(path.Join(debrickedDir, "soot-wrapper.jar"))
if err != nil {

return "", err
}
if _, err := fs.Stat(path); fs.IsNotExist(err) {
Expand All @@ -121,6 +125,7 @@ func (sh SootHandler) GetSootWrapper(version string, fs ioFs.IFileSystem, arc io
} else {
version = "11"
} // Handling correct jar to install

return path, downloadSootWrapper(arc, fs, path, version)
}

Expand Down
14 changes: 7 additions & 7 deletions internal/callgraph/language/java/soot_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestInitializeSootWrapper(t *testing.T) {

func TestInitializeSootWrapperOpenEmbedError(t *testing.T) {
errString := "fs open embed error"
fsMock := ioTestData.FileSystemMock{FsOpenEmbedError: fmt.Errorf(errString)}
fsMock := ioTestData.FileSystemMock{FsOpenEmbedError: fmt.Errorf(errString)} //nolint
tempDir, err := fsMock.MkdirTemp(".tmp")
assert.NoError(t, err)
_, err = initializeSootWrapper(fsMock, tempDir)
Expand All @@ -31,7 +31,7 @@ func TestInitializeSootWrapperOpenEmbedError(t *testing.T) {

func TestInitializeSootWrapperFsReadAllError(t *testing.T) {
errString := "fs read all error"
fsMock := ioTestData.FileSystemMock{FsReadAllError: fmt.Errorf(errString)}
fsMock := ioTestData.FileSystemMock{FsReadAllError: fmt.Errorf(errString)} //nolint
tempDir, err := fsMock.MkdirTemp(".tmp")
assert.NoError(t, err)
_, err = initializeSootWrapper(fsMock, tempDir)
Expand All @@ -41,7 +41,7 @@ func TestInitializeSootWrapperFsReadAllError(t *testing.T) {

func TestInitializeSootWrapperFsWriteFileError(t *testing.T) {
errString := "fs write file error"
fsMock := ioTestData.FileSystemMock{FsWriteFileError: fmt.Errorf(errString)}
fsMock := ioTestData.FileSystemMock{FsWriteFileError: fmt.Errorf(errString)} //nolint
tempDir, err := fsMock.MkdirTemp(".tmp")
assert.NoError(t, err)
_, err = initializeSootWrapper(fsMock, tempDir)
Expand All @@ -58,7 +58,7 @@ func TestDownloadSootWrapper(t *testing.T) {

func TestDownloadSootWrapperMkdirTempError(t *testing.T) {
errString := "mkdir temp error"
fsMock := ioTestData.FileSystemMock{MkdirTempError: fmt.Errorf(errString)}
fsMock := ioTestData.FileSystemMock{MkdirTempError: fmt.Errorf(errString)} //nolint
arcMock := ioTestData.ArchiveMock{}
err := downloadSootWrapper(arcMock, fsMock, "soot-wrapper.jar", "11")
assert.Error(t, err)
Expand All @@ -67,7 +67,7 @@ func TestDownloadSootWrapperMkdirTempError(t *testing.T) {

func TestDownloadSootWrapperCreateError(t *testing.T) {
errString := "create error"
fsMock := ioTestData.FileSystemMock{CreateError: fmt.Errorf(errString)}
fsMock := ioTestData.FileSystemMock{CreateError: fmt.Errorf(errString)} //nolint
arcMock := ioTestData.ArchiveMock{}
err := downloadSootWrapper(arcMock, fsMock, "soot-wrapper.jar", "11")
assert.Error(t, err)
Expand All @@ -77,7 +77,7 @@ func TestDownloadSootWrapperCreateError(t *testing.T) {
func TestDownloadSootWrapperUnzipError(t *testing.T) {
errString := "create error"
fsMock := ioTestData.FileSystemMock{}
arcMock := ioTestData.ArchiveMock{UnzipFileError: fmt.Errorf(errString)}
arcMock := ioTestData.ArchiveMock{UnzipFileError: fmt.Errorf(errString)} //nolint
err := downloadSootWrapper(arcMock, fsMock, "soot-wrapper.jar", "11")
assert.Error(t, err)
assert.Equal(t, err.Error(), errString)
Expand Down Expand Up @@ -165,7 +165,7 @@ func TestGetSootWrapperInitialize(t *testing.T) {

func TestGetSootWrapperMkdirError(t *testing.T) {
errString := "mkdir error"
fsMock := ioTestData.FileSystemMock{MkdirError: fmt.Errorf(errString), StatError: fmt.Errorf(""), IsNotExistBool: true}
fsMock := ioTestData.FileSystemMock{MkdirError: fmt.Errorf(errString), StatError: fmt.Errorf(""), IsNotExistBool: true} //nolint
arcMock := ioTestData.ArchiveMock{}
sootHandler := SootHandler{}
_, err := sootHandler.GetSootWrapper("11", fsMock, arcMock)
Expand Down
9 changes: 4 additions & 5 deletions internal/io/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package io

import (
"encoding/base64"
"fmt"
"path"
)

Expand Down Expand Up @@ -87,23 +86,23 @@ func (arc *Archive) UnzipFile(sourcePath string, targetPath string) error {

return err
}
defer arc.zip.CloseReader(r)
defer arc.zip.CloseReader(r) //nolint

f := r.File[1] //TODO: Change to first file and error-check for multiple once sootwrapper builds only one
fmt.Println("94")
outFile, err := arc.fs.Create(targetPath)
if err != nil {
return err
}
defer outFile.Close()
defer outFile.Close() //nolint

rc, err := arc.zip.Open(f)
if err != nil {
return err
}
defer arc.zip.Close(rc)
defer arc.zip.Close(rc) //nolint

_, err = arc.fs.Copy(outFile, rc)

return err
}

Expand Down
6 changes: 3 additions & 3 deletions internal/io/archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func TestUnzipFileCreateError(t *testing.T) {
reader := zip.Reader{
File: []*zip.File{nil, nil},
}
readCloser := zip.ReadCloser{Reader: reader}
readCloser := zip.ReadCloser{Reader: reader} //nolint
fsMock := ioTestData.FileSystemMock{CreateError: fmt.Errorf("%s", t.Name())}
zipMock := ioTestData.ZipMock{ReaderCloser: &readCloser}
a := Archive{
Expand All @@ -225,7 +225,7 @@ func TestUnzipFileOpenError(t *testing.T) {
reader := zip.Reader{
File: []*zip.File{nil, nil},
}
readCloser := zip.ReadCloser{Reader: reader}
readCloser := zip.ReadCloser{Reader: reader} //nolint
fsMock := ioTestData.FileSystemMock{}
zipMock := ioTestData.ZipMock{ReaderCloser: &readCloser, OpenError: fmt.Errorf("%s", t.Name())}
a := Archive{
Expand All @@ -242,7 +242,7 @@ func TestUnzipFileCopyError(t *testing.T) {
r, err := zipStruct.OpenReader("testdata/text.zip")
r.File = append(r.File, r.File[0]) // Hack solution (:
assert.NoError(t, err)
defer zipStruct.CloseReader(r)
defer zipStruct.CloseReader(r) //nolint

fsMock := ioTestData.FileSystemMock{CopyError: fmt.Errorf("%s", t.Name())}
zipMock := ioTestData.ZipMock{ReaderCloser: r}
Expand Down
12 changes: 6 additions & 6 deletions internal/io/zip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const fileNameZip = "debricked-test.zip"

func TestNewWriter(t *testing.T) {
testFile, err := filesystem.Create(fileNameZip)
defer deleteFile(t, testFile)
defer deleteFile(t, testFile) //nolint
assert.NoError(t, err)

writer := zipStruct.NewWriter(testFile)
Expand All @@ -26,7 +26,7 @@ func TestFileInfoHeader(t *testing.T) {
assert.NoError(t, err)

writer := zipStruct.NewWriter(testFile)
defer zipStruct.CloseWriter(writer)
defer zipStruct.CloseWriter(writer) //nolint

info, _ := filesystem.StatFile(testFile)
_, err = zipStruct.FileInfoHeader(info)
Expand All @@ -36,11 +36,11 @@ func TestFileInfoHeader(t *testing.T) {

func TestCreateHeader(t *testing.T) {
testFile, err := filesystem.Create(fileNameZip)
defer deleteFile(t, testFile)
defer deleteFile(t, testFile) //nolint
assert.NoError(t, err)

writer := zipStruct.NewWriter(testFile)
defer zipStruct.CloseWriter(writer)
defer zipStruct.CloseWriter(writer) //nolint

info, _ := filesystem.StatFile(testFile)
header, _ := zipStruct.FileInfoHeader(info)
Expand All @@ -57,7 +57,7 @@ func TestDeflate(t *testing.T) {
func TestOpenZip(t *testing.T) {
r, err := zipStruct.OpenReader("testdata/text.zip")
assert.NoError(t, err)
defer zipStruct.CloseReader(r)
defer zipStruct.CloseReader(r) //nolint

assert.NotNil(t, r, "reader not opened properly")
_, err = zipStruct.Open(r.File[0])
Expand All @@ -67,7 +67,7 @@ func TestOpenZip(t *testing.T) {
func TestCloseZip(t *testing.T) {
r, err := zipStruct.OpenReader("testdata/text.zip")
assert.NoError(t, err)
defer zipStruct.CloseReader(r)
defer zipStruct.CloseReader(r) //nolint

assert.NotNil(t, r, "reader not opened properly")
rc, err := zipStruct.Open(r.File[0])
Expand Down

0 comments on commit 5de834c

Please sign in to comment.