github.com/alloyci/alloy-runner@v1.0.1-0.20180222164613-925503ccafd6/shells/shell_writer_test.go (about) 1 package shells 2 3 import ( 4 "io/ioutil" 5 "os" 6 "os/exec" 7 "path/filepath" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 13 "gitlab.com/gitlab-org/gitlab-runner/helpers" 14 ) 15 16 type TestShellWriter interface { 17 ShellWriter 18 19 Finish(trace bool) string 20 GetTemporaryPath() string 21 } 22 23 func onShell(t *testing.T, name, command, extension string, cmdArgs []string, writer TestShellWriter) { 24 const TestPath = "test-path" 25 26 t.Run(name, func(t *testing.T) { 27 scriptFile := filepath.Join(writer.GetTemporaryPath(), name+"-test-script."+extension) 28 29 testTmpDir := writer.MkTmpDir(name + "-mkdir-test") 30 writer.Cd(testTmpDir) 31 writer.MkDir(TestPath) 32 writer.MkDir(TestPath) 33 script := writer.Finish(false) 34 35 err := ioutil.WriteFile(scriptFile, []byte(script), 0700) 36 require.NoError(t, err) 37 38 if helpers.SkipIntegrationTests(t, command) { 39 t.Skip() 40 } 41 42 cmdArgs = append(cmdArgs, scriptFile) 43 cmd := exec.Command(command, cmdArgs...) 44 err = cmd.Run() 45 assert.NoError(t, err) 46 47 createdPath := filepath.Join(testTmpDir, TestPath) 48 _, err = ioutil.ReadDir(createdPath) 49 assert.NoError(t, err) 50 }) 51 } 52 53 func TestMkDir(t *testing.T) { 54 tmpDir, err := ioutil.TempDir("", "test-shell-script") 55 defer os.RemoveAll(tmpDir) 56 require.NoError(t, err) 57 58 onShell(t, "bash", "bash", "sh", []string{}, &BashWriter{TemporaryPath: tmpDir}) 59 onShell(t, "cmd", "cmd.exe", "cmd", []string{"/Q", "/C"}, &CmdWriter{TemporaryPath: tmpDir}) 60 onShell(t, "powershell", "powershell.exe", "ps1", []string{"-noprofile", "-noninteractive", "-executionpolicy", "Bypass", "-command"}, &PsWriter{TemporaryPath: tmpDir}) 61 }