diff --git a/internal/mock/path.go b/internal/mock/path.go index 3967155..d38ce5a 100644 --- a/internal/mock/path.go +++ b/internal/mock/path.go @@ -13,7 +13,7 @@ func Path(filename string) string { // determine the path of this file during runtime _, thisfile, _, _ := runtime.Caller(0) - // compute the config path + // compute the mock path file, err := filepath.Abs( path.Join( filepath.Dir(thisfile), @@ -22,7 +22,7 @@ func Path(filename string) string { ), ) if err != nil { - panic(fmt.Errorf("failed to resolve config path: %v", err)) + panic(fmt.Errorf("failed to resolve mock path: %v", err)) } return file diff --git a/internal/sheets/load_test.go b/internal/sheets/load_test.go index 5147c09..762e2f2 100644 --- a/internal/sheets/load_test.go +++ b/internal/sheets/load_test.go @@ -1,3 +1,62 @@ package sheets -// TODO +import ( + "path" + "testing" + + "github.com/cheat/cheat/internal/cheatpath" + "github.com/cheat/cheat/internal/mock" +) + +// TestLoad asserts that sheets on valid cheatpaths can be loaded successfully +func TestLoad(t *testing.T) { + + // mock cheatpaths + cheatpaths := []cheatpath.Cheatpath{ + { + Name: "community", + Path: path.Join(mock.Path("cheatsheets"), "community"), + ReadOnly: true, + }, + { + Name: "personal", + Path: path.Join(mock.Path("cheatsheets"), "personal"), + ReadOnly: false, + }, + } + + // load cheatsheets + sheets, err := Load(cheatpaths) + if err != nil { + t.Errorf("failed to load cheatsheets: %v", err) + } + + // assert that the correct number of sheets loaded + // (sheet load details are tested in `sheet_test.go`) + want := 4 + if len(sheets) != want { + t.Errorf( + "failed to load correct number of cheatsheets: want: %d, got: %d", + want, + len(sheets), + ) + } +} + +// TestLoadBadPath asserts that an error is returned if a cheatpath is invalid +func TestLoadBadPath(t *testing.T) { + + // mock a bad cheatpath + cheatpaths := []cheatpath.Cheatpath{ + { + Name: "badpath", + Path: "/cheat/test/path/does/not/exist", + ReadOnly: true, + }, + } + + // attempt to load the cheatpath + if _, err := Load(cheatpaths); err == nil { + t.Errorf("failed to reject invalid cheatpath") + } +} diff --git a/mocks/cheatsheets/community/.hiddenfile b/mocks/cheatsheets/community/.hiddenfile new file mode 100644 index 0000000..e69de29 diff --git a/mocks/cheatsheets/community/bar b/mocks/cheatsheets/community/bar new file mode 100644 index 0000000..4792bbc --- /dev/null +++ b/mocks/cheatsheets/community/bar @@ -0,0 +1,4 @@ +--- +tags: [ community ] +--- +This is the bar cheatsheet. diff --git a/mocks/cheatsheets/community/foo b/mocks/cheatsheets/community/foo new file mode 100644 index 0000000..330cbc5 --- /dev/null +++ b/mocks/cheatsheets/community/foo @@ -0,0 +1,4 @@ +--- +tags: [ community ] +--- +This is the foo cheatsheet. diff --git a/mocks/cheatsheets/personal/bat b/mocks/cheatsheets/personal/bat new file mode 100644 index 0000000..fa2a841 --- /dev/null +++ b/mocks/cheatsheets/personal/bat @@ -0,0 +1,4 @@ +--- +tags: [ personal ] +--- +This is the bat cheatsheet. diff --git a/mocks/cheatsheets/personal/baz b/mocks/cheatsheets/personal/baz new file mode 100644 index 0000000..833264b --- /dev/null +++ b/mocks/cheatsheets/personal/baz @@ -0,0 +1,4 @@ +--- +tags: [ personal ] +--- +This is the baz cheatsheet.