github.com/markusbkk/elvish@v0.0.0-20231204143114-91dc52438621/pkg/shell/script_test.go (about)

     1  package shell
     2  
     3  import (
     4  	"testing"
     5  
     6  	. "github.com/markusbkk/elvish/pkg/prog/progtest"
     7  	"github.com/markusbkk/elvish/pkg/testutil"
     8  )
     9  
    10  func TestScript(t *testing.T) {
    11  	testutil.InTempDir(t)
    12  	testutil.MustWriteFile("a.elv", "echo hello")
    13  
    14  	Test(t, &Program{},
    15  		ThatElvish("a.elv").WritesStdout("hello\n"),
    16  		ThatElvish("-c", "echo hello").WritesStdout("hello\n"),
    17  		ThatElvish("non-existent.elv").
    18  			ExitsWith(2).
    19  			WritesStderrContaining("cannot read script"),
    20  
    21  		// parse error
    22  		ThatElvish("-c", "echo [").
    23  			ExitsWith(2).
    24  			WritesStderrContaining("parse error"),
    25  		// parse error with -compileonly
    26  		ThatElvish("-compileonly", "-json", "-c", "echo [").
    27  			ExitsWith(2).
    28  			WritesStdout(`[{"fileName":"code from -c","start":6,"end":6,"message":"should be ']'"}]`+"\n"),
    29  		// multiple parse errors with -compileonly -json
    30  		ThatElvish("-compileonly", "-json", "-c", "echo [{").
    31  			ExitsWith(2).
    32  			WritesStdout(`[{"fileName":"code from -c","start":7,"end":7,"message":"should be ',' or '}'"},{"fileName":"code from -c","start":7,"end":7,"message":"should be ']'"}]`+"\n"),
    33  
    34  		// compilation error
    35  		ThatElvish("-c", "echo $a").
    36  			ExitsWith(2).
    37  			WritesStderrContaining("compilation error"),
    38  		// compilation error with -compileonly
    39  		ThatElvish("-compileonly", "-json", "-c", "echo $a").
    40  			ExitsWith(2).
    41  			WritesStdout(`[{"fileName":"code from -c","start":5,"end":7,"message":"variable $a not found"}]`+"\n"),
    42  		// parse error and compilation error with -compileonly
    43  		ThatElvish("-compileonly", "-json", "-c", "echo [$a").
    44  			ExitsWith(2).
    45  			WritesStdout(`[{"fileName":"code from -c","start":8,"end":8,"message":"should be ']'"},{"fileName":"code from -c","start":6,"end":8,"message":"variable $a not found"}]`+"\n"),
    46  
    47  		// exception
    48  		ThatElvish("-c", "fail failure").
    49  			ExitsWith(2).
    50  			WritesStdout("").
    51  			WritesStderrContaining("fail failure"),
    52  		// exception with -compileonly
    53  		ThatElvish("-compileonly", "-c", "fail failure").
    54  			ExitsWith(0),
    55  	)
    56  }