Skip to content

Commit

Permalink
polish legacy test
Browse files Browse the repository at this point in the history
  • Loading branch information
alessio-perugini committed Sep 15, 2023
1 parent a7963f4 commit 6004057
Show file tree
Hide file tree
Showing 12 changed files with 156 additions and 737 deletions.
11 changes: 3 additions & 8 deletions arduino/builder/linker.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,10 @@ func (b *Builder) Link() error {
return nil
}

// TODO can we remove this multiple assignations?
objectFilesSketch := b.buildArtifacts.sketchObjectFiles
objectFilesLibraries := b.buildArtifacts.librariesObjectFiles
objectFilesCore := b.buildArtifacts.coreObjectsFiles

objectFiles := paths.NewPathList()
objectFiles.AddAll(objectFilesSketch)
objectFiles.AddAll(objectFilesLibraries)
objectFiles.AddAll(objectFilesCore)
objectFiles.AddAll(b.buildArtifacts.sketchObjectFiles)
objectFiles.AddAll(b.buildArtifacts.librariesObjectFiles)
objectFiles.AddAll(b.buildArtifacts.coreObjectsFiles)

coreDotARelPath, err := b.buildPath.RelTo(b.buildArtifacts.coreArchiveFilePath)
if err != nil {
Expand Down
133 changes: 133 additions & 0 deletions arduino/builder/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
package utils

import (
"os"
"testing"
"time"

"github.com/arduino/go-paths-helper"
"github.com/stretchr/testify/require"
)

Expand All @@ -39,3 +42,133 @@ func TestPrintableCommand(t *testing.T) {
result := printableCommand(parts)
require.Equal(t, correct, result)
}

func tempFile(t *testing.T, prefix string) *paths.Path {
file, err := os.CreateTemp("", prefix)
file.Close()
require.NoError(t, err)
return paths.New(file.Name())
}

func TestObjFileIsUpToDateObjMissing(t *testing.T) {
sourceFile := tempFile(t, "source")
defer sourceFile.RemoveAll()

upToDate, err := ObjFileIsUpToDate(sourceFile, nil, nil)
require.NoError(t, err)
require.False(t, upToDate)
}

func TestObjFileIsUpToDateDepMissing(t *testing.T) {
sourceFile := tempFile(t, "source")
defer sourceFile.RemoveAll()

objFile := tempFile(t, "obj")
defer objFile.RemoveAll()

upToDate, err := ObjFileIsUpToDate(sourceFile, objFile, nil)
require.NoError(t, err)
require.False(t, upToDate)
}

func TestObjFileIsUpToDateObjOlder(t *testing.T) {
objFile := tempFile(t, "obj")
defer objFile.RemoveAll()
depFile := tempFile(t, "dep")
defer depFile.RemoveAll()

time.Sleep(time.Second)

sourceFile := tempFile(t, "source")
defer sourceFile.RemoveAll()

upToDate, err := ObjFileIsUpToDate(sourceFile, objFile, depFile)
require.NoError(t, err)
require.False(t, upToDate)
}

func TestObjFileIsUpToDateObjNewer(t *testing.T) {
sourceFile := tempFile(t, "source")
defer sourceFile.RemoveAll()

time.Sleep(time.Second)

objFile := tempFile(t, "obj")
defer objFile.RemoveAll()
depFile := tempFile(t, "dep")
defer depFile.RemoveAll()

upToDate, err := ObjFileIsUpToDate(sourceFile, objFile, depFile)
require.NoError(t, err)
require.True(t, upToDate)
}

func TestObjFileIsUpToDateDepIsNewer(t *testing.T) {
sourceFile := tempFile(t, "source")
defer sourceFile.RemoveAll()

time.Sleep(time.Second)

objFile := tempFile(t, "obj")
defer objFile.RemoveAll()
depFile := tempFile(t, "dep")
defer depFile.RemoveAll()

time.Sleep(time.Second)

headerFile := tempFile(t, "header")
defer headerFile.RemoveAll()

data := objFile.String() + ": \\\n\t" + sourceFile.String() + " \\\n\t" + headerFile.String()
depFile.WriteFile([]byte(data))

upToDate, err := ObjFileIsUpToDate(sourceFile, objFile, depFile)
require.NoError(t, err)
require.False(t, upToDate)
}

func TestObjFileIsUpToDateDepIsOlder(t *testing.T) {
sourceFile := tempFile(t, "source")
defer sourceFile.RemoveAll()

headerFile := tempFile(t, "header")
defer headerFile.RemoveAll()

time.Sleep(time.Second)

objFile := tempFile(t, "obj")
defer objFile.RemoveAll()
depFile := tempFile(t, "dep")
defer depFile.RemoveAll()

res := objFile.String() + ": \\\n\t" + sourceFile.String() + " \\\n\t" + headerFile.String()
depFile.WriteFile([]byte(res))

upToDate, err := ObjFileIsUpToDate(sourceFile, objFile, depFile)
require.NoError(t, err)
require.True(t, upToDate)
}

func TestObjFileIsUpToDateDepIsWrong(t *testing.T) {
sourceFile := tempFile(t, "source")
defer sourceFile.RemoveAll()

time.Sleep(time.Second)

objFile := tempFile(t, "obj")
defer objFile.RemoveAll()
depFile := tempFile(t, "dep")
defer depFile.RemoveAll()

time.Sleep(time.Second)

headerFile := tempFile(t, "header")
defer headerFile.RemoveAll()

res := sourceFile.String() + ": \\\n\t" + sourceFile.String() + " \\\n\t" + headerFile.String()
depFile.WriteFile([]byte(res))

upToDate, err := ObjFileIsUpToDate(sourceFile, objFile, depFile)
require.NoError(t, err)
require.False(t, upToDate)
}
2 changes: 2 additions & 0 deletions internal/integrationtest/compile_4/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,13 +387,15 @@ func testBuilderBridgeExample(t *testing.T, env *integrationtest.Environment, cl

// Simulate a library use in libraries build path
require.NoError(t, buildPath.Join("libraries", "SPI").MkdirAll())
require.NoError(t, buildPath.Join("libraries", "dummy_file").WriteFile([]byte{}))

// Build again...
_, err = tryBuild(t, env, cli, "arduino:avr:leonardo", &buildOptions{NoClean: true})
require.NoError(t, err)

require.False(t, buildPath.Join("libraries", "SPI").Exist())
require.True(t, buildPath.Join("libraries", "Bridge").Exist())
require.True(t, buildPath.Join("libraries", "dummy_file").Exist())
})

