Skip to content

Commit

Permalink
tests: add unit tests
Browse files Browse the repository at this point in the history
Also upgrade josegonzalez/cli-skeleton to fix a few argument parsing bugs.
  • Loading branch information
Jose Diaz-Gonzalez committed Feb 21, 2021
1 parent 3381284 commit 5239944
Show file tree
Hide file tree
Showing 10 changed files with 321 additions and 5 deletions.
5 changes: 5 additions & 0 deletions commands/set_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func (c *SetCommand) Run(args []string) int {
name := arguments["name"].StringValue()
login := arguments["login"].StringValue()
password := arguments["password"].StringValue()
account := arguments["account"].StringValue()

usr, err := user.Current()
if err != nil {
Expand All @@ -117,6 +118,10 @@ func (c *SetCommand) Run(args []string) int {
machine.Set("password", password)
}

if account != "" {
machine.Set("account", account)
}

if err := n.Save(); err != nil {
c.Ui.Error(err.Error())
return 1
Expand Down
Empty file added fixtures/empty/.netrc
Empty file.
4 changes: 4 additions & 0 deletions fixtures/empty/github-account.netrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
machine github.com
login username
password password
account account
3 changes: 3 additions & 0 deletions fixtures/empty/github.netrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
machine github.com
login username
password password
3 changes: 3 additions & 0 deletions fixtures/valid/.netrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
machine heroku.com
login username
password longpassword
7 changes: 7 additions & 0 deletions fixtures/valid/github-account.netrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
machine heroku.com
login username
password longpassword
machine github.com
login username
password password
account account
6 changes: 6 additions & 0 deletions fixtures/valid/github.netrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
machine heroku.com
login username
password longpassword
machine github.com
login username
password password
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.15

require (
github.com/jdxcode/netrc v0.0.0-20210204082910-926c7f70242a
github.com/josegonzalez/cli-skeleton v0.1.1
github.com/josegonzalez/cli-skeleton v0.2.0
github.com/mitchellh/cli v1.1.2
github.com/posener/complete v1.1.1
github.com/spf13/pflag v1.0.5
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ github.com/jdxcode/netrc v0.0.0-20210204082910-926c7f70242a h1:d4+I1YEKVmWZrgkt6
github.com/jdxcode/netrc v0.0.0-20210204082910-926c7f70242a/go.mod h1:Zi/ZFkEqFHTm7qkjyNJjaWH4LQA9LQhGJyF0lTYGpxw=
github.com/josegonzalez/cli-skeleton v0.1.1 h1:MpqPid1g1PgE3lxyQB4QC29ES3MClBZNhDVSJkoesVc=
github.com/josegonzalez/cli-skeleton v0.1.1/go.mod h1:pNBybtIE54KcK5IyjVzYoMcPVmIjd+ON02WsrEI4vb0=
github.com/josegonzalez/cli-skeleton v0.2.0 h1:cCWlMLn7/mRi6Ht7aTi33hZpaxKoW052vQ5kXBVRNkg=
github.com/josegonzalez/cli-skeleton v0.2.0/go.mod h1:pNBybtIE54KcK5IyjVzYoMcPVmIjd+ON02WsrEI4vb0=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
Expand Down
294 changes: 290 additions & 4 deletions test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,299 @@ teardown() {
mv "$HOME/.netrc-bak" "$HOME/.netrc"
}

@test "version" {
@test "(version)" {
run $NETRC_BIN version
[[ "$status" -eq 0 ]]
assert_success

run $NETRC_BIN -v
[[ "$status" -eq 0 ]]
assert_success

run $NETRC_BIN --version
[[ "$status" -eq 0 ]]
assert_success
}

@test "(get) empty netrc" {
run $NETRC_BIN get
echo "output: $output"
echo "status: $status"
assert_failure
assert_output_contains "This command requires 1 argument: <name>"

run cp "fixtures/empty/.netrc" "$HOME/.netrc"
echo "output: $output"
echo "status: $status"
assert_success

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

@test "(get) valid netrc" {
run cp "fixtures/valid/.netrc" "$HOME/.netrc"
echo "output: $output"
echo "status: $status"
assert_success

run $NETRC_BIN get
echo "output: $output"
echo "status: $status"
assert_failure
assert_output_contains "This command requires 1 argument: <name>"

run $NETRC_BIN get heroku.com
echo "output: $output"
echo "status: $status"
assert_success
assert_output_contains "username:longpassword"

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

@test "(set) empty netrc" {
run cp "fixtures/empty/.netrc" "$HOME/.netrc"
echo "output: $output"
echo "status: $status"
assert_success

run $NETRC_BIN set github.com
echo "output: $output"
echo "status: $status"
assert_failure
assert_output_contains "This command requires 3 and at most 4 arguments, 1 argument given:"

run $NETRC_BIN set github.com username
echo "output: $output"
echo "status: $status"
assert_failure
assert_output_contains "This command requires 3 and at most 4 arguments, 2 arguments given:"

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 $NETRC_BIN set github.com username password account
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-account.netrc)"
}

@test "(set) valid netrc" {
run cp "fixtures/valid/.netrc" "$HOME/.netrc"
echo "output: $output"
echo "status: $status"
assert_success

run $NETRC_BIN set github.com
echo "output: $output"
echo "status: $status"
assert_failure
assert_output_contains "This command requires 3 and at most 4 arguments, 1 argument given:"

run $NETRC_BIN set github.com username
echo "output: $output"
echo "status: $status"
assert_failure
assert_output_contains "This command requires 3 and at most 4 arguments, 2 arguments given:"

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/valid/github.netrc)"

run $NETRC_BIN set github.com username password account
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/valid/github-account.netrc)"
}

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

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

run $NETRC_BIN unset github.com username
echo "output: $output"
echo "status: $status"
assert_failure
assert_output_contains "This command requires 1 argument, 2 arguments given: <name>"

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

@test "(unset) valid netrc" {
run cp "fixtures/valid/.netrc" "$HOME/.netrc"
echo "output: $output"
echo "status: $status"
assert_success

run $NETRC_BIN unset github.com username
echo "output: $output"
echo "status: $status"
assert_failure
assert_output_contains "This command requires 1 argument, 2 arguments given: <name>"

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/valid/.netrc)"

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

run $NETRC_BIN unset heroku.com username
echo "output: $output"
echo "status: $status"
assert_failure
assert_output_contains "This command requires 1 argument, 2 arguments given: <name>"

run $NETRC_BIN unset heroku.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 $NETRC_BIN unset heroku.com
echo "output: $output"
echo "status: $status"
assert_success
}

# test functions
flunk() {
{
if [[ "$#" -eq 0 ]]; then
cat -
else
echo "$*"
fi
}
return 1
}

assert_equal() {
if [[ "$1" != "$2" ]]; then
{
echo "expected: $1"
echo "actual: $2"
} | flunk
fi
}

# ShellCheck doesn't know about $status from Bats
# shellcheck disable=SC2154
# shellcheck disable=SC2120
assert_failure() {
if [[ "$status" -eq 0 ]]; then
flunk "expected failed exit status"
elif [[ "$#" -gt 0 ]]; then
assert_output "$1"
fi
}

# ShellCheck doesn't know about $output from Bats
# shellcheck disable=SC2154
assert_output() {
local expected
if [[ $# -eq 0 ]]; then
expected="$(cat -)"
else
expected="$1"
fi
assert_equal "$expected" "$output"
}

# ShellCheck doesn't know about $output from Bats
# shellcheck disable=SC2154
assert_output_contains() {
local input="$output"
local expected="$1"
local count="${2:-1}"
local found=0
until [ "${input/$expected/}" = "$input" ]; do
input="${input/$expected/}"
found=$((found + 1))
done
assert_equal "$count" "$found"
}

# ShellCheck doesn't know about $output from Bats
# shellcheck disable=SC2154
assert_output_exists() {
[[ -n "$output" ]] || flunk "expected output, found none"
}

# ShellCheck doesn't know about $output from Bats
# shellcheck disable=SC2154
assert_output_not_exists() {
[[ -z "$output" ]] || flunk "expected no output, found some"
}

# ShellCheck doesn't know about $status from Bats
# shellcheck disable=SC2154
# shellcheck disable=SC2120
assert_success() {
if [[ "$status" -ne 0 ]]; then
flunk "command failed with exit status $status"
elif [[ "$#" -gt 0 ]]; then
assert_output "$1"
fi
}

0 comments on commit 5239944

Please sign in to comment.