-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilder_test.go
More file actions
99 lines (82 loc) · 2.09 KB
/
Copy pathbuilder_test.go
File metadata and controls
99 lines (82 loc) · 2.09 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 rpmbuild_test
import (
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"testing"
"github.com/jesseduffield/go-git/v5"
"github.com/rustysys-dev/rpmbuild"
)
func TestGenRPMName(t *testing.T) {
}
func TestSetNameFromRepo(t *testing.T) {
t.Run("Successful Open", func(t *testing.T) {
tmpDir, fn := helpSetupGitRun(t)
defer fn()
builder := &rpmbuild.Builder{}
err := builder.SetNameFromRepo()
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
expectedName := filepath.Base(tmpDir)
if builder.Name != expectedName {
t.Errorf("Incorrect repository name. Got: %s, Want: %s", builder.Name, expectedName)
}
})
}
func helpSetupRun(t *testing.T) (string, func()) {
oldDir := os.Getenv("PWD")
tmpDir, err := os.MkdirTemp("", "git-repo-*")
if err != nil {
t.Fatalf("Failed to create temporary directory: %v", err)
}
err = os.Chdir(tmpDir)
if err != nil {
t.Fatalf("Failed to change directory: %v", err)
}
return tmpDir, func() {
os.RemoveAll(tmpDir)
os.Chdir(oldDir)
}
}
func helpSetupGitRun(t *testing.T) (string, func()) {
tmpDir, fn := helpSetupRun(t)
_, err := git.PlainInit(tmpDir, false)
if err != nil {
t.Fatalf("Failed to initialize Git repository: %v", err)
}
return tmpDir, fn
}
func recursiveCopy(oldDir, tmpDir string) error {
return filepath.WalkDir(oldDir, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return fmt.Errorf("error accessing path '%s': %w", path, err)
}
relPath, err := filepath.Rel(oldDir, path)
if err != nil {
return fmt.Errorf("error getting relative path: %w", err)
}
newPath := filepath.Join(tmpDir, relPath)
info, err := d.Info()
if err != nil {
return fmt.Errorf("error getting file info: %w", err)
}
if info.IsDir() {
return os.MkdirAll(newPath, info.Mode())
}
src, err := os.Open(path)
if err != nil {
return fmt.Errorf("error opening source file: %w", err)
}
defer src.Close()
dst, err := os.Create(newPath)
if err != nil {
return fmt.Errorf("error creating destination file: %w", err)
}
defer dst.Close()
_, err = io.Copy(dst, src)
return err
})
}