-
Notifications
You must be signed in to change notification settings - Fork 3
/
lib_test.go
127 lines (104 loc) · 3.27 KB
/
lib_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
package main
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
)
func TestE2E(t *testing.T) {
// Create a test env
tmpdir, err := os.MkdirTemp("", "test")
require.NoError(t, err)
defer os.RemoveAll(tmpdir)
// Create a repo in the test env so that we have a predictable name
repoPath := filepath.Join(tmpdir, "repo")
err = os.Mkdir(repoPath, 0700)
require.NoError(t, err)
// Create an existing CODEOWNERS file that should not be processed
existingCOFile := generatedFileWarning + `
/src/foobar @org/previousUser
`
writeFile(t, repoPath, generatedFileName, existingCOFile)
// Create a simple CODEOWNERS file for the happy path
simpleCOFile := `
@org/user
`
writeFile(t, repoPath, "src/dir1/CODEOWNERS", simpleCOFile)
// Create a complex CODEOWNERS file with edge cases
complexCOFile := `
# Dir rule
@org/user @singleUser email@server.com
# File rules
main.go @org/gopher
package/nested.go @org/nestedUser
# Glob rule (nonsensical but allowed)
*.js @org/frontend @fullstackUser
`
writeFile(t, repoPath, "src/dir2/CODEOWNERS", complexCOFile)
// Create a CODEOWNERS file in root which should be processed as well.
// Also tests the global owner edge case (should be assigned by glob, not path).
rootCOFile := `
# Default owner for the entire repo
@org/admin
go.mod @org/gopher
`
writeFile(t, repoPath, "CODEOWNERS", rootCOFile)
// Create a CODEOWNERS file in a hidden directory
hiddenDirCOFile := `
ci.yaml @org/ci-admin
`
writeFile(t, repoPath, ".github/workflows/CODEOWNERS", hiddenDirCOFile)
// Create a CODEOWNERS file in an ignored directory
ignoreFile := `
/src/shouldBeIgnored
`
ignoredCOFile := `
@org/shouldNotBeSeen
`
writeFile(t, repoPath, ".gitignore", ignoreFile)
writeFile(t, repoPath, "src/shouldBeIgnored/CODEOWNERS", ignoredCOFile)
// Test rule rewriting
expectedRules := []string{
// From /CODEOWNERS
"* @org/admin",
"/go.mod @org/gopher",
// From /.github/workflows/CODEOWNERS
"/.github/workflows/ci.yaml @org/ci-admin",
// From /src/dir1/CODEOWNERS
"/src/dir1 @org/user",
// From /src/dir2/CODEOWNERS
"/src/dir2 @org/user @singleUser email@server.com",
"/src/dir2/main.go @org/gopher",
"/src/dir2/package/nested.go @org/nestedUser",
"/src/dir2/*.js @org/frontend @fullstackUser",
}
rewrittenRules, err := RewriteCodeownersRules(repoPath)
require.NoError(t, err)
require.Equal(t, expectedRules, rewrittenRules)
// Test file generation
expectedFile := generatedFileWarning + `
* @org/admin
/go.mod @org/gopher
/.github/workflows/ci.yaml @org/ci-admin
/src/dir1 @org/user
/src/dir2 @org/user @singleUser email@server.com
/src/dir2/main.go @org/gopher
/src/dir2/package/nested.go @org/nestedUser
/src/dir2/*.js @org/frontend @fullstackUser
`
generatedFile := GenerateCodeownersFile(rewrittenRules)
require.Equal(t, expectedFile, generatedFile)
}
func writeFile(t *testing.T, root, path, content string) {
// Construct the abspath to the file's dir first so that we can
// create the parent dirs
relDir := filepath.Dir(path)
absDir := filepath.Join(root, relDir)
err := os.MkdirAll(absDir, 0700)
require.NoError(t, err)
// Now create the file in that directory
fileName := filepath.Base(path)
file := filepath.Join(absDir, fileName)
err = os.WriteFile(file, []byte(content), 0600)
require.NoError(t, err)
}