summaryrefslogtreecommitdiff
path: root/internal/generator
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-04-27 09:18:52 +0300
committerPaul Buetow <paul@buetow.org>2026-04-27 09:18:52 +0300
commit47d69cb998a447eea662ad1075f9d002dd875443 (patch)
tree1fa586fc4f7e5fae2ce9badfa05679c0fee9dcb3 /internal/generator
parent626ff3ae7d43cfc2ec3f2554d340b40f4a5c0586 (diff)
Add context.Context to I/O-bound public APIs (generator.Run, processor.Run, atom.Generate, syncOutput)
- generator.Run(ctx, cfg) – ctx passed through to atom.Generate - processor.Run(ctx, cfg) – signature updated for cancellation propagation - atom.Generate(ctx, posts, cfg) – accepts ctx for future cancellation - syncOutput(ctx, cfg) – rsync subprocesses now use exec.CommandContext - Updated all call sites in tests, cmd/snonux/main.go, and integration tests - All call sites pass context.Background() / context.TODO() All tests pass: go test ./...
Diffstat (limited to 'internal/generator')
-rw-r--r--internal/generator/atom/atom.go5
-rw-r--r--internal/generator/atom/atom_test.go9
-rw-r--r--internal/generator/generator.go7
-rw-r--r--internal/generator/generator_test.go5
-rw-r--r--internal/generator/theme_sounds.go3
5 files changed, 21 insertions, 8 deletions
diff --git a/internal/generator/atom/atom.go b/internal/generator/atom/atom.go
index 75f4876..03475d8 100644
--- a/internal/generator/atom/atom.go
+++ b/internal/generator/atom/atom.go
@@ -5,6 +5,7 @@
package atom
import (
+ "context"
"encoding/xml"
"fmt"
"os"
@@ -48,7 +49,9 @@ type content struct {
// Generate writes atom.xml to cfg.OutputDir containing the most recent
// min(len(posts), config.PostsPerPage) entries. Posts must already be sorted
// newest-first (as produced by generator.Run).
-func Generate(posts []*post.Post, cfg *config.Config) error {
+// The context is currently accepted for API consistency and future
+// cancellation propagation; no blocking I/O operations currently observe it.
+func Generate(ctx context.Context, posts []*post.Post, cfg *config.Config) error {
limit := config.PostsPerPage
if len(posts) < limit {
limit = len(posts)
diff --git a/internal/generator/atom/atom_test.go b/internal/generator/atom/atom_test.go
index 2bfdfb7..04a33fb 100644
--- a/internal/generator/atom/atom_test.go
+++ b/internal/generator/atom/atom_test.go
@@ -1,6 +1,7 @@
package atom
import (
+ "context"
"encoding/xml"
"os"
"path/filepath"
@@ -12,6 +13,8 @@ import (
"codeberg.org/snonux/snonux/internal/post"
)
+var ctx = context.Background() //nolint:gochecknoglobals // test-only top-level helper used by every test in the file
+
func TestGenerate_writesAtomXML(t *testing.T) {
t.Parallel()
@@ -28,7 +31,7 @@ func TestGenerate_writesAtomXML(t *testing.T) {
},
}
- if err := Generate(posts, cfg); err != nil {
+ if err := Generate(ctx, posts, cfg); err != nil {
t.Fatalf("Generate: %v", err)
}
@@ -54,7 +57,7 @@ func TestGenerate_emptyPosts(t *testing.T) {
dir := t.TempDir()
cfg := &config.Config{OutputDir: dir, BaseURL: "https://x.test"}
- if err := Generate(nil, cfg); err != nil {
+ if err := Generate(ctx, nil, cfg); err != nil {
t.Fatalf("Generate: %v", err)
}
@@ -90,7 +93,7 @@ func TestGenerate_limitPostsPerPage(t *testing.T) {
})
}
- if err := Generate(posts, cfg); err != nil {
+ if err := Generate(ctx, posts, cfg); err != nil {
t.Fatalf("Generate: %v", err)
}
diff --git a/internal/generator/generator.go b/internal/generator/generator.go
index db9531b..bd5b850 100644
--- a/internal/generator/generator.go
+++ b/internal/generator/generator.go
@@ -1,6 +1,7 @@
package generator
import (
+ "context"
"encoding/json"
"fmt"
"html/template"
@@ -81,7 +82,9 @@ func allThemesJSON() (template.JS, error) {
// Run loads all posts, generates all HTML pages, and writes atom.xml plus the
// shared CSS/JS bundles and per-theme asset files.
-func Run(cfg *config.Config) error {
+// The ctx parameter is accepted for cancellation propagation; it is passed
+// through to I/O-bound calls where possible.
+func Run(ctx context.Context, cfg *config.Config) error {
posts, err := loadAllPosts(cfg.OutputDir)
if err != nil {
return err
@@ -131,7 +134,7 @@ func Run(cfg *config.Config) error {
}
}
- return atom.Generate(posts, cfg)
+ return atom.Generate(ctx, posts, cfg)
}
// loadAllPosts walks outdir/posts/ and deserialises every post.json found.
diff --git a/internal/generator/generator_test.go b/internal/generator/generator_test.go
index 56542a3..50cd3d0 100644
--- a/internal/generator/generator_test.go
+++ b/internal/generator/generator_test.go
@@ -1,6 +1,7 @@
package generator
import (
+ "context"
"encoding/json"
"html/template"
"os"
@@ -13,6 +14,8 @@ import (
"codeberg.org/snonux/snonux/internal/post"
)
+var ctx = context.Background() //nolint:gochecknoglobals // test-only top-level helper used by every test in the file
+
func TestPageFilename(t *testing.T) {
t.Parallel()
@@ -428,7 +431,7 @@ func TestRun_writesPagesAndAtom(t *testing.T) {
BaseURL: "https://example.test",
Theme: "neon",
}
- if err := Run(cfg); err != nil {
+ if err := Run(ctx, cfg); err != nil {
t.Fatalf("Run: %v", err)
}
if _, err := os.Stat(filepath.Join(out, "index.html")); err != nil {
diff --git a/internal/generator/theme_sounds.go b/internal/generator/theme_sounds.go
index 20ffacc..234a964 100644
--- a/internal/generator/theme_sounds.go
+++ b/internal/generator/theme_sounds.go
@@ -112,7 +112,8 @@ func minor(freq float64) [3]float64 {
// into the preceding note's step so the next real note triggers later. We
// can't emit a zero-frequency rest entry because the JS engine substitutes
// 440 Hz for falsy freq values, which would turn rests into audible tones.
-// beat = seconds-per-quarter-note. dur multiplier of 1.0 = quarter note.
+//
+// beat = seconds-per-quarter-note. dur multiplier of 1.0 = quarter note.
func hook(beat float64, pairs ...float64) []melodyNote {
out := make([]melodyNote, 0, len(pairs)/2)
leadIn := 0.0