Skip to content

Commit 943f577

Browse files
committed
Fixed TestLibDownload test
1 parent 9b661d6 commit 943f577

File tree

2 files changed

+24
-51
lines changed

2 files changed

+24
-51
lines changed

commands/commands_test.go

Lines changed: 23 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -149,56 +149,29 @@ func TestLibSearch(t *testing.T) {
149149
require.Equal(t, "", string(output))
150150
}
151151

152-
func TestLibDownloadSuccessful(t *testing.T) {
153-
// getting the paths to create the want path of the want object.
154-
stagingFolder, err := configs.DownloadCacheFolder("libraries").Get()
155-
require.NoError(t, err, "Getting cache folder")
156-
157-
// desired output
158-
want := output.LibProcessResults{
159-
Libraries: map[string]output.ProcessResult{
160-
"invalidLibrary": {ItemName: "invalidLibrary", Error: "Library not found"},
161-
"YoutubeApi": {ItemName: "YoutubeApi", Status: "Downloaded", Path: stagingFolder + "/YoutubeApi-1.1.0.zip"},
162-
"YouMadeIt@invalidVersion": {ItemName: "YouMadeIt", Error: "Version Not Found"},
163-
},
164-
}
165-
166-
// lib download YoutubeApi invalidLibrary YouMadeIt@invalidVersion --format json
167-
librariesArgs := []string{}
168-
for libraryKey, _ := range want.Libraries {
169-
librariesArgs = append(librariesArgs, libraryKey)
170-
}
171-
172-
exitCode, d := executeWithArgs(t, append(append([]string{"lib", "download"}, librariesArgs...), "--format", "json")...)
173-
require.Equal(t, 0, exitCode, "exit code")
174-
175-
var have output.LibProcessResults
176-
err = json.Unmarshal(d, &have)
177-
require.NoError(t, err, "Unmarshaling json output")
178-
require.NotNil(t, have.Libraries, "Unmarshaling json output: have '%s'", d)
179-
180-
// checking output
181-
182-
assert.Equal(t, len(want.Libraries), len(have.Libraries), "Number of libraries in the output")
183-
184-
pop := func(lib *output.ProcessResult) bool {
185-
for idx, h := range have.Libraries {
186-
if lib.String() == h.String() {
187-
// XXX: Consider changing the Libraries field to an array of pointers
188-
//have.Libraries[idx] = nil
189-
have.Libraries[idx] = output.ProcessResult{ItemName: ""} // Mark library as matched
190-
return true
191-
}
192-
}
193-
return false
194-
}
195-
196-
for _, w := range want.Libraries {
197-
assert.True(t, pop(&w), "Expected library '%s' is missing from output", w)
198-
}
199-
for _, h := range have.Libraries {
200-
assert.Empty(t, h.String(), "Unexpected library '%s' is inside output", h)
201-
}
152+
func TestLibDownload(t *testing.T) {
153+
// Set staging folder to a temporary folder
154+
tmp, err := ioutil.TempDir(os.TempDir(), "test")
155+
require.NoError(t, err, "making temporary staging dir")
156+
defer os.RemoveAll(tmp)
157+
configs.ArduinoDataFolder.SetPath(tmp)
158+
159+
exitCode, d := executeWithArgs(t, "lib", "download", "inexistentLibrary", "--format", "json")
160+
require.NotZero(t, exitCode, "exit code")
161+
require.Contains(t, string(d), "library not found: inexistentLibrary")
162+
163+
exitCode, d = executeWithArgs(t, "lib", "download", "inexistentLibrary")
164+
require.NotZero(t, exitCode, "exit code")
165+
require.Contains(t, string(d), "library not found: inexistentLibrary")
166+
167+
exitCode, d = executeWithArgs(t, "lib", "download", "Audio")
168+
require.Zero(t, exitCode, "exit code")
169+
require.Contains(t, string(d), "Audio@")
170+
require.Contains(t, string(d), "downloaded")
171+
172+
exitCode, d = executeWithArgs(t, "lib", "download", "[email protected]")
173+
require.NotZero(t, exitCode, "exit code")
174+
require.Contains(t, string(d), "version not found")
202175
}
203176

204177
func TestCoreDownloadSuccessful(t *testing.T) {

commands/lib/download.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func runDownloadCommand(cmd *cobra.Command, args []string) {
7272
func downloadLibraries(status *libraries.StatusContext, pairs []libraries.NameVersionPair) {
7373
libsToDownload, err := status.Process(pairs)
7474
if err != nil {
75-
formatter.PrintError(err, "Error parsing libraries")
75+
formatter.PrintError(err, "Error parsing libraries arguments")
7676
os.Exit(commands.ErrBadCall)
7777
}
7878

0 commit comments

Comments
 (0)