From 8a313b92ca8f9f71371e88903df511c333a51e45 Mon Sep 17 00:00:00 2001 From: Chris Lane Date: Thu, 30 Jan 2020 19:25:53 -0500 Subject: [PATCH] chore: implements unit-tests for `config.Paths` --- internal/config/paths_test.go | 124 ++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 internal/config/paths_test.go diff --git a/internal/config/paths_test.go b/internal/config/paths_test.go new file mode 100644 index 0000000..fd955b0 --- /dev/null +++ b/internal/config/paths_test.go @@ -0,0 +1,124 @@ +package config + +import ( + "reflect" + "testing" + + "github.com/davecgh/go-spew/spew" +) + +// TestValidatePathsNix asserts that the proper config paths are returned on +// *nix platforms +func TestValidatePathsNix(t *testing.T) { + + // mock some envvars + envvars := map[string]string{ + "HOME": "/home/foo", + "XDG_CONFIG_HOME": "/home/bar", + } + + // specify the platforms to test + oses := []string{ + "darwin", + "freebsd", + "linux", + } + + // test each *nix os + for _, os := range oses { + // get the paths for the platform + paths, err := Paths(os, envvars) + if err != nil { + t.Errorf("paths returned an error: %v", err) + } + + // specify the expected output + want := []string{ + "/home/bar/cheat/conf.yml", + "/home/foo/.config/cheat/conf.yml", + "/home/foo/.cheat/conf.yml", + } + + // assert that output matches expectations + if !reflect.DeepEqual(paths, want) { + t.Errorf( + "failed to return expected paths: want:\n%s, got:\n%s", + spew.Sdump(want), + spew.Sdump(paths), + ) + } + } +} + +// TestValidatePathsWindows asserts that the proper config paths are returned +// on Windows platforms +func TestValidatePathsWindows(t *testing.T) { + + // mock some envvars + envvars := map[string]string{ + "APPDATA": "/apps", + "PROGRAMDATA": "/programs", + } + + // get the paths for the platform + paths, err := Paths("windows", envvars) + if err != nil { + t.Errorf("paths returned an error: %v", err) + } + + // specify the expected output + want := []string{ + "/apps/cheat/conf.yml", + "/programs/cheat/conf.yml", + } + + // assert that output matches expectations + if !reflect.DeepEqual(paths, want) { + t.Errorf( + "failed to return expected paths: want:\n%s, got:\n%s", + spew.Sdump(want), + spew.Sdump(paths), + ) + } +} + +// TestValidatePathsUnsupported asserts that an error is returned on +// unsupported platforms +func TestValidatePathsUnsupported(t *testing.T) { + _, err := Paths("unsupported", map[string]string{}) + if err == nil { + t.Errorf("failed to return error on unsupported platform") + } +} + +// TestValidatePathsCheatConfigPath asserts that the proper config path is +// returned when `CHEAT_CONFIG_PATH` is explicitly specified. +func TestValidatePathsCheatConfigPath(t *testing.T) { + + // mock some envvars + envvars := map[string]string{ + "HOME": "/home/foo", + "XDG_CONFIG_HOME": "/home/bar", + "CHEAT_CONFIG_PATH": "/home/baz/conf.yml", + } + + // get the paths for the platform + paths, err := Paths("linux", envvars) + if err != nil { + t.Errorf("paths returned an error: %v", err) + } + + // specify the expected output + want := []string{ + "/home/baz/conf.yml", + } + + // assert that output matches expectations + if !reflect.DeepEqual(paths, want) { + t.Errorf( + "failed to return expected paths: want:\n%s, got:\n%s", + spew.Sdump(want), + spew.Sdump(paths), + ) + } +}