Skip to content

Commit

Permalink
Addressed: No error on tmp or out not found on clean
Browse files Browse the repository at this point in the history
  • Loading branch information
soumeh01 committed Dec 4, 2024
1 parent 252da79 commit 3370ad5
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 51 deletions.
12 changes: 8 additions & 4 deletions pkg/builder/csolution/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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())
}
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions pkg/errutils/errutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
22 changes: 5 additions & 17 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
42 changes: 15 additions & 27 deletions pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
}
Expand All @@ -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)
Expand All @@ -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)
}
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 3370ad5

Please sign in to comment.