github.com/swaros/contxt/module/runner@v0.0.0-20240305083542-3dbd4436ac40/shellinstall_test.go (about)

     1  package runner_test
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/swaros/contxt/module/configure"
    10  	"github.com/swaros/contxt/module/mimiclog"
    11  	"github.com/swaros/contxt/module/runner"
    12  )
    13  
    14  func TestBashRcInstallFails(t *testing.T) {
    15  	installer := runner.NewShellInstall(mimiclog.NewNullLogger())
    16  	installer.SetUserHomePath("./test")
    17  	if err := installer.BashUserInstall(); err == nil {
    18  		t.Error("should return an error, because the test folder do not contains a .bashrc file")
    19  	}
    20  }
    21  
    22  func SetDefaultValues() {
    23  	configure.SetShortcut("ctx")
    24  	configure.SetCnShortcut("cn")
    25  	configure.SetBinaryName("contxt")
    26  }
    27  
    28  func TestBashRcInstall(t *testing.T) {
    29  	// defaults
    30  	SetDefaultValues()
    31  	defer os.RemoveAll("./test/fakehome/.bashrc")
    32  	installer := runner.NewShellInstall(mimiclog.NewNullLogger())
    33  	installer.SetUserHomePath("./test/fakehome")
    34  	os.WriteFile("./test/fakehome/.bashrc", []byte("# a fake bashrc"), 0644)
    35  	if err := installer.BashUserInstall(); err != nil {
    36  		t.Error("should not return an error, bot got:", err)
    37  	}
    38  	expected := `# a fake bashrc
    39  ### begin contxt bashrc
    40  function cn() { cd $(contxt dir find "$@"); }
    41  function ctx() {
    42  	contxt "$@";
    43  	[ $? -eq 0 ]  || return 1
    44          case $1 in
    45            switch)
    46            cd $(contxt dir --last);
    47            contxt dir paths --coloroff --nohints
    48            ;;
    49          esac
    50  }
    51  function ctxcompletion() {
    52          ORIG=$(contxt completion bash)
    53          CM="contxt"
    54          CT="ctx"
    55          CTXC="${ORIG//$CM/$CT}"
    56          echo "$CTXC"
    57  }
    58  source <(contxt completion bash)
    59  source <(ctxcompletion)
    60  ### end of contxt bashrc
    61  `
    62  	assertFileContent(t, "./test/fakehome/.bashrc", expected, AcceptContainsNoSpecials)
    63  }
    64  
    65  func TestBashRcInstallRenamed(t *testing.T) {
    66  	configure.SetShortcut("ctxV2")
    67  	configure.SetCnShortcut("cnV2")
    68  	configure.SetBinaryName("contxtV2")
    69  	defer os.RemoveAll("./test/fakehome/.bashrc")
    70  	installer := runner.NewShellInstall(mimiclog.NewNullLogger())
    71  	installer.SetUserHomePath("./test/fakehome")
    72  	os.WriteFile("./test/fakehome/.bashrc", []byte("# a fake bashrc"), 0644)
    73  	if err := installer.BashUserInstall(); err != nil {
    74  		t.Error("should not return an error, bot got:", err)
    75  	}
    76  	expected := `# a fake bashrc
    77  ### begin contxtV2 bashrc
    78  function cnV2() { cd $(contxtV2 dir find "$@"); }
    79  function ctxV2() {
    80  	contxtV2 "$@";
    81  	[ $? -eq 0 ]  || return 1
    82          case $1 in
    83            switch)
    84            cd $(contxtV2 dir --last);
    85            contxtV2 dir paths --coloroff --nohints
    86            ;;
    87          esac
    88  }
    89  function ctxV2completion() {
    90          ORIG=$(contxtV2 completion bash)
    91          CM="contxtV2"
    92          CT="ctxV2"
    93          CTXC="${ORIG//$CM/$CT}"
    94          echo "$CTXC"
    95  }
    96  source <(contxtV2 completion bash)
    97  source <(ctxV2completion)
    98  ### end of contxtV2 bashrc
    99  `
   100  	assertFileContent(t, "./test/fakehome/.bashrc", expected, AcceptContainsNoSpecials)
   101  }
   102  
   103  func TestZshRcInstallFails(t *testing.T) {
   104  	installer := runner.NewShellInstall(mimiclog.NewNullLogger())
   105  	installer.SetUserHomePath("./test")
   106  	if err := installer.ZshUserInstall(); err == nil {
   107  		t.Error("should return an error, because the test folder do not contains a .zshrc file")
   108  	}
   109  }
   110  
   111  func TestZshRcInstall(t *testing.T) {
   112  	// defaults
   113  	SetDefaultValues()
   114  	defer os.RemoveAll("./test/fakehome/.zshrc")
   115  	installer := runner.NewShellInstall(mimiclog.NewNullLogger())
   116  	installer.SetUserHomePath("./test/fakehome")
   117  	os.WriteFile("./test/fakehome/.zshrc", []byte("# a fake zshrc"), 0644)
   118  	if err := installer.ZshUserInstall(); err != nil {
   119  		t.Error("should not return an error, bot got:", err)
   120  	}
   121  	expected := `# a fake zshrc
   122  	### begin contxt zshrc
   123  	function cn() { cd $(contxt dir find "$@"); }
   124  	function ctx() {        
   125  		contxt "$@";
   126  		[ $? -eq 0 ]  || return $?
   127  			case $1 in
   128  			  switch)          
   129  			  cd $(contxt dir --last);
   130  			  contxt dir paths --coloroff --nohints
   131  			  ;;
   132  			esac
   133  	}
   134  	### end of contxt zshrc
   135  `
   136  	assertFileContent(t, "./test/fakehome/.zshrc", expected, AcceptContainsNoSpecials)
   137  }
   138  
   139  func TestZshRcInstallRenamed(t *testing.T) {
   140  	configure.SetShortcut("CNTXT") // force the shortcut to ctx
   141  	configure.SetCnShortcut("DD")  // force the shortcut to ctx
   142  	configure.SetBinaryName("ZULU")
   143  	defer os.RemoveAll("./test/fakehome/.zshrc")
   144  	installer := runner.NewShellInstall(mimiclog.NewNullLogger())
   145  	installer.SetUserHomePath("./test/fakehome")
   146  	os.WriteFile("./test/fakehome/.zshrc", []byte("# a fake zshrc"), 0644)
   147  	if err := installer.ZshUserInstall(); err != nil {
   148  		t.Error("should not return an error, bot got:", err)
   149  	}
   150  	expected := `# a fake zshrc
   151  	### begin ZULU zshrc
   152  	function DD() { cd $(ZULU dir find "$@"); }
   153  	function CNTXT() {        
   154  		ZULU "$@";
   155  		[ $? -eq 0 ]  || return $?
   156  			case $1 in
   157  			  switch)          
   158  			  cd $(ZULU dir --last);
   159  			  ZULU dir paths --coloroff --nohints
   160  			  ;;
   161  			esac
   162  	}
   163  	### end of ZULU zshrc
   164  `
   165  	assertFileContent(t, "./test/fakehome/.zshrc", expected, AcceptContainsNoSpecials)
   166  }
   167  
   168  func TestFishRcInstall(t *testing.T) {
   169  	// defaults
   170  	SetDefaultValues()
   171  	defer os.RemoveAll("./test/fakehome/.config")
   172  	installer := runner.NewShellInstall(mimiclog.NewNullLogger())
   173  	installer.SetUserHomePath("./test/fakehome")
   174  	//os.WriteFile("./test/fakehome/.config/fish/config.fish", []byte("# a fake fishrc"), 0644)
   175  	if err := installer.FishFunctionUpdate(); err != nil {
   176  		t.Error("should not return an error, bot got:", err)
   177  	}
   178  	functionFile := "test/fakehome/.config/fish/functions/ctx.fish"
   179  	cnFunctionFile := "test/fakehome/.config/fish/functions/cn.fish"
   180  	assertFileExists(t, functionFile)
   181  	fishFunc := `function ctx
   182      contxt $argv
   183      switch $argv[1]
   184         case switch
   185            cd (contxt dir --last)
   186            contxt dir paths --coloroff --nohints
   187      end
   188  end`
   189  	cnFunc := `function cn
   190  	cd (contxt dir find $argv)
   191  end`
   192  	assertFileContent(t, functionFile, fishFunc, AcceptContainsNoSpecials)
   193  	assertFileContent(t, cnFunctionFile, cnFunc, AcceptContainsNoSpecials)
   194  }
   195  
   196  func TestFishRcInstallRenamed(t *testing.T) {
   197  	configure.SetShortcut("FctxF") // force the shortcut to ctx
   198  	configure.SetCnShortcut("KKK") // force the shortcut to ctx
   199  	configure.SetBinaryName("UINMKOI")
   200  	defer os.RemoveAll("./test/fakehome/.config")
   201  	installer := runner.NewShellInstall(mimiclog.NewNullLogger())
   202  	installer.SetUserHomePath("./test/fakehome")
   203  	//os.WriteFile("./test/fakehome/.config/fish/config.fish", []byte("# a fake fishrc"), 0644)
   204  	if err := installer.FishFunctionUpdate(); err != nil {
   205  		t.Error("should not return an error, bot got:", err)
   206  	}
   207  	functionFile := "test/fakehome/.config/fish/functions/FctxF.fish"
   208  	cnFunctionFile := "test/fakehome/.config/fish/functions/KKK.fish"
   209  	assertFileExists(t, functionFile)
   210  	fishFunc := `function FctxF
   211      UINMKOI $argv
   212      switch $argv[1]
   213         case switch
   214            cd (UINMKOI dir --last)
   215            UINMKOI dir paths --coloroff --nohints
   216      end
   217  end`
   218  	cnFunc := `function KKK
   219  	cd (UINMKOI dir find $argv)
   220  end`
   221  	assertFileContent(t, functionFile, fishFunc, AcceptContainsNoSpecials)
   222  	assertFileContent(t, cnFunctionFile, cnFunc, AcceptContainsNoSpecials)
   223  }
   224  
   225  func TestFishCompletionUpdate(t *testing.T) {
   226  	// defaults
   227  	SetDefaultValues()
   228  	defer os.RemoveAll("./test/fakehome/.config")
   229  	installer := runner.NewShellInstall(mimiclog.NewNullLogger())
   230  	installer.SetUserHomePath("./test/fakehome")
   231  
   232  	cobra := runner.NewCobraCmds()
   233  	if err := installer.FishCompletionUpdate(cobra.RootCmd); err != nil {
   234  		t.Error("should not return an error, bot got:", err)
   235  	}
   236  
   237  	completionFile := "test/fakehome/.config/fish/completions/ctx.fish"
   238  	assertFileExists(t, completionFile)
   239  
   240  	completionFile = "test/fakehome/.config/fish/completions/contxt.fish"
   241  	assertFileExists(t, completionFile)
   242  }
   243  
   244  func TestFishCompletionUpdateRenamed(t *testing.T) {
   245  	configure.SetShortcut("UNU")
   246  	configure.SetBinaryName("UNUBIN")
   247  
   248  	defer os.RemoveAll("./test/fakehome/.config")
   249  	installer := runner.NewShellInstall(mimiclog.NewNullLogger())
   250  	installer.SetUserHomePath("./test/fakehome")
   251  
   252  	cobra := runner.NewCobraCmds()
   253  	if err := installer.FishCompletionUpdate(cobra.RootCmd); err != nil {
   254  		t.Error("should not return an error, bot got:", err)
   255  	}
   256  
   257  	completionFile := "test/fakehome/.config/fish/completions/UNU.fish"
   258  	assertFileExists(t, completionFile)
   259  
   260  	completionFile = "test/fakehome/.config/fish/completions/UNUBIN.fish"
   261  	assertFileExists(t, completionFile)
   262  }
   263  
   264  func TestZshFuncDir(t *testing.T) {
   265  	// defaults
   266  	SetDefaultValues()
   267  	installer := runner.NewShellInstall(mimiclog.NewNullLogger())
   268  	installer.SetUserHomePath("./test/fakehome")
   269  	fpath := "[ABS]/test/fakehome/.zfunc:[ABS]/test/fakehome/zFuncExists:[ABS]/test/fakehome/zFuncNotExists"
   270  	abs, _ := filepath.Abs(".")
   271  	fpath = strings.ReplaceAll(fpath, "[ABS]", abs)
   272  	os.Setenv("FPATH", fpath)
   273  
   274  	path, err := installer.ZshFuncDir()
   275  	if err != nil {
   276  		t.Error("should not return an error, bot got:", err)
   277  	}
   278  	path, _ = filepath.Abs(path)
   279  	expectedpath, err := filepath.Abs("./test/fakehome/zFuncExists")
   280  	if err != nil {
   281  		t.Error("should not return an error, bot got:", err)
   282  	} else if path != expectedpath {
   283  		t.Error("should return \n", expectedpath, ", but got:\n", path, "\n")
   284  	}
   285  }
   286  
   287  func TestZshUser(t *testing.T) {
   288  	// defaults
   289  	SetDefaultValues()
   290  	os.WriteFile("./test/fakehome/.zshrc", []byte("# a fake zshrc"), 0644)
   291  	defer os.Remove("./test/fakehome/.zshrc")
   292  	defer os.Remove("./test/fakehome/zFuncExists/_ctx")
   293  	defer os.Remove("./test/fakehome/zFuncExists/_contxt")
   294  	installer := runner.NewShellInstall(mimiclog.NewNullLogger())
   295  	installer.SetUserHomePath("./test/fakehome")
   296  	fpath := "[ABS]/test/fakehome/.zfunc:[ABS]/test/fakehome/zFuncExists:[ABS]/test/fakehome/zFuncNotExists"
   297  	abs, _ := filepath.Abs(".")
   298  	fpath = strings.ReplaceAll(fpath, "[ABS]", abs)
   299  	os.Setenv("FPATH", fpath)
   300  
   301  	cobra := runner.NewCobraCmds()
   302  
   303  	if err := installer.ZshUpdate(cobra.RootCmd); err != nil {
   304  		t.Error("should not return an error, bot got:", err)
   305  	}
   306  	assertFileExists(t, "test/fakehome/zFuncExists/_ctx")
   307  	assertFileExists(t, "test/fakehome/zFuncExists/_contxt")
   308  }
   309  
   310  func TestZshUserRenamed(t *testing.T) {
   311  	configure.SetShortcut("KATA")   // force the shortcut to UGA
   312  	configure.SetBinaryName("NANA") // chage binaray name to NANA
   313  	os.WriteFile("./test/fakehome/.zshrc", []byte("# a fake zshrc"), 0644)
   314  	defer os.Remove("./test/fakehome/.zshrc")
   315  	defer os.Remove("./test/fakehome/zFuncExists/_KATA")
   316  	defer os.Remove("./test/fakehome/zFuncExists/_NANA")
   317  	installer := runner.NewShellInstall(mimiclog.NewNullLogger())
   318  	installer.SetUserHomePath("./test/fakehome")
   319  	fpath := "[ABS]/test/fakehome/.zfunc:[ABS]/test/fakehome/zFuncExists:[ABS]/test/fakehome/zFuncNotExists"
   320  	abs, _ := filepath.Abs(".")
   321  	fpath = strings.ReplaceAll(fpath, "[ABS]", abs)
   322  	os.Setenv("FPATH", fpath)
   323  
   324  	cobra := runner.NewCobraCmds()
   325  
   326  	if err := installer.ZshUpdate(cobra.RootCmd); err != nil {
   327  		t.Error("should not return an error, bot got:", err)
   328  	}
   329  	assertFileExists(t, "test/fakehome/zFuncExists/_KATA")
   330  	assertFileExists(t, "test/fakehome/zFuncExists/_NANA")
   331  }
   332  
   333  func TestZshDirFind(t *testing.T) {
   334  	// testing depends if zsh is installed
   335  	zsh := runner.NewZshHelper()
   336  	zPath, err := zsh.GetBinPath()
   337  	if err != nil || zPath == "" {
   338  		t.Log("skipped zsh Testing, because it seems zsh not being installed.")
   339  		t.SkipNow()
   340  	}
   341  	// defaults
   342  	fpath := "[ABS]/test/fakehome/.zfunc:[ABS]/test/fakehome/zFuncExists:[ABS]/test/fakehome/zFuncNotExists"
   343  	abs, _ := filepath.Abs(".")
   344  	fpath = strings.ReplaceAll(fpath, "[ABS]", abs)
   345  	os.Setenv("FPATH", fpath)
   346  	installer := runner.NewShellInstall(mimiclog.NewNullLogger())
   347  	instDir, err := installer.ZshFuncDir()
   348  	if err != nil {
   349  		t.Error("should not return an error, bot got:", err)
   350  	}
   351  
   352  	if instDir == "" {
   353  		t.Error("should return a directory, but got:", instDir)
   354  	}
   355  
   356  }
   357  
   358  func TestZshDirFindWithFpathSet(t *testing.T) {
   359  	// testing depends if zsh is installed
   360  	zsh := runner.NewZshHelper()
   361  	zPath, err := zsh.GetBinPath()
   362  	if err != nil || zPath == "" {
   363  		t.Log("skipped zsh Testing, because it seems zsh not being installed.")
   364  		t.SkipNow()
   365  	}
   366  	// defaults
   367  	SetDefaultValues()
   368  	os.Setenv("FPATH", "fpath1:fpath2:fpath3")
   369  	installer := runner.NewShellInstall(mimiclog.NewNullLogger())
   370  	_, err = installer.ZshFuncDir()
   371  	// we expect an error, because the fpath is not a directory
   372  	if err == nil {
   373  		t.Error("should return an error, bot got:", err)
   374  	}
   375  
   376  }