Skip to content

Commit aba69a2

Browse files
authored
Adding validation to ensure collision of api defintions do not occur (#112)
1 parent e52978b commit aba69a2

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// +build awsinclude
2+
3+
package apis
4+
5+
import (
6+
"os/exec"
7+
"strings"
8+
"testing"
9+
)
10+
11+
func TestCollidingFolders(t *testing.T) {
12+
m := map[string]struct{}{}
13+
folders, err := getFolderNames()
14+
if err != nil {
15+
t.Error(err)
16+
}
17+
18+
for _, folder := range folders {
19+
lcName := strings.ToLower(folder)
20+
if _, ok := m[lcName]; ok {
21+
t.Errorf("folder %q collision detected", folder)
22+
}
23+
m[lcName] = struct{}{}
24+
}
25+
}
26+
27+
func getFolderNames() ([]string, error) {
28+
cmd := exec.Command("git", "ls-tree", "-d", "--name-only", "HEAD")
29+
output, err := cmd.Output()
30+
if err != nil {
31+
return nil, err
32+
}
33+
34+
return strings.Split(string(output), "\n"), nil
35+
}

models/apis/stub.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// +build awsinclude
2+
3+
package apis

private/model/cli/gen-api/main.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func main() {
131131

132132
for svcName := range excludeServices {
133133
if strings.Contains(os.Getenv("SERVICES"), svcName) {
134-
fmt.Printf("Service %s is not supported\n", svcName)
134+
fmt.Fprintf(os.Stderr, "Service %s is not supported\n", svcName)
135135
os.Exit(1)
136136
}
137137
}
@@ -140,6 +140,10 @@ func main() {
140140

141141
// Remove old API versions from list
142142
m := map[string]bool{}
143+
// caches paths to ensure we are not overriding previously generated
144+
// code.
145+
servicePaths := map[string]struct{}{}
146+
143147
for i := range files {
144148
idx := len(files) - 1 - i
145149
parts := strings.Split(files[idx], string(filepath.Separator))
@@ -167,6 +171,13 @@ func main() {
167171
continue
168172
}
169173

174+
if _, ok := servicePaths[genInfo.PackageDir]; ok {
175+
fmt.Fprintf(os.Stderr, "Path %q has already been generated", genInfo.PackageDir)
176+
os.Exit(1)
177+
}
178+
179+
servicePaths[genInfo.PackageDir] = struct{}{}
180+
170181
wg.Add(1)
171182
go func(g *generateInfo, filename string) {
172183
defer wg.Done()

0 commit comments

Comments
 (0)