t.Run("Preprocess", func(t *testing.T) {
Expand Down
38 changes: 13 additions & 25 deletions legacy/builder/test/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,9 @@ func cleanUpBuilderTestContext(t *testing.T, ctx *types.Context) {
}
}

type skipContextPreparationStepName string

const skipLibraries = skipContextPreparationStepName("libraries")

func prepareBuilderTestContext(t *testing.T, ctx *types.Context, sketchPath *paths.Path, fqbnString string, skips ...skipContextPreparationStepName) *types.Context {
func prepareBuilderTestContext(t *testing.T, ctx *types.Context, sketchPath *paths.Path, fqbnString string) *types.Context {
DownloadCoresAndToolsAndLibraries(t)

stepToSkip := map[skipContextPreparationStepName]bool{}
for _, skip := range skips {
stepToSkip[skip] = true
}

if ctx == nil {
ctx = &types.Context{}
}
Expand Down Expand Up @@ -123,7 +114,6 @@ func prepareBuilderTestContext(t *testing.T, ctx *types.Context, sketchPath *pat
_, err = pme.FindToolsRequiredForBuild(targetPlatform, buildPlatform)
require.NoError(t, err)


ctx.Builder, err = bldr.NewBuilder(
sk, boardBuildProperties, buildPath, false, nil, 0, nil,
ctx.HardwareDirs, ctx.BuiltInToolsDirs, ctx.OtherLibrariesDirs,
Expand All @@ -133,21 +123,19 @@ func prepareBuilderTestContext(t *testing.T, ctx *types.Context, sketchPath *pat
ctx.PackageManager = pme
}

if !stepToSkip[skipLibraries] {
lm, libsResolver, _, err := detector.LibrariesLoader(
false, nil,
ctx.BuiltInLibrariesDirs, nil, ctx.OtherLibrariesDirs,
actualPlatform, targetPlatform,
)
require.NoError(t, err)
lm, libsResolver, _, err := detector.LibrariesLoader(
false, nil,
ctx.BuiltInLibrariesDirs, nil, ctx.OtherLibrariesDirs,
actualPlatform, targetPlatform,
)
require.NoError(t, err)

ctx.SketchLibrariesDetector = detector.NewSketchLibrariesDetector(
lm, libsResolver,
false,
false,
builderLogger,
)
}
ctx.SketchLibrariesDetector = detector.NewSketchLibrariesDetector(
lm, libsResolver,
false,
false,
builderLogger,
)

return ctx
}
156 changes: 0 additions & 156 deletions legacy/builder/test/builder_utils_test.go

This file was deleted.

Loading

0 comments on commit 6004057

Please sign in to comment.