Skip to content

Commit 369eb72

Browse files
committed
fixing tests: fixes arduino#58 definitely and adds tests
1 parent 8bef373 commit 369eb72

File tree

5 files changed

+69
-39
lines changed

5 files changed

+69
-39
lines changed

cmd/arduino.go

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,27 @@ func init() {
102102

103103
// InitFlags reinitialize flags (useful for testing too)
104104
func InitFlags() {
105+
GlobalFlags = globalFlags{}
106+
rootCmdFlags = rootFlags{}
107+
arduinoLibFlags = libFlags{}
108+
arduinoCoreFlags = coreFlags{}
109+
105110
ArduinoCmd.ResetFlags()
106-
arduinoCoreCmd.ResetFlags()
111+
arduinoVersionCmd.ResetFlags()
112+
107113
arduinoLibCmd.ResetFlags()
114+
arduinoLibInstallCmd.ResetFlags()
115+
arduinoLibDownloadCmd.ResetFlags()
116+
arduinoLibListCmd.ResetFlags()
117+
arduinoLibSearchCmd.ResetFlags()
118+
arduinoLibUninstallCmd.ResetFlags()
119+
arduinoLibVersionCmd.ResetFlags()
120+
121+
arduinoCoreCmd.ResetFlags()
122+
arduinoCoreDownloadCmd.ResetFlags()
123+
arduinoCoreInstallCmd.ResetFlags()
124+
arduinoCoreListCmd.ResetFlags()
125+
arduinoCoreVersionCmd.ResetFlags()
108126

109127
ArduinoCmd.PersistentFlags().CountVarP(&GlobalFlags.Verbose, "verbose", "v", "enables verbose output (use more times for a higher level)")
110128
ArduinoCmd.PersistentFlags().StringVar(&GlobalFlags.Format, "format", "invalid", "the output format, can be [text|json]")
@@ -123,21 +141,13 @@ func InitCommands() {
123141
arduinoLibCmd.ResetCommands()
124142
arduinoCoreCmd.ResetCommands()
125143

126-
ArduinoCmd.AddCommand(arduinoVersionCmd)
127-
ArduinoCmd.AddCommand(arduinoLibCmd)
128-
ArduinoCmd.AddCommand(arduinoCoreCmd)
129-
130-
arduinoLibCmd.AddCommand(arduinoLibInstallCmd)
131-
arduinoLibCmd.AddCommand(arduinoLibUninstallCmd)
132-
arduinoLibCmd.AddCommand(arduinoLibSearchCmd)
133-
arduinoLibCmd.AddCommand(arduinoLibDownloadCmd)
134-
arduinoLibCmd.AddCommand(arduinoLibVersionCmd)
135-
arduinoLibCmd.AddCommand(arduinoLibListCmd)
136-
137-
arduinoCoreCmd.AddCommand(arduinoCoreListCmd)
138-
arduinoCoreCmd.AddCommand(arduinoCoreDownloadCmd)
139-
arduinoCoreCmd.AddCommand(arduinoCoreVersionCmd)
140-
arduinoCoreCmd.AddCommand(arduinoCoreInstallCmd)
144+
ArduinoCmd.AddCommand(arduinoVersionCmd, arduinoLibCmd, arduinoCoreCmd)
145+
146+
arduinoLibCmd.AddCommand(arduinoLibInstallCmd, arduinoLibUninstallCmd, arduinoLibSearchCmd,
147+
arduinoLibVersionCmd, arduinoLibListCmd, arduinoLibDownloadCmd)
148+
149+
arduinoCoreCmd.AddCommand(arduinoCoreListCmd, arduinoCoreDownloadCmd, arduinoCoreVersionCmd,
150+
arduinoCoreInstallCmd)
141151
}
142152

143153
func arduinoPreRun(cmd *cobra.Command, args []string) {

cmd/arduino_core.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,17 +167,18 @@ func executeCoreDownloadCommand(cmd *cobra.Command, args []string) error {
167167
}
168168

169169
IDTuples := cores.ParseArgs(args)
170+
170171
coresToDownload, toolsToDownload, failOutputs := status.Process(IDTuples)
171172
outputResults := output.CoreProcessResults{
172173
Cores: failOutputs,
174+
Tools: make([]output.ProcessResult, 0, 10),
173175
}
174-
175176
downloads := make([]releases.DownloadItem, len(toolsToDownload))
176177
for i := range toolsToDownload {
177178
downloads[i] = toolsToDownload[i].DownloadItem
178179
}
179-
releases.ParallelDownload(downloads, true, "Downloaded", GlobalFlags.Verbose, &outputResults.Tools, "tool")
180180

181+
releases.ParallelDownload(downloads, true, "Downloaded", GlobalFlags.Verbose, &outputResults.Tools, "tool")
181182
downloads = make([]releases.DownloadItem, len(coresToDownload))
182183
for i := range coresToDownload {
183184
downloads[i] = coresToDownload[i].DownloadItem

cmd/cmd_test.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,17 @@ func createTempRedirect(t *testing.T) *os.File {
5959
return tempFile
6060
}
6161

62-
func cleanTempRedirect(tempFile *os.File) {
62+
func cleanTempRedirect(t *testing.T, tempFile *os.File) {
6363
tempFile.Close()
64-
os.Remove(tempFile.Name())
64+
err := os.Remove(tempFile.Name())
65+
assert.NoError(t, err, "Removing temp file")
6566
os.Stdout = stdOut
6667
}
6768

6869
func executeWithArgs(t *testing.T, args ...string) {
70+
cmd.InitFlags()
71+
cmd.InitCommands()
6972
if args != nil {
70-
cmd.InitFlags()
71-
cmd.InitCommands()
7273
cmd.ArduinoCmd.SetArgs(args)
7374
}
7475
err := cmd.ArduinoCmd.Execute()
@@ -77,7 +78,7 @@ func executeWithArgs(t *testing.T, args ...string) {
7778

7879
func TestArduinoCmd(t *testing.T) {
7980
tempFile := createTempRedirect(t)
80-
defer cleanTempRedirect(tempFile)
81+
defer cleanTempRedirect(t, tempFile)
8182
want := []string{
8283
`{"error":"Invalid Call : should show Help, but it is available only in TEXT mode"}`,
8384
}
@@ -91,7 +92,7 @@ func TestArduinoCmd(t *testing.T) {
9192

9293
func TestLibSearch(t *testing.T) {
9394
tempFile := createTempRedirect(t)
94-
defer cleanTempRedirect(tempFile)
95+
defer cleanTempRedirect(t, tempFile)
9596
want := []string{
9697
`"YouMadeIt"`,
9798
`"YoutubeApi"`,
@@ -109,7 +110,7 @@ func TestLibSearch(t *testing.T) {
109110

110111
func TestLibDownload(t *testing.T) {
111112
tempFile := createTempRedirect(t)
112-
defer cleanTempRedirect(tempFile)
113+
defer cleanTempRedirect(t, tempFile)
113114

114115
// getting the paths to create the want path of the want object.
115116
stagingFolder, err := common.GetDownloadCacheFolder("libraries")
@@ -163,7 +164,7 @@ func TestLibDownload(t *testing.T) {
163164

164165
func TestCoreDownload(t *testing.T) {
165166
tempFile := createTempRedirect(t)
166-
defer cleanTempRedirect(tempFile)
167+
defer cleanTempRedirect(t, tempFile)
167168

168169
// getting the paths to create the want path of the want object.
169170
stagingFolder, err := common.GetDownloadCacheFolder("packages")
@@ -199,6 +200,9 @@ func TestCoreDownload(t *testing.T) {
199200
var have output.CoreProcessResults
200201
err = json.Unmarshal(d, &have)
201202
require.NoError(t, err, "Unmarshaling json output")
203+
t.Log("HAVE: \n", have)
204+
t.Log("D:\n", string(d))
205+
t.Log("WANT: \n", want)
202206

203207
// checking output
204208

cmd/flags.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,32 @@
3030
package cmd
3131

3232
// GlobalFlags represents flags available in all the program.
33-
var GlobalFlags struct {
33+
type globalFlags struct {
3434
Verbose int // More time verbose flag is written, the more the Verbose count increases. Represents verbosity level.
3535
Format string // The Output format (e.g. text, json).
3636
Home string // The Custom Home directory.
3737
}
3838

39-
// rootCmdFlags represent flags available to the root command.
40-
var rootCmdFlags struct {
39+
// GlobalFlags represents flags available in all the program.
40+
var GlobalFlags globalFlags
41+
42+
type rootFlags struct {
4143
GenerateDocs bool // if true, generates manpages and bash autocompletion.
4244
}
4345

44-
// arduinoLibFlags represents `arduino lib` flags.
45-
var arduinoLibFlags struct {
46+
// rootCmdFlags represent flags available to the root command.
47+
var rootCmdFlags rootFlags
48+
49+
type libFlags struct {
4650
updateIndex bool
4751
}
4852

49-
// arduinoCoreFlags represents `arduino core` flags.
50-
var arduinoCoreFlags struct {
53+
// arduinoLibFlags represents `arduino lib` flags.
54+
var arduinoLibFlags libFlags
55+
56+
type coreFlags struct {
5157
updateIndex bool
5258
}
59+
60+
// arduinoCoreFlags represents `arduino core` flags.
61+
var arduinoCoreFlags coreFlags

task/concurrency.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ package task
3131

3232
import (
3333
"fmt"
34+
"sync"
3435

3536
"github.com/bcmi-labs/arduino-cli/cmd/formatter"
3637
)
@@ -80,20 +81,22 @@ func ExecuteSequence(taskWrappers []Wrapper, ignoreOnFailure []bool, verbosity i
8081
// ExecuteParallelFromMap executes a set of taskwrappers in parallel, taking input from a map[string]Wrapper.
8182
func ExecuteParallelFromMap(taskMap map[string]Wrapper, verbosity int) map[string]Result {
8283
results := make(chan resultWithKey, len(taskMap))
83-
turn := make(chan bool)
84+
85+
var wg sync.WaitGroup
86+
wg.Add(len(taskMap))
8487

8588
for key, task := range taskMap {
8689
go func(key string, task Wrapper) {
87-
turn <- true
90+
defer wg.Done()
8891
results <- resultWithKey{
8992
Key: key,
9093
Result: func() Result {
9194
return task.Execute(verbosity)
9295
}(),
9396
}
94-
<-turn
9597
}(key, task)
9698
}
99+
wg.Wait()
97100
close(results)
98101
mapResult := make(map[string]Result, len(results))
99102
for result := range results {
@@ -106,16 +109,19 @@ func ExecuteParallelFromMap(taskMap map[string]Wrapper, verbosity int) map[strin
106109
// ExecuteParallel executes a set of Wrappers in parallel, handling concurrency for results.
107110
func ExecuteParallel(taskWrappers []Wrapper, verbosity int) []Result {
108111
results := make(chan Result, len(taskWrappers))
109-
turn := make(chan bool)
112+
113+
var wg sync.WaitGroup
114+
wg.Add(len(taskWrappers))
115+
110116
for _, task := range taskWrappers {
111117
go func(task Wrapper) {
112-
turn <- true
118+
defer wg.Done()
113119
results <- func() Result {
114120
return task.Execute(verbosity)
115121
}()
116-
<-turn
117122
}(task)
118123
}
124+
wg.Wait()
119125
close(results)
120126
array := make([]Result, len(results))
121127
for i := range array {

0 commit comments

Comments
 (0)