diff --git a/pkg/builder/csolution/builder.go b/pkg/builder/csolution/builder.go index 6211e2d..22c359c 100644 --- a/pkg/builder/csolution/builder.go +++ b/pkg/builder/csolution/builder.go @@ -712,8 +712,10 @@ func (b CSolutionBuilder) Clean() (err error) { } // Clean tmp dir - if err := utils.DeleteDir(tmpDir); err != nil { - return err + if err := utils.DeleteAll(tmpDir); err != nil { + if !b.Options.Clean { + log.Warn(err.Error()) + } } // Clean out dir @@ -733,8 +735,10 @@ func (b CSolutionBuilder) Clean() (err error) { if err != nil { log.Error("error cleaning '" + context + "'") } - if err = utils.DeleteDir(outDir); err != nil { - return err + if err = utils.DeleteAll(outDir); err != nil { + if !b.Options.Clean { + log.Warn(err.Error()) + } } } } diff --git a/pkg/errutils/errutils.go b/pkg/errutils/errutils.go index 0633911..e647e44 100644 --- a/pkg/errutils/errutils.go +++ b/pkg/errutils/errutils.go @@ -29,10 +29,8 @@ const ( ErrInvalidVersionString = "invalid version %s. Expected %s" ErrInvalidNumJobs = "invalid number of job slots specified for parallel execution. Expected: j>0" ErrMissingRequiredArg = "setup command is missing mandatory option '--context-set'" + ErrDeleteFailed = "failed to delete: '%s'" ErrPathNotExist = "path does not exist: '%s'" - ErrDeleteDirFailed = "failed to delete directory: '%s', %s" - ErrInvalidPath = "invalid path: '%s', %s" - ErrNoPathInfoFound = "unable to retrieve information for path: '%s', %s" ) const ( diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 69106c5..114753d 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -501,27 +501,15 @@ func GetOutDir(cbuildIdxFile string, context string) (string, error) { return outDir, nil } -// DeleteDir deletes a directory and all its contents. -func DeleteDir(dirPath string) error { +func DeleteAll(path string) error { // Check if the path exists - info, err := os.Stat(dirPath) - if os.IsNotExist(err) { - return errutils.New(errutils.ErrPathNotExist, dirPath) - } - if err != nil { - return errutils.New(errutils.ErrNoPathInfoFound, dirPath, err.Error()) + if _, err := os.Stat(path); os.IsNotExist(err) { + return errutils.New(errutils.ErrPathNotExist, path) } - // Ensure the path is a directory - if !info.IsDir() { - return errutils.New(errutils.ErrInvalidPath, dirPath, "path is not a directory") - } - - // Remove the directory and its contents - err = os.RemoveAll(dirPath) + err := os.RemoveAll(path) if err != nil { - return errutils.New(errutils.ErrDeleteDirFailed, dirPath, err.Error()) + return errutils.New(errutils.ErrDeleteFailed, path) } - return nil } diff --git a/pkg/utils/utils_test.go b/pkg/utils/utils_test.go index 8e40368..9e38e95 100644 --- a/pkg/utils/utils_test.go +++ b/pkg/utils/utils_test.go @@ -468,9 +468,9 @@ func TestGetOutDir(t *testing.T) { }) } -func TestDeleteDir(t *testing.T) { +func TestDeleteAll(t *testing.T) { testDir := filepath.Join(testRoot, testDir) - t.Run("DeleteExistingDirectory", func(t *testing.T) { + t.Run("Delete Existing Directory", func(t *testing.T) { // Create a test directory with files and subdirectories delDir := filepath.Join(testDir, "test_dir") subDir := filepath.Join(delDir, "sub_dir") @@ -483,8 +483,8 @@ func TestDeleteDir(t *testing.T) { t.Fatalf("Test setup failed: %s does not exist", delDir) } - // Call the DeleteDir function - err := DeleteDir(delDir) + // Call the DeleteAll function + err := DeleteAll(delDir) if err != nil { t.Fatalf("Unexpected error: %v", err) } @@ -495,21 +495,16 @@ func TestDeleteDir(t *testing.T) { } }) - t.Run("DeleteNonExistentDirectory", func(t *testing.T) { + t.Run("Delete NonExistent Directory", func(t *testing.T) { // Test deleting a non-existent directory nonExistentDir := filepath.Join(testDir, "non_existent_dir") - err := DeleteDir(nonExistentDir) + err := DeleteAll(nonExistentDir) - // Verify the error is correct - if err == nil { - t.Fatalf("Expected an error but got none") - } - if !strings.Contains(err.Error(), "path does not exist") { - t.Fatalf("Unexpected error message: %v", err) - } + // Verify no error + assert.Error(t, err) }) - t.Run("DeleteEmptyDirectory", func(t *testing.T) { + t.Run("Delete Empty Directory", func(t *testing.T) { // Create an empty test directory emptyDir := filepath.Join(testDir, "empty_dir") _ = os.Mkdir(emptyDir, 0755) @@ -519,8 +514,8 @@ func TestDeleteDir(t *testing.T) { t.Fatalf("Test setup failed: %s does not exist", emptyDir) } - // Call the DeleteDir function - err := DeleteDir(emptyDir) + // Call the DeleteAll function + err := DeleteAll(emptyDir) if err != nil { t.Fatalf("Unexpected error: %v", err) } @@ -531,7 +526,7 @@ func TestDeleteDir(t *testing.T) { } }) - t.Run("DeleteFileInsteadOfDirectory", func(t *testing.T) { + t.Run("Delete File Instead Of Directory", func(t *testing.T) { // Create a test file testFile := filepath.Join(testDir, "test_file.txt") _ = os.WriteFile(testFile, []byte("test content"), 0600) @@ -541,16 +536,9 @@ func TestDeleteDir(t *testing.T) { t.Fatalf("Test setup failed: %s does not exist", testFile) } - // Call the DeleteDir function - err := DeleteDir(testFile) - if err == nil { - t.Fatalf("Expected an error but got none") - } - - // Verify the error message - if !strings.Contains(err.Error(), "test_file.txt', path is not a directory") { - t.Fatalf("Unexpected error message: %v", err) - } + // Call the DeleteAll function + err := DeleteAll(testFile) + assert.NoError(t, err) // Clean up os.Remove(testFile)