github.com/nilium/gitlab-runner@v12.5.0+incompatible/shells/cmd_test.go (about)

     1  package shells
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  type testCase struct {
    12  	in  string
    13  	out string
    14  }
    15  
    16  type outputColor struct {
    17  	fn    func(string, ...interface{})
    18  	color string
    19  }
    20  
    21  func TestCMD_EchoShellEscapes(t *testing.T) {
    22  	for i, tc := range []testCase{
    23  		{`abcdefghijklmnopqrstuvwxyz`, `abcdefghijklmnopqrstuvwxyz`},
    24  		{`^ & < > |`, `^^ ^& ^< ^> ^|`},
    25  		// FIXME: this currently escapes to ^! when it doesn't need to
    26  		// {`!`, `!`},
    27  		{`( )`, `^( ^)`},
    28  	} {
    29  		writer := &CmdWriter{}
    30  		for j, functionsToTest := range []outputColor{
    31  			{writer.Notice, "\x1b[32;1m"},
    32  			{writer.Warning, "\x1b[0;33m"},
    33  			{writer.Error, "\x1b[31;1m"},
    34  			{writer.Print, "\x1b[0;m"},
    35  		} {
    36  			functionsToTest.fn(tc.in)
    37  			expected := fmt.Sprintf("echo %s%s\x1b[0;m\r\n", functionsToTest.color, tc.out)
    38  			assert.Equal(t, expected, writer.String(), "case %d : %d", i, j)
    39  			writer.Reset()
    40  		}
    41  	}
    42  }
    43  
    44  func TestCMD_CDShellEscapes(t *testing.T) {
    45  	for i, tc := range []testCase{
    46  		{`c:\`, `c:\`},
    47  		{`c:/`, `c:\`},
    48  		{`c:\Program Files`, `c:\Program Files`},
    49  		{`c:\Program Files (x86)`, `c:\Program Files (x86)`},      // Don't escape the parens
    50  		{`c: | rd Windows\System32`, `c: ^| rd Windows\System32`}, // Escape the |
    51  	} {
    52  		writer := &CmdWriter{}
    53  		writer.Cd(tc.in)
    54  		expected := fmt.Sprintf("cd /D \"%s\"\r\nIF !errorlevel! NEQ 0 exit /b !errorlevel!\r\n\r\n", tc.out)
    55  		assert.Equal(t, expected, writer.String(), "case %d", i)
    56  	}
    57  }
    58  
    59  func TestCMD_CommandShellEscapes(t *testing.T) {
    60  	writer := &CmdWriter{}
    61  	writer.Command("foo", "x&(y)")
    62  
    63  	assert.Equal(t, "\"foo\" \"x^&(y)\"\r\nIF !errorlevel! NEQ 0 exit /b !errorlevel!\r\n\r\n", writer.String())
    64  }
    65  
    66  func TestCMD_IfCmdShellEscapes(t *testing.T) {
    67  	writer := &CmdWriter{}
    68  	writer.IfCmd("foo", "x&(y)")
    69  
    70  	assert.Equal(t, "\"foo\" \"x^&(y)\" 2>NUL 1>NUL\r\nIF !errorlevel! EQU 0 (\r\n", writer.String())
    71  }
    72  
    73  func TestCMD_DelayedExpanstionFeatureFlag(t *testing.T) {
    74  	cases := map[bool]string{
    75  		true:  "\"foo\"\r\nIF %errorlevel% NEQ 0 exit /b %errorlevel%\r\n\r\n",
    76  		false: "\"foo\"\r\nIF !errorlevel! NEQ 0 exit /b !errorlevel!\r\n\r\n",
    77  	}
    78  
    79  	for disableDelayedErrorLevelExpansion, expectedCmd := range cases {
    80  		t.Run("disableDelayedErrorLevelExpansion_"+strconv.FormatBool(disableDelayedErrorLevelExpansion), func(t *testing.T) {
    81  			writer := &CmdWriter{
    82  				disableDelayedErrorLevelExpansion: disableDelayedErrorLevelExpansion,
    83  			}
    84  			writer.Command("foo")
    85  
    86  			assert.Equal(t, expectedCmd, writer.String())
    87  		})
    88  	}
    89  
    90  }