Skip to content

Commit

Permalink
fix: defer to os.Open for windows
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdecaf committed Mar 21, 2024
1 parent f5efa42 commit 1f02b1b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
30 changes: 30 additions & 0 deletions merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"io/fs"
"os"
"path/filepath"
"runtime"
"strings"
"sync"

Expand Down Expand Up @@ -187,10 +188,26 @@ func MergeDir(dir string, conditions Conditions, opts *MergeDirOptions) ([]*File
if opts.AcceptFile == nil {
opts.AcceptFile = DefaultFileAcceptor
}
fmt.Printf("BEFORE: dir=%v opts.FS=%#v\n", dir, opts.FS) //nolint:forbidigo
if runtime.GOOS == "windows" {
// Go running on windows does not support os.DirFS properly
// See: https://github.com/golang/go/issues/44279
fmt.Println(" windows") //nolint:forbidigo
opts.FS = &osFilesystem{}
dir = filepath.Join(filepath.VolumeName(dir), dir)
fmt.Printf("MIDDLE2: dir=%v opts.FS=%#v\n", dir, opts.FS) //nolint:forbidigo
}
if opts.FS == nil {
fmt.Println(" other") //nolint:forbidigo
opts.FS = os.DirFS(dir)
dir = "."
fmt.Printf("MIDDLE1: dir=%v opts.FS=%#v\n", dir, opts.FS) //nolint:forbidigo
}
fmt.Printf("AFTER: dir=%v opts.FS=%#v\n", dir, opts.FS) //nolint:forbidigo

// dir + sub: "C:\\Users\\RUNNER~1\\AppData\\Local\\Temp\\TestMergeDir_SubFS959700692\\001\\a\\b\\c"
// BEFORE: dir="a\\b\\c" opts.FS="C:\\Users\\RUNNER~1\\AppData\\Local\\Temp\\TestMergeDir_SubFS959700692\\001"
// AFTER: dir="a\\b\\c" opts.FS="C:\\Users\\RUNNER~1\\AppData\\Local\\Temp\\TestMergeDir_SubFS959700692\\001"

sorted := &outFile{}
var setup sync.Once
Expand Down Expand Up @@ -278,7 +295,20 @@ func MergeDir(dir string, conditions Conditions, opts *MergeDirOptions) ([]*File
return convertToFiles(sorted, conditions)
}

// osFilesystem is an io/fs.FS which defers to the os package for opening files
// See: https://github.com/golang/go/issues/44279
type osFilesystem struct{}

func (*osFilesystem) Open(name string) (fs.File, error) {
fmt.Printf("osFilesystem.Open(%v)\n", name) //nolint:forbidigo
return os.Open(name)
}

var _ fs.FS = new(osFilesystem)

func walkDir(fsys fs.FS, dir string, discoveredPaths chan string) error {
fmt.Printf("walkDir: dir=%v fsys=%#v\n", dir, fsys) //nolint:forbidigo

reader, ok := fsys.(fs.ReadDirFS)
if !ok {
return fmt.Errorf("unexpected %T wanted fs.ReadDirFS", fsys)
Expand Down
3 changes: 2 additions & 1 deletion merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,9 +585,10 @@ func TestMergeDir__DefaultFileAcceptor(t *testing.T) {
require.Equal(t, SkipFile, output)
}

func TestMergeDir_SubFS(t *testing.T) {
func TestMergeDir_WithFS(t *testing.T) {
dir := t.TempDir()
sub := filepath.Join("a", "b", "c")
fmt.Printf("dir + sub: %v\n", filepath.Join(dir, sub)) //nolint:forbidigo
require.NoError(t, os.MkdirAll(filepath.Join(dir, sub), 0777))

src, err := os.Open(filepath.Join("test", "testdata", "ppd-debit.ach"))
Expand Down

0 comments on commit 1f02b1b

Please sign in to comment.