Skip to content

Commit

Permalink
some more refactor
Browse files Browse the repository at this point in the history
Signed-off-by: Pravin Pushkar <[email protected]>
  • Loading branch information
pravinpushkar committed Dec 30, 2022
1 parent e7ccf04 commit 771b256
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 68 deletions.
31 changes: 2 additions & 29 deletions pkg/standalone/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,33 +68,7 @@ func DefaultConfigFilePath() string {
return path_filepath.Join(defaultDaprDirPath(), defaultConfigFileName)
}

// copyFilesAndCreateSymlink copies files from src to dest. It deletes the existing files in dest before copying from src.
// this method also deletes the components dir and makes it as a symlink to resources directory.
// please see this comment for more details:https://github.com/dapr/cli/pull/1149#issuecomment-1364424345
// TODO: Remove this function when `--components-path` flag is removed.
func copyFilesAndCreateSymlink(src, dest string) error {
var err error
if _, err = os.Stat(src); err != nil {
// if the src directory does not exist, create symlink and return nil, because there is nothing to copy from.
if os.IsNotExist(err) {
err = createSymLink(dest, src)
if err != nil {
return err
}
return nil
}
return fmt.Errorf("error reading directory %s: %w", src, err)
}
if err = moveDir(src, dest); err != nil {
return err
}
if err = createSymLink(dest, src); err != nil {
return err
}
return nil
}

// moveDir moves files from src to dest. If there are files in src, it deletes the existing files in dest before copying from src.
// moveDir moves files from src to dest. If there are files in src, it deletes the existing files in dest before copying from src and then deletes the src directory.
func moveDir(src, dest string) error {
destFiles, err := os.ReadDir(dest)
if err != nil {
Expand All @@ -105,7 +79,7 @@ func moveDir(src, dest string) error {
return fmt.Errorf("error reading files from %s: %w", src, err)
}
if len(srcFiles) > 0 {
// delete the existing files in dest before copying from src iff there are files in src.
// delete the existing files in dest before copying from src if there are files in src.
for _, file := range destFiles {
err = os.Remove(path_filepath.Join(dest, file.Name()))
if err != nil {
Expand All @@ -126,7 +100,6 @@ func moveDir(src, dest string) error {
}
}
}
// delete the components dir and make it as a symlink to resources directory.
err = os.RemoveAll(src)
if err != nil {
return fmt.Errorf("error removing directory %s: %w", src, err)
Expand Down
39 changes: 0 additions & 39 deletions pkg/standalone/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,45 +60,6 @@ func TestCreateSymLink(t *testing.T) {
}
}

func TestCopyFilesAndCreateSymlink(t *testing.T) {
// create a temp dir to hold the symlink and actual directory.
tempDir := createTempDir(t, "dapr-test", "")
defer cleanupTempDir(t, tempDir)
destDir := createTempDir(t, "dest", tempDir)
srcDir := createTempDir(t, "src", tempDir)
srcFile := createTempFile(t, srcDir, "pubsub.yaml")
tests := []struct {
name string
destDirName string
srcDirName string
expectedError bool
presentFileName string
}{
{
name: "copy files and create symlink for destination directory when source dir exists",
destDirName: destDir,
srcDirName: srcDir,
expectedError: false,
presentFileName: srcFile,
},
{
name: "copy files and create symlink for destination directory when source dir does not exists",
destDirName: destDir,
srcDirName: path_filepath.Join(tempDir, "non-existent-source-dir"),
expectedError: false,
presentFileName: path_filepath.Base(srcFile),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := copyFilesAndCreateSymlink(tt.srcDirName, tt.destDirName)
assert.Equal(t, tt.expectedError, err != nil)
// check if the files are copied.
assert.FileExists(t, path_filepath.Join(tt.srcDirName, path_filepath.Base(tt.presentFileName)))
})
}
}

func TestMoveDir(t *testing.T) {
// create a temp dir to hold the source and destination directory.
tempDir := createTempDir(t, "dapr-test", "")
Expand Down
26 changes: 26 additions & 0 deletions pkg/standalone/standalone.go
Original file line number Diff line number Diff line change
Expand Up @@ -1242,3 +1242,29 @@ func setAirGapInit(fromDir string) {
// mostly this is used for unit testing aprat from one use in Init() function.
isAirGapInit = (len(strings.TrimSpace(fromDir)) != 0)
}

// copyFilesAndCreateSymlink copies files from src to dest. It deletes the existing files in dest before copying from src.
// this method also deletes the src dir and makes it as a symlink to resources directory.
// please see this comment for more details:https://github.com/dapr/cli/pull/1149#issuecomment-1364424345
// TODO: Remove this function when `--components-path` flag is removed.
func copyFilesAndCreateSymlink(src, dest string) error {
var err error
if _, err = os.Stat(src); err != nil {
// if the src directory does not exist, create symlink and return nil, because there is nothing to copy from.
if os.IsNotExist(err) {
err = createSymLink(dest, src)
if err != nil {
return err
}
return nil
}
return fmt.Errorf("error reading directory %s: %w", src, err)
}
if err = moveDir(src, dest); err != nil {
return err
}
if err = createSymLink(dest, src); err != nil {
return err
}
return nil
}
40 changes: 40 additions & 0 deletions pkg/standalone/standalone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package standalone

import (
"os"
path_filepath "path/filepath"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -304,3 +305,42 @@ func TestIsAirGapInit(t *testing.T) {
})
}
}

func TestCopyFilesAndCreateSymlink(t *testing.T) {
// create a temp dir to hold the symlink and actual directory.
tempDir := createTempDir(t, "dapr-test", "")
defer cleanupTempDir(t, tempDir)
destDir := createTempDir(t, "dest", tempDir)
srcDir := createTempDir(t, "src", tempDir)
srcFile := createTempFile(t, srcDir, "pubsub.yaml")
tests := []struct {
name string
destDirName string
srcDirName string
expectedError bool
presentFileName string
}{
{
name: "copy files and create symlink for destination directory when source dir exists",
destDirName: destDir,
srcDirName: srcDir,
expectedError: false,
presentFileName: srcFile,
},
{
name: "copy files and create symlink for destination directory when source dir does not exists",
destDirName: destDir,
srcDirName: path_filepath.Join(tempDir, "non-existent-source-dir"),
expectedError: false,
presentFileName: path_filepath.Base(srcFile),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := copyFilesAndCreateSymlink(tt.srcDirName, tt.destDirName)
assert.Equal(t, tt.expectedError, err != nil)
// check if the files are copied.
assert.FileExists(t, path_filepath.Join(tt.srcDirName, path_filepath.Base(tt.presentFileName)))
})
}
}

0 comments on commit 771b256

Please sign in to comment.