github.com/bitrise-io/go-xamarin@v0.0.0-20211005113058-bf60a8bae851/tools/nunit/nunit.go (about) 1 package nunit 2 3 import ( 4 "fmt" 5 "io" 6 "os" 7 "path/filepath" 8 9 "github.com/bitrise-io/go-utils/command" 10 "github.com/bitrise-io/go-utils/pathutil" 11 "github.com/bitrise-io/go-xamarin/constants" 12 ) 13 14 const ( 15 nunit3Console = "nunit3-console.exe" 16 ) 17 18 // Model ... 19 type Model struct { 20 nunitConsolePth string 21 22 projectPth string 23 config string 24 25 dllPth string 26 test string 27 28 resultLogPth string 29 30 customOptions []string 31 } 32 33 // SystemNunit3ConsolePath ... 34 func SystemNunit3ConsolePath() (string, error) { 35 nunitDir := os.Getenv("NUNIT_PATH") 36 if nunitDir == "" { 37 return "", fmt.Errorf("NUNIT_PATH environment is not set, failed to determin nunit console path") 38 } 39 40 nunitConsolePth := filepath.Join(nunitDir, nunit3Console) 41 if exist, err := pathutil.IsPathExists(nunitConsolePth); err != nil { 42 return "", fmt.Errorf("Failed to check if nunit console exist at (%s), error: %s", nunitConsolePth, err) 43 } else if !exist { 44 return "", fmt.Errorf("nunit console not exist at: %s", nunitConsolePth) 45 } 46 47 return nunitConsolePth, nil 48 } 49 50 // New ... 51 func New(nunitConsolePth string) (*Model, error) { 52 absNunitConsolePth, err := pathutil.AbsPath(nunitConsolePth) 53 if err != nil { 54 return nil, fmt.Errorf("Failed to expand path (%s), error: %s", nunitConsolePth, err) 55 } 56 57 return &Model{nunitConsolePth: absNunitConsolePth}, nil 58 } 59 60 // SetProjectPth ... 61 func (nunitConsole *Model) SetProjectPth(projectPth string) *Model { 62 nunitConsole.projectPth = projectPth 63 return nunitConsole 64 } 65 66 // SetConfig ... 67 func (nunitConsole *Model) SetConfig(config string) *Model { 68 nunitConsole.config = config 69 return nunitConsole 70 } 71 72 // SetDLLPth ... 73 func (nunitConsole *Model) SetDLLPth(dllPth string) *Model { 74 nunitConsole.dllPth = dllPth 75 return nunitConsole 76 } 77 78 // SetTestToRun ... 79 func (nunitConsole *Model) SetTestToRun(test string) *Model { 80 nunitConsole.test = test 81 return nunitConsole 82 } 83 84 // SetResultLogPth ... 85 func (nunitConsole *Model) SetResultLogPth(resultLogPth string) *Model { 86 nunitConsole.resultLogPth = resultLogPth 87 return nunitConsole 88 } 89 90 // SetCustomOptions ... 91 func (nunitConsole *Model) SetCustomOptions(options ...string) { 92 nunitConsole.customOptions = options 93 } 94 95 func (nunitConsole Model) commandSlice() []string { 96 cmdSlice := []string{constants.MonoPath} 97 cmdSlice = append(cmdSlice, nunitConsole.nunitConsolePth) 98 99 if nunitConsole.projectPth != "" { 100 cmdSlice = append(cmdSlice, nunitConsole.projectPth) 101 } 102 if nunitConsole.config != "" { 103 cmdSlice = append(cmdSlice, fmt.Sprintf("/config:%s", nunitConsole.config)) 104 } 105 106 if nunitConsole.dllPth != "" { 107 cmdSlice = append(cmdSlice, nunitConsole.dllPth) 108 } 109 if nunitConsole.test != "" { 110 cmdSlice = append(cmdSlice, "--test", nunitConsole.test) 111 } 112 113 if nunitConsole.resultLogPth != "" { 114 cmdSlice = append(cmdSlice, "--result", nunitConsole.resultLogPth) 115 } 116 117 cmdSlice = append(cmdSlice, nunitConsole.customOptions...) 118 return cmdSlice 119 } 120 121 // String ... 122 func (nunitConsole Model) String() string { 123 cmdSlice := nunitConsole.commandSlice() 124 return command.PrintableCommandArgs(true, cmdSlice) 125 } 126 127 // Run ... 128 func (nunitConsole Model) Run(outWriter, errWriter io.Writer) error { 129 if outWriter == nil { 130 outWriter = os.Stdout 131 } 132 if errWriter == nil { 133 errWriter = os.Stderr 134 } 135 136 cmdSlice := nunitConsole.commandSlice() 137 138 command, err := command.NewFromSlice(cmdSlice) 139 if err != nil { 140 return err 141 } 142 143 command.SetStdout(outWriter) 144 command.SetStderr(errWriter) 145 146 return command.Run() 147 }