From 0b21ccf6f851a5d450efd48e2c3ec2fd6df18cda Mon Sep 17 00:00:00 2001 From: Chris Lane Date: Mon, 2 Nov 2020 21:41:05 -0500 Subject: [PATCH] feat(tests): improve test coverage --- internal/config/color_test.go | 22 ++++++++++++++ internal/config/init_test.go | 38 +++++++++++++++++++++++ internal/config/path_test.go | 53 +++++++++++++++++++++++++++++++++ internal/sheet/colorize_test.go | 34 +++++++++++++++++++++ internal/sheet/sheet_test.go | 16 ++++++++++ mocks/sheet/bad-fm | 4 +++ 6 files changed, 167 insertions(+) create mode 100644 internal/config/color_test.go create mode 100644 internal/config/init_test.go create mode 100644 internal/config/path_test.go create mode 100644 internal/sheet/colorize_test.go create mode 100644 mocks/sheet/bad-fm diff --git a/internal/config/color_test.go b/internal/config/color_test.go new file mode 100644 index 0000000..f0493a1 --- /dev/null +++ b/internal/config/color_test.go @@ -0,0 +1,22 @@ +package config + +import ( + "testing" +) + +// TestColor asserts that colorization rules are properly respected +func TestColor(t *testing.T) { + + // mock a config + conf := Config{} + + opts := map[string]interface{}{"--colorize": false} + if conf.Color(opts) { + t.Errorf("failed to respect --colorize (false)") + } + + opts = map[string]interface{}{"--colorize": true} + if !conf.Color(opts) { + t.Errorf("failed to respect --colorize (true)") + } +} diff --git a/internal/config/init_test.go b/internal/config/init_test.go new file mode 100644 index 0000000..4d17bb2 --- /dev/null +++ b/internal/config/init_test.go @@ -0,0 +1,38 @@ +package config + +import ( + "io/ioutil" + "os" + "testing" +) + +// TestInit asserts that configs are properly initialized +func TestInit(t *testing.T) { + + // initialize a temporary config file + confFile, err := ioutil.TempFile("", "cheat-test") + if err != nil { + t.Errorf("failed to create temp file: %v", err) + } + + // clean up the temp file + defer os.Remove(confFile.Name()) + + // initialize the config file + conf := "mock config data" + if err = Init(confFile.Name(), conf); err != nil { + t.Errorf("failed to init config file: %v", err) + } + + // read back the config file contents + bytes, err := ioutil.ReadFile(confFile.Name()) + if err != nil { + t.Errorf("failed to read config file: %v", err) + } + + // assert that the contents were written correctly + got := string(bytes) + if got != conf { + t.Errorf("failed to write configs: want: %s, got: %s", conf, got) + } +} diff --git a/internal/config/path_test.go b/internal/config/path_test.go new file mode 100644 index 0000000..2121ff9 --- /dev/null +++ b/internal/config/path_test.go @@ -0,0 +1,53 @@ +package config + +import ( + "io/ioutil" + "os" + "testing" +) + +// TestPathConfigNotExists asserts that `Path` identifies non-existent config +// files +func TestPathConfigNotExists(t *testing.T) { + + // package (invalid) cheatpaths + paths := []string{"/cheat-test-conf-does-not-exist"} + + // assert + if _, err := Path(paths); err == nil { + t.Errorf("failed to identify non-existent config file") + } + +} + +// TestPathConfigExists asserts that `Path` identifies existent config files +func TestPathConfigExists(t *testing.T) { + + // initialize a temporary config file + confFile, err := ioutil.TempFile("", "cheat-test") + if err != nil { + t.Errorf("failed to create temp file: %v", err) + } + + // clean up the temp file + defer os.Remove(confFile.Name()) + + // package cheatpaths + paths := []string{ + "/cheat-test-conf-does-not-exist", + confFile.Name(), + } + + // assert + got, err := Path(paths) + if err != nil { + t.Errorf("failed to identify config file: %v", err) + } + if got != confFile.Name() { + t.Errorf( + "failed to return config path: want: %s, got: %s", + confFile.Name(), + got, + ) + } +} diff --git a/internal/sheet/colorize_test.go b/internal/sheet/colorize_test.go new file mode 100644 index 0000000..9c59626 --- /dev/null +++ b/internal/sheet/colorize_test.go @@ -0,0 +1,34 @@ +package sheet + +import ( + "testing" + + "github.com/cheat/cheat/internal/config" +) + +// TestColorize asserts that syntax-highlighting is correctly applied +func TestColorize(t *testing.T) { + + // mock configs + conf := config.Config{ + Formatter: "terminal16m", + Style: "solarized-dark", + } + + // mock a sheet + s := Sheet{ + Text: "echo 'foo'", + } + + // colorize the sheet text + s.Colorize(conf) + + // initialize expectations + want := "echo" + want += " 'foo'" + + // assert + if s.Text != want { + t.Errorf("failed to colorize sheet: want: %s, got: %s", want, s.Text) + } +} diff --git a/internal/sheet/sheet_test.go b/internal/sheet/sheet_test.go index 225c926..6ab3ad2 100644 --- a/internal/sheet/sheet_test.go +++ b/internal/sheet/sheet_test.go @@ -69,3 +69,19 @@ func TestSheetFailure(t *testing.T) { t.Errorf("failed to return an error on unreadable sheet") } } + +// TestSheetFrontMatterFailure asserts that an error is returned if the sheet's +// frontmatter cannot be parsed. +func TestSheetFrontMatterFailure(t *testing.T) { + + // initialize a sheet + _, err := New( + "foo", + mock.Path("sheet/bad-fm"), + []string{"alpha", "bravo"}, + false, + ) + if err == nil { + t.Errorf("failed to return an error on malformed front-matter") + } +} diff --git a/mocks/sheet/bad-fm b/mocks/sheet/bad-fm new file mode 100644 index 0000000..a497f6f --- /dev/null +++ b/mocks/sheet/bad-fm @@ -0,0 +1,4 @@ +--- +syntax: sh + +This is malformed frontmatter.