From 1326c470b6b33dc2694d7830dc91a6ab8b302343 Mon Sep 17 00:00:00 2001 From: Yoofi Quansah Date: Wed, 25 Oct 2023 21:07:44 -0500 Subject: [PATCH] chore: fix error about invalid offset with FS backend (#2286) Co-authored-by: Mark Phelps <209477+markphelps@users.noreply.github.com> --- .../fs/fixtures/empty_features/features.yml | 0 internal/storage/fs/snapshot.go | 4 ++++ internal/storage/fs/snapshot_test.go | 18 ++++++++++++++++++ 3 files changed, 22 insertions(+) create mode 100644 internal/storage/fs/fixtures/empty_features/features.yml diff --git a/internal/storage/fs/fixtures/empty_features/features.yml b/internal/storage/fs/fixtures/empty_features/features.yml new file mode 100644 index 0000000000..e69de29bb2 diff --git a/internal/storage/fs/snapshot.go b/internal/storage/fs/snapshot.go index 828ae492bc..d461615c8b 100644 --- a/internal/storage/fs/snapshot.go +++ b/internal/storage/fs/snapshot.go @@ -883,6 +883,10 @@ func find[T any](match func(T) bool, ts ...T) (t T, _ bool) { } func paginate[T any](params storage.QueryParams, less func(i, j int) bool, items ...T) (storage.ResultSet[T], error) { + if len(items) == 0 { + return storage.ResultSet[T]{}, nil + } + set := storage.ResultSet[T]{ Results: items, } diff --git a/internal/storage/fs/snapshot_test.go b/internal/storage/fs/snapshot_test.go index b5ed850254..e845c4168e 100644 --- a/internal/storage/fs/snapshot_test.go +++ b/internal/storage/fs/snapshot_test.go @@ -1662,6 +1662,24 @@ func TestFS_Invalid_BooleanFlag_Segment(t *testing.T) { require.EqualError(t, err, "flag fruit/apple rule 1 references unknown segment \"unknown\"") } +func TestFS_Empty_Features_File(t *testing.T) { + fs, _ := fs.Sub(testdata, "fixtures/empty_features") + ss, err := SnapshotFromFS(zaptest.NewLogger(t), fs) + require.NoError(t, err) + + _, err = ss.ListFlags(context.TODO(), flipt.DefaultNamespace) + require.NoError(t, err) + + _, err = ss.ListSegments(context.TODO(), flipt.DefaultNamespace) + require.NoError(t, err) + + _, err = ss.ListRollouts(context.TODO(), flipt.DefaultNamespace, "no-flag") + require.NoError(t, err) + + _, err = ss.ListRules(context.TODO(), flipt.DefaultNamespace, "no-flag") + require.NoError(t, err) +} + func TestFS_YAML_Stream(t *testing.T) { fs, _ := fs.Sub(testdata, "fixtures/yaml_stream") ss, err := SnapshotFromFS(zaptest.NewLogger(t), fs)