diff --git a/Makefile b/Makefile index 3a5a6f9..c03d245 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,10 @@ clean: - rm -r test/bender_test - rm -r tmp + rm -rf test/bender_test 2> /dev/null + rm -rf tmp 2> /dev/null sandbox: - mkdir -p ./tmp/ - cp -r ~/.config/ ./tmp/config + mkdir -p ./tmp/ 2> /dev/null + cp -r ~/.config/ ./tmp/config 2> /dev/null + +unit-test: + TESTING=true go test -v ./test diff --git a/cmd/init.go b/cmd/init.go index 5506815..025c12d 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -53,6 +53,9 @@ var initCommand = &cobra.Command { func runInitCommand(cmd *cobra.Command, args []string) { fs := FileSystem + // if user has passed a dotfile path flag need to add it to + // viper's search path for a config file + viper.AddConfigPath(filepath.Join(DotfilePath, "bender")) if(viper.Get("testing") == true && fs.Name() != "MemMapFS") { log.Fatalf("wrong filesystem, got %s", fs.Name()) @@ -69,8 +72,8 @@ func runInitCommand(cmd *cobra.Command, args []string) { } err = viper.WriteConfig() - if err != nil { - fmt.Printf("Unable to write config on init: %s\n", err) + if err != nil && viper.Get("testing") != true { + log.Fatalf("Unable to write config on init: %s\n", err) } if (viper.Get("testing") != "true"){ diff --git a/cmd/link.go b/cmd/link.go index 2ccc513..8fe259a 100644 --- a/cmd/link.go +++ b/cmd/link.go @@ -20,12 +20,12 @@ func init() { } func runLinkCommand(cmd *cobra.Command, args []string) { - fs := UseFilesystem() + fs := FileSystem fmt.Println("Symlinking dotfiles...") dotfileRoot := viper.Get("dotfile-path").(string) entries, err := afero.ReadDir(fs, dotfileRoot) if err != nil { - log.Fatal(err) + log.Fatalf("Could not read dotfiles directory: %s\n",err) } for _, entry := range(entries) { configName := entry.Name() @@ -34,7 +34,10 @@ func runLinkCommand(cmd *cobra.Command, args []string) { } dotPath := filepath.Join(dotfileRoot, entry.Name()) - configPath := viper.Get(configName).(string) + configPath := viper.GetString(configName) + if configPath == ""{ + fmt.Fprintf(cmd.OutOrStdout(), "Warning: could not find config for %s\n", entry.Name()) + } // destination needs to be removed before symlink diff --git a/cmd/root.go b/cmd/root.go index 9564e06..0517c89 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -67,6 +67,7 @@ func init() { viper.SetConfigName("config") viper.SetConfigType("yaml") viper.AddConfigPath("./tmp/dotfiles/bender") + fmt.Printf("dotfile path is %s\n", DotfilePath) viper.AddConfigPath(filepath.Join(DotfilePath, "bender")) err := viper.ReadInConfig() @@ -89,29 +90,4 @@ func UseFilesystem() afero.Fs { } // TODO: this can probably be removed -func SetUpForTesting() afero.Fs { - viper.Set("testing", true) - fs := UseFilesystem() - - homedir := "bender_test/" - fs.MkdirAll(filepath.Join(homedir, ".config/"), 0755) - fs.MkdirAll(filepath.Join(homedir, ".dotfiles/"), 0755) - - fs.Mkdir("bin/", 0755) - fs.Create("bin/alacritty") - fs.Create("bin/nvim") - fs.Create("bin/tmux") - - fs.Mkdir(filepath.Join(homedir, ".config/alacritty"), 0755) - fs.Mkdir(filepath.Join(homedir, ".config/nvim"), 0755) - fs.Mkdir(filepath.Join(homedir, ".config/tmux"), 0755) - - fs.Create(filepath.Join(homedir, ".config/alacritty/alacritty.conf")) - fs.Create(filepath.Join(homedir, ".config/nvim/nvim.conf")) - fs.Create(filepath.Join(homedir, ".config/tmux/tmux.conf")) - - FileSystem = fs - - return fs -} diff --git a/test/link_test.go b/test/link_test.go new file mode 100644 index 0000000..723ae04 --- /dev/null +++ b/test/link_test.go @@ -0,0 +1,52 @@ +package test + +import ( + "bytes" + "fmt" + "os" + "path/filepath" + "testing" + + "github.com/Marcusk19/bender/cmd" + "github.com/spf13/viper" + "github.com/stretchr/testify/assert" +) + + +func TestLinkCommand(t *testing.T) { + setUpTesting() + bender := cmd.RootCmd + actual := new(bytes.Buffer) + + bender.SetOut(actual) + bender.SetErr(actual) + bender.SetArgs([]string{"link"}) + + bender.Execute() + + homedir := os.Getenv("HOME") + someconfig := filepath.Join(homedir, ".config/someconfig/") + somedot := filepath.Join(homedir, ".dotfiles/someconfig/") + + expected := fmt.Sprintf("%s,%s", someconfig, somedot) + + assert.Equal(t, expected, actual.String(), "actual differs from expected") + + tearDownTesting() +} + +func setUpTesting() { + fs := cmd.FileSystem + homedir := os.Getenv("HOME") + fs.MkdirAll(filepath.Join(homedir, ".dotfiles/bender"), 0755) + fs.Create(filepath.Join(homedir, ".dotfiles/bender/config")) + fs.MkdirAll(filepath.Join(homedir, ".dotfiles/someconfig/"), 0755) + + viper.Set("someconfig", filepath.Join(homedir, ".config/someconfig/")) + +} + +func tearDownTesting() { + fs := cmd.FileSystem + fs.RemoveAll("bender_test/") +}