Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make addAuthFromNetrc ignore ENOTDIR errors #433

Merged
merged 1 commit into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion netrc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/url"
"os"
"runtime"
"syscall"

"github.com/bgentry/go-netrc/netrc"
"github.com/mitchellh/go-homedir"
Expand Down Expand Up @@ -38,7 +39,7 @@ func addAuthFromNetrc(u *url.URL) error {
// If the file is not a file, then do nothing
if fi, err := os.Stat(path); err != nil {
// File doesn't exist, do nothing
if os.IsNotExist(err) {
if serr, ok := err.(*os.PathError); ok && (os.IsNotExist(serr.Err) || serr.Err == syscall.ENOTDIR) {
return nil
}

Expand Down
40 changes: 40 additions & 0 deletions netrc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,43 @@ func TestAddAuthFromNetrc_hasUsername(t *testing.T) {
t.Fatalf("Mismatch: %q != %q", actual, expected)
}
}

func TestAddAuthFromNetrc_isNotExist(t *testing.T) {
defer tempEnv(t, "NETRC", "./testdata/netrc/_does_not_exist")()

u, err := url.Parse("http://example.com")
if err != nil {
t.Fatalf("err: %s", err)
}

if err := addAuthFromNetrc(u); err != nil {
t.Fatalf("err: %s", err)
}

// no netrc, no change:
expected := "http://example.com"
actual := u.String()
if expected != actual {
t.Fatalf("Mismatch: %q != %q", actual, expected)
}
}

func TestAddAuthFromNetrc_isNotADirectory(t *testing.T) {
defer tempEnv(t, "NETRC", "./testdata/netrc/basic/parent-not-a-dir")()

u, err := url.Parse("http://example.com")
if err != nil {
t.Fatalf("err: %s", err)
}

if err := addAuthFromNetrc(u); err != nil {
t.Fatalf("err: %s", err)
}

// no netrc, no change:
expected := "http://example.com"
actual := u.String()
if expected != actual {
t.Fatalf("Mismatch: %q != %q", actual, expected)
}
}