package extract import ( "errors" "os" "path/filepath" "strings" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func rootWith(t *testing.T, name string, size int) *os.Root { t.Helper() dir := t.TempDir() require.NoError(t, os.WriteFile(filepath.Join(dir, name), []byte(strings.Repeat("x", size)), 0o644)) r, err := os.OpenRoot(dir) require.NoError(t, err) t.Cleanup(func() { _ = r.Close() }) return r } // hyperguild#198: the size check in the watcher is a pre-check on Lstat; a note // that grows before it is read must still be capped at the read. func TestTextInMax(t *testing.T) { tests := []struct { name string size int max int64 wantErr bool }{ {"under the cap", 100, 1024, false}, {"exactly at the cap", 1024, 1024, false}, {"one byte over", 1025, 1024, true}, {"far over", 1 << 20, 1024, true}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { r := rootWith(t, "n.md", tt.size) got, err := TextInMax(r, "n.md", tt.max) if tt.wantErr { require.Error(t, err) assert.True(t, errors.Is(err, ErrTooLarge)) return } require.NoError(t, err) assert.Len(t, got, tt.size) }) } } func TestTextInMax_TheBinaryTempCopyIsCappedToo(t *testing.T) { r := rootWith(t, "n.pdf", 4096) _, err := TextInMax(r, "n.pdf", 1024) require.Error(t, err) assert.True(t, errors.Is(err, ErrTooLarge), "the temp copy of a pdf must not exceed the cap either: %v", err) } func TestTextIn_HasADefaultCap(t *testing.T) { assert.Positive(t, DefaultMaxBytes) }