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 efc60b7
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 14 deletions.
91 changes: 78 additions & 13 deletions merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,38 @@ func MergeDir(dir string, conditions Conditions, opts *MergeDirOptions) ([]*File
if opts.AcceptFile == nil {
opts.AcceptFile = DefaultFileAcceptor
}
if opts.FS == nil {
opts.FS = os.DirFS(dir)
dir = "."
}
// 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("opts.FS == nil") //nolint:forbidigo
// opts.FS = os.DirFS(dir)
// fmt.Printf("MIDDLE1: dir=%v opts.FS=%#v\n", dir, opts.FS) //nolint:forbidigo
// } else {
// var err error
// fmt.Printf("MIDDLE2A: dir=%v opts.FS=%#v\n", dir, opts.FS) //nolint:forbidigo
// opts.FS, err = fs.Sub(opts.FS, dir)
// if err != nil {
// return nil, fmt.Errorf("fs.Sub of %v and %v failed: %w", opts.FS, dir, err)
// }
// // if runtime.GOOS == "windows" {
// // dir = ""
// // } else {
// // dir = "."
// // }
// fmt.Printf("MIDDLE2B: 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,15 +306,39 @@ 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 {
reader, ok := fsys.(fs.ReadDirFS)
if !ok {
return fmt.Errorf("unexpected %T wanted fs.ReadDirFS", fsys)
}
fmt.Printf("walkDir: dir=%v fsys=%#v\n", dir, fsys) //nolint:forbidigo

items, err := reader.ReadDir(dir)
var items []fs.DirEntry
var err error

if fsys != nil {
fmt.Printf("walkDir: A\n") //nolint:forbidigo
if rr, ok := fsys.(fs.ReadDirFS); ok {
fmt.Printf("walkDir: B\n") //nolint:forbidigo
items, err = rr.ReadDir(dir)
}
}
if err != nil {
return fmt.Errorf("fs.readdir %s failed: %w", dir, err)
}
if len(items) == 0 {
fmt.Printf("walkDir: C\n") //nolint:forbidigo
items, err = os.ReadDir(dir)
}
if err != nil {
return fmt.Errorf("listing %s failed: %w", dir, err)
return fmt.Errorf("os.readdir %s failed: %w", dir, err)
}

for i := range items {
Expand Down Expand Up @@ -356,7 +408,14 @@ func readValidateOptsFromFile(path string, opts *MergeDirOptions) *ValidateOpts
if opts.ValidateOptsExtension != "" {
where := strings.TrimSuffix(path, filepath.Ext(path)) + opts.ValidateOptsExtension

fd, err := opts.FS.Open(where)
var fd fs.File
var err error

if opts.FS != nil {
fd, err = opts.FS.Open(where)
} else {
fd, err = os.Open(where)
}
if err != nil {
return nil
}
Expand All @@ -369,12 +428,18 @@ func readValidateOptsFromFile(path string, opts *MergeDirOptions) *ValidateOpts
return nil
}

func readFile(fs fs.FS, path string, as FileAcceptance, validateOpts *ValidateOpts) (*File, error) {
func readFile(fsys fs.FS, path string, as FileAcceptance, validateOpts *ValidateOpts) (*File, error) {
if as == SkipFile {
return nil, nil
}

fd, err := fs.Open(path)
var fd fs.File
var err error
if fsys != nil {
fd, err = fsys.Open(path)
} else {
fd, err = os.Open(path)
}
if err != nil {
return nil, fmt.Errorf("opening %s failed: %w", path, err)
}
Expand Down
32 changes: 31 additions & 1 deletion merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,9 +585,30 @@ func TestMergeDir__DefaultFileAcceptor(t *testing.T) {
require.Equal(t, SkipFile, output)
}

func TestMergeDir_SubFS(t *testing.T) {
func TestMergeDir(t *testing.T) {
dir := t.TempDir()

src, err := os.Open(filepath.Join("test", "testdata", "ppd-debit.ach"))
require.NoError(t, err)
t.Cleanup(func() { src.Close() })

dst, err := os.Create(filepath.Join(dir, "input.ach"))
require.NoError(t, err)

_, err = io.Copy(dst, src)
require.NoError(t, err)
require.NoError(t, dst.Close())

var conditions Conditions
merged, err := MergeDir(dir, conditions, nil)
require.NoError(t, err)
require.Len(t, merged, 1)
}

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 All @@ -602,11 +623,20 @@ func TestMergeDir_SubFS(t *testing.T) {
require.NoError(t, dst.Close())

var conditions Conditions

// partial dir + sub
merged, err := MergeDir(sub, conditions, &MergeDirOptions{
FS: os.DirFS(dir),
})
require.NoError(t, err)
require.Len(t, merged, 1)

// full dir
merged, err = MergeDir(".", conditions, &MergeDirOptions{
FS: os.DirFS(filepath.Join(dir, sub)),
})
require.NoError(t, err)
require.Len(t, merged, 1)
}

func TestMergeDirHelpers(t *testing.T) {
Expand Down

0 comments on commit efc60b7

Please sign in to comment.