Skip to content

Commit

Permalink
refactor: Improve error handling in delete.go
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgaspardev committed Oct 23, 2024
1 parent 82cfe98 commit dddfb1c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 50 deletions.
22 changes: 12 additions & 10 deletions internal/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,42 @@ func DeleteApp(appName string) {
}

appFolderPath := getAppFolderPath(appName)
checkIfAppFolderExists(appFolderPath)
if exists := folderExists(appFolderPath); !exists {
return
}

// Delete the app folder
err := os.RemoveAll(appFolderPath)
if err != nil {
panic("Error deleting app folder: "+ err.Error())
if err := os.RemoveAll(appFolderPath); err != nil {
panic("Error deleting app folder: " + err.Error())
}

fmt.Printf("App deleted successfully: %s\n", appName)
}

func isAppNameValid(appName string) bool {
if appName == "app" {
fmt.Println("You can't delete the app folder, it's the example component")
fmt.Println("You can't delete the app folder: it's the example component")
return false
}

if appName == "bff" {
fmt.Println("You can't delete the bff folder, it's required to run the application")
fmt.Println("You can't delete the bff folder: it's required to run the application")
return false
}

return true
}

func checkIfAppFolderExists(pathToDelete string) {
func folderExists(pathToDelete string) bool {
if _, err := os.Stat(pathToDelete); err != nil {
if os.IsNotExist(err) {
fmt.Println("App folder not found")
} else {
fmt.Println("Error checking app folder: ", err)
return false
}
panic(0)

panic("Error checking app folder: " + err.Error())
}
return true
}

func getAppFolderPath(appName string) string {
Expand Down
65 changes: 25 additions & 40 deletions internal/delete_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package internal

import (
"bytes"
"io"
"os"
"path/filepath"
"testing"
Expand All @@ -14,85 +16,68 @@ func TestDeleteApp(t *testing.T) {
t.Log("tmpDir: ", tmpDir)

// Create a temporary app folder
err := os.MkdirAll(appFolderPath, 0755)
if err != nil {
if err := os.MkdirAll(appFolderPath, 0755); err != nil {
t.Fatalf("Failed to create app folder: %v", err)
}

// Change the working directory to the temporary directory
err = os.Chdir(tmpDir)
if err != nil {
if err := os.Chdir(tmpDir); err != nil {
t.Fatalf("Failed to change directory: %v", err)
}

// Call DeleteApp
DeleteApp(appName)

// Verify the app folder has been deleted
if _, err = os.Stat(appFolderPath); !os.IsNotExist(err) {
if _, err := os.Stat(appFolderPath); !os.IsNotExist(err) {
t.Fatalf("App folder was not deleted")
}
}

func TestDeleteApp_NotFound(t *testing.T) {
tmpDir := t.TempDir()
appName := "test-app"
appFolderPath := filepath.Join(tmpDir, appName)

// Change the working directory to the temporary directory
err := os.Chdir(tmpDir)
if err != nil {
if err := os.Chdir(tmpDir); err != nil {
t.Fatalf("Failed to change directory: %v", err)
}

// Call DeleteApp and expect a panic
defer func() {
if r := recover(); r == nil {
t.Fatalf("Expected panic but did not occur")
}
}()
// Save the current stdout
originalStdout := os.Stdout

// Create a buffer to capture the output
r, w, _ := os.Pipe()
os.Stdout = w

DeleteApp(appName)

// Verify the app folder does not exist
if _, err = os.Stat(appFolderPath); !os.IsNotExist(err) {
t.Fatalf("App folder should not exist")
// Restore the original stdout and close the writer
w.Close()
os.Stdout = originalStdout

// Read the captured output
var buf bytes.Buffer
io.Copy(&buf, r)

if buf.String() != "App folder not found\n" {
t.Fatalf("Unexpected output: %s", buf.String())
}
}

func TestDeleteApp_ErrorDeleting(t *testing.T) {
tmpDir := t.TempDir()
appName := "test-app"
appFolderPath := filepath.Join(tmpDir, appName)

// Create a temporary app folder
err := os.Mkdir(appFolderPath, 0755)
if err != nil {
t.Fatalf("Failed to create app folder: %v", err)
}

// Change the working directory to the temporary directory
err = os.Chdir(tmpDir)
if err != nil {
if err := os.Chdir(tmpDir); err != nil {
t.Fatalf("Failed to change directory: %v", err)
}

// Make the app folder read-only to simulate a deletion error
err = os.Chmod(appFolderPath, 0444)
if err != nil {
t.Fatalf("Failed to change folder permissions: %v", err)
}

// Call DeleteApp and expect a panic
defer func() {
if r := recover(); r == nil {
t.Fatalf("Expected panic but did not occur")
}
}()
DeleteApp(appName)

// Clean up by making the app folder writable again
err = os.Chmod(appFolderPath, 0755)
if err != nil {
t.Fatalf("Failed to change folder permissions: %v", err)
}
DeleteApp("\x00")
}

0 comments on commit dddfb1c

Please sign in to comment.