-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopy.go
More file actions
99 lines (94 loc) · 2.24 KB
/
Copy pathcopy.go
File metadata and controls
99 lines (94 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package clitest
import (
"fmt"
"os"
"path/filepath"
)
func CopyFile(src, dst string) error {
data, err := os.ReadFile(src)
if err != nil {
return err
}
return os.WriteFile(dst, data, 0644)
}
func CopyDir(src, dst string) error {
src = filepath.Clean(src)
dst = filepath.Clean(dst)
si, err := os.Stat(src)
if err != nil {
return err
}
if !si.IsDir() {
return fmt.Errorf("source is not a directory")
}
if err := os.MkdirAll(dst, si.Mode()); err != nil {
return err
}
entries, err := os.ReadDir(src)
if err != nil {
return err
}
for _, e := range entries {
s := filepath.Join(src, e.Name())
d := filepath.Join(dst, e.Name())
if e.IsDir() {
if err := CopyDir(s, d); err != nil {
return err
}
} else {
if err := CopyFile(s, d); err != nil {
return err
}
}
}
return nil
}
// CopyAllDirectoryContents copies every entry from testDir into tempDir.
func CopyAllDirectoryContents(testDir, tempDir string) error {
entries, err := os.ReadDir(testDir)
if err != nil {
return fmt.Errorf("read test directory: %w", err)
}
for _, entry := range entries {
src := filepath.Join(testDir, entry.Name())
dst := filepath.Join(tempDir, entry.Name())
if entry.IsDir() {
if err := CopyDir(src, dst); err != nil {
return fmt.Errorf("copy directory %s: %w", entry.Name(), err)
}
} else {
if err := CopyFile(src, dst); err != nil {
return fmt.Errorf("copy file %s: %w", entry.Name(), err)
}
}
}
return nil
}
// CopyTestDirectoryContents copies test fixture files into tempDir, excluding act.sh and assert.txt.
func CopyTestDirectoryContents(testDir, tempDir string) error {
entries, err := os.ReadDir(testDir)
if err != nil {
return fmt.Errorf("read test directory: %w", err)
}
skipFiles := map[string]bool{
"act.sh": true,
"assert.txt": true,
}
for _, entry := range entries {
if skipFiles[entry.Name()] {
continue
}
src := filepath.Join(testDir, entry.Name())
dst := filepath.Join(tempDir, entry.Name())
if entry.IsDir() {
if err := CopyDir(src, dst); err != nil {
return fmt.Errorf("copy directory %s: %w", entry.Name(), err)
}
} else {
if err := CopyFile(src, dst); err != nil {
return fmt.Errorf("copy file %s: %w", entry.Name(), err)
}
}
}
return nil
}