From e4ffebaaa167b34bd5bacdad5212155801519fda Mon Sep 17 00:00:00 2001 From: AntonDebricked Date: Thu, 1 Jun 2023 11:11:05 +0200 Subject: [PATCH] Fix an issue where the zipping tool was looking in root instead of the real location of the call graph. --- internal/callgraph/language/java11/job.go | 10 +++++---- .../callgraph/language/java11/job_test.go | 21 ++++++++++++++++++- internal/io/testdata/archive_mock.go | 7 +++++++ 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/internal/callgraph/language/java11/job.go b/internal/callgraph/language/java11/job.go index 1ab7d981..5b749ecb 100644 --- a/internal/callgraph/language/java11/job.go +++ b/internal/callgraph/language/java11/job.go @@ -99,9 +99,11 @@ func (j *Job) runCallGraph(callgraph ICallgraph) { } func (j *Job) runPostProcess() { - outputNameZip := outputName + ".zip" + workingDirectory := j.GetDir() + outputFullPath := path.Join(workingDirectory, outputName) + outputFullPathZip := outputFullPath + ".zip" j.SendStatus("zipping callgraph") - err := j.archive.ZipFile(outputName, outputNameZip) + err := j.archive.ZipFile(outputFullPath, outputFullPathZip) if err != nil { j.Errors().Critical(err) @@ -109,7 +111,7 @@ func (j *Job) runPostProcess() { } j.SendStatus("base64 encoding zipped callgraph") - err = j.archive.B64(outputNameZip, outputName) + err = j.archive.B64(outputFullPathZip, outputFullPath) if err != nil { j.Errors().Critical(err) @@ -117,7 +119,7 @@ func (j *Job) runPostProcess() { } j.SendStatus("cleanup") - err = j.archive.Cleanup(outputNameZip) + err = j.archive.Cleanup(outputFullPathZip) if err != nil { e, ok := err.(*os.PathError) if ok && e.Err == syscall.ENOENT { diff --git a/internal/callgraph/language/java11/job_test.go b/internal/callgraph/language/java11/job_test.go index cdf98d7e..6d86da4f 100644 --- a/internal/callgraph/language/java11/job_test.go +++ b/internal/callgraph/language/java11/job_test.go @@ -121,7 +121,7 @@ func TestRunPostProcessMock(t *testing.T) { fs := ioTestData.FileSystemMock{} zip := ioTestData.ZipMock{} - archiveMock := io.NewArchiveWithStructs("dir", fs, zip) + archiveMock := io.NewArchiveWithStructs(dir, fs, zip) j := NewJob(dir, files, cmdFactoryMock, fileWriterMock, archiveMock, config, ctx) go jobTestdata.WaitStatus(j) @@ -190,3 +190,22 @@ func TestRunPostProcessCleanupNoFileExistError(t *testing.T) { assert.False(t, j.Errors().HasError()) } + +func TestRunPostProcessFromRoot(t *testing.T) { + fileWriterMock := &ioTestData.FileWriterMock{} + cmdFactoryMock := testdata.NewEchoCmdFactory() + config := conf.NewConfig("java", nil, map[string]string{"pm": maven}, true, "maven") + ctx, _ := ctxTestdata.NewContextMock() + + err := &os.PathError{} + err.Err = syscall.ENOENT + archiveMock := ioTestData.ArchiveMock{PathError: err, Dir: "."} + + j := NewJob(dir, files, cmdFactoryMock, fileWriterMock, archiveMock, config, ctx) + go jobTestdata.WaitStatus(j) + j.runPostProcess() + + jobErrors := j.Errors().GetAll() + assert.True(t, jobErrors[0] == err) + +} diff --git a/internal/io/testdata/archive_mock.go b/internal/io/testdata/archive_mock.go index 001dac03..fe8517ec 100644 --- a/internal/io/testdata/archive_mock.go +++ b/internal/io/testdata/archive_mock.go @@ -1,12 +1,19 @@ package testdata +import "strings" + type ArchiveMock struct { ZipFileError error B64Error error CleanupError error + PathError error + Dir string } func (am ArchiveMock) ZipFile(sourceName string, targetName string) error { + if !strings.HasPrefix(sourceName, am.Dir) || !strings.HasPrefix(targetName, am.Dir) { + return am.PathError + } return am.ZipFileError }