diff options
| author | Shulhan <ms@kilabit.info> | 2023-12-18 01:57:46 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2023-12-18 01:57:46 +0700 |
| commit | 207cc263d4f28e11af896f0b1e3fc06d305c2a62 (patch) | |
| tree | 9afb9e9a1f179227b45fc74cd40edbd98b3c9848 | |
| parent | 1640caca8245096a7fb1bc6f1b1e03881ce5824e (diff) | |
| download | awwan-207cc263d4f28e11af896f0b1e3fc06d305c2a62.tar.xz | |
all: fix panic due to out of range when running "#require" statement
| -rw-r--r-- | awwan_local_test.go | 55 | ||||
| -rw-r--r-- | session.go | 4 | ||||
| -rw-r--r-- | testdata/local/local_test.data | 6 |
3 files changed, 45 insertions, 20 deletions
diff --git a/awwan_local_test.go b/awwan_local_test.go index 756469f..f13d09f 100644 --- a/awwan_local_test.go +++ b/awwan_local_test.go @@ -17,15 +17,16 @@ import ( ) func TestAwwanLocal(t *testing.T) { + type testCase struct { + lineRange string + tagOutput string + } + var ( - baseDir = `testdata/local` - scriptDir = baseDir - scriptFile = filepath.Join(scriptDir, `local.aww`) - tdataFile = filepath.Join(scriptDir, `local_test.data`) - mockrw = mock.ReadWriter{} + baseDir = `testdata/local` + tdataFile = filepath.Join(baseDir, `local_test.data`) tdata *test.Data - aww *Awwan err error ) @@ -34,6 +35,11 @@ func TestAwwanLocal(t *testing.T) { t.Fatal(err) } + var ( + mockrw = mock.ReadWriter{} + aww *Awwan + ) + aww, err = New(baseDir) if err != nil { t.Fatal(err) @@ -42,25 +48,36 @@ func TestAwwanLocal(t *testing.T) { // Mock terminal to read passphrase for private key. aww.cryptoc.termrw = &mockrw + var cases = []testCase{{ + lineRange: `1-`, + tagOutput: `local:1-`, + }, { + lineRange: `100-`, + tagOutput: `local:100-`, + }} + var ( - req *ExecRequest - logw bytes.Buffer + scriptFile = filepath.Join(baseDir, `local.aww`) + req *ExecRequest + logw bytes.Buffer + c testCase ) + for _, c = range cases { + req, err = NewExecRequest(CommandModeLocal, scriptFile, c.lineRange) + if err != nil { + t.Fatal(err) + } - req, err = NewExecRequest(CommandModeLocal, scriptFile, `1-`) - if err != nil { - t.Fatal(err) - } + logw.Reset() + req.registerLogWriter(`output`, &logw) - req.registerLogWriter(`output`, &logw) + err = aww.Local(req) + if err != nil { + t.Fatal(err) + } - err = aww.Local(req) - if err != nil { - t.Fatal(err) + test.Assert(t, `stdout`, string(tdata.Output[c.tagOutput]), logw.String()) } - - var exp = string(tdata.Output[`local:output`]) - test.Assert(t, `stdout`, exp, logw.String()) } func TestAwwanLocal_Get(t *testing.T) { @@ -414,6 +414,10 @@ func (ses *Session) close() (err error) { // executeRequires run the "#require:" statements from line 0 until // the start argument in the local system. func (ses *Session) executeRequires(req *ExecRequest, pos linePosition) (err error) { + if pos.start >= int64(len(req.script.requires)) { + return nil + } + var ( stmt *Statement x int64 diff --git a/testdata/local/local_test.data b/testdata/local/local_test.data index 621d0b2..331293f 100644 --- a/testdata/local/local_test.data +++ b/testdata/local/local_test.data @@ -1,6 +1,6 @@ Test on "local" command. -<<< local:output +<<< local:1- - === BEGIN: local testdata/local/local.aww 1- - --> 1: echo "hello" hello @@ -9,3 +9,7 @@ nothing - --> 5: echo "local" local - === END: local testdata/local/local.aww 1- + +<<< local:100- +- === BEGIN: local testdata/local/local.aww 100- +- === END: local testdata/local/local.aww 100- |
