From cb8b6bc2ffb05f108d9b406cfe7c598875181849 Mon Sep 17 00:00:00 2001 From: LandonTClipp Date: Mon, 7 Sep 2020 00:05:27 -0500 Subject: [PATCH 1/2] Make MemMapFs implement Lstater --- go.mod | 1 + memmap.go | 5 +++++ memmap_test.go | 23 +++++++++++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/go.mod b/go.mod index abe4fe1c..7966d4bd 100644 --- a/go.mod +++ b/go.mod @@ -2,6 +2,7 @@ module github.com/spf13/afero require ( github.com/pkg/sftp v1.10.1 + github.com/stretchr/testify v1.4.0 golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586 golang.org/x/text v0.3.3 ) diff --git a/memmap.go b/memmap.go index bbcc2381..0fa95924 100644 --- a/memmap.go +++ b/memmap.go @@ -317,6 +317,11 @@ func (m *MemMapFs) Rename(oldname, newname string) error { return nil } +func (m *MemMapFs) LstatIfPossible(name string) (os.FileInfo, bool, error) { + fileInfo, err := m.Stat(name) + return fileInfo, false, err +} + func (m *MemMapFs) Stat(name string) (os.FileInfo, error) { f, err := m.Open(name) if err != nil { diff --git a/memmap_test.go b/memmap_test.go index 6ce742cb..25e7f09d 100644 --- a/memmap_test.go +++ b/memmap_test.go @@ -8,6 +8,9 @@ import ( "runtime" "testing" "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestNormalizePath(t *testing.T) { @@ -650,3 +653,23 @@ func TestMemFsOpenFileModeIllegal(t *testing.T) { t.Fatalf("should not be able to use OpenFile to set illegal mode: %s", info.Mode().String()) } } + +// LstatIfPossible should always return false, since MemMapFs does not +// support symlinks. +func TestMemFsLstatIfPossible(t *testing.T) { + t.Parallel() + + fs := NewMemMapFs() + + // We assert that fs implements Lstater + fsAsserted, ok := fs.(Lstater) + require.True(t, ok, "The filesytem does not implement Lstater") + + file, err := fs.OpenFile("/a.txt", os.O_CREATE, 0o644) + require.NoError(t, err) + defer file.Close() + + _, lstatCalled, err := fsAsserted.LstatIfPossible("/a.txt") + assert.NoError(t, err) + assert.False(t, lstatCalled) +} From bf960e8dcb8b0489801bb18444499d29c02a961d Mon Sep 17 00:00:00 2001 From: LandonTClipp Date: Sun, 13 Sep 2020 14:01:19 -0500 Subject: [PATCH 2/2] Removing use of testify --- go.mod | 1 - memmap_test.go | 19 ++++++++++++------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/go.mod b/go.mod index 7966d4bd..abe4fe1c 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,6 @@ module github.com/spf13/afero require ( github.com/pkg/sftp v1.10.1 - github.com/stretchr/testify v1.4.0 golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586 golang.org/x/text v0.3.3 ) diff --git a/memmap_test.go b/memmap_test.go index 25e7f09d..476909d6 100644 --- a/memmap_test.go +++ b/memmap_test.go @@ -8,9 +8,6 @@ import ( "runtime" "testing" "time" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" ) func TestNormalizePath(t *testing.T) { @@ -663,13 +660,21 @@ func TestMemFsLstatIfPossible(t *testing.T) { // We assert that fs implements Lstater fsAsserted, ok := fs.(Lstater) - require.True(t, ok, "The filesytem does not implement Lstater") + if !ok { + t.Fatalf("The filesytem does not implement Lstater") + } file, err := fs.OpenFile("/a.txt", os.O_CREATE, 0o644) - require.NoError(t, err) + if err != nil { + t.Fatalf("Error when opening file: %v", err) + } defer file.Close() _, lstatCalled, err := fsAsserted.LstatIfPossible("/a.txt") - assert.NoError(t, err) - assert.False(t, lstatCalled) + if err != nil { + t.Fatalf("Function returned err: %v", err) + } + if lstatCalled { + t.Fatalf("Function indicated lstat was called. This should never be true.") + } }