Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mortymacs committed Feb 6, 2025
1 parent d2b3dd6 commit 544772d
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/app/wait4x/cmd/dns/a.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func NewACommand() *cobra.Command {
}

command.Flags().StringArray("expect-ip", nil, "Expect ipv4s.")
command.Flags().StringP("nameserver", "n", "", "DNS nameserver.")

return command
}
Expand Down
68 changes: 68 additions & 0 deletions internal/app/wait4x/cmd/dns/a_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package dns

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestNewACommand(t *testing.T) {
cmd := NewACommand()
assert.NotNil(t, cmd)
assert.Equal(t, "A ADDRESS [value] [--command [args...]]", cmd.Use)
assert.Equal(t, []string{"a"}, cmd.Aliases)
}

func TestACommand_NoArgs(t *testing.T) {
cmd := NewACommand()
err := cmd.Args(cmd, []string{})
assert.Error(t, err)
assert.Equal(t, "ADDRESS is required argument for the dns command", err.Error())
}

func TestACommand_WithArgs(t *testing.T) {
cmd := NewACommand()
err := cmd.Args(cmd, []string{"example.com"})
assert.NoError(t, err)
}

func TestRunA(t *testing.T) {
tests := []struct {
name string
args []string
flags map[string]string
}{
{
name: "basic check",
args: []string{"example.com"},
},
{
name: "with expected IP",
args: []string{"example.com"},
flags: map[string]string{
"expect-ip": "93.184.216.34",
},
},
{
name: "with nameserver",
args: []string{"example.com"},
flags: map[string]string{
"nameserver": "8.8.8.8",
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd := NewACommand()

for flag, value := range tt.flags {
err := cmd.Flags().Set(flag, value)
assert.NoError(t, err)
}

err := cmd.Args(cmd, tt.args)
assert.NoError(t, err)
})
}
}

0 comments on commit 544772d

Please sign in to comment.