github.com/robgonnella/ardi/v2@v2.4.5-0.20230102052001-11a49de978c3/testutil/helpers.go (about) 1 package testutil 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 "path" 8 "path/filepath" 9 10 rpccommands "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" 11 cli "github.com/robgonnella/ardi/v2/cli-wrapper" 12 log "github.com/sirupsen/logrus" 13 ) 14 15 var here string 16 17 func init() { 18 here, _ = filepath.Abs(".") 19 log.SetOutput(ioutil.Discard) 20 } 21 22 // CleanCoreDir removes test data from core directory 23 func CleanCoreDir() { 24 dataDir := path.Join(here, "../core/.ardi") 25 jsonFile := path.Join(here, "../core/ardi.json") 26 os.RemoveAll(dataDir) 27 os.Remove(jsonFile) 28 } 29 30 // CleanCommandsDir removes project data from commands directory 31 func CleanCommandsDir() { 32 projectDataDir := path.Join(here, "../commands/.ardi") 33 projectJSONFile := path.Join(here, "../commands/ardi.json") 34 os.RemoveAll(projectDataDir) 35 os.Remove(projectJSONFile) 36 } 37 38 // CleanPixieDir removes project data from test pixie project directory 39 func CleanPixieDir() { 40 projectDataDir := path.Join(here, "../test_projects/pixie/.ardi") 41 projectJSONFile := path.Join(here, "../test_projects/pixie/ardi.json") 42 os.RemoveAll(projectDataDir) 43 os.Remove(projectJSONFile) 44 } 45 46 // CleanBuilds removes compiled test project builds 47 func CleanBuilds() { 48 os.RemoveAll(path.Join(BlinkProjectDir(), "build")) 49 os.RemoveAll(path.Join(BlinkCopyProjectDir(), "build")) 50 os.RemoveAll(path.Join(Blink14400ProjectDir(), "build")) 51 os.RemoveAll(path.Join(PixieProjectDir(), "build")) 52 } 53 54 // CleanAll removes all test data 55 func CleanAll() { 56 CleanCoreDir() 57 CleanCommandsDir() 58 CleanBuilds() 59 } 60 61 // ArduinoMegaFQBN returns appropriate fqbn for arduino mega 2560 62 func ArduinoMegaFQBN() string { 63 return "arduino:avr:mega" 64 } 65 66 // Esp8266Platform returns appropriate platform for esp8266 67 func Esp8266Platform() string { 68 return "esp8266:esp8266" 69 } 70 71 // Esp8266WifiduinoFQBN returns appropriate fqbn for esp8266 board 72 func Esp8266WifiduinoFQBN() string { 73 return "esp8266:esp8266:wifiduino" 74 } 75 76 // Esp8266BoardURL returns appropriate board url for esp8266 board 77 func Esp8266BoardURL() string { 78 return "https://arduino.esp8266.com/stable/package_esp8266com_index.json" 79 } 80 81 // GenerateCmdBoard returns a single arduino-cli command Board 82 func GenerateCmdBoard(name, fqbn string) *rpccommands.Board { 83 if fqbn == "" { 84 fqbn = fmt.Sprintf("%s-fqbn", name) 85 } 86 return &rpccommands.Board{Name: name, Fqbn: fqbn} 87 } 88 89 // GenerateCmdBoards generate a list of arduino-cli command boards 90 func GenerateCmdBoards(n int) []*rpccommands.Board { 91 var boards []*rpccommands.Board 92 for i := 0; i < n; i++ { 93 name := fmt.Sprintf("test-board-%02d", i) 94 b := GenerateCmdBoard(name, "") 95 boards = append(boards, b) 96 } 97 return boards 98 } 99 100 // GenerateCmdPlatform generates a single named arduino-cli command platform 101 func GenerateCmdPlatform(name string, boards []*rpccommands.Board) *rpccommands.Platform { 102 return &rpccommands.Platform{ 103 Id: name, 104 Boards: boards, 105 } 106 } 107 108 // GenerateRPCBoard returns a single ardi rpc Board 109 func GenerateRPCBoard(name, fqbn string) *cli.BoardWithPort { 110 if fqbn == "" { 111 fqbn = fmt.Sprintf("%s-fqbn", name) 112 } 113 return &cli.BoardWithPort{ 114 Name: name, 115 FQBN: fqbn, 116 Port: "/dev/null", 117 } 118 } 119 120 // GenerateRPCBoards generate a list of ardi rpc boards 121 func GenerateRPCBoards(n int) []*cli.BoardWithPort { 122 var boards []*cli.BoardWithPort 123 for i := 0; i < n; i++ { 124 name := fmt.Sprintf("test-board-%02d", i) 125 b := GenerateRPCBoard(name, "") 126 boards = append(boards, b) 127 } 128 return boards 129 } 130 131 // BlinkProjectDir returns path to blink project directory 132 func BlinkProjectDir() string { 133 return path.Join(here, "../test_projects/blink") 134 } 135 136 // BlinkCopyProjectDir returns path to blink project directory 137 func BlinkCopyProjectDir() string { 138 return path.Join(here, "../test_projects/blink2") 139 } 140 141 // Blink14400ProjectDir returns path to blink14400 project directory 142 func Blink14400ProjectDir() string { 143 return path.Join(here, "../test_projects/blink14400") 144 } 145 146 // PixieProjectDir returns path to blink project directory 147 func PixieProjectDir() string { 148 return path.Join(here, "../test_projects/pixie") 149 }