-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding functions for copying files and directories when EnvTfAccPersi…
…stWorkingDir env var is set (#18)
- Loading branch information
1 parent
04e9a2c
commit 47f2592
Showing
7 changed files
with
286 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package resource | ||
|
||
import ( | ||
"io" | ||
"os" | ||
"path" | ||
) | ||
|
||
// CopyFile copies a single file from src to dest. | ||
func CopyFile(src, dest string) error { | ||
var srcFileInfo os.FileInfo | ||
|
||
srcFile, err := os.Open(src) | ||
if err != nil { | ||
return err | ||
} | ||
defer srcFile.Close() | ||
|
||
destFile, err := os.Create(dest) | ||
if err != nil { | ||
return err | ||
} | ||
defer destFile.Close() | ||
|
||
if _, err = io.Copy(destFile, srcFile); err != nil { | ||
return err | ||
} | ||
|
||
if srcFileInfo, err = os.Stat(src); err != nil { | ||
return err | ||
} | ||
|
||
return os.Chmod(dest, srcFileInfo.Mode()) | ||
} | ||
|
||
// CopyDir recursively copies directories and files | ||
// from src to dest. | ||
func CopyDir(src string, dest string) error { | ||
srcInfo, err := os.Stat(src) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err = os.MkdirAll(dest, srcInfo.Mode()); err != nil { | ||
return err | ||
} | ||
|
||
dirEntries, err := os.ReadDir(src) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, dirEntry := range dirEntries { | ||
srcFilepath := path.Join(src, dirEntry.Name()) | ||
destFilepath := path.Join(dest, dirEntry.Name()) | ||
|
||
if dirEntry.IsDir() { | ||
if err = CopyDir(srcFilepath, destFilepath); err != nil { | ||
return err | ||
} | ||
} else { | ||
if err = CopyFile(srcFilepath, destFilepath); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package resource | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
func TestCopyFile(t *testing.T) { | ||
srcDir, err := os.MkdirTemp("", "") | ||
if err != nil { | ||
t.Fatalf("cannot create src dir: %s", err) | ||
} | ||
defer os.RemoveAll(srcDir) | ||
|
||
_, err = os.Create(filepath.Join(srcDir, "src.txt")) | ||
if err != nil { | ||
t.Fatalf("cannot create src file: %s", err) | ||
} | ||
|
||
destDir, err := os.MkdirTemp("", "") | ||
if err != nil { | ||
t.Fatalf("cannot create dest dir: %s", err) | ||
} | ||
defer os.RemoveAll(destDir) | ||
|
||
err = CopyFile(filepath.Join(srcDir, "src.txt"), filepath.Join(destDir, "src.txt")) | ||
if err != nil { | ||
t.Fatalf("cannot copy src file: %s", err) | ||
} | ||
|
||
srcDirEntries, err := os.ReadDir(srcDir) | ||
if err != nil { | ||
t.Fatalf("cannot read src dir: %s", srcDir) | ||
} | ||
|
||
destDirEntries, err := os.ReadDir(srcDir) | ||
if err != nil { | ||
t.Fatalf("cannot read dest dir: %s", srcDir) | ||
} | ||
|
||
if !Equal(t, srcDirEntries, destDirEntries) { | ||
t.Fatalf("dir entries differ: %v, %v", srcDirEntries, destDirEntries) | ||
} | ||
} | ||
|
||
func TestCopyDir(t *testing.T) { | ||
srcDir, err := os.MkdirTemp("", "") | ||
if err != nil { | ||
t.Fatalf("cannot create src dir: %s", err) | ||
} | ||
defer os.RemoveAll(srcDir) | ||
|
||
_, err = os.Create(filepath.Join(srcDir, "src.txt")) | ||
if err != nil { | ||
t.Fatalf("cannot create src file: %s", err) | ||
} | ||
|
||
err = CopyDir(srcDir, srcDir+"_1") | ||
if err != nil { | ||
t.Fatalf("cannot copy dir: %s", err) | ||
} | ||
defer os.RemoveAll(srcDir + "_1") | ||
|
||
srcDirEntries, err := os.ReadDir(srcDir) | ||
if err != nil { | ||
t.Fatalf("cannot read src dir: %s", srcDir) | ||
} | ||
|
||
destDirEntries, err := os.ReadDir(srcDir) | ||
if err != nil { | ||
t.Fatalf("cannot read dest dir: %s", srcDir) | ||
} | ||
|
||
if !Equal(t, srcDirEntries, destDirEntries) { | ||
t.Fatalf("dir entries differ: %v, %v", srcDirEntries, destDirEntries) | ||
} | ||
} | ||
|
||
func Equal(t *testing.T, a, b []os.DirEntry) bool { | ||
if len(a) != len(b) { | ||
return false | ||
} | ||
|
||
for i, v := range a { | ||
if v.Type() != b[i].Type() { | ||
return false | ||
} | ||
|
||
if v.Name() != b[i].Name() { | ||
return false | ||
} | ||
|
||
aInfo, err := v.Info() | ||
if err != nil { | ||
t.Fatalf("cannot get file info: %s", err) | ||
} | ||
|
||
bInfo, err := b[i].Info() | ||
if err != nil { | ||
t.Fatalf("cannot get file info: %s", err) | ||
} | ||
|
||
if aInfo.Mode() != bInfo.Mode() { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters