Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions internal/tools/verifyreadmes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,11 @@ func verifyReadme(path string, info os.FileInfo, err error) error {
}
}


// Check that a README.md exists in the same directory as the go.mod file.
readme := filepath.Join(filepath.Dir(path), readmeFilename)
_, err = os.Stat(readme)
if os.IsNotExist(err) {
err = fmt.Errorf("couldn't find %s for %q", readmeFilename, filepath.Dir(path))
return fmt.Errorf("couldn't find %s for %q", readmeFilename, filepath.Dir(path))
}

return err
Expand All @@ -65,13 +64,18 @@ func main() {
fmt.Println("Verifying READMEs in", root)

var errs []string
filepath.Walk(root, func(path string, info fs.FileInfo, err error) error {
walkErr := filepath.Walk(root, func(path string, info fs.FileInfo, err error) error {
if err := verifyReadme(path, info, err); err != nil {
errs = append(errs, err.Error())
}
return nil // continue walking
})

if walkErr != nil {
fmt.Println("Error walking directory: ", walkErr)
os.Exit(1)
}

if len(errs) > 0 {
fmt.Println("Some readme files couldn't be found.")
fmt.Println(strings.Join(errs, "\n"))
Expand Down
98 changes: 98 additions & 0 deletions internal/tools/verifyreadmes/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

// Package verifyreadmes is used to verify that all go modules in the repository
// have a README.md file.
package main

import (
"os"
"path/filepath"
"testing"
)

func TestVerifyReadme(t *testing.T) {
// Create a temporary directory for testing
tmpDir, err := os.MkdirTemp("", "test-readme")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)

testCases := []struct {
name string
setup func(t *testing.T, dir string) (string, os.FileInfo)
want bool
}{
{
name: "README exists",
setup: func(t *testing.T, dir string) (string, os.FileInfo) {
path := filepath.Join(dir, "with-readme")
if err := os.Mkdir(path, 0755); err != nil {
t.Fatal(err)
}
goModPath := filepath.Join(path, "go.mod")
if _, err := os.Create(goModPath); err != nil {
t.Fatal(err)
}
if _, err := os.Create(filepath.Join(path, "README.md")); err != nil {
t.Fatal(err)
}
info, err := os.Stat(goModPath)
if err != nil {
t.Fatal(err)
}
return goModPath, info
},
want: false,
},
{
name: "Excluded directory",
setup: func(t *testing.T, dir string) (string, os.FileInfo) {
path := filepath.Join(dir, "internal", "excluded")
if err := os.MkdirAll(path, 0755); err != nil {
t.Fatal(err)
}
goModPath := filepath.Join(path, "go.mod")
if _, err := os.Create(goModPath); err != nil {
t.Fatal(err)
}
info, err := os.Stat(goModPath)
if err != nil {
t.Fatal(err)
}
return goModPath, info
},
want: false,
},
{
name: "No go.mod",
setup: func(t *testing.T, dir string) (string, os.FileInfo) {
path := filepath.Join(dir, "no-go-mod")
if err := os.Mkdir(path, 0755); err != nil {
t.Fatal(err)
}
readmePath := filepath.Join(path, "README.md")
if _, err := os.Create(readmePath); err != nil {
t.Fatal(err)
}
info, err := os.Stat(readmePath)
if err != nil {
t.Fatal(err)
}
return readmePath, info
},
want: false,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
path, info := tc.setup(t, tmpDir)
got := verifyReadme(path, info, nil)
if (err != nil) != tc.want {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we check the actual error rather than its presence?

t.Errorf("Expected error: %v, got: %v", tc.want, got)
}
})
}
}
Loading