github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/pkg/pogosh/shell_test.go (about)

     1  // Copyright 2020 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package pogosh
     6  
     7  import "testing"
     8  
     9  // The positive tests are expected to exit with code 123.
    10  var shellPositiveTests = []struct {
    11  	name string
    12  	in   string
    13  }{
    14  	{
    15  		"Exit",
    16  		`exit 123`,
    17  	},
    18  	{
    19  		"Exit2",
    20  		`exit 123
    21  		exit 0`,
    22  	},
    23  	{
    24  		"Echo",
    25  		`/bin/echo hello
    26  		exit 123
    27  		`,
    28  	},
    29  	/*{
    30  			"If Statement",
    31  			`if true; then
    32  	exit 123
    33  	done`,
    34  		},
    35  		{
    36  			"If-Else Statement",
    37  			`if false; then
    38  		exit 124
    39  	else
    40  		exit 123
    41  	done`,
    42  		},
    43  		{
    44  			"Arithmetic",
    45  			`exit $((2 + 010 + 0x10 + 5 * (1 + 2) + 3 / 2 + 7 % 4 - 8 + (2 << 3) + (7 >> 1) + \
    46  	(1 < 2) + (3 > 4) + (80 <= 80) + (90 >= 90) + (7 == 7) + (8 == 8) + (5 != 3) + (2 & 3) + \
    47  	(3 ^ 2) + (3 | 2) + (3 && 2) + (3 || 0) + (1 ? 2 : 3)))`, // TODO: add difference with 123
    48  		},
    49  		{
    50  			"Arithmetic Assignment",
    51  			`X=5
    52  	Y=$(((X*=5) == 25 ? X : 0))
    53  	exit $((X + Y + 193))`,
    54  		},*/
    55  }
    56  
    57  func TestRunPositive(t *testing.T) {
    58  	for _, tt := range shellPositiveTests {
    59  		t.Run(tt.name, func(t *testing.T) {
    60  			state := DefaultState()
    61  			code, err := state.Run(tt.in)
    62  
    63  			if err != nil {
    64  				t.Error(err)
    65  			} else {
    66  				if code != 123 {
    67  					t.Errorf("got %v, want 123", code)
    68  				}
    69  			}
    70  		})
    71  	}
    72  }
    73  
    74  // The negative tests are expected to return an error.
    75  var shellNegativeTests = []struct {
    76  	name string
    77  	in   string
    78  	err  string
    79  }{
    80  	{
    81  		"Non-ASCII Characters",
    82  		"echo hello\xbd",
    83  		`<pogosh>:1:11: non-ascii character, '\xbd'`,
    84  	},
    85  	/*{
    86  		"Division by Zero",
    87  		"X=0; echo $((15/X))",
    88  		"<pogosh>:1:10: division by zero, '15/0'",
    89  	},*/
    90  }
    91  
    92  func TestRunNegative(t *testing.T) {
    93  	for _, tt := range shellNegativeTests {
    94  		t.Run(tt.name, func(t *testing.T) {
    95  			state := DefaultState()
    96  			_, err := state.Run(tt.in)
    97  
    98  			errStr := "nil"
    99  			if err != nil {
   100  				errStr = err.Error()
   101  			}
   102  
   103  			if errStr != tt.err {
   104  				t.Errorf("got \"%s\", want \"%s\"", errStr, tt.err)
   105  			}
   106  		})
   107  	}
   108  }
   109  
   110  /*func ExampleRun() {
   111  	state := DefaultState()
   112  	state.Run(`echo Launching rocket...`)
   113  	state.Run(`ACTION='BLASTOFF!!!'`)
   114  	state.Run(`for T in $(seq 10 -1 1); do echo -n -- "T-$T "; done`)
   115  	state.Run(`echo "$ACTION"`)
   116  	// Output:
   117  	// Launching rocket...
   118  	// 10 9 8 7 6 5 4 3 2 1 BLASTOFF!!!
   119  }
   120  
   121  func ExampleRun_parallel() {
   122  
   123  }
   124  
   125  func ExampleRunInteractive() {
   126  	state := DefaultState()
   127  	state.Prompt = func () { return "> " }
   128  	for {
   129  		code, err := state.RunInteractive()
   130  		if err == nil {
   131  			os.Exit(code)
   132  		}
   133  		fmt.Println("Error:", err)
   134  	}
   135  }
   136  
   137  func ExampleExec() {
   138  	DefaultState().Run("example.sh")
   139  	if err != nil {
   140  		fmt.Println("Error:", err)
   141  	}
   142  }*/