Skip to content

Commit ca13057

Browse files
authored
Add nash.Setvar and nash.Getvar (#135)
Signed-off-by: Tiago Natel de Moura <tiago.natel@neoway.com.br>
1 parent a66cee4 commit ca13057

4 files changed

Lines changed: 69 additions & 24 deletions

File tree

internal/sh/shell.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -294,13 +294,13 @@ func (shell *Shell) SetEnviron(processEnv []string) {
294294
}
295295
}
296296

297-
func (shell *Shell) GetVar(name string) (sh.Obj, bool) {
297+
func (shell *Shell) Getvar(name string) (sh.Obj, bool) {
298298
if value, ok := shell.vars[name]; ok {
299299
return value, ok
300300
}
301301

302302
if shell.parent != nil {
303-
return shell.parent.GetVar(name)
303+
return shell.parent.Getvar(name)
304304
}
305305

306306
return nil, false
@@ -1355,7 +1355,7 @@ func (shell *Shell) evalVariable(a ast.Expr) (sh.Obj, error) {
13551355
vexpr := a.(*ast.VarExpr)
13561356
varName := vexpr.Name()
13571357

1358-
if v, ok = shell.GetVar(varName[1:]); !ok {
1358+
if v, ok = shell.Getvar(varName[1:]); !ok {
13591359
return nil, fmt.Errorf("Variable %s not set on shell %s", varName, shell.name)
13601360
}
13611361

@@ -1419,7 +1419,7 @@ func (shell *Shell) executeSetenv(v *ast.SetenvNode) error {
14191419

14201420
varName := v.Identifier()
14211421

1422-
if varValue, ok = shell.GetVar(varName); !ok {
1422+
if varValue, ok = shell.Getvar(varName); !ok {
14231423
return fmt.Errorf("Variable '%s' not set on shell %s", varName, shell.name)
14241424
}
14251425

@@ -1492,7 +1492,7 @@ func (shell *Shell) executeExecAssign(v *ast.ExecAssignNode) error {
14921492

14931493
outStr := string(varOut.Bytes())
14941494

1495-
if ifs, ok := shell.GetVar("IFS"); ok && ifs.Type() == sh.ListType {
1495+
if ifs, ok := shell.Getvar("IFS"); ok && ifs.Type() == sh.ListType {
14961496
ifslist := ifs.(*sh.ListObj)
14971497

14981498
if len(ifslist.List()) > 0 {

internal/sh/shell_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1865,7 +1865,7 @@ func TestExecuteErrorSuppressionAll(t *testing.T) {
18651865
return
18661866
}
18671867

1868-
scode, ok := shell.GetVar("status")
1868+
scode, ok := shell.Getvar("status")
18691869

18701870
if !ok || scode.Type() != sh.StringType || scode.String() != strconv.Itoa(ENotFound) {
18711871
t.Errorf("Invalid status code %s", scode.String())
@@ -1879,7 +1879,7 @@ func TestExecuteErrorSuppressionAll(t *testing.T) {
18791879
return
18801880
}
18811881

1882-
scode, ok = shell.GetVar("status")
1882+
scode, ok = shell.Getvar("status")
18831883

18841884
if !ok || scode.Type() != sh.StringType || scode.String() != "0" {
18851885
t.Errorf("Invalid status code %s", scode)
@@ -1898,7 +1898,7 @@ func TestExecuteErrorSuppressionAll(t *testing.T) {
18981898
return
18991899
}
19001900

1901-
scode, ok = shell.GetVar("status")
1901+
scode, ok = shell.Getvar("status")
19021902

19031903
if !ok || scode.Type() != sh.StringType || scode.String() != "255|127" {
19041904
t.Errorf("Invalid status code %s", scode)

nash.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,13 @@ func (nash *Shell) SetStdin(in io.Reader) {
141141
func (nash *Shell) Stdin() io.Reader { return nash.interp.Stdin() }
142142
func (nash *Shell) Stdout() io.Writer { return nash.interp.Stdout() }
143143
func (nash *Shell) Stderr() io.Writer { return nash.interp.Stderr() }
144+
145+
// Setvar sets or updates the variable in the nash session
146+
func (nash *Shell) Setvar(name string, value sh.Obj) {
147+
nash.interp.Setvar(name, value)
148+
}
149+
150+
// Getvar retrieves a variable from nash session
151+
func (nash *Shell) Getvar(name string) (sh.Obj, bool) {
152+
return nash.interp.Getvar(name)
153+
}

nash_test.go

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"bytes"
55
"os"
66
"testing"
7+
8+
"github.com/NeowayLabs/nash/sh"
79
)
810

911
// only testing the public API
@@ -33,19 +35,19 @@ func TestExecuteFile(t *testing.T) {
3335

3436
var out bytes.Buffer
3537

36-
sh, err := New()
38+
shell, err := New()
3739

3840
if err != nil {
3941
t.Error(err)
4042
return
4143
}
4244

43-
sh.SetNashdPath(nashdPath)
44-
sh.SetStdout(&out)
45-
sh.SetStderr(os.Stderr)
46-
sh.SetStdin(os.Stdin)
45+
shell.SetNashdPath(nashdPath)
46+
shell.SetStdout(&out)
47+
shell.SetStderr(os.Stderr)
48+
shell.SetStdin(os.Stdin)
4749

48-
err = sh.ExecuteFile(testfile)
50+
err = shell.ExecuteFile(testfile)
4951

5052
if err != nil {
5153
t.Error(err)
@@ -59,7 +61,7 @@ func TestExecuteFile(t *testing.T) {
5961
}
6062

6163
func TestExecuteString(t *testing.T) {
62-
sh, err := New()
64+
shell, err := New()
6365

6466
if err != nil {
6567
t.Error(err)
@@ -68,9 +70,9 @@ func TestExecuteString(t *testing.T) {
6870

6971
var out bytes.Buffer
7072

71-
sh.SetStdout(&out)
73+
shell.SetStdout(&out)
7274

73-
err = sh.ExecuteString("-ínput-", "echo -n AAA")
75+
err = shell.ExecuteString("-ínput-", "echo -n AAA")
7476

7577
if err != nil {
7678
t.Error(err)
@@ -84,7 +86,7 @@ func TestExecuteString(t *testing.T) {
8486

8587
out.Reset()
8688

87-
err = sh.ExecuteString("-input-", `
89+
err = shell.ExecuteString("-input-", `
8890
PROMPT="humpback> "
8991
setenv PROMPT
9092
`)
@@ -94,7 +96,7 @@ func TestExecuteString(t *testing.T) {
9496
return
9597
}
9698

97-
prompt := sh.Prompt()
99+
prompt := shell.Prompt()
98100

99101
if prompt != "humpback> " {
100102
t.Errorf("Invalid prompt = %s", prompt)
@@ -104,7 +106,7 @@ func TestExecuteString(t *testing.T) {
104106
}
105107

106108
func TestSetDotDir(t *testing.T) {
107-
sh, err := New()
109+
shell, err := New()
108110

109111
if err != nil {
110112
t.Error(err)
@@ -113,18 +115,18 @@ func TestSetDotDir(t *testing.T) {
113115

114116
var out bytes.Buffer
115117

116-
sh.SetStdout(&out)
118+
shell.SetStdout(&out)
117119

118-
sh.SetDotDir("/tmp")
120+
shell.SetDotDir("/tmp")
119121

120-
dotDir := sh.DotDir()
122+
dotDir := shell.DotDir()
121123

122124
if dotDir != "/tmp" {
123125
t.Errorf("Invalid .nash = %s", dotDir)
124126
return
125127
}
126128

127-
err = sh.ExecuteString("-ínput-", "echo -n $NASHPATH")
129+
err = shell.ExecuteString("-ínput-", "echo -n $NASHPATH")
128130

129131
if err != nil {
130132
t.Error(err)
@@ -136,3 +138,36 @@ func TestSetDotDir(t *testing.T) {
136138
return
137139
}
138140
}
141+
142+
func TestSetvar(t *testing.T) {
143+
shell, err := New()
144+
145+
if err != nil {
146+
t.Error(err)
147+
return
148+
}
149+
150+
shell.Setvar("__TEST__", sh.NewStrObj("something"))
151+
152+
var out bytes.Buffer
153+
shell.SetStdout(&out)
154+
155+
err = shell.Exec("TestSetvar", `echo -n $__TEST__`)
156+
157+
if err != nil {
158+
t.Error(err)
159+
return
160+
}
161+
162+
if string(out.Bytes()) != "something" {
163+
t.Errorf("Value differ: '%s' != '%s'", string(out.Bytes()), "something")
164+
return
165+
}
166+
167+
val, ok := shell.Getvar("__TEST__")
168+
169+
if !ok || val.String() != "something" {
170+
t.Errorf("Getvar doesn't work: '%s' != '%s'", val, "something")
171+
return
172+
}
173+
}

0 commit comments

Comments
 (0)