add unit test for link command

pull/13/head
Marcusk19 2 years ago
parent 38af0d5ad7
commit 843385b004

@ -1,7 +1,10 @@
clean: clean:
rm -r test/bender_test rm -rf test/bender_test 2> /dev/null
rm -r tmp rm -rf tmp 2> /dev/null
sandbox: sandbox:
mkdir -p ./tmp/ mkdir -p ./tmp/ 2> /dev/null
cp -r ~/.config/ ./tmp/config cp -r ~/.config/ ./tmp/config 2> /dev/null
unit-test:
TESTING=true go test -v ./test

@ -53,6 +53,9 @@ var initCommand = &cobra.Command {
func runInitCommand(cmd *cobra.Command, args []string) { func runInitCommand(cmd *cobra.Command, args []string) {
fs := FileSystem 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") { if(viper.Get("testing") == true && fs.Name() != "MemMapFS") {
log.Fatalf("wrong filesystem, got %s", fs.Name()) log.Fatalf("wrong filesystem, got %s", fs.Name())
@ -69,8 +72,8 @@ func runInitCommand(cmd *cobra.Command, args []string) {
} }
err = viper.WriteConfig() err = viper.WriteConfig()
if err != nil { if err != nil && viper.Get("testing") != true {
fmt.Printf("Unable to write config on init: %s\n", err) log.Fatalf("Unable to write config on init: %s\n", err)
} }
if (viper.Get("testing") != "true"){ if (viper.Get("testing") != "true"){

@ -20,12 +20,12 @@ func init() {
} }
func runLinkCommand(cmd *cobra.Command, args []string) { func runLinkCommand(cmd *cobra.Command, args []string) {
fs := UseFilesystem() fs := FileSystem
fmt.Println("Symlinking dotfiles...") fmt.Println("Symlinking dotfiles...")
dotfileRoot := viper.Get("dotfile-path").(string) dotfileRoot := viper.Get("dotfile-path").(string)
entries, err := afero.ReadDir(fs, dotfileRoot) entries, err := afero.ReadDir(fs, dotfileRoot)
if err != nil { if err != nil {
log.Fatal(err) log.Fatalf("Could not read dotfiles directory: %s\n",err)
} }
for _, entry := range(entries) { for _, entry := range(entries) {
configName := entry.Name() configName := entry.Name()
@ -34,7 +34,10 @@ func runLinkCommand(cmd *cobra.Command, args []string) {
} }
dotPath := filepath.Join(dotfileRoot, entry.Name()) 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 // destination needs to be removed before symlink

@ -67,6 +67,7 @@ func init() {
viper.SetConfigName("config") viper.SetConfigName("config")
viper.SetConfigType("yaml") viper.SetConfigType("yaml")
viper.AddConfigPath("./tmp/dotfiles/bender") viper.AddConfigPath("./tmp/dotfiles/bender")
fmt.Printf("dotfile path is %s\n", DotfilePath)
viper.AddConfigPath(filepath.Join(DotfilePath, "bender")) viper.AddConfigPath(filepath.Join(DotfilePath, "bender"))
err := viper.ReadInConfig() err := viper.ReadInConfig()
@ -89,29 +90,4 @@ func UseFilesystem() afero.Fs {
} }
// TODO: this can probably be removed // 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
}

@ -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/")
}
Loading…
Cancel
Save