Add test hosts file and basic test for Ls command; fix ls panic

pull/16/head
Chris Bednarski 8 years ago
parent 5950705e9d
commit 35413097c7

1
.gitignore vendored

@ -3,3 +3,4 @@
/hostess_*
/coverage.out
/coverage.html
/.vscode

@ -76,7 +76,11 @@ func MaybeSaveHostFile(c *cli.Context, hostfile *Hostfile) {
// StrPadRight adds spaces to the right of a string until it reaches l length.
// If the input string is already that long, do nothing.
func StrPadRight(s string, l int) string {
return s + strings.Repeat(" ", l-len(s))
r := l - len(s)
if r < 0 {
r = 0
}
return s + strings.Repeat(" ", r)
}
// Add command parses <hostname> <ip> and adds or updates a hostname in the

@ -0,0 +1,23 @@
package hostess
import (
"flag"
"os"
"testing"
"github.com/codegangsta/cli"
"github.com/stretchr/testify/assert"
)
func TestStrPadRight(t *testing.T) {
assert.Equal(t, "", StrPadRight("", 0), "Zero-length no padding")
assert.Equal(t, " ", StrPadRight("", 10), "Zero-length 10 padding")
assert.Equal(t, "string", StrPadRight("string", 0), "6-length 0 padding")
}
func TestLs(t *testing.T) {
os.Setenv("HOSTESS_PATH", "test-fixtures/hostfile1")
defer os.Setenv("HOSTESS_PATH", "")
c := cli.NewContext(cli.NewApp(), &flag.FlagSet{}, nil)
Ls(c)
}

@ -0,0 +1,13 @@
#192.168.0.1 pie.dev.example.com
192.168.0.2 cookie.example.com
::1 hostname.pie hostname.candy cake.example.com
fe:23b3:890e:342e::ef strawberry.pie.example.com
# fe:23b3:890e:342e::ef dev.strawberry.pie.example.com
192.168.1.3 pie.example.com
127.0.1.1 robobrain
# fe:23b3:890e:342e::ef chocolate.cake.example.com chocolate.ru.example.com chocolate.tr.example.com chocolate.cookie.example.com
fe:23b3:890e:342e::ef chocolate.pie.example.com
::1 localhost
127.0.0.1 localhost
192.168.1.1 pie.example.com
192.168.1.1 strawberry.pie.example.com
Loading…
Cancel
Save