add unit test for init command

pull/9/head
Marcusk19 2 years ago
parent 9a28f04258
commit f14475a54b

@ -8,18 +8,19 @@ import (
"github.com/Marcusk19/bender/tools" "github.com/Marcusk19/bender/tools"
"github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5"
"github.com/spf13/afero"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper"
) )
var fs = tools.AppFs
func init() { func init() {
RootCmd.AddCommand(initCommand) RootCmd.AddCommand(initCommand)
} }
func copyExistingConfigs(programs []string, destRootOpt ...string) { func copyExistingConfigs(programs []string, fs afero.Fs, destRootOpt ...string) {
// takes list of programs and backs up configs for them // takes list of programs and backs up configs for them
destRoot := os.Getenv("HOME") + "/.dotfiles/" destRoot := DotfilePath
if len(destRootOpt) > 0 { if len(destRootOpt) > 0 {
destRoot = destRootOpt[0] destRoot = destRootOpt[0]
} }
@ -27,19 +28,18 @@ func copyExistingConfigs(programs []string, destRootOpt ...string) {
configRoot := ConfigPath configRoot := ConfigPath
for _, program := range(programs) { for _, program := range(programs) {
// TODO: do something here // TODO: do something here
print(configRoot + program)
err := tools.CopyDir(fs, filepath.Join(configRoot, program), filepath.Join(destRoot, program)) err := tools.CopyDir(fs, filepath.Join(configRoot, program), filepath.Join(destRoot, program))
if err != nil { if err != nil {
log.Fatal(err) log.Fatalf("Problem copying %s", err.Error())
} }
} }
} }
func createDotfileStructure(programs []string) { func createDotfileStructure(programs []string, fs afero.Fs) {
// takes list of programs and creates dotfiles for them // takes list of programs and creates dotfiles for them
dotfileRoot := os.Getenv("HOME") + "/.dotfiles/" dotfileRoot := DotfilePath
fmt.Printf("creating dotfile directory structure at %s\n", dotfileRoot)
for _, program := range(programs) { for _, program := range(programs) {
fmt.Printf("attempting to create directory %s%s\n", dotfileRoot, program)
if err := fs.MkdirAll(dotfileRoot + program, os.ModePerm); err != nil { if err := fs.MkdirAll(dotfileRoot + program, os.ModePerm); err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -51,6 +51,13 @@ var initCommand = &cobra.Command {
Short: "Copy configs to dotfile directory", Short: "Copy configs to dotfile directory",
Long: "Searches existing config directory for configs and then copies them to dotfile directory", Long: "Searches existing config directory for configs and then copies them to dotfile directory",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
fs := FileSystem
if(viper.Get("testing") == true && fs.Name() != "MemMapFS") {
log.Fatalf("wrong filesystem, got %s", fs.Name())
}
var rootpath string var rootpath string
if len(args) <= 0 { if len(args) <= 0 {
fmt.Fprintf(cmd.OutOrStdout(), "no path provided, assuming /usr/bin/\n") fmt.Fprintf(cmd.OutOrStdout(), "no path provided, assuming /usr/bin/\n")
@ -60,7 +67,7 @@ var initCommand = &cobra.Command {
} }
if rootpath[len(rootpath)-1:] != "/" { if rootpath[len(rootpath)-1:] != "/" {
log.Fatal("path needs trailing slash") log.Fatal("path needs trailing slash\n")
} }
// TODO make a configurable list of binaries we want to look for // TODO make a configurable list of binaries we want to look for
@ -70,9 +77,9 @@ var initCommand = &cobra.Command {
acceptedprograms[1] = "tmux" acceptedprograms[1] = "tmux"
acceptedprograms[2] = "alacritty" acceptedprograms[2] = "alacritty"
err := filepath.Walk(rootpath, func(path string, info os.FileInfo, err error) error { err := afero.Walk(fs, rootpath, func(path string, info os.FileInfo, err error) error {
if err != nil { if err != nil {
log.Fatalf("problem walking path %s", err) log.Fatalf("problem walking path %s\n", err)
return nil return nil
} }
@ -88,18 +95,20 @@ var initCommand = &cobra.Command {
log.Fatal(err) log.Fatal(err)
} }
fmt.Fprintf(cmd.OutOrStdout(), "binaries installed: \n =======================\n") fmt.Fprintf(cmd.OutOrStdout(), "binaries found: \n =======================\n")
for _, program := range(programs) { for _, program := range(programs) {
fmt.Fprintf(cmd.OutOrStdout(), program + "\n" ) fmt.Fprintf(cmd.OutOrStdout(), program + "\n" )
} }
createDotfileStructure(programs) createDotfileStructure(programs, fs)
copyExistingConfigs(programs) copyExistingConfigs(programs, fs)
_, err = git.PlainInit(filepath.Join(os.Getenv("HOME"), "/.dotfiles"), false) if (viper.Get("testing") != true){
_, err = git.PlainInit(DotfilePath, false)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
}
fmt.Fprintf(cmd.OutOrStdout(), "Successfully created dotfiles repository\n") fmt.Fprintf(cmd.OutOrStdout(), "Successfully created dotfiles repository\n")
}, },
} }

@ -19,6 +19,7 @@ var linkCommand = &cobra.Command {
} }
func runLinkCommand(cmd *cobra.Command, args []string) { func runLinkCommand(cmd *cobra.Command, args []string) {
fs := UseFilesystem()
fmt.Println("Symlinking dotfiles...") fmt.Println("Symlinking dotfiles...")
entries, err := afero.ReadDir(fs, DotfilePath) entries, err := afero.ReadDir(fs, DotfilePath)
if err != nil { if err != nil {

@ -1,12 +1,13 @@
/* /*
Copyright © 2024 Marcus Kok Copyright © 2024 Marcus Kok
*/ */
package cmd package cmd
import ( import (
"os" "os"
"path/filepath"
"github.com/spf13/afero"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -32,6 +33,8 @@ func Execute() {
var DotfilePath string var DotfilePath string
var ConfigPath string var ConfigPath string
var FileSystem afero.Fs
func init() { func init() {
// define flags and config sections // define flags and config sections
@ -39,6 +42,7 @@ func init() {
// Cobra also supports local flags, which will only run // Cobra also supports local flags, which will only run
// when this action is called directly. // when this action is called directly.
print("init of root\n")
defaultDotPath := os.Getenv("HOME") + "/.dotfiles/" defaultDotPath := os.Getenv("HOME") + "/.dotfiles/"
defaultConfPath := os.Getenv("HOME") + "/.config/" defaultConfPath := os.Getenv("HOME") + "/.config/"
RootCmd.PersistentFlags().StringVar( RootCmd.PersistentFlags().StringVar(
@ -54,7 +58,49 @@ func init() {
"Path pointing to config directory", "Path pointing to config directory",
) )
viper.BindPFlag("dotfile-path", RootCmd.PersistentFlags().Lookup("dotfile-path")) viper.BindPFlag("dotfile-path", RootCmd.PersistentFlags().Lookup("dotfile-path"))
RootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") viper.BindPFlag("config-path", RootCmd.PersistentFlags().Lookup("config-path"))
viper.BindEnv("testing")
viper.SetDefault("testing", false)
FileSystem = UseFilesystem()
}
func UseFilesystem() afero.Fs {
testing := viper.Get("testing")
if(testing == true) {
print("Using temporary testing filesystem\n")
return afero.NewMemMapFs()
} else {
return afero.NewOsFs()
}
} }
func SetUpForTesting() afero.Fs {
print("Setting up testing environment\n")
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,35 @@
package test
import (
"bytes"
"path/filepath"
"testing"
"github.com/Marcusk19/bender/cmd"
"github.com/spf13/afero"
"github.com/spf13/viper"
)
func TestInitCommand(t *testing.T) {
print("setting test var\n")
viper.Set("testing", true)
fs := cmd.SetUpForTesting()
bender := cmd.RootCmd
actual := new(bytes.Buffer)
bender.SetOut(actual)
bender.SetErr(actual)
bender.SetArgs([]string{"init", "bin/", "--dotfile-path=bender_test/.dotfiles", "--config-path=bender_test/.config"})
bender.Execute()
homedir := "bender_test/"
_, err := afero.ReadFile(fs, filepath.Join(homedir, ".dotfiles/alacritty/alacritty.conf"))
if err != nil {
t.Error(err.Error())
}
}
Loading…
Cancel
Save