@@ -19,6 +19,7 @@ import (
1919 "testing"
2020
2121 "github.com/arduino/arduino-cli/internal/integrationtest"
22+ "github.com/arduino/go-paths-helper"
2223 "github.com/stretchr/testify/require"
2324 "go.bug.st/testifyjson/requirejson"
2425)
@@ -150,3 +151,243 @@ func TestCompileWithDefaultProfile(t *testing.T) {
150151 jsonOut .Query (".builder_result.build_properties" ).MustContain (`[ "build.fqbn=arduino:avr:nano" ]` )
151152 }
152153}
154+
155+ func createTempSketch (t * testing.T , cli * integrationtest.ArduinoCLI , sketchName string ) * paths.Path {
156+ sketchDir := cli .SketchbookDir ().Join (sketchName )
157+ t .Cleanup (func () { require .NoError (t , sketchDir .RemoveAll ()) })
158+ _ , _ , err := cli .Run ("sketch" , "new" , sketchDir .String ())
159+ require .NoError (t , err )
160+ return sketchDir
161+ }
162+
163+ func TestProfileCreate (t * testing.T ) {
164+ env , cli := integrationtest .CreateArduinoCLIWithEnvironment (t )
165+ defer env .CleanUp ()
166+
167+ // Init the environment explicitly
168+ _ , _ , err := cli .Run ("core" , "update-index" )
169+ require .NoError (t , err )
170+ _ ,
_ ,
err = cli .
Run (
"core" ,
"install" ,
"arduino:[email protected] " )
171+ require .NoError (t , err )
172+
173+ t .Run ("WithInvalidSketchDir" , func (t * testing.T ) {
174+ invalidSketchDir := cli .SketchbookDir ().Join ("tempSketch" )
175+
176+ _ , stderr , err := cli .Run ("profile" , "create" , invalidSketchDir .String (), "-m" , "test" , "-b" , "arduino:avr:uno" )
177+ require .Error (t , err )
178+ require .Contains (t , string (stderr ), "no such file or directory" )
179+
180+ require .NoError (t , invalidSketchDir .MkdirAll ())
181+ t .Cleanup (func () { require .NoError (t , invalidSketchDir .RemoveAll ()) })
182+
183+ _ , stderr , err = cli .Run ("profile" , "create" , invalidSketchDir .String (), "-m" , "test" , "-b" , "arduino:avr:uno" )
184+ require .Error (t , err )
185+ require .Contains (t , string (stderr ), "main file missing from sketch" )
186+ })
187+
188+ t .Run ("WithNotInstalledPlatform" , func (t * testing.T ) {
189+ sketchDir := createTempSketch (t , cli , "TestSketch" )
190+ _ , stderr , err := cli .Run ("profile" , "create" , sketchDir .String (), "-m" , "uno" , "-b" , "arduino:samd:zero" )
191+ require .Error (t , err )
192+ require .Contains (t , string (stderr ), "platform not installed" )
193+ })
194+
195+ t .Run ("WithoutSketchYAML" , func (t * testing.T ) {
196+ sketchDir := createTempSketch (t , cli , "TestSketch" )
197+ projectFile := sketchDir .Join ("sketch.yaml" )
198+
199+ stdout , _ , err := cli .Run ("profile" , "create" , sketchDir .String (), "-m" , "test" , "-b" , "arduino:avr:uno" )
200+ require .NoError (t , err )
201+ require .Contains (t , string (stdout ), "Project file created in: " + projectFile .String ())
202+ require .FileExists (t , projectFile .String ())
203+ fileContent , err := projectFile .ReadFile ()
204+ require .NoError (t , err )
205+ require .Contains (t , string (fileContent ), "profiles:\n test:\n " )
206+
207+ t .Run ("AddNewProfile" , func (t * testing.T ) {
208+ // Add a new profile
209+ _ , _ , err := cli .Run ("profile" , "create" , sketchDir .String (), "-m" , "uno" , "-b" , "arduino:avr:uno" )
210+ require .NoError (t , err )
211+ fileContent , err := projectFile .ReadFile ()
212+ require .NoError (t , err )
213+ require .Contains (t , string (fileContent ), " uno:\n fqbn: arduino:avr:uno\n platforms:\n - platform: arduino:avr (1.8.6)\n " )
214+ require .NotContains (t , string (fileContent ), "default_profile: uno" )
215+ })
216+
217+ t .Run ("AddAndSetDefaultProfile" , func (t * testing.T ) {
218+ // Add a new profile and set it as default
219+ _ , _ , err := cli .Run ("profile" , "create" , sketchDir .String (), "-m" , "leonardo" , "-b" , "arduino:avr:leonardo" , "--set-default" )
220+ require .NoError (t , err )
221+ fileContent , err := projectFile .ReadFile ()
222+ require .NoError (t , err )
223+ require .Contains (t , string (fileContent ), " leonardo:\n fqbn: arduino:avr:leonardo\n platforms:\n - platform: arduino:avr (1.8.6)\n " )
224+ require .Contains (t , string (fileContent ), "default_profile: leonardo" )
225+ })
226+
227+ t .Run ("WrongFQBN" , func (t * testing.T ) {
228+ // Adding a profile with an incorrect FQBN should return an error
229+ _ , stderr , err := cli .Run ("profile" , "create" , sketchDir .String (), "-m" , "wrong_fqbn" , "-b" , "foo:bar" )
230+ require .Error (t , err )
231+ require .Contains (t , string (stderr ), "Invalid FQBN" )
232+ })
233+
234+ t .Run ("MissingFQBN" , func (t * testing.T ) {
235+ // Add a profile with no FQBN should return an error
236+ _ , _ , err := cli .Run ("profile" , "create" , sketchDir .String (), "-m" , "Uno" )
237+ require .Error (t , err )
238+ })
239+
240+ t .Run ("AlreadyExistingProfile" , func (t * testing.T ) {
241+ // Adding a profile with a name that already exists should return an error
242+ _ , stderr , err := cli .Run ("profile" , "create" , sketchDir .String (), "-m" , "uno" , "-b" , "arduino:avr:uno" )
243+ require .Error (t , err )
244+ require .Contains (t , string (stderr ), "Profile 'uno' already exists" )
245+ })
246+ })
247+ }
248+
249+ func TestProfileLib (t * testing.T ) {
250+ env , cli := integrationtest .CreateArduinoCLIWithEnvironment (t )
251+ defer env .CleanUp ()
252+
253+ // Init the environment explicitly
254+ _ , _ , err := cli .Run ("core" , "update-index" )
255+ require .NoError (t , err )
256+ _ , _ , err = cli .Run ("core" , "install" , "arduino:avr" )
257+ require .NoError (t , err )
258+
259+ t .Run ("AddLibToDefaultProfile" , func (t * testing.T ) {
260+ sk := createTempSketch (t , cli , "AddLibSketch" )
261+
262+ _ , _ , err = cli .Run ("profile" , "create" , sk .String (), "-m" , "uno" , "-b" , "arduino:avr:uno" , "--set-default" )
263+ require .NoError (t , err )
264+
265+ out ,
_ ,
err := cli .
Run (
"profile" ,
"lib" ,
"add" ,
"[email protected] " ,
"--dest-dir" ,
sk .
String (),
"--json" )
266+ require .NoError (t , err )
267+ requirejson .Parse (t , out ).Query (".added_libraries" ).MustContain (`
268+ [
269+ { "kind": "index",
270+ "library": {"name": "Arduino_Modulino", "version": "0.7.0" }
271+ }
272+ ]` )
273+
274+ fileContent , err := sk .Join ("sketch.yaml" ).ReadFile ()
275+ require .NoError (t , err )
276+ require .Contains (t , string (fileContent ), " - Arduino_Modulino (0.7.0)\n " )
277+ // dependency added as well
278+ require .Contains (t , string (fileContent ), " - dependency: Arduino_LSM6DSOX (" )
279+
280+ t .Run ("ChangeLibVersionToDefaultProfile" , func (t * testing.T ) {
281+ out ,
_ ,
err := cli .
Run (
"profile" ,
"lib" ,
"add" ,
"[email protected] " ,
"--dest-dir" ,
sk .
String (),
"--json" )
282+ require .NoError (t , err )
283+ outjson := requirejson .Parse (t , out )
284+ outjson .Query (".added_libraries" ).MustContain (`
285+ [
286+ { "kind": "index",
287+ "library": {"name": "Arduino_Modulino", "version": "0.6.0"}
288+ }
289+ ]` )
290+ outjson .Query (".skipped_libraries" ).MustContain (`
291+ [
292+ { "kind": "index",
293+ "library": {"name":"Arduino_LSM6DSOX"}
294+ }
295+ ]` )
296+
297+ fileContent , err := sk .Join ("sketch.yaml" ).ReadFile ()
298+ require .NoError (t , err )
299+ require .Contains (t , string (fileContent ), " - Arduino_Modulino (0.6.0)\n " )
300+ })
301+
302+ t .Run ("RemoveLibFromDefaultProfile" , func (t * testing.T ) {
303+ _ , _ , err = cli .Run ("profile" , "lib" , "remove" , "Arduino_Modulino" , "--dest-dir" , sk .String ())
304+ require .NoError (t , err )
305+ fileContent , err := sk .Join ("sketch.yaml" ).ReadFile ()
306+ require .NoError (t , err )
307+ require .NotContains (t , string (fileContent ), "Arduino_Modulino" )
308+ // dependency removed as well
309+ require .NotContains (t , string (fileContent ), "Arduino_LSM6DSOX" )
310+ })
311+
312+ t .Run ("AddInexistentLibToDefaultProfile" , func (t * testing.T ) {
313+ _ , stderr , err := cli .Run ("profile" , "lib" , "add" , "foobar123" , "--dest-dir" , sk .String ())
314+ require .Error (t , err )
315+ require .Equal (t , "Error adding foobar123: Library 'foobar123@latest' not found\n " , string (stderr ))
316+ })
317+
318+ t .Run ("RemoveLibNotInProfile" , func (t * testing.T ) {
319+ _ , stderr , err := cli .Run ("profile" , "lib" , "remove" , "Arduino_JSON" , "--dest-dir" , sk .String ())
320+ require .Error (t , err )
321+ require .Equal (t , "Error removing library Arduino_JSON from the profile: could not remove library: Library 'Arduino_JSON' not found\n " , string (stderr ))
322+ })
323+ })
324+
325+ }
326+
327+ func TestProfileLibAddRemoveFromSpecificProfile (t * testing.T ) {
328+ env , cli := integrationtest .CreateArduinoCLIWithEnvironment (t )
329+ defer env .CleanUp ()
330+
331+ // Init the environment explicitly
332+ _ , _ , err := cli .Run ("core" , "update-index" )
333+ require .NoError (t , err )
334+ _ , _ , err = cli .Run ("core" , "install" , "arduino:avr" )
335+ require .NoError (t , err )
336+ sk := createTempSketch (t , cli , "Simple" )
337+
338+ _ , _ , err = cli .Run ("profile" , "create" , sk .String (), "-m" , "uno" , "-b" , "arduino:avr:uno" )
339+ require .NoError (t , err )
340+ // Add a second profile
341+ _ , _ , err = cli .Run ("profile" , "create" , sk .String (), "-m" , "my_profile" , "-b" , "arduino:avr:uno" )
342+ require .NoError (t , err )
343+
344+ // Add library to a specific profile
345+ _ ,
_ ,
err = cli .
Run (
"profile" ,
"lib" ,
"add" ,
"[email protected] " ,
"-m" ,
"my_profile" ,
"--dest-dir" ,
sk .
String (),
"--no-deps" )
346+ require .NoError (t , err )
347+ fileContent , err := sk .Join ("sketch.yaml" ).ReadFile ()
348+ require .NoError (t , err )
349+ require .Contains (t , string (fileContent ), " my_profile:\n fqbn: arduino:avr:uno\n platforms:\n - platform: arduino:avr (1.8.6)\n libraries:\n - Arduino_Modulino (0.7.0)\n " )
350+
351+ // Remove library from a specific profile
352+ _ , _ , err = cli .Run ("profile" , "lib" , "remove" , "Arduino_Modulino" , "-m" , "my_profile" , "--dest-dir" , sk .String ())
353+ require .NoError (t , err )
354+ fileContent , err = sk .Join ("sketch.yaml" ).ReadFile ()
355+ require .NoError (t , err )
356+ require .NotContains (t , string (fileContent ), "Arduino_Modulino" )
357+ }
358+
359+ func TestProfileSetDefault (t * testing.T ) {
360+ env , cli := integrationtest .CreateArduinoCLIWithEnvironment (t )
361+ defer env .CleanUp ()
362+
363+ // Init the environment explicitly
364+ _ , _ , err := cli .Run ("core" , "update-index" )
365+ require .NoError (t , err )
366+ _ , _ , err = cli .Run ("core" , "install" , "arduino:avr" )
367+ require .NoError (t , err )
368+ sk := createTempSketch (t , cli , "Simple" )
369+
370+ // Create two profiles and set both as default (the second one should override the first)
371+ _ , _ , err = cli .Run ("profile" , "create" , sk .String (), "-m" , "my_profile" , "-b" , "arduino:avr:uno" , "--set-default" )
372+ require .NoError (t , err )
373+ _ , _ , err = cli .Run ("profile" , "create" , sk .String (), "-m" , "uno" , "-b" , "arduino:avr:uno" , "--set-default" )
374+ require .NoError (t , err )
375+
376+ fileContent , err := sk .Join ("sketch.yaml" ).ReadFileAsLines ()
377+ require .NoError (t , err )
378+ require .Contains (t , fileContent , "default_profile: uno" )
379+ require .NotContains (t , fileContent , "default_profile: my_profile" )
380+
381+ // Change default profile
382+ _ , _ , err = cli .Run ("profile" , "set-default" , "my_profile" , "--dest-dir" , sk .String ())
383+ require .NoError (t , err )
384+ fileContent , err = sk .Join ("sketch.yaml" ).ReadFileAsLines ()
385+ require .NoError (t , err )
386+ require .NotContains (t , fileContent , "default_profile: uno" )
387+ require .Contains (t , fileContent , "default_profile: my_profile" )
388+
389+ // Changing to an inexistent profile returns an error
390+ _ , stderr , err := cli .Run ("profile" , "set-default" , "inexistent_profile" , "--dest-dir" , sk .String ())
391+ require .Error (t , err )
392+ require .Equal (t , "Cannot set inexistent_profile as default profile: Profile 'inexistent_profile' not found\n " , string (stderr ))
393+ }
0 commit comments