From 89fb1ee7bb59555c2017d0e2f16e4f911eb1919d Mon Sep 17 00:00:00 2001 From: Heathcliff Date: Fri, 3 Jan 2025 10:51:48 +0100 Subject: [PATCH] filesystem: Improve test coverage Signed-off-by: Heathcliff --- pkg/fileserver/server_test.go | 14 ++++++++++++++ pkg/filesystem/filesystem.go | 5 +---- pkg/filesystem/filesystem_test.go | 16 +++++++++++----- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/pkg/fileserver/server_test.go b/pkg/fileserver/server_test.go index bee96a6..d4aa852 100644 --- a/pkg/fileserver/server_test.go +++ b/pkg/fileserver/server_test.go @@ -37,3 +37,17 @@ func TestLoggingWrapper(t *testing.T) { assert.Contains(output, "status=200") assert.Contains(output, "path=\"/test.html\"") } + +func TestUseSSL(t *testing.T) { + fs := NewFileserver("../filesystem/testdata", true) + + assert := assert.New(t) + + assert.Empty(fs.SSL, "New server should not have ssl config yet") + + fs.UseSSL("test.crt", "test.key") + + assert.True(fs.SSL.Enabled, "SSL should be enabled") + assert.Equal("test.crt", fs.SSL.Certificate, "SSL certificate should match") + assert.Equal("test.key", fs.SSL.Key, "SSL key should match") +} diff --git a/pkg/filesystem/filesystem.go b/pkg/filesystem/filesystem.go index f3418ce..9efb9e3 100644 --- a/pkg/filesystem/filesystem.go +++ b/pkg/filesystem/filesystem.go @@ -25,10 +25,7 @@ func (ifs IndexlessFilesystem) Open(path string) (http.File, error) { index := path + "/index.html" _, err := ifs.fs.Open(index) if err != nil { - closeErr := f.Close() - if closeErr != nil { - return nil, closeErr - } + f.Close() return nil, err } } diff --git a/pkg/filesystem/filesystem_test.go b/pkg/filesystem/filesystem_test.go index 4786f6c..cc6758b 100644 --- a/pkg/filesystem/filesystem_test.go +++ b/pkg/filesystem/filesystem_test.go @@ -59,6 +59,13 @@ func TestIndexlessFilesystem(t *testing.T) { } } }) + t.Run("InvalidPath", func(t *testing.T) { + assert := assert.New(t) + + f, err := fs.Open("not-a-valid-path") + assert.Nil(f) + assert.Error(err) + }) testMatrix := map[string]string{ "File": "/test.html", @@ -68,11 +75,10 @@ func TestIndexlessFilesystem(t *testing.T) { for name, path := range testMatrix { t.Run(name, func(t *testing.T) { f, err := fs.Open(path) - if assert.Nil(t, err) { - err = f.Close() - if err != nil { - t.Fatalf("Unexpected error closing file: %v", err) - } + if assert.NoError(t, err) { + t.Cleanup(func() { + f.Close() + }) } }) }