github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/integration/integration_piper_test.go (about) 1 //go:build integration 2 // +build integration 3 4 // can be executed with 5 // go test -v -tags integration -run TestPiperIntegration ./integration/... 6 7 package main 8 9 import ( 10 "bytes" 11 "fmt" 12 "io" 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 TestPiperIntegrationHelp(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.DirEntry 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 = os.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 // createTmpDir calls t.TempDir() and returns the path name after the evaluation 116 // of any symbolic links. 117 // 118 // On Docker for Mac, t.TempDir() returns e.g. 119 // /var/folders/bl/wbxjgtzx7j5_mjsmfr3ynlc00000gp/T/<the-test-name>/001 120 func createTmpDir(t *testing.T) (string, error) { 121 tmpDir, err := filepath.EvalSymlinks(t.TempDir()) 122 if err != nil { 123 return "", err 124 } 125 return tmpDir, nil 126 }