From 62ff944469dab2ddb84854f43e283db57af2d0e0 Mon Sep 17 00:00:00 2001 From: Linghua Zhang Date: Mon, 29 Jul 2024 19:54:36 +0900 Subject: [PATCH] fix: #33 don't show empty section --- internal/indexer/indexer.go | 4 +++- internal/indexer/indexer_test.go | 14 +++++++++++++- internal/testdata/testdata.go | 11 +++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/internal/indexer/indexer.go b/internal/indexer/indexer.go index 3c4a64f..001654f 100644 --- a/internal/indexer/indexer.go +++ b/internal/indexer/indexer.go @@ -56,7 +56,9 @@ func Build(metadata []config.SectionMetadata, option config.ExtractOption) ([]Se } slugs[slug] = true - sections = append(sections, s) + if len(s.ImageSets) > 0 { + sections = append(sections, s) + } } return sections, nil diff --git a/internal/indexer/indexer_test.go b/internal/indexer/indexer_test.go index 7544d68..c4f447c 100644 --- a/internal/indexer/indexer_test.go +++ b/internal/indexer/indexer_test.go @@ -61,6 +61,19 @@ func TestBuildDuplicatedSlugs(t *testing.T) { assert.NotNil(t, err) } +func TestBuildEmptySection(t *testing.T) { + var meta config.SectionMetadata + var emptyMeta config.SectionMetadata + _ = mapstructure.Decode(testdata.Collection1, &meta) + _ = mapstructure.Decode(testdata.EmptyCollection, &emptyMeta) + + data := []config.SectionMetadata{meta, emptyMeta} + + sections, _ := Build(data, defaultOption) + assert.Equal(t, 1, len(sections)) + assert.Equal(t, testdata.Collection1["title"], sections[0].Title) +} + func TestBuildImageSets(t *testing.T) { expectedAscendingFileNames := []string{ testdata.Collection1FileName1, @@ -89,7 +102,6 @@ func TestBuildImageSets(t *testing.T) { sets[2].FileName, }) } - func TestInvalidBuildImageSets(t *testing.T) { tmp, _ := os.MkdirTemp("", "foto-test") path := filepath.Join(tmp, "folder-not-exist") diff --git a/internal/testdata/testdata.go b/internal/testdata/testdata.go index 4b85516..3924948 100644 --- a/internal/testdata/testdata.go +++ b/internal/testdata/testdata.go @@ -102,6 +102,17 @@ var ( Collection2FileName3 = "2023-04-29.jpg" ) +var ( + EmptyCollection = map[string]interface{}{ + "title": "Empty Collection", + "text": "This is an empty collection", + "slug": "empty-slug-section", + "folder": "../../testdata/empty-section", + "ascending": true, + "imageSets": []map[string]interface{}{}, + } +) + var ( RotatedImageFile = "../../testdata/rotated.jpg" RotatedImageWidth = 1440