Skip to content

Commit

Permalink
fix: handle case where netrc file does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
Jose Diaz-Gonzalez committed Mar 4, 2021
1 parent 9eafdb5 commit 1a26b08
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 3 deletions.
15 changes: 14 additions & 1 deletion commands/get_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,20 @@ func (c *GetCommand) Run(args []string) int {
return 1
}

n, err := netrc.Parse(filepath.Join(usr.HomeDir, ".netrc"))
netrcFile := filepath.Join(usr.HomeDir, ".netrc")
if _, err := os.Stat(netrcFile); os.IsNotExist(err) {
file, err := os.OpenFile(netrcFile, os.O_RDONLY|os.O_CREATE, 0600)
if err != nil {
c.Ui.Error(err.Error())
return 1
}
if err := file.Close(); err != nil {
c.Ui.Error(err.Error())
return 1
}
}

n, err := netrc.Parse(netrcFile)
if err != nil {
c.Ui.Error(err.Error())
return 1
Expand Down
15 changes: 14 additions & 1 deletion commands/set_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,20 @@ func (c *SetCommand) Run(args []string) int {
return 1
}

n, err := netrc.Parse(filepath.Join(usr.HomeDir, ".netrc"))
netrcFile := filepath.Join(usr.HomeDir, ".netrc")
if _, err := os.Stat(netrcFile); os.IsNotExist(err) {
file, err := os.OpenFile(netrcFile, os.O_RDONLY|os.O_CREATE, 0600)
if err != nil {
c.Ui.Error(err.Error())
return 1
}
if err := file.Close(); err != nil {
c.Ui.Error(err.Error())
return 1
}
}

n, err := netrc.Parse(netrcFile)
if err != nil {
c.Ui.Error(err.Error())
return 1
Expand Down
15 changes: 14 additions & 1 deletion commands/unset_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,20 @@ func (c *UnsetCommand) Run(args []string) int {
return 1
}

n, err := netrc.Parse(filepath.Join(usr.HomeDir, ".netrc"))
netrcFile := filepath.Join(usr.HomeDir, ".netrc")
if _, err := os.Stat(netrcFile); os.IsNotExist(err) {
file, err := os.OpenFile(netrcFile, os.O_RDONLY|os.O_CREATE, 0600)
if err != nil {
c.Ui.Error(err.Error())
return 1
}
if err := file.Close(); err != nil {
c.Ui.Error(err.Error())
return 1
}
}

n, err := netrc.Parse(netrcFile)
if err != nil {
c.Ui.Error(err.Error())
return 1
Expand Down
65 changes: 65 additions & 0 deletions test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,24 @@ teardown() {
assert_success
}

@test "(get) no netrc" {
run test -f "$HOME/.netrc"
echo "output: $output"
echo "status: $status"
assert_failure

run $NETRC_BIN get invalid
echo "output: $output"
echo "status: $status"
assert_failure
assert_output_contains "Invalid machine 'invalid' specified"

run test -f "$HOME/.netrc"
echo "output: $output"
echo "status: $status"
assert_success
}

@test "(get) empty netrc" {
run $NETRC_BIN get
echo "output: $output"
Expand Down Expand Up @@ -68,6 +86,30 @@ teardown() {
assert_output_contains "Invalid machine 'invalid' specified"
}

@test "(set) no netrc" {
run test -f "$HOME/.netrc"
echo "output: $output"
echo "status: $status"
assert_failure

run $NETRC_BIN set github.com username password
echo "output: $output"
echo "status: $status"
assert_success
assert_output_not_exists

run cat "$HOME/.netrc"
echo "output: $output"
echo "status: $status"
assert_success
assert_output "$(cat fixtures/empty/github.netrc)"

run test -f "$HOME/.netrc"
echo "output: $output"
echo "status: $status"
assert_success
}

@test "(set) empty netrc" {
run cp "fixtures/empty/.netrc" "$HOME/.netrc"
echo "output: $output"
Expand Down Expand Up @@ -154,6 +196,29 @@ teardown() {
assert_output "$(cat fixtures/valid/github-account.netrc)"
}

@test "(unset) no netrc" {
run test -f "$HOME/.netrc"
echo "output: $output"
echo "status: $status"
assert_failure

run $NETRC_BIN unset github.com
echo "output: $output"
echo "status: $status"
assert_success

run cat "$HOME/.netrc"
echo "output: $output"
echo "status: $status"
assert_success
assert_output "$(cat fixtures/empty/.netrc)"

run test -f "$HOME/.netrc"
echo "output: $output"
echo "status: $status"
assert_success
}

@test "(unset) empty netrc" {
run cp "fixtures/empty/.netrc" "$HOME/.netrc"
echo "output: $output"
Expand Down

0 comments on commit 1a26b08

Please sign in to comment.