Skip to content

Commit

Permalink
refactor: Remove unused code and update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
tikazyq committed Jul 13, 2024
1 parent 54cf19f commit 9073f1c
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 134 deletions.
7 changes: 0 additions & 7 deletions core/apps/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,3 @@ func initModule(name string, fn func() error) (err error) {
log.Info(fmt.Sprintf("initialized %s successfully", name))
return nil
}

func initApp(name string, app App) {
_ = initModule(name, func() error {
app.Init()
return nil
})
}
5 changes: 0 additions & 5 deletions core/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ func Execute() error {
return rootCmd.Execute()
}

// GetRootCmd get rootCmd instance
func GetRootCmd() *cobra.Command {
return rootCmd
}

func init() {
rootCmd.PersistentFlags().StringVar(&cfgFile, "c", "", "Use Custom Config File")
}
12 changes: 0 additions & 12 deletions core/config/version.go

This file was deleted.

115 changes: 80 additions & 35 deletions core/fs/service_v2_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package fs

import (
"io/ioutil"
"github.com/apex/log"
"os"
"path/filepath"
"testing"
Expand All @@ -10,20 +10,25 @@ import (
)

func TestServiceV2_List(t *testing.T) {
rootDir, err := ioutil.TempDir("", "fsTest")
rootDir, err := os.MkdirTemp("", "fsTest")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(rootDir) // clean up
defer func() {
err := os.RemoveAll(rootDir) // clean up
if err != nil {
log.Errorf("Failed to remove temp dir: %v", err)
}
}()

testDir := filepath.Join(rootDir, "dir")
os.Mkdir(testDir, 0755)
ioutil.WriteFile(filepath.Join(testDir, "file1.txt"), []byte("hello world"), 0644)
ioutil.WriteFile(filepath.Join(testDir, "file2.txt"), []byte("hello again"), 0644)
_ = os.Mkdir(testDir, 0755)
_ = os.WriteFile(filepath.Join(testDir, "file1.txt"), []byte("hello world"), 0644)
_ = os.WriteFile(filepath.Join(testDir, "file2.txt"), []byte("hello again"), 0644)
subDir := filepath.Join(testDir, "subdir")
os.Mkdir(subDir, 0755)
ioutil.WriteFile(filepath.Join(subDir, "file3.txt"), []byte("subdir file"), 0644)
os.Mkdir(filepath.Join(testDir, "empty"), 0755) // explicitly testing empty dir inclusion
_ = os.Mkdir(subDir, 0755)
_ = os.WriteFile(filepath.Join(subDir, "file3.txt"), []byte("subdir file"), 0644)
_ = os.Mkdir(filepath.Join(testDir, "empty"), 0755) // explicitly testing empty dir inclusion

svc := NewFsServiceV2(rootDir)

Expand Down Expand Up @@ -56,14 +61,19 @@ func TestServiceV2_List(t *testing.T) {
}

func TestServiceV2_GetFile(t *testing.T) {
rootDir, err := ioutil.TempDir("", "fsTest")
rootDir, err := os.MkdirTemp("", "fsTest")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(rootDir) // clean up
defer func() {
err := os.RemoveAll(rootDir) // clean up
if err != nil {
log.Errorf("Failed to remove temp dir: %v", err)
}
}()

expectedContent := []byte("hello world")
ioutil.WriteFile(filepath.Join(rootDir, "file.txt"), expectedContent, 0644)
_ = os.WriteFile(filepath.Join(rootDir, "file.txt"), expectedContent, 0644)

svc := NewFsServiceV2(rootDir)

Expand All @@ -75,14 +85,19 @@ func TestServiceV2_GetFile(t *testing.T) {
}

func TestServiceV2_Delete(t *testing.T) {
rootDir, err := ioutil.TempDir("", "fsTest")
rootDir, err := os.MkdirTemp("", "fsTest")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(rootDir) // clean up
defer func() {
err := os.RemoveAll(rootDir) // clean up
if err != nil {
log.Errorf("Failed to remove temp dir: %v", err)
}
}()

filePath := filepath.Join(rootDir, "file.txt")
ioutil.WriteFile(filePath, []byte("hello world"), 0644)
_ = os.WriteFile(filePath, []byte("hello world"), 0644)

svc := NewFsServiceV2(rootDir)

Expand All @@ -98,11 +113,16 @@ func TestServiceV2_Delete(t *testing.T) {
}

func TestServiceV2_CreateDir(t *testing.T) {
rootDir, err := ioutil.TempDir("", "fsTest")
rootDir, err := os.MkdirTemp("", "fsTest")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(rootDir) // clean up
defer func() {
err := os.RemoveAll(rootDir) // clean up
if err != nil {
log.Errorf("Failed to remove temp dir: %v", err)
}
}()

svc := NewFsServiceV2(rootDir)

Expand All @@ -118,11 +138,16 @@ func TestServiceV2_CreateDir(t *testing.T) {
}

func TestServiceV2_Save(t *testing.T) {
rootDir, err := ioutil.TempDir("", "fsTest")
rootDir, err := os.MkdirTemp("", "fsTest")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(rootDir) // clean up
defer func() {
err := os.RemoveAll(rootDir) // clean up
if err != nil {
log.Errorf("Failed to remove temp dir: %v", err)
}
}()

svc := NewFsServiceV2(rootDir)

Expand All @@ -133,22 +158,27 @@ func TestServiceV2_Save(t *testing.T) {
}

// Verify the file was saved
data, err := ioutil.ReadFile(filepath.Join(rootDir, "newFile.txt"))
data, err := os.ReadFile(filepath.Join(rootDir, "newFile.txt"))
assert.NoError(t, err)
assert.Equal(t, "Hello, world!", string(data))
}

func TestServiceV2_Rename(t *testing.T) {
rootDir, err := ioutil.TempDir("", "fsTest")
rootDir, err := os.MkdirTemp("", "fsTest")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(rootDir) // clean up
defer func() {
err := os.RemoveAll(rootDir) // clean up
if err != nil {
log.Errorf("Failed to remove temp dir: %v", err)
}
}()

svc := NewFsServiceV2(rootDir)

// Create a file to rename
ioutil.WriteFile(filepath.Join(rootDir, "oldName.txt"), []byte("Hello, world!"), 0644)
_ = os.WriteFile(filepath.Join(rootDir, "oldName.txt"), []byte("Hello, world!"), 0644)

// Rename the file
err = svc.Rename("oldName.txt", "newName.txt")
Expand All @@ -162,16 +192,21 @@ func TestServiceV2_Rename(t *testing.T) {
}

func TestServiceV2_RenameDir(t *testing.T) {
rootDir, err := ioutil.TempDir("", "fsTest")
rootDir, err := os.MkdirTemp("", "fsTest")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(rootDir) // clean up
defer func() {
err := os.RemoveAll(rootDir) // clean up
if err != nil {
log.Errorf("Failed to remove temp dir: %v", err)
}
}()

svc := NewFsServiceV2(rootDir)

// Create a directory to rename
os.Mkdir(filepath.Join(rootDir, "oldName"), 0755)
_ = os.Mkdir(filepath.Join(rootDir, "oldName"), 0755)

// Rename the directory
err = svc.Rename("oldName", "newName")
Expand All @@ -185,16 +220,21 @@ func TestServiceV2_RenameDir(t *testing.T) {
}

func TestServiceV2_Copy(t *testing.T) {
rootDir, err := ioutil.TempDir("", "fsTest")
rootDir, err := os.MkdirTemp("", "fsTest")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(rootDir) // clean up
defer func() {
err := os.RemoveAll(rootDir) // clean up
if err != nil {
log.Errorf("Failed to remove temp dir: %v", err)
}
}()

svc := NewFsServiceV2(rootDir)

// Create a file to copy
ioutil.WriteFile(filepath.Join(rootDir, "source.txt"), []byte("Hello, world!"), 0644)
_ = os.WriteFile(filepath.Join(rootDir, "source.txt"), []byte("Hello, world!"), 0644)

// Copy the file
err = svc.Copy("source.txt", "copy.txt")
Expand All @@ -203,23 +243,28 @@ func TestServiceV2_Copy(t *testing.T) {
}

// Verify the file was copied
data, err := ioutil.ReadFile(filepath.Join(rootDir, "copy.txt"))
data, err := os.ReadFile(filepath.Join(rootDir, "copy.txt"))
assert.NoError(t, err)
assert.Equal(t, "Hello, world!", string(data))
}

func TestServiceV2_CopyDir(t *testing.T) {
rootDir, err := ioutil.TempDir("", "fsTest")
rootDir, err := os.MkdirTemp("", "fsTest")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(rootDir) // clean up
defer func() {
err := os.RemoveAll(rootDir) // clean up
if err != nil {
log.Errorf("Failed to remove temp dir: %v", err)
}
}()

svc := NewFsServiceV2(rootDir)

// Create a directory to copy
os.Mkdir(filepath.Join(rootDir, "sourceDir"), 0755)
ioutil.WriteFile(filepath.Join(rootDir, "sourceDir", "file.txt"), []byte("Hello, world!"), 0644)
_ = os.Mkdir(filepath.Join(rootDir, "sourceDir"), 0755)
_ = os.WriteFile(filepath.Join(rootDir, "sourceDir", "file.txt"), []byte("Hello, world!"), 0644)

// Copy the directory
err = svc.Copy("sourceDir", "copyDir")
Expand All @@ -232,7 +277,7 @@ func TestServiceV2_CopyDir(t *testing.T) {
assert.NoError(t, err)

// Verify the file inside the directory was copied
data, err := ioutil.ReadFile(filepath.Join(rootDir, "copyDir", "file.txt"))
data, err := os.ReadFile(filepath.Join(rootDir, "copyDir", "file.txt"))
assert.NoError(t, err)
assert.Equal(t, "Hello, world!", string(data))
}
Loading

0 comments on commit 9073f1c

Please sign in to comment.