Skip to content

Commit 4591ba3

Browse files
committed
Simplified builder test
Now that caching is optional old tests may run as before (without the need to cleanup cache). Signed-off-by: Cristian Maglie <c.maglie@arduino.cc>
1 parent 4764159 commit 4591ba3

File tree

2 files changed

+32
-54
lines changed

2 files changed

+32
-54
lines changed

src/arduino.cc/builder/test/builder_test.go

+18-48
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func prepareBuilderTestContext(sketchPath, fqbn string) *types.Context {
5555
Verbose: false,
5656
}
5757
}
58+
5859
func TestBuilderEmptySketch(t *testing.T) {
5960
DownloadCoresAndToolsAndLibraries(t)
6061

@@ -64,11 +65,6 @@ func TestBuilderEmptySketch(t *testing.T) {
6465
buildPath := SetupBuildPath(t, ctx)
6566
defer os.RemoveAll(buildPath)
6667

67-
// Cleanup cached core
68-
coreFolder := filepath.Join("downloaded_hardware", "arduino", "avr")
69-
coreFile := builder_utils.GetCachedCoreArchiveFileName(ctx.FQBN, coreFolder)
70-
os.Remove(coreFile)
71-
7268
// Run builder
7369
command := builder.Builder{}
7470
err := command.Run(ctx)
@@ -94,11 +90,6 @@ func TestBuilderBridge(t *testing.T) {
9490
buildPath := SetupBuildPath(t, ctx)
9591
defer os.RemoveAll(buildPath)
9692

97-
// Cleanup cached core
98-
coreFolder := filepath.Join("downloaded_hardware", "arduino", "avr")
99-
coreFile := builder_utils.GetCachedCoreArchiveFileName(ctx.FQBN, coreFolder)
100-
os.Remove(coreFile)
101-
10293
// Run builder
10394
command := builder.Builder{}
10495
err := command.Run(ctx)
@@ -126,11 +117,6 @@ func TestBuilderSketchWithConfig(t *testing.T) {
126117
buildPath := SetupBuildPath(t, ctx)
127118
defer os.RemoveAll(buildPath)
128119

129-
// Cleanup cached core
130-
coreFolder := filepath.Join("downloaded_hardware", "arduino", "avr")
131-
coreFile := builder_utils.GetCachedCoreArchiveFileName(ctx.FQBN, coreFolder)
132-
os.Remove(coreFile)
133-
134120
// Run builder
135121
command := builder.Builder{}
136122
err := command.Run(ctx)
@@ -158,11 +144,6 @@ func TestBuilderBridgeTwice(t *testing.T) {
158144
buildPath := SetupBuildPath(t, ctx)
159145
defer os.RemoveAll(buildPath)
160146

161-
// Cleanup cached core
162-
coreFolder := filepath.Join("downloaded_hardware", "arduino", "avr")
163-
coreFile := builder_utils.GetCachedCoreArchiveFileName(ctx.FQBN, coreFolder)
164-
os.Remove(coreFile)
165-
166147
// Run builder
167148
command := builder.Builder{}
168149
err := command.Run(ctx)
@@ -191,17 +172,11 @@ func TestBuilderBridgeSAM(t *testing.T) {
191172
DownloadCoresAndToolsAndLibraries(t)
192173

193174
ctx := prepareBuilderTestContext(filepath.Join("downloaded_libraries", "Bridge", "examples", "Bridge", "Bridge.ino"), "arduino:sam:arduino_due_x_dbg")
175+
ctx.WarningsLevel = "all"
194176

195177
buildPath := SetupBuildPath(t, ctx)
196178
defer os.RemoveAll(buildPath)
197179

198-
ctx.WarningsLevel = "all"
199-
200-
// Cleanup cached core
201-
coreFolder := filepath.Join("downloaded_hardware", "arduino", "sam")
202-
coreFile := builder_utils.GetCachedCoreArchiveFileName(ctx.FQBN, coreFolder)
203-
os.Remove(coreFile)
204-
205180
// Run builder
206181
command := builder.Builder{}
207182
err := command.Run(ctx)
@@ -236,14 +211,10 @@ func TestBuilderBridgeRedBearLab(t *testing.T) {
236211
ctx := prepareBuilderTestContext(filepath.Join("downloaded_libraries", "Bridge", "examples", "Bridge", "Bridge.ino"), "RedBearLab:avr:blend")
237212
ctx.HardwareFolders = append(ctx.HardwareFolders, "downloaded_board_manager_stuff")
238213
ctx.ToolsFolders = append(ctx.ToolsFolders, "downloaded_board_manager_stuff")
214+
239215
buildPath := SetupBuildPath(t, ctx)
240216
defer os.RemoveAll(buildPath)
241217

242-
// Cleanup cached core
243-
coreFolder := filepath.Join("downloaded_hardware", "arduino", "avr")
244-
coreFile := builder_utils.GetCachedCoreArchiveFileName(ctx.FQBN, coreFolder)
245-
os.Remove(coreFile)
246-
247218
// Run builder
248219
command := builder.Builder{}
249220
err := command.Run(ctx)
@@ -273,6 +244,7 @@ func TestBuilderSketchNoFunctions(t *testing.T) {
273244
buildPath := SetupBuildPath(t, ctx)
274245
defer os.RemoveAll(buildPath)
275246

247+
// Run builder
276248
command := builder.Builder{}
277249
err := command.Run(ctx)
278250
require.Error(t, err)
@@ -288,6 +260,7 @@ func TestBuilderSketchWithBackup(t *testing.T) {
288260
buildPath := SetupBuildPath(t, ctx)
289261
defer os.RemoveAll(buildPath)
290262

263+
// Run builder
291264
command := builder.Builder{}
292265
err := command.Run(ctx)
293266
NoError(t, err)
@@ -301,6 +274,7 @@ func TestBuilderSketchWithOldLib(t *testing.T) {
301274
buildPath := SetupBuildPath(t, ctx)
302275
defer os.RemoveAll(buildPath)
303276

277+
// Run builder
304278
command := builder.Builder{}
305279
err := command.Run(ctx)
306280
NoError(t, err)
@@ -314,6 +288,7 @@ func TestBuilderSketchWithSubfolders(t *testing.T) {
314288
buildPath := SetupBuildPath(t, ctx)
315289
defer os.RemoveAll(buildPath)
316290

291+
// Run builder
317292
command := builder.Builder{}
318293
err := command.Run(ctx)
319294
NoError(t, err)
@@ -329,6 +304,7 @@ func TestBuilderSketchBuildPathContainsUnusedPreviouslyCompiledLibrary(t *testin
329304

330305
NoError(t, os.MkdirAll(filepath.Join(buildPath, constants.FOLDER_LIBRARIES, "SPI"), os.FileMode(0755)))
331306

307+
// Run builder
332308
command := builder.Builder{}
333309
err := command.Run(ctx)
334310
NoError(t, err)
@@ -350,19 +326,11 @@ func TestBuilderWithBuildPathInSketchDir(t *testing.T) {
350326
NoError(t, err)
351327
defer os.RemoveAll(ctx.BuildPath)
352328

353-
// Cleanup cached core
354-
coreFolder := filepath.Join("downloaded_hardware", "arduino", "avr")
355-
coreFile := builder_utils.GetCachedCoreArchiveFileName(ctx.FQBN, coreFolder)
356-
os.Remove(coreFile)
357-
358329
// Run build
359330
command := builder.Builder{}
360331
err = command.Run(ctx)
361332
NoError(t, err)
362333

363-
// Cleanup cached core
364-
os.Remove(coreFile)
365-
366334
// Run build twice, to verify the build still works when the
367335
// build directory is present at the start
368336
err = command.Run(ctx)
@@ -376,24 +344,26 @@ func TestBuilderCacheCoreAFile(t *testing.T) {
376344

377345
SetupBuildPath(t, ctx)
378346
defer os.RemoveAll(ctx.BuildPath)
379-
380-
// Cleanup cached core
381-
coreFolder := filepath.Join("downloaded_hardware", "arduino", "avr")
382-
coreFile := builder_utils.GetCachedCoreArchiveFileName(ctx.FQBN, coreFolder)
383-
os.Remove(coreFile)
347+
SetupBuildCachePath(t, ctx)
348+
defer os.RemoveAll(ctx.BuildCachePath)
384349

385350
// Run build
386351
bldr := builder.Builder{}
387352
err := bldr.Run(ctx)
388353
NoError(t, err)
389-
coreStatBefore, err := os.Stat(coreFile)
354+
355+
// Pick timestamp of cached core
356+
coreFolder := filepath.Join("downloaded_hardware", "arduino", "avr")
357+
coreFileName := builder_utils.GetCachedCoreArchiveFileName(ctx.FQBN, coreFolder)
358+
cachedCoreFile := filepath.Join(ctx.CoreBuildCachePath, coreFileName)
359+
coreStatBefore, err := os.Stat(cachedCoreFile)
390360
require.NoError(t, err)
391361

392362
// Run build again, to verify that the builder skips rebuilding core.a
393363
err = bldr.Run(ctx)
394364
NoError(t, err)
395365

396-
coreStatAfterRebuild, err := os.Stat(coreFile)
366+
coreStatAfterRebuild, err := os.Stat(cachedCoreFile)
397367
require.NoError(t, err)
398368
require.Equal(t, coreStatBefore.ModTime(), coreStatAfterRebuild.ModTime())
399369

@@ -407,7 +377,7 @@ func TestBuilderCacheCoreAFile(t *testing.T) {
407377
err = bldr.Run(ctx)
408378
NoError(t, err)
409379

410-
coreStatAfterTouch, err := os.Stat(coreFile)
380+
coreStatAfterTouch, err := os.Stat(cachedCoreFile)
411381
require.NoError(t, err)
412382
require.NotEqual(t, coreStatBefore.ModTime(), coreStatAfterTouch.ModTime())
413383
}

src/arduino.cc/builder/test/helper.go

+14-6
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,18 @@
3131
package test
3232

3333
import (
34-
"arduino.cc/builder/constants"
35-
"arduino.cc/builder/types"
36-
"arduino.cc/builder/utils"
3734
"bytes"
3835
"fmt"
39-
"github.com/go-errors/errors"
40-
"github.com/stretchr/testify/assert"
4136
"io/ioutil"
4237
"path/filepath"
4338
"testing"
4439
"text/template"
40+
41+
"arduino.cc/builder/constants"
42+
"arduino.cc/builder/types"
43+
"arduino.cc/builder/utils"
44+
"github.com/go-errors/errors"
45+
"github.com/stretchr/testify/assert"
4546
)
4647

4748
func LoadAndInterpolate(t *testing.T, filename string, ctx *types.Context) string {
@@ -78,12 +79,19 @@ func NoError(t *testing.T, err error, msgAndArgs ...interface{}) {
7879
}
7980

8081
func SetupBuildPath(t *testing.T, ctx *types.Context) string {
81-
buildPath, err := ioutil.TempDir(constants.EMPTY_STRING, "test")
82+
buildPath, err := ioutil.TempDir(constants.EMPTY_STRING, "test_build_path")
8283
NoError(t, err)
8384
ctx.BuildPath = buildPath
8485
return buildPath
8586
}
8687

88+
func SetupBuildCachePath(t *testing.T, ctx *types.Context) string {
89+
buildCachePath, err := ioutil.TempDir(constants.EMPTY_STRING, "test_build_cache")
90+
NoError(t, err)
91+
ctx.BuildCachePath = buildCachePath
92+
return buildCachePath
93+
}
94+
8795
type ByLibraryName []*types.Library
8896

8997
func (s ByLibraryName) Len() int {

0 commit comments

Comments
 (0)