@@ -150,3 +150,304 @@ func TestCompileWithDefaultProfile(t *testing.T) {
150150 jsonOut .Query (".builder_result.build_properties" ).MustContain (`[ "build.fqbn=arduino:avr:nano" ]` )
151151 }
152152}
153+
154+ func TestInitProfile (t * testing.T ) {
155+ env , cli := integrationtest .CreateArduinoCLIWithEnvironment (t )
156+ defer env .CleanUp ()
157+
158+ // Init the environment explicitly
159+ _ , _ , err := cli .Run ("core" , "update-index" )
160+ require .NoError (t , err )
161+
162+ _ , _ , err = cli .Run ("sketch" , "new" , cli .SketchbookDir ().Join ("Simple" ).String ())
163+ require .NoError (t , err )
164+
165+ _ , _ , err = cli .Run ("core" , "install" , "arduino:avr" )
166+ require .NoError (t , err )
167+
168+ integrationtest.CLISubtests {
169+ {"NoProfile" , initNoProfile },
170+ {"ProfileCorrectFQBN" , initWithCorrectFqbn },
171+ {"ProfileWrongFQBN" , initWithWrongFqbn },
172+ {"ProfileMissingFQBN" , initMissingFqbn },
173+ {"ExistingProfile" , initExistingProfile },
174+ {"SetDefaultProfile" , initSetDefaultProfile },
175+ }.Run (t , env , cli )
176+ }
177+
178+ func initNoProfile (t * testing.T , env * integrationtest.Environment , cli * integrationtest.ArduinoCLI ) {
179+ projectFile := cli .SketchbookDir ().Join ("Simple" , "sketch.yaml" )
180+ // Create an empty project file
181+ stdout , _ , err := cli .Run ("profile" , "init" , cli .SketchbookDir ().Join ("Simple" ).String ())
182+ require .NoError (t , err )
183+ require .Contains (t , string (stdout ), "Project file created in: " + projectFile .String ())
184+ require .FileExists (t , projectFile .String ())
185+ fileContent , err := projectFile .ReadFile ()
186+ require .NoError (t , err )
187+ require .Equal (t , "profiles: {}\n " , string (fileContent ))
188+ }
189+
190+ func initWithCorrectFqbn (t * testing.T , env * integrationtest.Environment , cli * integrationtest.ArduinoCLI ) {
191+ projectFile := cli .SketchbookDir ().Join ("Simple" , "sketch.yaml" )
192+ // Add a profile with a correct FQBN
193+ _ , _ , err := cli .Run ("profile" , "init" , cli .SketchbookDir ().Join ("Simple" ).String (), "-m" , "Uno" , "-b" , "arduino:avr:uno" )
194+ require .NoError (t , err )
195+ require .FileExists (t , projectFile .String ())
196+ fileContent , err := projectFile .ReadFile ()
197+ require .NoError (t , err )
198+ require .Equal (t , "profiles:\n Uno:\n fqbn: arduino:avr:uno\n platforms:\n - platform: arduino:avr (1.8.6)\n \n default_profile: Uno\n " , string (fileContent ))
199+ }
200+
201+ func initWithWrongFqbn (t * testing.T , env * integrationtest.Environment , cli * integrationtest.ArduinoCLI ) {
202+ // Adding a profile with an incorrect FQBN should return an error
203+ _ , stderr , err := cli .Run ("profile" , "init" , cli .SketchbookDir ().Join ("Simple" ).String (), "-m" , "wrong_fqbn" , "-b" , "foo:bar" )
204+ require .Error (t , err )
205+ require .Contains (t , string (stderr ), "Invalid FQBN" )
206+ }
207+
208+ func initMissingFqbn (t * testing.T , env * integrationtest.Environment , cli * integrationtest.ArduinoCLI ) {
209+ // Add a profile with no FQBN should return an error
210+ _ , stderr , err := cli .Run ("profile" , "init" , cli .SketchbookDir ().Join ("Simple" ).String (), "-m" , "Uno" )
211+ require .Error (t , err )
212+ require .Contains (t , string (stderr ), "Missing FQBN (Fully Qualified Board Name)" )
213+ }
214+
215+ func initExistingProfile (t * testing.T , env * integrationtest.Environment , cli * integrationtest.ArduinoCLI ) {
216+ // Adding a profile with a name that already exists should return an error
217+ _ , stderr , err := cli .Run ("profile" , "init" , cli .SketchbookDir ().Join ("Simple" ).String (), "-m" , "Uno" , "-b" , "arduino:avr:uno" )
218+ require .Error (t , err )
219+ require .Contains (t , string (stderr ), "Profile 'Uno' already exists" )
220+ }
221+
222+ func initSetDefaultProfile (t * testing.T , env * integrationtest.Environment , cli * integrationtest.ArduinoCLI ) {
223+ // Adding a profile with a name that already exists should return an error
224+ _ , _ , err := cli .Run ("profile" , "init" , cli .SketchbookDir ().Join ("Simple" ).String (), "-m" , "new_profile" , "-b" , "arduino:avr:uno" , "--default" )
225+ require .NoError (t , err )
226+ fileContent , err := cli .SketchbookDir ().Join ("Simple" , "sketch.yaml" ).ReadFileAsLines ()
227+ require .NoError (t , err )
228+ require .Contains (t , fileContent , "default_profile: new_profile" )
229+ }
230+
231+ func TestInitProfileMissingSketchFile (t * testing.T ) {
232+ env , cli := integrationtest .CreateArduinoCLIWithEnvironment (t )
233+ defer env .CleanUp ()
234+
235+ // Init the environment explicitly
236+ _ , _ , err := cli .Run ("core" , "update-index" )
237+ require .NoError (t , err )
238+
239+ _ , stderr , err := cli .Run ("profile" , "init" , cli .SketchbookDir ().Join ("Simple" ).String ())
240+ require .Error (t , err )
241+ require .Contains (t , string (stderr ), "no such file or directory" )
242+
243+ err = cli .SketchbookDir ().Join ("Simple" ).MkdirAll ()
244+ require .NoError (t , err )
245+ _ , stderr , err = cli .Run ("profile" , "init" , cli .SketchbookDir ().Join ("Simple" ).String ())
246+ require .Error (t , err )
247+ require .Contains (t , string (stderr ), "main file missing from sketch" )
248+ }
249+
250+ func TestInitProfilePlatformNotInstalled (t * testing.T ) {
251+ env , cli := integrationtest .CreateArduinoCLIWithEnvironment (t )
252+ defer env .CleanUp ()
253+
254+ // Init the environment explicitly
255+ _ , _ , err := cli .Run ("core" , "update-index" )
256+ require .NoError (t , err )
257+
258+ _ , _ , err = cli .Run ("sketch" , "new" , cli .SketchbookDir ().Join ("Simple" ).String ())
259+ require .NoError (t , err )
260+
261+ // Adding a profile with a name that already exists should return an error
262+ _ , stderr , err := cli .Run ("profile" , "init" , cli .SketchbookDir ().Join ("Simple" ).String (), "-m" , "Uno" , "-b" , "arduino:avr:uno" )
263+ require .Error (t , err )
264+ require .Contains (t , string (stderr ), "platform not installed" )
265+ }
266+
267+ func TestProfileLib (t * testing.T ) {
268+ env , cli := integrationtest .CreateArduinoCLIWithEnvironment (t )
269+ defer env .CleanUp ()
270+
271+ // Init the environment explicitly
272+ _ , _ , err := cli .Run ("core" , "update-index" )
273+ require .NoError (t , err )
274+
275+ _ , _ , err = cli .Run ("core" , "install" , "arduino:avr" )
276+ require .NoError (t , err )
277+
278+ integrationtest.CLISubtests {
279+ {"AddLibToDefaultProfile" , addLibToDefaultProfile },
280+ {"ChangeLibVersionDefaultProfile" , changeLibVersionDefaultProfile },
281+ {"RemoveLibFromDefaultProfile" , removeLibFromDefaultProfile },
282+ {"AddInexistentLibToDefaultProfile" , addInexistentLibToDefaultProfile },
283+ {"RemoveLibNotInDefaultProfile" , removeLibNotInDefaultProfile },
284+ }.Run (t , env , cli )
285+ }
286+
287+ func addLibToDefaultProfile (t * testing.T , env * integrationtest.Environment , cli * integrationtest.ArduinoCLI ) {
288+ sk := cli .SketchbookDir ().Join ("addLibToDefaultProfile" )
289+ _ , _ , err := cli .Run ("sketch" , "new" , sk .String ())
290+ require .NoError (t , err )
291+ _ , _ , err = cli .Run ("profile" , "init" , sk .String (), "-m" , "Uno" , "-b" , "arduino:avr:uno" )
292+ require .NoError (t , err )
293+
294+ out ,
_ ,
err := cli .
Run (
"profile" ,
"lib" ,
"add" ,
"[email protected] " ,
"--dest-dir" ,
sk .
String (),
"--json" )
295+ require .NoError (t , err )
296+ requirejson .Parse (t , out ).Query (".added_libraries" ).MustContain (`[{"name":"Modulino", "version":"0.5.0"}]` )
297+
298+ fileContent , err := sk .Join ("sketch.yaml" ).ReadFile ()
299+ require .NoError (t , err )
300+ require .Contains (t , string (fileContent ), " - Modulino (0.5.0)\n " )
301+ require .Contains (t , string (fileContent ), " - Arduino_LSM6DSOX (" ) // dependency added as well
302+ }
303+
304+ func changeLibVersionDefaultProfile (t * testing.T , env * integrationtest.Environment , cli * integrationtest.ArduinoCLI ) {
305+ sk := cli .SketchbookDir ().Join ("changeLibVersionDefaultProfile" )
306+ _ , _ , err := cli .Run ("sketch" , "new" , sk .String ())
307+ require .NoError (t , err )
308+ _ , _ , err = cli .Run ("profile" , "init" , sk .String (), "-m" , "Uno" , "-b" , "arduino:avr:uno" )
309+ require .NoError (t , err )
310+
311+ {
312+ out ,
_ ,
err := cli .
Run (
"profile" ,
"lib" ,
"add" ,
"[email protected] " ,
"--dest-dir" ,
sk .
String (),
"--json" )
313+ require .NoError (t , err )
314+ outjson := requirejson .Parse (t , out )
315+ outjson .Query (".added_libraries" ).MustContain (`[{"name":"Modulino", "version":"0.5.0"},{"name":"Arduino_LSM6DSOX"}]` )
316+ outjson .Query (".skipped_libraries" ).LengthMustEqualTo (0 )
317+
318+ fileContent , err := sk .Join ("sketch.yaml" ).ReadFile ()
319+ require .NoError (t , err )
320+ require .Contains (t , string (fileContent ), " - Modulino (0.5.0)\n " )
321+ require .Contains (t , string (fileContent ), " - Arduino_LSM6DSOX (" ) // dependency added as well
322+ }
323+
324+ {
325+ out ,
_ ,
err := cli .
Run (
"profile" ,
"lib" ,
"add" ,
"[email protected] " ,
"--dest-dir" ,
sk .
String (),
"--json" )
326+ require .NoError (t , err )
327+ outjson := requirejson .Parse (t , out )
328+ outjson .Query (".added_libraries" ).MustContain (`[{"name":"Modulino", "version":"0.4.0"}]` )
329+ outjson .Query (".skipped_libraries" ).MustContain (`[{"name":"Arduino_LSM6DSOX"}]` )
330+
331+ fileContent , err := sk .Join ("sketch.yaml" ).ReadFile ()
332+ require .NoError (t , err )
333+ require .Contains (t , string (fileContent ), " - Modulino (0.4.0)\n " )
334+ }
335+ }
336+
337+ func removeLibFromDefaultProfile (t * testing.T , env * integrationtest.Environment , cli * integrationtest.ArduinoCLI ) {
338+ sk := cli .SketchbookDir ().Join ("removeLibFromDefaultProfile" )
339+ _ , _ , err := cli .Run ("sketch" , "new" , sk .String ())
340+ require .NoError (t , err )
341+ _ , _ , err = cli .Run ("profile" , "init" , sk .String (), "-m" , "Uno" , "-b" , "arduino:avr:uno" )
342+ require .NoError (t , err )
343+
344+ _ ,
_ ,
err = cli .
Run (
"profile" ,
"lib" ,
"add" ,
"[email protected] " ,
"--dest-dir" ,
sk .
String (),
"--json" )
345+ require .NoError (t , err )
346+
347+ _ , _ , err = cli .Run ("profile" , "lib" , "remove" , "Modulino" , "--dest-dir" , sk .String ())
348+ require .NoError (t , err )
349+ fileContent , err := sk .Join ("sketch.yaml" ).ReadFile ()
350+ require .NoError (t , err )
351+ require .NotContains (t , string (fileContent ), " - Modulino" )
352+ }
353+
354+ func addInexistentLibToDefaultProfile (t * testing.T , env * integrationtest.Environment , cli * integrationtest.ArduinoCLI ) {
355+ sk := cli .SketchbookDir ().Join ("addInexistentLibToDefaultProfile" )
356+ _ , _ , err := cli .Run ("sketch" , "new" , sk .String ())
357+ require .NoError (t , err )
358+ _ , _ , err = cli .Run ("profile" , "init" , sk .String (), "-m" , "Uno" , "-b" , "arduino:avr:uno" )
359+ require .NoError (t , err )
360+
361+ _ , stderr , err := cli .Run ("profile" , "lib" , "add" , "foobar" , "--dest-dir" , sk .String ())
362+ require .Error (t , err )
363+ require .Equal (t , "Error adding foobar to the profile : Library 'foobar@latest' not found\n " , string (stderr ))
364+ }
365+
366+ func removeLibNotInDefaultProfile (t * testing.T , env * integrationtest.Environment , cli * integrationtest.ArduinoCLI ) {
367+ sk := cli .SketchbookDir ().Join ("removeLibNotInDefaultProfile" )
368+ _ , _ , err := cli .Run ("sketch" , "new" , sk .String ())
369+ require .NoError (t , err )
370+ _ , _ , err = cli .Run ("profile" , "init" , sk .String (), "-m" , "Uno" , "-b" , "arduino:avr:uno" )
371+ require .NoError (t , err )
372+
373+ _ , stderr , err := cli .Run ("profile" , "lib" , "remove" , "Arduino_JSON" , "--dest-dir" , sk .String ())
374+ require .Error (t , err )
375+ require .Equal (t , "Error removing library Arduino_JSON from the profile: Library 'Arduino_JSON' not found\n " , string (stderr ))
376+ }
377+
378+ func TestProfileLibSpecificProfile (t * testing.T ) {
379+ env , cli := integrationtest .CreateArduinoCLIWithEnvironment (t )
380+ defer env .CleanUp ()
381+
382+ // Init the environment explicitly
383+ _ , _ , err := cli .Run ("core" , "update-index" )
384+ require .NoError (t , err )
385+
386+ sk := cli .SketchbookDir ().Join ("Simple" )
387+ _ , _ , err = cli .Run ("sketch" , "new" , sk .String ())
388+ require .NoError (t , err )
389+
390+ _ , _ , err = cli .Run ("core" , "install" , "arduino:avr" )
391+ require .NoError (t , err )
392+
393+ _ , _ , err = cli .Run ("profile" , "init" , sk .String (), "-m" , "Uno" , "-b" , "arduino:avr:uno" )
394+ require .NoError (t , err )
395+
396+ // Add a second profile
397+ _ , _ , err = cli .Run ("profile" , "init" , sk .String (), "-m" , "my_profile" , "-b" , "arduino:avr:uno" )
398+ require .NoError (t , err )
399+
400+ // Add library to a specific profile
401+ _ ,
_ ,
err = cli .
Run (
"profile" ,
"lib" ,
"add" ,
"[email protected] " ,
"-m" ,
"my_profile" ,
"--dest-dir" ,
sk .
String (),
"--no-deps" )
402+ require .NoError (t , err )
403+ fileContent , err := sk .Join ("sketch.yaml" ).ReadFile ()
404+ require .NoError (t , err )
405+ require .Contains (t , string (fileContent ), " my_profile:\n fqbn: arduino:avr:uno\n platforms:\n - platform: arduino:avr (1.8.6)\n libraries:\n - Modulino (0.5.0)\n " )
406+
407+ // Remove library from a specific profile
408+ _ , _ , err = cli .Run ("profile" , "lib" , "remove" , "Modulino" , "-m" , "my_profile" , "--dest-dir" , sk .String ())
409+ require .NoError (t , err )
410+ fileContent , err = sk .Join ("sketch.yaml" ).ReadFile ()
411+ require .NoError (t , err )
412+ require .NotContains (t , string (fileContent ), "- Modulino (0.5.0)" )
413+ }
414+
415+ func TestProfileSetDefault (t * testing.T ) {
416+ env , cli := integrationtest .CreateArduinoCLIWithEnvironment (t )
417+ defer env .CleanUp ()
418+
419+ // Init the environment explicitly
420+ _ , _ , err := cli .Run ("core" , "update-index" )
421+ require .NoError (t , err )
422+
423+ sk := cli .SketchbookDir ().Join ("Simple" )
424+ _ , _ , err = cli .Run ("sketch" , "new" , sk .String ())
425+ require .NoError (t , err )
426+
427+ _ , _ , err = cli .Run ("core" , "install" , "arduino:avr" )
428+ require .NoError (t , err )
429+
430+ _ , _ , err = cli .Run ("profile" , "init" , sk .String (), "-m" , "Uno" , "-b" , "arduino:avr:uno" )
431+ require .NoError (t , err )
432+
433+ // Add a second profile
434+ _ , _ , err = cli .Run ("profile" , "init" , sk .String (), "-m" , "my_profile" , "-b" , "arduino:avr:uno" )
435+ require .NoError (t , err )
436+ fileContent , err := sk .Join ("sketch.yaml" ).ReadFileAsLines ()
437+ require .NoError (t , err )
438+ require .Contains (t , fileContent , "default_profile: Uno" )
439+ require .NotContains (t , fileContent , "default_profile: my_profile" )
440+
441+ // Change default profile
442+ _ , _ , err = cli .Run ("profile" , "set-default" , "my_profile" , "--dest-dir" , sk .String ())
443+ require .NoError (t , err )
444+ fileContent , err = sk .Join ("sketch.yaml" ).ReadFileAsLines ()
445+ require .NoError (t , err )
446+ require .NotContains (t , fileContent , "default_profile: Uno" )
447+ require .Contains (t , fileContent , "default_profile: my_profile" )
448+
449+ // Changing to an inexistent profile returns an error
450+ _ , stderr , err := cli .Run ("profile" , "set-default" , "inexistent_profile" , "--dest-dir" , sk .String ())
451+ require .Error (t , err )
452+ require .Equal (t , "Cannot set inexistent_profile as default profile: Profile 'inexistent_profile' not found\n " , string (stderr ))
453+ }
0 commit comments