feat(bookfile): add Contains and OpenReaderAt utilities

This commit is contained in:
2026-09-14 19:49:50 +08:00
parent 18bd32e0f6
commit 5ad277241c
2 changed files with 88 additions and 0 deletions
+51
View File
@@ -0,0 +1,51 @@
package bookfile
import (
"os"
"path/filepath"
"testing"
)
func TestContains(t *testing.T) {
dir := t.TempDir()
sub := filepath.Join(dir, "sub")
if err := os.MkdirAll(sub, 0o755); err != nil {
t.Fatal(err)
}
if !Contains(dir, sub) {
t.Fatal("sub should be inside dir")
}
if Contains(sub, dir) {
t.Fatal("dir should not be inside sub")
}
if Contains(dir, "/completely/different") {
t.Fatal("unrelated path should not be inside dir")
}
// Same path.
if !Contains(dir, dir) {
t.Fatal("dir should contain itself")
}
}
func TestOpenReaderAt(t *testing.T) {
dir := t.TempDir()
// Create a test file.
path := filepath.Join(dir, "test.txt")
if err := os.WriteFile(path, []byte("hello"), 0o644); err != nil {
t.Fatal(err)
}
f, size, err := OpenReaderAt(dir, "test.txt")
if err != nil {
t.Fatal(err)
}
defer f.Close()
if size != 5 {
t.Fatalf("size = %d, want 5", size)
}
// Non-existent file.
_, _, err = OpenReaderAt(dir, "nope.txt")
if err == nil {
t.Fatal("expected error for non-existent file")
}
}