Skip to content

Commit

Permalink
Add more tests to config, pathutil, and model
Browse files Browse the repository at this point in the history
  • Loading branch information
saheljalal committed Aug 25, 2019
1 parent d748676 commit 83b6418
Show file tree
Hide file tree
Showing 4 changed files with 255 additions and 39 deletions.
1 change: 1 addition & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func TestSave(t *testing.T) {
}{
{"invalid path", "", nil, true},
{"nil manifest", "path", nil, true},
{"no perms", "/tmp/no-perms/.nostromo", fakeManifest(), true},
{"valid manifest", "/tmp/test.nostromo", fakeManifest(), false},
}

Expand Down
63 changes: 63 additions & 0 deletions model/manifest_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package model

import (
"io/ioutil"
"os"
"strconv"
"testing"

"github.com/pokanop/nostromo/pathutil"
)

func TestManifestAddCommand(t *testing.T) {
Expand Down Expand Up @@ -133,6 +137,65 @@ func TestManifestRemoveSubstitution(t *testing.T) {
}
}

func TestLink(t *testing.T) {
tests := []struct {
name string
manifest *Manifest
}{
{"valid manifest", fakeManifest(2, 4)},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
for _, cmd := range test.manifest.Commands {
cmd.forwardWalk(func(child *Command, stop *bool) {
child.parent = nil
})
}

test.manifest.Link()

for _, cmd := range test.manifest.Commands {
cmd.forwardWalk(func(child *Command, stop *bool) {
if child != cmd && child.parent == nil {
t.Errorf("command parent not linked correctly")
}
})
}
})
}
}

func TestAsJSON(t *testing.T) {
tests := []struct {
name string
manifest *Manifest
fixturePath string
}{
{"valid manifest", fakeManifest(2, 4), "../testdata/manifest.json"},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
f, err := os.Open(pathutil.Abs(test.fixturePath))
if err != nil {
t.Fatal(err)
}
defer f.Close()

b, err := ioutil.ReadAll(f)
if err != nil {
t.Fatal(err)
}

expected := string(b)
if actual := test.manifest.AsJSON(); actual != expected {
t.Errorf("expected: %s, actual: %s", expected, actual)
}
})
}
}

func fakeManifest(n, depth int) *Manifest {
m := NewManifest()
for i := 0; i < n; i++ {
Expand Down
107 changes: 68 additions & 39 deletions pathutil/pathutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,64 +7,93 @@ import (
"testing"
)

func TestExpand(t *testing.T) {
func TestAbs(t *testing.T) {
u, err := user.Current()
if err != nil {
t.Fatalf("err: %s", err)
}

wd, err := os.Getwd()
if err != nil {
t.Fatalf("err: %s", err)
}

tests := []struct {
name string
path string
home string
expected string
env bool
}{
{
"/foo",
"/foo",
false,
},

{
"~/foo",
filepath.Join(u.HomeDir, "foo"),
false,
},

{
"",
"",
false,
},

{
"~",
u.HomeDir,
false,
},

{
"~foo/foo",
"~foo/foo",
false,
},
{
"~/foo",
"~/foo",
true,
},
{"empty path", "", u.HomeDir, wd},
{"home not set", "~/foo", "", filepath.Join(wd, "~/foo")},
{"valid path", "~/foo", u.HomeDir, filepath.Join(u.HomeDir, "foo")},
}

for _, test := range tests {
if test.env {
defer patchEnv("HOME", "")()
}
t.Run(test.name, func(t *testing.T) {
defer patchEnv("HOME", test.home)()

if actual := Abs(test.path); actual != test.expected {
t.Errorf("expected: %s, actual: %s", test.expected, actual)
}
})
}
}

func TestExpand(t *testing.T) {
u, err := user.Current()
if err != nil {
t.Fatalf("err: %s", err)
}

tests := []struct {
path string
expected string
}{
{"/foo", "/foo"},
{"~/foo", filepath.Join(u.HomeDir, "foo")},
{"", ""},
{"~", u.HomeDir},
{"~foo/foo", "~foo/foo"},
}

defer patchEnv("HOME", u.HomeDir)()

for _, test := range tests {
if actual := Expand(test.path); actual != test.expected {
t.Errorf("expected: %s actual: %s", test.expected, actual)
}
}
}

func TestEnsurePath(t *testing.T) {
tests := []struct {
name string
path string
expErr bool
}{
{"valid path", "/tmp/pathutil_tests/valid_path", false},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
defer func() {
err := os.RemoveAll("/tmp/pathutil_tests")
if err != nil {
t.Fatalf("unable to clean up tmp folder: %s", err)
}
}()

err := EnsurePath(test.path)
if err != nil && !test.expErr {
t.Errorf("expected no error but got %s", err)
} else if err == nil && test.expErr {
t.Errorf("expected error but got none")
}
})
}
}

func patchEnv(key, value string) func() {
bck := os.Getenv(key)
deferFunc := func() {
Expand Down
123 changes: 123 additions & 0 deletions testdata/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
{
"version": "1.0",
"commands": {
"0-one-alias": {
"keyPath": "0-one-alias",
"name": "0-one",
"alias": "0-one-alias",
"comment": "",
"commands": {
"0-two-alias": {
"keyPath": "0-one-alias.0-two-alias",
"name": "0-two",
"alias": "0-two-alias",
"comment": "",
"commands": {
"0-three-alias": {
"keyPath": "0-one-alias.0-two-alias.0-three-alias",
"name": "0-three",
"alias": "0-three-alias",
"comment": "",
"commands": {
"0-four-alias": {
"keyPath": "0-one-alias.0-two-alias.0-three-alias.0-four-alias",
"name": "0-four",
"alias": "0-four-alias",
"comment": "",
"commands": {},
"subs": {
"0-four-sub": {
"Name": "0-four",
"Alias": "0-four-sub"
}
},
"code": null
}
},
"subs": {
"0-three-sub": {
"Name": "0-three",
"Alias": "0-three-sub"
}
},
"code": null
}
},
"subs": {
"0-two-sub": {
"Name": "0-two",
"Alias": "0-two-sub"
}
},
"code": null
}
},
"subs": {
"0-one-sub": {
"Name": "0-one",
"Alias": "0-one-sub"
}
},
"code": null
},
"1-one-alias": {
"keyPath": "1-one-alias",
"name": "1-one",
"alias": "1-one-alias",
"comment": "",
"commands": {
"1-two-alias": {
"keyPath": "1-one-alias.1-two-alias",
"name": "1-two",
"alias": "1-two-alias",
"comment": "",
"commands": {
"1-three-alias": {
"keyPath": "1-one-alias.1-two-alias.1-three-alias",
"name": "1-three",
"alias": "1-three-alias",
"comment": "",
"commands": {
"1-four-alias": {
"keyPath": "1-one-alias.1-two-alias.1-three-alias.1-four-alias",
"name": "1-four",
"alias": "1-four-alias",
"comment": "",
"commands": {},
"subs": {
"1-four-sub": {
"Name": "1-four",
"Alias": "1-four-sub"
}
},
"code": null
}
},
"subs": {
"1-three-sub": {
"Name": "1-three",
"Alias": "1-three-sub"
}
},
"code": null
}
},
"subs": {
"1-two-sub": {
"Name": "1-two",
"Alias": "1-two-sub"
}
},
"code": null
}
},
"subs": {
"1-one-sub": {
"Name": "1-one",
"Alias": "1-one-sub"
}
},
"code": null
}
}
}

0 comments on commit 83b6418

Please sign in to comment.