Answer architecture round 2: read go.mod's module path as its first field, unquoted, and fail when no import matches it
This commit is contained in:
+9
-3
@@ -17,15 +17,17 @@ func TestPackageImports(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
module, found := "", false
|
module := ""
|
||||||
for _, line := range strings.Split(string(mod), "\n") {
|
for _, line := range strings.Split(string(mod), "\n") {
|
||||||
if module, found = strings.CutPrefix(strings.TrimSpace(line), "module "); found {
|
if f := strings.Fields(line); len(f) > 1 && f[0] == "module" {
|
||||||
|
module = strings.Trim(f[1], `"`)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !found {
|
if module == "" {
|
||||||
t.Fatal("go.mod names no module")
|
t.Fatal("go.mod names no module")
|
||||||
}
|
}
|
||||||
|
checked := 0
|
||||||
allowed := map[string][]string{
|
allowed := map[string][]string{
|
||||||
".": {"internal/drawstate"},
|
".": {"internal/drawstate"},
|
||||||
"cmd/fejkdata": {"."},
|
"cmd/fejkdata": {"."},
|
||||||
@@ -58,6 +60,7 @@ func TestPackageImports(t *testing.T) {
|
|||||||
if imp != module && !strings.HasPrefix(imp, module+"/") {
|
if imp != module && !strings.HasPrefix(imp, module+"/") {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
checked++
|
||||||
rel := strings.TrimPrefix(strings.TrimPrefix(imp, module), "/")
|
rel := strings.TrimPrefix(strings.TrimPrefix(imp, module), "/")
|
||||||
if rel == "" {
|
if rel == "" {
|
||||||
rel = "."
|
rel = "."
|
||||||
@@ -71,6 +74,9 @@ func TestPackageImports(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
if checked == 0 {
|
||||||
|
t.Fatalf("no package imports a package of module %s, which go.mod names", module)
|
||||||
|
}
|
||||||
for dir := range allowed {
|
for dir := range allowed {
|
||||||
if !seen[dir] {
|
if !seen[dir] {
|
||||||
t.Errorf("listed package %s does not exist", dir)
|
t.Errorf("listed package %s does not exist", dir)
|
||||||
|
|||||||
Reference in New Issue
Block a user