github.com/NeowayLabs/nash@v0.2.2-0.20200127205349-a227041ffd50/internal/sh/shell_import_test.go (about) 1 package sh_test 2 3 import ( 4 "bytes" 5 "path/filepath" 6 "strings" 7 "testing" 8 9 "github.com/madlambda/nash/internal/sh" 10 "github.com/madlambda/nash/internal/sh/internal/fixture" 11 ) 12 13 func TestImportsLibFromNashPathLibDir(t *testing.T) { 14 15 nashdirs := fixture.SetupNashDirs(t) 16 defer nashdirs.Cleanup() 17 18 writeFile(t, filepath.Join(nashdirs.Lib, "lib.sh"), ` 19 fn test() { 20 echo "hasnashpath" 21 } 22 `) 23 24 newTestShell(t, nashdirs.Path, nashdirs.Root).ExecCheckingOutput(t, ` 25 import lib 26 test() 27 `, "hasnashpath\n") 28 } 29 30 func TestImportsLibFromNashPathLibDirBeforeNashRootStdlib(t *testing.T) { 31 32 nashdirs := fixture.SetupNashDirs(t) 33 defer nashdirs.Cleanup() 34 35 writeFile(t, filepath.Join(nashdirs.Lib, "lib.sh"), ` 36 fn test() { 37 echo "libcode" 38 } 39 `) 40 41 writeFile(t, filepath.Join(nashdirs.Stdlib, "lib.sh"), ` 42 fn test() { 43 echo "stdlibcode" 44 } 45 `) 46 47 newTestShell(t, nashdirs.Path, nashdirs.Root).ExecCheckingOutput(t, ` 48 import lib 49 test() 50 `, "libcode\n") 51 } 52 53 func TestImportsLibFromNashRootStdlib(t *testing.T) { 54 55 nashdirs := fixture.SetupNashDirs(t) 56 defer nashdirs.Cleanup() 57 58 writeFile(t, filepath.Join(nashdirs.Stdlib, "lib.sh"), ` 59 fn test() { 60 echo "stdlibcode" 61 } 62 `) 63 64 newTestShell(t, nashdirs.Path, nashdirs.Root).ExecCheckingOutput(t, ` 65 import lib 66 test() 67 `, "stdlibcode\n") 68 } 69 70 func TestImportsLibFromWorkingDirBeforeLibAndStdlib(t *testing.T) { 71 72 workingdir, rmdir := fixture.Tmpdir(t) 73 defer rmdir() 74 75 curwd := getwd(t) 76 chdir(t, workingdir) 77 defer chdir(t, curwd) 78 79 nashdirs := fixture.SetupNashDirs(t) 80 defer nashdirs.Cleanup() 81 82 writeFile(t, filepath.Join(workingdir, "lib.sh"), ` 83 fn test() { 84 echo "localcode" 85 } 86 `) 87 88 writeFile(t, filepath.Join(nashdirs.Lib, "lib.sh"), ` 89 fn test() { 90 echo "libcode" 91 } 92 `) 93 94 writeFile(t, filepath.Join(nashdirs.Stdlib, "lib.sh"), ` 95 fn test() { 96 echo "stdlibcode" 97 } 98 `) 99 100 newTestShell(t, nashdirs.Path, nashdirs.Root).ExecCheckingOutput(t, ` 101 import lib 102 test() 103 `, "localcode\n") 104 } 105 106 func TestStdErrOnInvalidSearchPaths(t *testing.T) { 107 type testCase struct { 108 name string 109 nashpath string 110 nashroot string 111 errmsg string 112 } 113 114 const nashrooterr = "invalid nashroot" 115 const nashpatherr = "invalid nashpath" 116 117 validDir, rmdir := fixture.Tmpdir(t) 118 defer rmdir() 119 120 validfile := filepath.Join(validDir, "notdir") 121 writeFile(t, validfile, "whatever") 122 123 cases := []testCase{ 124 { 125 name: "EmptyNashPath", 126 nashpath: "", 127 nashroot: validDir, 128 errmsg: nashpatherr, 129 }, 130 { 131 name: "NashPathDontExists", 132 nashpath: filepath.Join(validDir, "dontexists"), 133 nashroot: validDir, 134 errmsg: nashpatherr, 135 }, 136 { 137 name: "EmptyNashRoot", 138 nashpath: validDir, 139 nashroot: "", 140 errmsg: nashrooterr, 141 }, 142 { 143 name: "NashRootDontExists", 144 nashroot: filepath.Join(validDir, "dontexists"), 145 nashpath: validDir, 146 errmsg: nashrooterr, 147 }, 148 { 149 name: "NashPathIsFile", 150 nashroot: validDir, 151 nashpath: validfile, 152 errmsg: nashpatherr, 153 }, 154 { 155 name: "NashRootIsFile", 156 nashroot: validfile, 157 nashpath: validDir, 158 errmsg: nashrooterr, 159 }, 160 { 161 name: "NashPathIsRelative", 162 nashroot: validDir, 163 nashpath: "./", 164 errmsg: nashpatherr, 165 }, 166 { 167 name: "NashRootIsRelative", 168 nashroot: "./", 169 nashpath: validDir, 170 errmsg: nashrooterr, 171 }, 172 { 173 name: "NashRootAndNashPathAreEqual", 174 nashroot: validDir, 175 nashpath: validDir, 176 errmsg: "invalid nashpath and nashroot", 177 }, 178 } 179 180 for _, c := range cases { 181 t.Run(c.name, func(t *testing.T) { 182 _, err := sh.NewAbortShell(c.nashpath, c.nashroot) 183 if c.errmsg != "" { 184 if err == nil { 185 t.Fatalf("expected err[%s]", c.errmsg) 186 } 187 if !strings.HasPrefix(err.Error(), c.errmsg) { 188 t.Fatalf("errors mismatch: [%s] didnt contains [%s]", err, c.errmsg) 189 } 190 } else if err != nil { 191 t.Fatalf("got unexpected error[%s]", err) 192 } 193 }) 194 } 195 } 196 197 type testshell struct { 198 shell *sh.Shell 199 stdout *bytes.Buffer 200 } 201 202 func (s *testshell) ExecCheckingOutput(t *testing.T, code string, expectedOutupt string) { 203 err := s.shell.Exec("shellenvtest", code) 204 if err != nil { 205 t.Fatal(err) 206 } 207 208 output := s.stdout.String() 209 s.stdout.Reset() 210 211 if output != expectedOutupt { 212 t.Fatalf( 213 "expected output: [%s] got: [%s]", 214 expectedOutupt, 215 output, 216 ) 217 } 218 } 219 220 func newTestShell(t *testing.T, nashpath string, nashroot string) *testshell { 221 222 shell, err := sh.NewShell(nashpath, nashroot) 223 if err != nil { 224 t.Fatal(err) 225 } 226 var out bytes.Buffer 227 shell.SetStdout(&out) 228 229 return &testshell{shell: shell, stdout: &out} 230 }