Skip to content

Commit 771b256

Browse files
committed
some more refactor
Signed-off-by: Pravin Pushkar <[email protected]>
1 parent e7ccf04 commit 771b256

File tree

4 files changed

+68
-68
lines changed

4 files changed

+68
-68
lines changed

pkg/standalone/common.go

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -68,33 +68,7 @@ func DefaultConfigFilePath() string {
6868
return path_filepath.Join(defaultDaprDirPath(), defaultConfigFileName)
6969
}
7070

71-
// copyFilesAndCreateSymlink copies files from src to dest. It deletes the existing files in dest before copying from src.
72-
// this method also deletes the components dir and makes it as a symlink to resources directory.
73-
// please see this comment for more details:https://github.com/dapr/cli/pull/1149#issuecomment-1364424345
74-
// TODO: Remove this function when `--components-path` flag is removed.
75-
func copyFilesAndCreateSymlink(src, dest string) error {
76-
var err error
77-
if _, err = os.Stat(src); err != nil {
78-
// if the src directory does not exist, create symlink and return nil, because there is nothing to copy from.
79-
if os.IsNotExist(err) {
80-
err = createSymLink(dest, src)
81-
if err != nil {
82-
return err
83-
}
84-
return nil
85-
}
86-
return fmt.Errorf("error reading directory %s: %w", src, err)
87-
}
88-
if err = moveDir(src, dest); err != nil {
89-
return err
90-
}
91-
if err = createSymLink(dest, src); err != nil {
92-
return err
93-
}
94-
return nil
95-
}
96-
97-
// moveDir moves files from src to dest. If there are files in src, it deletes the existing files in dest before copying from src.
71+
// 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.
9872
func moveDir(src, dest string) error {
9973
destFiles, err := os.ReadDir(dest)
10074
if err != nil {
@@ -105,7 +79,7 @@ func moveDir(src, dest string) error {
10579
return fmt.Errorf("error reading files from %s: %w", src, err)
10680
}
10781
if len(srcFiles) > 0 {
108-
// delete the existing files in dest before copying from src iff there are files in src.
82+
// delete the existing files in dest before copying from src if there are files in src.
10983
for _, file := range destFiles {
11084
err = os.Remove(path_filepath.Join(dest, file.Name()))
11185
if err != nil {
@@ -126,7 +100,6 @@ func moveDir(src, dest string) error {
126100
}
127101
}
128102
}
129-
// delete the components dir and make it as a symlink to resources directory.
130103
err = os.RemoveAll(src)
131104
if err != nil {
132105
return fmt.Errorf("error removing directory %s: %w", src, err)

pkg/standalone/common_test.go

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -60,45 +60,6 @@ func TestCreateSymLink(t *testing.T) {
6060
}
6161
}
6262

63-
func TestCopyFilesAndCreateSymlink(t *testing.T) {
64-
// create a temp dir to hold the symlink and actual directory.
65-
tempDir := createTempDir(t, "dapr-test", "")
66-
defer cleanupTempDir(t, tempDir)
67-
destDir := createTempDir(t, "dest", tempDir)
68-
srcDir := createTempDir(t, "src", tempDir)
69-
srcFile := createTempFile(t, srcDir, "pubsub.yaml")
70-
tests := []struct {
71-
name string
72-
destDirName string
73-
srcDirName string
74-
expectedError bool
75-
presentFileName string
76-
}{
77-
{
78-
name: "copy files and create symlink for destination directory when source dir exists",
79-
destDirName: destDir,
80-
srcDirName: srcDir,
81-
expectedError: false,
82-
presentFileName: srcFile,
83-
},
84-
{
85-
name: "copy files and create symlink for destination directory when source dir does not exists",
86-
destDirName: destDir,
87-
srcDirName: path_filepath.Join(tempDir, "non-existent-source-dir"),
88-
expectedError: false,
89-
presentFileName: path_filepath.Base(srcFile),
90-
},
91-
}
92-
for _, tt := range tests {
93-
t.Run(tt.name, func(t *testing.T) {
94-
err := copyFilesAndCreateSymlink(tt.srcDirName, tt.destDirName)
95-
assert.Equal(t, tt.expectedError, err != nil)
96-
// check if the files are copied.
97-
assert.FileExists(t, path_filepath.Join(tt.srcDirName, path_filepath.Base(tt.presentFileName)))
98-
})
99-
}
100-
}
101-
10263
func TestMoveDir(t *testing.T) {
10364
// create a temp dir to hold the source and destination directory.
10465
tempDir := createTempDir(t, "dapr-test", "")

pkg/standalone/standalone.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,3 +1242,29 @@ func setAirGapInit(fromDir string) {
12421242
// mostly this is used for unit testing aprat from one use in Init() function.
12431243
isAirGapInit = (len(strings.TrimSpace(fromDir)) != 0)
12441244
}
1245+
1246+
// copyFilesAndCreateSymlink copies files from src to dest. It deletes the existing files in dest before copying from src.
1247+
// this method also deletes the src dir and makes it as a symlink to resources directory.
1248+
// please see this comment for more details:https://github.com/dapr/cli/pull/1149#issuecomment-1364424345
1249+
// TODO: Remove this function when `--components-path` flag is removed.
1250+
func copyFilesAndCreateSymlink(src, dest string) error {
1251+
var err error
1252+
if _, err = os.Stat(src); err != nil {
1253+
// if the src directory does not exist, create symlink and return nil, because there is nothing to copy from.
1254+
if os.IsNotExist(err) {
1255+
err = createSymLink(dest, src)
1256+
if err != nil {
1257+
return err
1258+
}
1259+
return nil
1260+
}
1261+
return fmt.Errorf("error reading directory %s: %w", src, err)
1262+
}
1263+
if err = moveDir(src, dest); err != nil {
1264+
return err
1265+
}
1266+
if err = createSymLink(dest, src); err != nil {
1267+
return err
1268+
}
1269+
return nil
1270+
}

pkg/standalone/standalone_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ package standalone
1515

1616
import (
1717
"os"
18+
path_filepath "path/filepath"
1819
"testing"
1920

2021
"github.com/stretchr/testify/assert"
@@ -304,3 +305,42 @@ func TestIsAirGapInit(t *testing.T) {
304305
})
305306
}
306307
}
308+
309+
func TestCopyFilesAndCreateSymlink(t *testing.T) {
310+
// create a temp dir to hold the symlink and actual directory.
311+
tempDir := createTempDir(t, "dapr-test", "")
312+
defer cleanupTempDir(t, tempDir)
313+
destDir := createTempDir(t, "dest", tempDir)
314+
srcDir := createTempDir(t, "src", tempDir)
315+
srcFile := createTempFile(t, srcDir, "pubsub.yaml")
316+
tests := []struct {
317+
name string
318+
destDirName string
319+
srcDirName string
320+
expectedError bool
321+
presentFileName string
322+
}{
323+
{
324+
name: "copy files and create symlink for destination directory when source dir exists",
325+
destDirName: destDir,
326+
srcDirName: srcDir,
327+
expectedError: false,
328+
presentFileName: srcFile,
329+
},
330+
{
331+
name: "copy files and create symlink for destination directory when source dir does not exists",
332+
destDirName: destDir,
333+
srcDirName: path_filepath.Join(tempDir, "non-existent-source-dir"),
334+
expectedError: false,
335+
presentFileName: path_filepath.Base(srcFile),
336+
},
337+
}
338+
for _, tt := range tests {
339+
t.Run(tt.name, func(t *testing.T) {
340+
err := copyFilesAndCreateSymlink(tt.srcDirName, tt.destDirName)
341+
assert.Equal(t, tt.expectedError, err != nil)
342+
// check if the files are copied.
343+
assert.FileExists(t, path_filepath.Join(tt.srcDirName, path_filepath.Base(tt.presentFileName)))
344+
})
345+
}
346+
}

0 commit comments

Comments
 (0)