16
16
package builder_test
17
17
18
18
import (
19
+ "fmt"
19
20
"io/ioutil"
20
21
"os"
21
22
"path/filepath"
@@ -24,7 +25,7 @@ import (
24
25
25
26
"github.com/arduino/arduino-cli/arduino/builder"
26
27
"github.com/arduino/arduino-cli/arduino/sketch"
27
- "github.com/stretchr/testify/assert "
28
+ "github.com/stretchr/testify/require "
28
29
)
29
30
30
31
func TestSaveSketch (t * testing.T ) {
@@ -38,64 +39,64 @@ func TestSaveSketch(t *testing.T) {
38
39
t .Fatalf ("unable to read golden file %s: %v" , sketchFile , err )
39
40
}
40
41
41
- builder .SaveSketchItemCpp (& sketch.Item {Path : sketchName , Source : source }, tmp )
42
+ builder .SketchSaveItemCpp (& sketch.Item {Path : sketchName , Source : source }, tmp )
42
43
43
44
out , err := ioutil .ReadFile (filepath .Join (tmp , outName ))
44
45
if err != nil {
45
46
t .Fatalf ("unable to read output file %s: %v" , outName , err )
46
47
}
47
48
48
- assert .Equal (t , source , out )
49
+ require .Equal (t , source , out )
49
50
}
50
51
51
52
func TestLoadSketchFolder (t * testing.T ) {
52
53
// pass the path to the sketch folder
53
54
sketchPath := filepath .Join ("testdata" , t .Name ())
54
55
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 ))
67
68
68
69
// pass the path to the main file
69
70
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 ))
81
82
}
82
83
83
84
func TestLoadSketchFolderWrongMain (t * testing.T ) {
84
85
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" )
88
89
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" )
92
93
}
93
94
94
95
func TestMergeSketchSources (t * testing.T ) {
95
96
// 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 )
99
100
100
101
// load expected result
101
102
mergedPath := filepath .Join ("testdata" , t .Name ()+ ".txt" )
@@ -104,17 +105,51 @@ func TestMergeSketchSources(t *testing.T) {
104
105
t .Fatalf ("unable to read golden file %s: %v" , mergedPath , err )
105
106
}
106
107
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 )
110
111
}
111
112
112
113
func 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 )
116
117
117
118
// 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 ())
120
155
}
0 commit comments