Skip to content

Calls to NewPath from other functions like Glob() and Parent() reset the separator to the os default/don'r preserve path opts #67

@lmvlmv

Description

@lmvlmv

When calling some of the methods like Glob() or Parent() on an pathlib.Path with a non-os matching separator defined, they can result in a call to NewPath() which defaults to the os.PathSeparator value

// NewPath returns a new OS path
func NewPath(path string, opts ...PathOpts) *Path {
	p := &Path{
		path:            path,
		fs:              afero.NewOsFs(),
		DefaultFileMode: DefaultFileMode,
		DefaultDirMode:  DefaultDirMode,
		Sep:             string(os.PathSeparator),
	}
	for _, opt := range opts {
		opt(p)
	}
	return p
}

I'm writing tests that operate on an afero in-memoryFS and am passing Path objects with the separator set to "/". Running the tests/code on a Windows machine gives unstable results as the calls keep resetting the separator to "\".

e.g.

fs := afero.NewMemMapFs()
fs.Create("testdata/main")
principal := pathlib.NewPath("testdata/main", pathlib.PathWithAfero(*fs), pathlib.PathWithSeperator("/"))
parent := principal.Parent()
fmt.Printf("Parent sep: %s\n", parent.Sep)

gives

principal sep: /
Parent sep: \

I'm new to golang and porting some code I have in python over using this library and that code needs to be multi-platform. Testing could happen on any OS.

Afero seems to handle both \ and / as valid separators, but as I'm checking correctness of the returned path the conversion the os dependent separator makes that tricky.

Seems they all visit this call:

// NewPathAfero returns a Path object with the given Afero object
//
// Deprecated: Use the PathWithAfero option in Newpath instead.
func NewPathAfero(path string, fs afero.Fs) *Path {
	return NewPath(path, PathWithAfero(fs))
}

Perhaps calls to NewPath in the utility methods could preserve the path options of the path object, unless the call needs to modify them?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions