diff --git a/TODO.md b/TODO.md index ec56ac3..2a3d807 100644 --- a/TODO.md +++ b/TODO.md @@ -21,6 +21,15 @@ Run `make frontend-viewport-test` in CI as its own step; it is not part of # Completed Steps +- 2026-10-18: when the backend runs on its own, a `DATA_DIR` that cannot be + created or read stops the start with an error naming `DATA_DIR` and its value, + as the other settings' errors do + ([#148](https://git.eeqj.de/sneak/netwatch/issues/148)); it named only the + path. A unit test starts the buffer with `DATA_DIR` below a regular file and + checks the error. The report file number is an `atomic.Int64`, as the Go style + guide asks for int types + ([#161](https://git.eeqj.de/sneak/netwatch/issues/161)); file names are + unchanged - 2026-10-18: a browser that cannot format the header's clocks still probes ([#149](https://git.eeqj.de/sneak/netwatch/issues/149)). The clocks name the local time zone through `Intl.DateTimeFormat`'s `shortGeneric` option, which diff --git a/internal/reportbuf/reportbuf.go b/internal/reportbuf/reportbuf.go index f4e925b..07aa41f 100644 --- a/internal/reportbuf/reportbuf.go +++ b/internal/reportbuf/reportbuf.go @@ -88,7 +88,7 @@ type Buffer struct { now func() time.Time // seq numbers the report files, so that two named in the same // millisecond still get different names. - seq atomic.Uint64 + seq atomic.Int64 stopOnce sync.Once // usedBytes is what Append checks against maxBytes: the size // of the report files in dataDir, plus the reports waiting to @@ -127,16 +127,18 @@ func New( lc.Append(fx.Hook{ OnStart: func(_ context.Context) error { + // The errors name DATA_DIR, as config.New's do for the + // settings it checks. err := os.MkdirAll(b.dataDir, dirPerms) if err != nil { - return fmt.Errorf("create data dir: %w", err) + return fmt.Errorf("DATA_DIR %q: %w", b.dataDir, err) } // Report files left by earlier runs count too, and are // the first to be deleted to make room. files, err := reportFiles(b.dataDir) if err != nil { - return err + return fmt.Errorf("DATA_DIR %q: %w", b.dataDir, err) } b.mu.Lock() diff --git a/internal/reportbuf/reportbuf_test.go b/internal/reportbuf/reportbuf_test.go index 9e277f4..5493fd6 100644 --- a/internal/reportbuf/reportbuf_test.go +++ b/internal/reportbuf/reportbuf_test.go @@ -108,6 +108,38 @@ func TestFailedFinalFlushFailsStop(t *testing.T) { } } +// TestUncreatableDataDirStopsStart sets DATA_DIR below a regular file, +// where no directory can be created, even by root, as the tests run in +// the backend image. The start must fail with an error naming DATA_DIR +// and its value, as the errors for the other settings do. +func TestUncreatableDataDirStopsStart(t *testing.T) { + file := filepath.Join(t.TempDir(), "file") + writeBytes(t, file, 1) + + dir := filepath.Join(file, "reports") + t.Setenv("DATA_DIR", dir) + + app := fxtest.New(t, + fx.Provide( + globals.New, + logger.New, + config.New, + reportbuf.New, + ), + fx.Invoke(func(*reportbuf.Buffer) {}), + ) + + err := app.Start(t.Context()) + if err == nil { + t.Fatal("start succeeded with DATA_DIR below a regular file") + } + + want := fmt.Sprintf("DATA_DIR %q: ", dir) + if !strings.Contains(err.Error(), want) { + t.Fatalf("start error = %q, want it to contain %q", err, want) + } +} + // TestStopWaitsForPeriodicWrite holds open the write the periodic flush // starts, then stops the app. The stop must wait until the write is // released, and the report must then be in its file. The test runs in