1616package builder_test
1717
1818import (
19+ "fmt"
1920 "io/ioutil"
2021 "os"
2122 "path/filepath"
@@ -24,7 +25,7 @@ import (
2425
2526 "github.com/arduino/arduino-cli/arduino/builder"
2627 "github.com/arduino/arduino-cli/arduino/sketch"
27- "github.com/stretchr/testify/assert "
28+ "github.com/stretchr/testify/require "
2829)
2930
3031func TestSaveSketch (t * testing.T ) {
@@ -38,64 +39,64 @@ func TestSaveSketch(t *testing.T) {
3839 t .Fatalf ("unable to read golden file %s: %v" , sketchFile , err )
3940 }
4041
41- builder .SaveSketchItemCpp (& sketch.Item {Path : sketchName , Source : source }, tmp )
42+ builder .SketchSaveItemCpp (& sketch.Item {Path : sketchName , Source : source }, tmp )
4243
4344 out , err := ioutil .ReadFile (filepath .Join (tmp , outName ))
4445 if err != nil {
4546 t .Fatalf ("unable to read output file %s: %v" , outName , err )
4647 }
4748
48- assert .Equal (t , source , out )
49+ require .Equal (t , source , out )
4950}
5051
5152func TestLoadSketchFolder (t * testing.T ) {
5253 // pass the path to the sketch folder
5354 sketchPath := filepath .Join ("testdata" , t .Name ())
5455 mainFilePath := filepath .Join (sketchPath , t .Name ()+ ".ino" )
55- s , err := builder .LoadSketch (sketchPath , "" )
56- assert .Nil (t , err )
57- assert .NotNil (t , s )
58- assert .Equal (t , mainFilePath , s .MainFile .Path )
59- assert .Equal (t , sketchPath , s .LocationPath )
60- assert .Len (t , s .OtherSketchFiles , 2 )
61- assert .Equal (t , "old.pde" , filepath .Base (s .OtherSketchFiles [0 ].Path ))
62- assert .Equal (t , "other.ino" , filepath .Base (s .OtherSketchFiles [1 ].Path ))
63- assert .Len (t , s .AdditionalFiles , 3 )
64- assert .Equal (t , "header.h" , filepath .Base (s .AdditionalFiles [0 ].Path ))
65- assert .Equal (t , "s_file.S" , filepath .Base (s .AdditionalFiles [1 ].Path ))
66- assert .Equal (t , "helper.h" , filepath .Base (s .AdditionalFiles [2 ].Path ))
56+ s , err := builder .SketchLoad (sketchPath , "" )
57+ require .Nil (t , err )
58+ require .NotNil (t , s )
59+ require .Equal (t , mainFilePath , s .MainFile .Path )
60+ require .Equal (t , sketchPath , s .LocationPath )
61+ require .Len (t , s .OtherSketchFiles , 2 )
62+ require .Equal (t , "old.pde" , filepath .Base (s .OtherSketchFiles [0 ].Path ))
63+ require .Equal (t , "other.ino" , filepath .Base (s .OtherSketchFiles [1 ].Path ))
64+ require .Len (t , s .AdditionalFiles , 3 )
65+ require .Equal (t , "header.h" , filepath .Base (s .AdditionalFiles [0 ].Path ))
66+ require .Equal (t , "s_file.S" , filepath .Base (s .AdditionalFiles [1 ].Path ))
67+ require .Equal (t , "helper.h" , filepath .Base (s .AdditionalFiles [2 ].Path ))
6768
6869 // pass the path to the main file
6970 sketchPath = mainFilePath
70- s , err = builder .LoadSketch (sketchPath , "" )
71- assert .Nil (t , err )
72- assert .NotNil (t , s )
73- assert .Equal (t , mainFilePath , s .MainFile .Path )
74- assert .Len (t , s .OtherSketchFiles , 2 )
75- assert .Equal (t , "old.pde" , filepath .Base (s .OtherSketchFiles [0 ].Path ))
76- assert .Equal (t , "other.ino" , filepath .Base (s .OtherSketchFiles [1 ].Path ))
77- assert .Len (t , s .AdditionalFiles , 3 )
78- assert .Equal (t , "header.h" , filepath .Base (s .AdditionalFiles [0 ].Path ))
79- assert .Equal (t , "s_file.S" , filepath .Base (s .AdditionalFiles [1 ].Path ))
80- assert .Equal (t , "helper.h" , filepath .Base (s .AdditionalFiles [2 ].Path ))
71+ s , err = builder .SketchLoad (sketchPath , "" )
72+ require .Nil (t , err )
73+ require .NotNil (t , s )
74+ require .Equal (t , mainFilePath , s .MainFile .Path )
75+ require .Len (t , s .OtherSketchFiles , 2 )
76+ require .Equal (t , "old.pde" , filepath .Base (s .OtherSketchFiles [0 ].Path ))
77+ require .Equal (t , "other.ino" , filepath .Base (s .OtherSketchFiles [1 ].Path ))
78+ require .Len (t , s .AdditionalFiles , 3 )
79+ require .Equal (t , "header.h" , filepath .Base (s .AdditionalFiles [0 ].Path ))
80+ require .Equal (t , "s_file.S" , filepath .Base (s .AdditionalFiles [1 ].Path ))
81+ require .Equal (t , "helper.h" , filepath .Base (s .AdditionalFiles [2 ].Path ))
8182}
8283
8384func TestLoadSketchFolderWrongMain (t * testing.T ) {
8485 sketchPath := filepath .Join ("testdata" , t .Name ())
85- _ , err := builder .LoadSketch (sketchPath , "" )
86- assert .Error (t , err )
87- assert .Contains (t , err .Error (), "unable to find the main sketch file" )
86+ _ , err := builder .SketchLoad (sketchPath , "" )
87+ require .Error (t , err )
88+ require .Contains (t , err .Error (), "unable to find the main sketch file" )
8889
89- _ , err = builder .LoadSketch ("does/not/exist" , "" )
90- assert .Error (t , err )
91- assert .Contains (t , err .Error (), "no such file or directory" )
90+ _ , err = builder .SketchLoad ("does/not/exist" , "" )
91+ require .Error (t , err )
92+ require .Contains (t , err .Error (), "no such file or directory" )
9293}
9394
9495func TestMergeSketchSources (t * testing.T ) {
9596 // borrow the sketch from TestLoadSketchFolder to avoid boilerplate
96- s , err := builder .LoadSketch (filepath .Join ("testdata" , "TestLoadSketchFolder" ), "" )
97- assert .Nil (t , err )
98- assert .NotNil (t , s )
97+ s , err := builder .SketchLoad (filepath .Join ("testdata" , "TestLoadSketchFolder" ), "" )
98+ require .Nil (t , err )
99+ require .NotNil (t , s )
99100
100101 // load expected result
101102 mergedPath := filepath .Join ("testdata" , t .Name ()+ ".txt" )
@@ -104,17 +105,51 @@ func TestMergeSketchSources(t *testing.T) {
104105 t .Fatalf ("unable to read golden file %s: %v" , mergedPath , err )
105106 }
106107
107- offset , source := builder .MergeSketchSources (s )
108- assert .Equal (t , 2 , offset )
109- assert .Equal (t , string (mergedBytes ), source )
108+ offset , source := builder .SketchMergeSources (s )
109+ require .Equal (t , 2 , offset )
110+ require .Equal (t , string (mergedBytes ), source )
110111}
111112
112113func TestMergeSketchSourcesArduinoIncluded (t * testing.T ) {
113- s , err := builder .LoadSketch (filepath .Join ("testdata" , t .Name ()), "" )
114- assert .Nil (t , err )
115- assert .NotNil (t , s )
114+ s , err := builder .SketchLoad (filepath .Join ("testdata" , t .Name ()), "" )
115+ require .Nil (t , err )
116+ require .NotNil (t , s )
116117
117118 // ensure not to include Arduino.h when it's already there
118- _ , source := builder .MergeSketchSources (s )
119- assert .Equal (t , 1 , strings .Count (source , "<Arduino.h>" ))
119+ _ , source := builder .SketchMergeSources (s )
120+ require .Equal (t , 1 , strings .Count (source , "<Arduino.h>" ))
121+ }
122+
123+ func TestCopyAdditionalFiles (t * testing.T ) {
124+ tmp := tmpDirOrDie ()
125+ defer os .RemoveAll (tmp )
126+
127+ // load the golden sketch
128+ s1 , err := builder .SketchLoad (filepath .Join ("testdata" , t .Name ()), "" )
129+ require .Nil (t , err )
130+ require .Len (t , s1 .AdditionalFiles , 1 )
131+
132+ // copy the sketch over, create a fake main file we don't care about it
133+ // but we need it for `SketchLoad` to suceed later
134+ err = builder .SketchCopyAdditionalFiles (s1 , tmp )
135+ require .Nil (t , err )
136+ fakeIno := filepath .Join (tmp , fmt .Sprintf ("%s.ino" , filepath .Base (tmp )))
137+ require .Nil (t , ioutil .WriteFile (fakeIno , []byte {}, os .FileMode (0644 )))
138+
139+ // compare
140+ s2 , err := builder .SketchLoad (tmp , "" )
141+ require .Nil (t , err )
142+ require .Len (t , s2 .AdditionalFiles , 1 )
143+
144+ // save file info
145+ info1 , err := os .Stat (s2 .AdditionalFiles [0 ].Path )
146+ require .Nil (t , err )
147+
148+ // copy again
149+ err = builder .SketchCopyAdditionalFiles (s1 , tmp )
150+ require .Nil (t , err )
151+
152+ // verify file hasn't changed
153+ info2 , err := os .Stat (s2 .AdditionalFiles [0 ].Path )
154+ require .Equal (t , info1 .ModTime (), info2 .ModTime ())
120155}
0 commit comments