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
28 changes: 28 additions & 0 deletions docs/asciidoc/asciidoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,12 +362,40 @@ func inspect(rego []string) ([]ast.FlatAnnotationsRefSet, error) {
return annotations, nil
}

// removeAdocFiles removes all .adoc files from the given directory.
// If the directory does not exist, it returns nil.
func removeAdocFiles(dir string) error {
entries, err := os.ReadDir(dir)
if err != nil {
if os.IsNotExist(err) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[low] best-practice

Uses os.IsNotExist(err) instead of the idiomatic errors.Is(err, fs.ErrNotExist). While both work correctly here (os.ReadDir returns an unwrapped *os.PathError), the errors.Is form handles wrapped errors and is the Go 1.13+ convention. The io/fs package is already imported in this file.

Suggested fix: Replace if os.IsNotExist(err) with if errors.Is(err, fs.ErrNotExist) and add "errors" to the import block.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Will fullsend the coder notice this suggestion made by fullsend the reviewer, and make the suggested change?

return nil
}
return fmt.Errorf("reading directory %q: %w", dir, err)
}
for _, entry := range entries {
if !entry.IsDir() && strings.HasSuffix(entry.Name(), ".adoc") {
if err := os.Remove(filepath.Join(dir, entry.Name())); err != nil {
return fmt.Errorf("removing file %q: %w", entry.Name(), err)
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[low] nil-dereference

Pre-existing bug (not introduced by this PR): for _, p := range *d.Packages panics when d.Packages is nil, which occurs when SetAnnotations finds zero packages for a doc category. While all five categories currently have packages, adding a nil guard would prevent a crash if a category ever becomes empty.

Suggested fix: Wrap the range loop in if d.Packages != nil { ... }.

}
}
return nil
}

func GenerateAsciidoc(module string, rego ...string) error {
annotations, err := inspect(rego)
if err != nil {
return err
}

// Remove all .adoc files from the packages directory before
// regenerating. Everything in this directory is generated, so
// removing stale files from deleted packages is safe.
packagesDir := filepath.Join(module, "pages", "packages")
if err := removeAdocFiles(packagesDir); err != nil {
return err
}

for _, d := range docs {
d.SetAnnotations(annotations)
if err := d.generateNav(module); err != nil {
Expand Down
93 changes: 93 additions & 0 deletions docs/asciidoc/asciidoc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package asciidoc

import (
"os"
"path/filepath"
"testing"
"time"
)
Expand Down Expand Up @@ -151,3 +153,94 @@ func TestFormatTimeRoundTrip(t *testing.T) {
t.Errorf("Round-trip failed: original %v != parsed %v", original, parsed)
}
}

func TestRemoveAdocFiles(t *testing.T) {
t.Run("removes .adoc files", func(t *testing.T) {
dir := t.TempDir()

// Create .adoc files
for _, name := range []string{"release_foo.adoc", "pipeline_bar.adoc"} {
if err := os.WriteFile(filepath.Join(dir, name), []byte("content"), 0644); err != nil {
t.Fatal(err)
}
}

if err := removeAdocFiles(dir); err != nil {
t.Fatalf("removeAdocFiles() returned error: %v", err)
}

entries, err := os.ReadDir(dir)
if err != nil {
t.Fatal(err)
}
if len(entries) != 0 {
t.Errorf("expected empty directory, got %d entries", len(entries))
}
})

t.Run("preserves non-adoc files", func(t *testing.T) {
dir := t.TempDir()

// Create a mix of files
if err := os.WriteFile(filepath.Join(dir, "stale.adoc"), []byte("stale"), 0644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "keep.txt"), []byte("keep"), 0644); err != nil {
t.Fatal(err)
}

if err := removeAdocFiles(dir); err != nil {
t.Fatalf("removeAdocFiles() returned error: %v", err)
}

entries, err := os.ReadDir(dir)
if err != nil {
t.Fatal(err)
}
if len(entries) != 1 {
t.Fatalf("expected 1 entry, got %d", len(entries))
}
if entries[0].Name() != "keep.txt" {
t.Errorf("expected keep.txt, got %s", entries[0].Name())
}
})

t.Run("preserves subdirectories", func(t *testing.T) {
dir := t.TempDir()

subDir := filepath.Join(dir, "subdir")
if err := os.Mkdir(subDir, 0755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "stale.adoc"), []byte("stale"), 0644); err != nil {
t.Fatal(err)
}

if err := removeAdocFiles(dir); err != nil {
t.Fatalf("removeAdocFiles() returned error: %v", err)
}

entries, err := os.ReadDir(dir)
if err != nil {
t.Fatal(err)
}
if len(entries) != 1 {
t.Fatalf("expected 1 entry, got %d", len(entries))
}
if entries[0].Name() != "subdir" || !entries[0].IsDir() {
t.Errorf("expected subdir directory, got %s", entries[0].Name())
}
})

t.Run("nonexistent directory is not an error", func(t *testing.T) {
if err := removeAdocFiles(filepath.Join(t.TempDir(), "nonexistent")); err != nil {
t.Fatalf("removeAdocFiles() returned error for nonexistent dir: %v", err)
}
})

t.Run("empty directory is not an error", func(t *testing.T) {
if err := removeAdocFiles(t.TempDir()); err != nil {
t.Fatalf("removeAdocFiles() returned error for empty dir: %v", err)
}
})
}
Loading