Skip to content

Commit

Permalink
Merge pull request #433 from adrian-bl/netrc-fix
Browse files Browse the repository at this point in the history
Make addAuthFromNetrc ignore ENOTDIR errors
  • Loading branch information
crw committed Dec 4, 2023
2 parents 0298a22 + 5ccb39a commit 975961f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
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)
}
}

0 comments on commit 975961f

Please sign in to comment.