Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions internal/bundles/matcher/walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ type matchingWalker struct {
// for every file and directory that matches the match list.
func (i *matchingWalker) Walk(base util.AbsolutePath, fn util.AbsoluteWalkFunc) error {
return base.Walk(func(path util.AbsolutePath, info fs.FileInfo, err error) error {
if errors.Is(err, os.ErrPermission) {
i.log.Warn("permission error; skipping", "path", path)
return nil
}
if err != nil {
i.log.Warn("Unknown error while accessing file", "path", path, "error", err.Error())
return nil
}
if path != base {
m := i.matchList.Match(path)
if info.IsDir() {
Expand All @@ -84,14 +92,6 @@ func (i *matchingWalker) Walk(base util.AbsolutePath, fn util.AbsoluteWalkFunc)
}
}
}
if errors.Is(err, os.ErrPermission) {
i.log.Warn("permission error; skipping", "path", path)
return nil
}
if err != nil {
i.log.Warn("Unknown error while accessing file", "path", path, "error", err.Error())
return nil
}
return fn(path, info, err)
})
}
Expand Down
43 changes: 39 additions & 4 deletions internal/bundles/matcher/walker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ import (
"io/fs"
"os"
"path/filepath"
"strings"
"testing"

"github.com/spf13/afero"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"

"github.com/posit-dev/publisher/internal/logging"
"github.com/posit-dev/publisher/internal/logging/loggingtest"
"github.com/posit-dev/publisher/internal/util"
"github.com/posit-dev/publisher/internal/util/utiltest"
"github.com/spf13/afero"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
)

type WalkerSuite struct {
Expand Down Expand Up @@ -64,7 +66,7 @@ func (s *WalkerSuite) TestWalk() {
s.NoError(err)

includedFile := includedDir.Join("includeme")
err = includedFile.WriteFile([]byte("this is an included file"), 0600)
err = includedFile.WriteFile([]byte("this is an included subdir"), 0600)
s.NoError(err)

// This will be excluded by default
Expand Down Expand Up @@ -139,6 +141,39 @@ func (s *WalkerSuite) TestWalkPermissionErr() {
s.Equal([]string{"."}, seen)
}

type StatPermissionErrFs struct {
afero.Fs
}

func (m StatPermissionErrFs) Stat(name string) (os.FileInfo, error) {
if strings.HasSuffix(name, "foobar") {
return nil, fs.ErrPermission
}
return m.Fs.Stat(name)
}

func (s *WalkerSuite) TestWalkStatPermissionErr() {
afs := StatPermissionErrFs{Fs: s.fs}

baseDir := s.cwd.WithFs(afs)
s.cwd.Join("foobar").Mkdir(0777)

w, err := NewMatchingWalker([]string{"*"}, s.cwd, logging.New())
s.NoError(err)
s.NotNil(w)

seen := []string{}
err = w.Walk(baseDir, func(path util.AbsolutePath, info fs.FileInfo, err error) error {
s.NoError(err)
relPath, err := path.Rel(s.cwd)
s.NoError(err)
seen = append(seen, relPath.String())
return nil
})
s.NoError(err)
s.Equal([]string{"."}, seen)
}

func (s *WalkerSuite) TestWalkErr_Logged() {
// Errors while walking through files are logged but won't halt the current process
afs := utiltest.NewMockFs()
Expand Down