github.com/markusbkk/elvish@v0.0.0-20231204143114-91dc52438621/pkg/shell/shell_test.go (about) 1 package shell 2 3 import ( 4 "os" 5 "path/filepath" 6 "testing" 7 8 "github.com/markusbkk/elvish/pkg/env" 9 . "github.com/markusbkk/elvish/pkg/prog/progtest" 10 . "github.com/markusbkk/elvish/pkg/testutil" 11 ) 12 13 func TestShell_LegacyLibPath(t *testing.T) { 14 home := setupHomePaths(t) 15 MustWriteFile(filepath.Join(home, ".elvish", "lib", "a.elv"), "echo mod a") 16 17 Test(t, &Program{}, 18 ThatElvish("-c", "use a").WritesStdout("mod a\n"), 19 ) 20 } 21 22 // Most high-level tests against Program are specific to either script mode or 23 // interactive mode, and are found in script_test.go and interact_test.go. 24 25 var incSHLVLTests = []struct { 26 name string 27 old string 28 unset bool 29 wantNew string 30 }{ 31 {name: "normal", old: "10", wantNew: "11"}, 32 {name: "unset", unset: true, wantNew: "1"}, 33 {name: "invalid", old: "invalid", wantNew: "1"}, 34 // Other shells don't agree on what to do when SHLVL is negative: 35 // 36 // ~> E:SHLVL=-100 bash -c 'echo $SHLVL' 37 // 0 38 // ~> E:SHLVL=-100 zsh -c 'echo $SHLVL' 39 // -99 40 // ~> E:SHLVL=-100 fish -c 'echo $SHLVL' 41 // 1 42 // 43 // Elvish follows Zsh here. 44 {name: "negative", old: "-100", wantNew: "-99"}, 45 } 46 47 func TestIncSHLVL(t *testing.T) { 48 Setenv(t, env.SHLVL, "") 49 50 for _, test := range incSHLVLTests { 51 t.Run(test.name, func(t *testing.T) { 52 if test.unset { 53 os.Unsetenv(env.SHLVL) 54 } else { 55 os.Setenv(env.SHLVL, test.old) 56 } 57 58 restore := IncSHLVL() 59 shlvl := os.Getenv(env.SHLVL) 60 if shlvl != test.wantNew { 61 t.Errorf("got SHLVL = %q, want %q", shlvl, test.wantNew) 62 } 63 64 restore() 65 // Test that state of SHLVL is restored. 66 restored, restoredSet := os.LookupEnv(env.SHLVL) 67 if test.unset { 68 if restoredSet { 69 t.Errorf("SHLVL not unset") 70 } 71 } else { 72 if restored != test.old { 73 t.Errorf("SHLVL restored to %q, want %q", restored, test.old) 74 } 75 } 76 }) 77 } 78 } 79 80 // Common test utilities. 81 82 func setupHomePaths(t Cleanuper) string { 83 Unsetenv(t, env.XDG_CONFIG_HOME) 84 Unsetenv(t, env.XDG_DATA_HOME) 85 return TempHome(t) 86 }