github.com/jaylevin/jenkins-library@v1.230.4/integration/integration_test.go (about) 1 //go:build integration 2 // +build integration 3 4 // can be execute with go test -tags=integration ./integration/... 5 6 package main 7 8 import ( 9 "bytes" 10 "fmt" 11 "io" 12 "io/ioutil" 13 "os" 14 "path" 15 "path/filepath" 16 "testing" 17 18 "github.com/stretchr/testify/assert" 19 20 "github.com/SAP/jenkins-library/pkg/command" 21 "github.com/SAP/jenkins-library/pkg/piperutils" 22 ) 23 24 func TestPiperHelp(t *testing.T) { 25 t.Parallel() 26 piperHelpCmd := command.Command{} 27 28 var commandOutput bytes.Buffer 29 piperHelpCmd.Stdout(&commandOutput) 30 31 err := piperHelpCmd.RunExecutable(getPiperExecutable(), "--help") 32 33 assert.NoError(t, err, "Calling piper --help failed") 34 assert.Contains(t, commandOutput.String(), "Use \"piper [command] --help\" for more information about a command.") 35 } 36 37 func getPiperExecutable() string { 38 if p := os.Getenv("PIPER_INTEGRATION_EXECUTABLE"); len(p) > 0 { 39 fmt.Println("Piper executable for integration test: " + p) 40 return p 41 } 42 43 f := piperutils.Files{} 44 wd, _ := os.Getwd() 45 localPiper := path.Join(wd, "..", "piper") 46 exists, _ := f.FileExists(localPiper) 47 if exists { 48 fmt.Println("Piper executable for integration test: " + localPiper) 49 return localPiper 50 } 51 52 fmt.Println("Piper executable for integration test: Using 'piper' from PATH") 53 return "piper" 54 } 55 56 // copyDir copies a directory 57 func copyDir(source string, target string) error { 58 var err error 59 var fileInfo []os.FileInfo 60 var sourceInfo os.FileInfo 61 62 if sourceInfo, err = os.Stat(source); err != nil { 63 return err 64 } 65 66 if err = os.MkdirAll(target, sourceInfo.Mode()); err != nil { 67 return err 68 } 69 70 if fileInfo, err = ioutil.ReadDir(source); err != nil { 71 return err 72 } 73 for _, info := range fileInfo { 74 sourcePath := path.Join(source, info.Name()) 75 targetPath := path.Join(target, info.Name()) 76 77 if info.IsDir() { 78 if err = copyDir(sourcePath, targetPath); err != nil { 79 return err 80 } 81 } else { 82 if err = copyFile(sourcePath, targetPath); err != nil { 83 return err 84 } 85 } 86 } 87 return nil 88 } 89 90 func copyFile(source, target string) error { 91 var err error 92 var sourceFile *os.File 93 var targetFile *os.File 94 var sourceInfo os.FileInfo 95 96 if sourceFile, err = os.Open(source); err != nil { 97 return err 98 } 99 defer sourceFile.Close() 100 101 if targetFile, err = os.Create(target); err != nil { 102 return err 103 } 104 defer targetFile.Close() 105 106 if _, err = io.Copy(targetFile, sourceFile); err != nil { 107 return err 108 } 109 if sourceInfo, err = os.Stat(source); err != nil { 110 return err 111 } 112 return os.Chmod(target, sourceInfo.Mode()) 113 } 114 115 func createTmpDir(prefix string) (string, error) { 116 dirName := os.TempDir() 117 tmpDir, err := filepath.EvalSymlinks(dirName) 118 if err != nil { 119 return "", err 120 } 121 tmpDir = filepath.Clean(tmpDir) 122 path, err := ioutil.TempDir(tmpDir, prefix) 123 if err != nil { 124 return "", err 125 } 126 return path, nil 127 }