Skip to content

Commit

Permalink
Merge pull request #108 from xushiwei/t
Browse files Browse the repository at this point in the history
gsh exec:  use parent env
  • Loading branch information
xushiwei authored Feb 25, 2024
2 parents 21addd6 + be916da commit c82bb10
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
20 changes: 4 additions & 16 deletions gsh/classfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,6 @@ func (p *App) Exec__0(env map[string]string, name string, args ...string) error
func (p *App) Exec__1(cmdline string) error {
var iCmd = -1
var items = strings.Fields(cmdline)
var env []string
var initEnv = func() {
env = Sys.Environ()
if iCmd > 0 {
env = Setenv__2(env, items[:iCmd])
}
}
var mapping = func(name string) string {
if env == nil {
initEnv()
}
return Getenv(env, name)
}
for i, e := range items {
pos := strings.IndexAny(e, "=$")
if pos >= 0 && e[pos] == '=' {
Expand All @@ -104,14 +91,15 @@ func (p *App) Exec__1(cmdline string) error {
iCmd = i
}
if pos >= 0 {
items[i] = os.Expand(e, mapping)
items[i] = Sys.ExpandEnv(e)
}
}
if iCmd < 0 {
return errors.New("exec: no command")
}
if env == nil && iCmd > 0 {
initEnv()
var env []string
if iCmd > 0 {
env = Setenv__2(Sys.Environ(), items[:iCmd])
}
return p.execWith(env, items[iCmd], items[iCmd+1:]...)
}
Expand Down
16 changes: 14 additions & 2 deletions gsh/gsh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
type mockOS struct{}

func (p mockOS) Environ() []string {
return mockEnv
return append(make([]string, 0, len(mockEnv)), mockEnv...)
}

func (p mockOS) ExpandEnv(s string) string {
Expand Down Expand Up @@ -142,7 +142,19 @@ func TestExecSh2(t *testing.T) {
err := app.Exec__1("FOO=$BAR ./app $FOO")
check(t, err)
})
if v := app.Output(); v != "[FOO=bar BAR=bar] [./app bar]\n" {
if v := app.Output(); v != "[FOO=bar BAR=bar] [./app foo]\n" {
t.Fatal("TestExecSh2:", v)
}
}

func TestExecSh2_Env(t *testing.T) {
var app App
app.initApp()
capout(&app, func() {
err := app.Exec__1("FOO=$BAR ./app ${FOO}")
check(t, err)
})
if v := app.Output(); v != "[FOO=bar BAR=bar] [./app foo]\n" {
t.Fatal("TestExecSh2:", v)
}
}
Expand Down

0 comments on commit c82bb10

Please sign in to comment.