-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
167 lines (144 loc) · 3.74 KB
/
main_test.go
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"testing"
"github.com/google/go-cmdtest"
)
func TestCLIFlags(t *testing.T) {
ts := configure(t, "testdata/base")
ts.Run(t, false)
}
func TestCLIWrite_withLoadingErrors(t *testing.T) {
cases := []string{
"invalid-config-files",
"invalid-config-files-2",
"invalid-templates-files",
"multiple-invalid-files",
"multiple-unknown-files",
"permission-issue",
"permission-issue-2",
"invalid-workspace-dir",
"multiple-config-for-same-repo",
"multiple-config-for-same-repo-2",
"invalid-yaml",
"invalid-yaml-2",
}
for _, tcname := range cases {
t.Run(
tcname,
func(t *testing.T) {
ts := configure(t, filepath.Join("testdata/write/loading-errors", tcname))
// ts.KeepRootDirs = true
ts.Run(t, false)
},
)
}
}
func TestCLIWrite_withComputationErrors(t *testing.T) {
cases := []string{
"unknown-template",
"default-branch-template-without-default-branch",
}
for _, tcname := range cases {
t.Run(
tcname,
func(t *testing.T) {
configure(t, filepath.Join("testdata/write/computation-errors", tcname)).Run(t, false)
},
)
}
}
func TestCLIWrite_withTerraformErrors(t *testing.T) {
cases := []string{
"missing-terraform-directory",
"terraform-directory-as-file",
"permission-issue",
}
for _, tcname := range cases {
t.Run(
tcname,
func(t *testing.T) {
configure(t, filepath.Join("testdata/write/terraform-errors", tcname)).Run(t, false)
},
)
}
}
func TestCLIWrite_working(t *testing.T) {
cases := []string{
"base",
"full",
"yml-vs-yaml",
"with-templates",
"with-templates-and-anchors",
"multiple-branch-protection-for-same-pattern",
"default-branch-branch-protection-template-with-existing-config",
}
for _, tcname := range cases {
t.Run(
tcname,
func(t *testing.T) {
configure(t, filepath.Join("testdata/write/working", tcname)).Run(t, false)
},
)
}
}
func configure(t *testing.T, testdataPath string) *cmdtest.TestSuite {
t.Helper()
suite, err := cmdtest.Read(testdataPath)
if err != nil {
t.Fatal(err)
}
suite.Commands["github-tf"] = cmdtest.InProcessProgram("github-tf", run)
suite.Commands["chmod"] = chmodCmd
suite.Setup = func(rootDir string) error {
_, testFileName, _, ok := runtime.Caller(0)
if !ok {
return errors.New("failed get real working directory from caller")
}
projectRootDir := filepath.Dir(testFileName)
// fmt.Printf("Project dir %s\n", projectRootDir)
// fmt.Printf("ROOTDIR %s\n", rootDir)
// copy {testdataPath}/testdata to ROOTDIR/testdata if it exists
testdataSourcePath := filepath.Join(projectRootDir, testdataPath, "testdata")
if _, err = os.Stat(testdataSourcePath); !os.IsNotExist(err) {
//nolint:forbidigo // Test file
fmt.Printf("Copy testdata %s\n", testdataSourcePath)
testdataTargetPath := filepath.Join(rootDir, "testdata")
cmd := exec.Command("cp", "-r", testdataSourcePath, testdataTargetPath)
if _, err = cmd.Output(); err != nil {
return fmt.Errorf("Error during testdata copy (%s -> %s): %w", testdataSourcePath, testdataTargetPath, err)
}
}
return nil
}
return suite
}
func chmodCmd(args []string, inputFile string) ([]byte, error) {
if inputFile != "" {
return nil, errors.New("input redirection not supported")
}
if err := checkPath(args[0]); err != nil {
return nil, err
}
perm, err := strconv.Atoi(args[1])
if err != nil {
return nil, err
}
if err = os.Chmod(args[0], os.FileMode(perm)); err != nil {
return nil, err
}
return nil, nil
}
func checkPath(path string) error {
if strings.ContainsRune(path, '/') || strings.ContainsRune(path, '\\') {
return fmt.Errorf("argument must be in the current directory (%q contains '/')", path)
}
return nil
}