add file/folder createa and delete funcs
This commit is contained in:
@@ -57,3 +57,67 @@ func walkFileSystemAndBuildTree(rootPath string, node *FsNode) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func createFile(path string, content []byte) error {
|
||||
if path == "" {
|
||||
return errors.New("file path cannot be empty")
|
||||
}
|
||||
|
||||
if _, err := os.Stat(path); err == nil {
|
||||
return errors.New("file already exists")
|
||||
}
|
||||
|
||||
return os.WriteFile(path, content, 0644)
|
||||
}
|
||||
|
||||
func createFolder(path string) error {
|
||||
if path == "" {
|
||||
return errors.New("folder path cannot be empty")
|
||||
}
|
||||
|
||||
if _, err := os.Stat(path); err == nil {
|
||||
return errors.New("folder already exists")
|
||||
}
|
||||
|
||||
return os.Mkdir(path, 0755)
|
||||
}
|
||||
|
||||
func deleteFile(path string) error {
|
||||
if path == "" {
|
||||
return errors.New("file path cannot be empty")
|
||||
}
|
||||
|
||||
info, err := os.Stat(path)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return errors.New("file does not exist")
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
if info.IsDir() {
|
||||
return errors.New("path is a directory, not a file")
|
||||
}
|
||||
|
||||
return os.Remove(path)
|
||||
}
|
||||
|
||||
func deleteFolder(path string) error {
|
||||
if path == "" {
|
||||
return errors.New("folder path cannot be empty")
|
||||
}
|
||||
|
||||
info, err := os.Stat(path)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return errors.New("folder does not exist")
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
if !info.IsDir() {
|
||||
return errors.New("path is a file, not a directory")
|
||||
}
|
||||
|
||||
return os.RemoveAll(path)
|
||||
}
|
||||
|
||||
+226
@@ -151,3 +151,229 @@ func TestWalkFileSystemAndBuildTree(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestCreateFile(t *testing.T) {
|
||||
t.Run("create file successfully", func(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
filePath := filepath.Join(tmpDir, "test.txt")
|
||||
content := []byte("test content")
|
||||
|
||||
err := createFile(filePath, content)
|
||||
if err != nil {
|
||||
t.Fatalf("createFile() error = %v", err)
|
||||
}
|
||||
|
||||
data, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to read created file: %v", err)
|
||||
}
|
||||
if string(data) != string(content) {
|
||||
t.Errorf("file content = %q, want %q", string(data), string(content))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("create empty file", func(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
filePath := filepath.Join(tmpDir, "empty.txt")
|
||||
|
||||
err := createFile(filePath, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("createFile() error = %v", err)
|
||||
}
|
||||
|
||||
info, err := os.Stat(filePath)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to stat file: %v", err)
|
||||
}
|
||||
if info.Size() != 0 {
|
||||
t.Errorf("file size = %d, want 0", info.Size())
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("error when path is empty", func(t *testing.T) {
|
||||
err := createFile("", []byte("content"))
|
||||
if err == nil {
|
||||
t.Fatal("createFile() with empty path should return error")
|
||||
}
|
||||
if err.Error() != "file path cannot be empty" {
|
||||
t.Errorf("error message = %q", err.Error())
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("error when file already exists", func(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
filePath := filepath.Join(tmpDir, "existing.txt")
|
||||
os.WriteFile(filePath, []byte("existing"), 0644)
|
||||
|
||||
err := createFile(filePath, []byte("new"))
|
||||
if err == nil {
|
||||
t.Fatal("createFile() on existing file should return error")
|
||||
}
|
||||
if err.Error() != "file already exists" {
|
||||
t.Errorf("error message = %q", err.Error())
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestCreateFolder(t *testing.T) {
|
||||
t.Run("create folder successfully", func(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
folderPath := filepath.Join(tmpDir, "testfolder")
|
||||
|
||||
err := createFolder(folderPath)
|
||||
if err != nil {
|
||||
t.Fatalf("createFolder() error = %v", err)
|
||||
}
|
||||
|
||||
info, err := os.Stat(folderPath)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to stat folder: %v", err)
|
||||
}
|
||||
if !info.IsDir() {
|
||||
t.Error("created path is not a directory")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("error when path is empty", func(t *testing.T) {
|
||||
err := createFolder("")
|
||||
if err == nil {
|
||||
t.Fatal("createFolder() with empty path should return error")
|
||||
}
|
||||
if err.Error() != "folder path cannot be empty" {
|
||||
t.Errorf("error message = %q", err.Error())
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("error when folder already exists", func(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
folderPath := filepath.Join(tmpDir, "existing")
|
||||
os.Mkdir(folderPath, 0755)
|
||||
|
||||
err := createFolder(folderPath)
|
||||
if err == nil {
|
||||
t.Fatal("createFolder() on existing folder should return error")
|
||||
}
|
||||
if err.Error() != "folder already exists" {
|
||||
t.Errorf("error message = %q", err.Error())
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestDeleteFile(t *testing.T) {
|
||||
t.Run("delete file successfully", func(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
filePath := filepath.Join(tmpDir, "delete.txt")
|
||||
os.WriteFile(filePath, []byte("content"), 0644)
|
||||
|
||||
err := deleteFile(filePath)
|
||||
if err != nil {
|
||||
t.Fatalf("deleteFile() error = %v", err)
|
||||
}
|
||||
|
||||
if _, err := os.Stat(filePath); !os.IsNotExist(err) {
|
||||
t.Error("file still exists after deletion")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("error when path is empty", func(t *testing.T) {
|
||||
err := deleteFile("")
|
||||
if err == nil {
|
||||
t.Fatal("deleteFile() with empty path should return error")
|
||||
}
|
||||
if err.Error() != "file path cannot be empty" {
|
||||
t.Errorf("error message = %q", err.Error())
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("error when file does not exist", func(t *testing.T) {
|
||||
err := deleteFile("/nonexistent/file.txt")
|
||||
if err == nil {
|
||||
t.Fatal("deleteFile() on nonexistent file should return error")
|
||||
}
|
||||
if err.Error() != "file does not exist" {
|
||||
t.Errorf("error message = %q", err.Error())
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("error when path is a directory", func(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
folderPath := filepath.Join(tmpDir, "folder")
|
||||
os.Mkdir(folderPath, 0755)
|
||||
|
||||
err := deleteFile(folderPath)
|
||||
if err == nil {
|
||||
t.Fatal("deleteFile() on directory should return error")
|
||||
}
|
||||
if err.Error() != "path is a directory, not a file" {
|
||||
t.Errorf("error message = %q", err.Error())
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestDeleteFolder(t *testing.T) {
|
||||
t.Run("delete empty folder successfully", func(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
folderPath := filepath.Join(tmpDir, "emptyfolder")
|
||||
os.Mkdir(folderPath, 0755)
|
||||
|
||||
err := deleteFolder(folderPath)
|
||||
if err != nil {
|
||||
t.Fatalf("deleteFolder() error = %v", err)
|
||||
}
|
||||
|
||||
if _, err := os.Stat(folderPath); !os.IsNotExist(err) {
|
||||
t.Error("folder still exists after deletion")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("delete folder with contents", func(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
folderPath := filepath.Join(tmpDir, "withcontents")
|
||||
os.Mkdir(folderPath, 0755)
|
||||
os.WriteFile(filepath.Join(folderPath, "file.txt"), []byte("content"), 0644)
|
||||
os.Mkdir(filepath.Join(folderPath, "subfolder"), 0755)
|
||||
|
||||
err := deleteFolder(folderPath)
|
||||
if err != nil {
|
||||
t.Fatalf("deleteFolder() error = %v", err)
|
||||
}
|
||||
|
||||
if _, err := os.Stat(folderPath); !os.IsNotExist(err) {
|
||||
t.Error("folder still exists after deletion")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("error when path is empty", func(t *testing.T) {
|
||||
err := deleteFolder("")
|
||||
if err == nil {
|
||||
t.Fatal("deleteFolder() with empty path should return error")
|
||||
}
|
||||
if err.Error() != "folder path cannot be empty" {
|
||||
t.Errorf("error message = %q", err.Error())
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("error when folder does not exist", func(t *testing.T) {
|
||||
err := deleteFolder("/nonexistent/folder")
|
||||
if err == nil {
|
||||
t.Fatal("deleteFolder() on nonexistent folder should return error")
|
||||
}
|
||||
if err.Error() != "folder does not exist" {
|
||||
t.Errorf("error message = %q", err.Error())
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("error when path is a file", func(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
filePath := filepath.Join(tmpDir, "file.txt")
|
||||
os.WriteFile(filePath, []byte("content"), 0644)
|
||||
|
||||
err := deleteFolder(filePath)
|
||||
if err == nil {
|
||||
t.Fatal("deleteFolder() on file should return error")
|
||||
}
|
||||
if err.Error() != "path is a file, not a directory" {
|
||||
t.Errorf("error message = %q", err.Error())
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user