Skip to content

Commit

Permalink
[skip-changelog] Porting legacy tests to new integration-test infra…
Browse files Browse the repository at this point in the history
… (part 3...) (#2300)

* Ported TestCoreCaching to current integration test infra

* Removed useless TestBuilderEmptySketch

The same features are already tested in a number of other integration tests.

* Ported TestBuilderWithBuildPathInSketchDir to current integration test infra

* Ported TestLoadHardware to current integration test infra

* Moved user_hardware testdata resources

* Ported TestLoadHardwareMixingUserHardwareFolder to current integration test infra

* Removed redundant legacy tests

* Removed useless 'sleep' helper function

* Removed unused LoadAndInterpolate function

* Removed useless NoError helper function

* Grouped tests inside the same correct sub-group

* Ported TestFailIfBuildPathEqualsSketchPath and TestFailIfBuildPathEqualsSketchPathSketchPathDiffers to current integration test infra
  • Loading branch information
cmaglie authored Sep 8, 2023
1 parent 67f461e commit 1c110e9
Show file tree
Hide file tree
Showing 32 changed files with 454 additions and 492 deletions.
251 changes: 251 additions & 0 deletions internal/integrationtest/board/hardware_loading_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,251 @@
// This file is part of arduino-cli.
//
// Copyright 2022 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to [email protected].

package board_test

import (
"runtime"
"testing"

"github.com/arduino/arduino-cli/internal/integrationtest"
"github.com/arduino/go-paths-helper"
"github.com/stretchr/testify/require"
"go.bug.st/testifyjson/requirejson"
)

func TestHardwareLoading(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()

// install two cores, boards must be ordered by package name and platform name
_, _, err := cli.Run("core", "install", "arduino:[email protected]")
require.NoError(t, err)
// _, _, err = cli.Run("core", "install", "arduino:sam")
// require.NoError(t, err)

localTxt, err := paths.New("testdata", "custom_local_txts").Abs()
require.NoError(t, err)
downloadedHardwareAvr := cli.DataDir().Join("packages", "arduino", "hardware", "avr", "1.8.6")
localTxt.Join("boards.local.txt").CopyTo(downloadedHardwareAvr.Join("boards.local.txt"))
localTxt.Join("platform.local.txt").CopyTo(downloadedHardwareAvr.Join("platform.local.txt"))

t.Run("Simple", func(t *testing.T) {
{
out, _, err := cli.Run("core", "list", "--format", "json")
require.NoError(t, err)
jsonOut := requirejson.Parse(t, out)
jsonOut.LengthMustEqualTo(1)
jsonOut.MustContain(`[
{
"id": "arduino:avr",
"installed": "1.8.6",
"name": "Arduino AVR Boards",
"boards": [
{
"name": "Arduino Uno",
"fqbn": "arduino:avr:uno"
},
{
"name": "Arduino Yún",
"fqbn": "arduino:avr:yun"
}
]
}
]`)
}

{
// Also test local platform.txt properties override
out, _, err := cli.Run("board", "details", "-b", "arduino:avr:uno", "--format", "json")
require.NoError(t, err)
jsonOut := requirejson.Parse(t, out)
jsonOut.MustContain(`{
"version": "1.8.6",
"properties_id": "uno",
"build_properties": [
"_id=uno",
"tools.avrdude.bootloader.params.verbose=-v",
"tools.avrdude.cmd.path=/my/personal/avrdude"
],
"programmers": [
{
"platform": "Arduino AVR Boards",
"id": "usbasp",
"name": "USBasp"
},
{
"platform": "Arduino AVR Boards",
"id": "avrispmkii",
"name": "AVRISP mkII"
}
]
}`)
}

{
out, _, err := cli.Run("board", "details", "-b", "arduino:avr:yun", "--format", "json")
require.NoError(t, err)
jsonOut := requirejson.Parse(t, out)
jsonOut.MustContain(`{
"version": "1.8.6",
"properties_id": "yun",
"build_properties": [
"_id=yun",
"upload.wait_for_upload_port=true"
]
}`)
}

{
// Check un-expansion of board_properties
out, _, err := cli.Run("board", "details", "-b", "arduino:avr:robotMotor", "--show-properties=unexpanded", "--format", "json")
require.NoError(t, err)
jsonOut := requirejson.Parse(t, out)
jsonOut.MustContain(`{
"version": "1.8.6",
"properties_id": "robotMotor",
"build_properties": [
"_id=robotMotor",
"build.extra_flags={build.usb_flags}",
"upload.wait_for_upload_port=true"
]
}`)
}

{
// Also test local boards.txt properties override
out, _, err := cli.Run("board", "details", "-b", "arduino:avr:diecimila", "--show-properties=unexpanded", "--format", "json")
require.NoError(t, err)
jsonOut := requirejson.Parse(t, out)
jsonOut.MustContain(`{
"version": "1.8.6",
"properties_id": "diecimila",
"build_properties": [
"_id=diecimila",
"menu.cpu.atmega123=ATmega123"
]
}`)
}
})

t.Run("MixingUserHardware", func(t *testing.T) {
// Install custom hardware required for tests
customHwDir, err := paths.New("..", "testdata", "user_hardware").Abs()
require.NoError(t, err)
require.NoError(t, customHwDir.CopyDirTo(cli.SketchbookDir().Join("hardware")))

{
out, _, err := cli.Run("core", "list", "--format", "json")
require.NoError(t, err)
jsonOut := requirejson.Parse(t, out)
if runtime.GOOS == "windows" {
//a package is a symlink, and windows does not support them
jsonOut.LengthMustEqualTo(2)
} else {
jsonOut.LengthMustEqualTo(3)
}
jsonOut.MustContain(`[
{
"id": "arduino:avr",
"installed": "1.8.6",
"name": "Arduino AVR Boards",
"boards": [
{
"name": "Arduino Uno",
"fqbn": "arduino:avr:uno"
},
{
"name": "Arduino Yún",
"fqbn": "arduino:avr:yun"
}
]
}
]`)
jsonOut.MustContain(`[
{
"id": "my_avr_platform:avr",
"installed": "9.9.9",
"name": "My AVR Boards",
"boards": [
{
"name": "Arduino Yún",
"fqbn": "my_avr_platform:avr:custom_yun"
}
],
"manually_installed": true,
"missing_metadata": true
}
]`)

// require.False(t, myAVRPlatformAvrArch.Properties.ContainsKey("preproc.includes.flags"))

if runtime.GOOS != "windows" {
jsonOut.MustContain(`[
{
"id": "my_symlinked_avr_platform:avr",
"manually_installed": true,
"missing_metadata": true
}
]`)
}
}

{
// Also test local platform.txt properties override
out, _, err := cli.Run("board", "details", "-b", "arduino:avr:uno", "--format", "json")
require.NoError(t, err)
jsonOut := requirejson.Parse(t, out)
jsonOut.MustContain(`{
"version": "1.8.6",
"properties_id": "uno",
"build_properties": [
"_id=uno",
"tools.avrdude.bootloader.params.verbose=-v",
"tools.avrdude.cmd.path=/my/personal/avrdude"
],
"programmers": [
{
"platform": "Arduino AVR Boards",
"id": "usbasp",
"name": "USBasp"
},
{
"platform": "Arduino AVR Boards",
"id": "avrispmkii",
"name": "AVRISP mkII"
}
]
}`)
}

{
out, _, err := cli.Run("board", "details", "-b", "arduino:avr:yun", "--show-properties=unexpanded", "--format", "json")
require.NoError(t, err)
jsonOut := requirejson.Parse(t, out)
jsonOut.MustContain(`{
"version": "1.8.6",
"properties_id": "yun",
"build_properties": [
"_id=yun",
"upload.wait_for_upload_port=true",
"preproc.includes.flags=-w -x c++ -M -MG -MP",
"preproc.macros.flags=-w -x c++ -E -CC",
"recipe.preproc.includes=\"{compiler.path}{compiler.cpp.cmd}\" {compiler.cpp.flags} {preproc.includes.flags} -mmcu={build.mcu} -DF_CPU={build.f_cpu} -DARDUINO={runtime.ide.version} -DARDUINO_{build.board} -DARDUINO_ARCH_{build.arch} {compiler.cpp.extra_flags} {build.extra_flags} {includes} \"{source_file}\""
]
}`)
jsonOut.Query(`isempty( .build_properties[] | select(startswith("preproc.macros.compatibility_flags")) )`).MustEqual("true")
}
})
}
95 changes: 59 additions & 36 deletions internal/integrationtest/compile_1/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ func TestCompile(t *testing.T) {
{"WithMultipleBuildPropertyFlags", compileWithMultipleBuildPropertyFlags},
{"WithOutputDirFlag", compileWithOutputDirFlag},
{"WithExportBinariesFlag", compileWithExportBinariesFlag},
{"WithCustomBuildPath", compileWithCustomBuildPath},
{"WithExportBinariesEnvVar", compileWithExportBinariesEnvVar},
{"WithExportBinariesConfig", compileWithExportBinariesConfig},
{"WithInvalidUrl", compileWithInvalidUrl},
Expand All @@ -73,6 +72,7 @@ func TestCompile(t *testing.T) {
{"WithRelativeBuildPath", compileWithRelativeBuildPath},
{"WithFakeSecureBootCore", compileWithFakeSecureBootCore},
{"PreprocessFlagDoNotMessUpWithOutput", preprocessFlagDoNotMessUpWithOutput},
{"WithCustomBuildPath", buildWithCustomBuildPath},
}.Run(t, env, cli)
}

Expand Down Expand Up @@ -448,41 +448,6 @@ func compileWithExportBinariesFlag(t *testing.T, env *integrationtest.Environmen
require.FileExists(t, sketchPath.Join("build", fqbn, sketchName+".ino.with_bootloader.hex").String())
}

func compileWithCustomBuildPath(t *testing.T, env *integrationtest.Environment, cli *integrationtest.ArduinoCLI) {
sketchName := "CompileWithBuildPath"
sketchPath := cli.SketchbookDir().Join(sketchName)
defer sketchPath.RemoveAll()
fqbn := "arduino:avr:uno"

// Create a test sketch
_, _, err := cli.Run("sketch", "new", sketchPath.String())
require.NoError(t, err)

// Test the --build-path flag with absolute path
buildPath := cli.DataDir().Join("test_dir", "build_dir")
_, _, err = cli.Run("compile", "-b", fqbn, sketchPath.String(), "--build-path", buildPath.String())
require.NoError(t, err)

// Verifies expected binaries have been built to build_path
require.DirExists(t, buildPath.String())
require.FileExists(t, buildPath.Join(sketchName+".ino.eep").String())
require.FileExists(t, buildPath.Join(sketchName+".ino.elf").String())
require.FileExists(t, buildPath.Join(sketchName+".ino.hex").String())
require.FileExists(t, buildPath.Join(sketchName+".ino.with_bootloader.bin").String())
require.FileExists(t, buildPath.Join(sketchName+".ino.with_bootloader.hex").String())

// Verifies there are no binaries in temp directory
md5 := md5.Sum(([]byte(sketchPath.String())))
sketchPathMd5 := strings.ToUpper(hex.EncodeToString(md5[:]))
require.NotEmpty(t, sketchPathMd5)
buildDir := paths.TempDir().Join("arduino", "sketches", sketchPathMd5)
require.NoFileExists(t, buildDir.Join(sketchName+".ino.eep").String())
require.NoFileExists(t, buildDir.Join(sketchName+".ino.elf").String())
require.NoFileExists(t, buildDir.Join(sketchName+".ino.hex").String())
require.NoFileExists(t, buildDir.Join(sketchName+".ino.with_bootloader.bin").String())
require.NoFileExists(t, buildDir.Join(sketchName+".ino.with_bootloader.hex").String())
}

func compileWithExportBinariesEnvVar(t *testing.T, env *integrationtest.Environment, cli *integrationtest.ArduinoCLI) {
sketchName := "CompileWithExportBinariesEnvVar"
sketchPath := cli.SketchbookDir().Join(sketchName)
Expand Down Expand Up @@ -1204,3 +1169,61 @@ void loop() {
require.NoError(t, err)
require.Equal(t, expected, string(output))
}

func buildWithCustomBuildPath(t *testing.T, env *integrationtest.Environment, cli *integrationtest.ArduinoCLI) {
sketchName := "bare_minimum"
sketchPath := cli.CopySketch(sketchName)
defer sketchPath.RemoveAll()

t.Run("OutsideSketch", func(t *testing.T) {
sketchName := "CompileWithBuildPath"
sketchPath := cli.SketchbookDir().Join(sketchName)
defer sketchPath.RemoveAll()
fqbn := "arduino:avr:uno"

// Create a test sketch
_, _, err := cli.Run("sketch", "new", sketchPath.String())
require.NoError(t, err)

// Test the --build-path flag with absolute path
buildPath := cli.DataDir().Join("test_dir", "build_dir")
_, _, err = cli.Run("compile", "-b", fqbn, sketchPath.String(), "--build-path", buildPath.String())
require.NoError(t, err)

// Verifies expected binaries have been built to build_path
require.DirExists(t, buildPath.String())
require.FileExists(t, buildPath.Join(sketchName+".ino.eep").String())
require.FileExists(t, buildPath.Join(sketchName+".ino.elf").String())
require.FileExists(t, buildPath.Join(sketchName+".ino.hex").String())
require.FileExists(t, buildPath.Join(sketchName+".ino.with_bootloader.bin").String())
require.FileExists(t, buildPath.Join(sketchName+".ino.with_bootloader.hex").String())

// Verifies there are no binaries in temp directory
md5 := md5.Sum(([]byte(sketchPath.String())))
sketchPathMd5 := strings.ToUpper(hex.EncodeToString(md5[:]))
require.NotEmpty(t, sketchPathMd5)
buildDir := paths.TempDir().Join("arduino", "sketches", sketchPathMd5)
require.NoFileExists(t, buildDir.Join(sketchName+".ino.eep").String())
require.NoFileExists(t, buildDir.Join(sketchName+".ino.elf").String())
require.NoFileExists(t, buildDir.Join(sketchName+".ino.hex").String())
require.NoFileExists(t, buildDir.Join(sketchName+".ino.with_bootloader.bin").String())
require.NoFileExists(t, buildDir.Join(sketchName+".ino.with_bootloader.hex").String())
})

t.Run("InsideSketch", func(t *testing.T) {
buildPath := sketchPath.Join("build")

// Run build
_, _, err := cli.Run("compile", "-b", "arduino:avr:uno", "--build-path", buildPath.String(), sketchPath.String())
require.NoError(t, err)
// Run build twice, to verify the build still works when the build directory is present at the start
_, _, err = cli.Run("compile", "-b", "arduino:avr:uno", "--build-path", buildPath.String(), sketchPath.String())
require.NoError(t, err)
})

t.Run("SameAsSektch", func(t *testing.T) {
// Run build
_, _, err := cli.Run("compile", "-b", "arduino:avr:uno", "--build-path", sketchPath.String(), sketchPath.String())
require.Error(t, err)
})
}
Loading

0 comments on commit 1c110e9

Please sign in to comment.