Skip to content

Commit

Permalink
Add unit and integration testing for the sketch recursive load functi…
Browse files Browse the repository at this point in the history
…onality (#462)

* add testcases to cover #358 (Symbolic link as sketch directory) and #424 (folder with .ino extension)

* add test to check error throw on symlink loop detection

* create symlink via test code to ensure cross platform creation

* fixed assert to make the check cross platform

* fixed all symlink check asserts to make the check cross platform

* nitpicking
  • Loading branch information
Roberto Sora committed Oct 30, 2019
1 parent 44d0f4d commit 292277f
Show file tree
Hide file tree
Showing 12 changed files with 121 additions and 1 deletion.
42 changes: 42 additions & 0 deletions arduino/builder/sketch_test.go
Expand Up @@ -82,6 +82,48 @@ func TestLoadSketchFolder(t *testing.T) {
require.Equal(t, "helper.h", filepath.Base(s.AdditionalFiles[2].Path))
}

func TestLoadSketchFolderSymlink(t *testing.T) {
// pass the path to the sketch folder
symlinkSketchPath := filepath.Join("testdata", t.Name())
srcSketchPath := t.Name() + "Src"
os.Symlink(srcSketchPath, symlinkSketchPath)
mainFilePath := filepath.Join(symlinkSketchPath, t.Name()+".ino")
s, err := builder.SketchLoad(symlinkSketchPath, "")
require.Nil(t, err)
require.NotNil(t, s)
require.Equal(t, mainFilePath, s.MainFile.Path)
require.Equal(t, symlinkSketchPath, s.LocationPath)
require.Len(t, s.OtherSketchFiles, 2)
require.Equal(t, "old.pde", filepath.Base(s.OtherSketchFiles[0].Path))
require.Equal(t, "other.ino", filepath.Base(s.OtherSketchFiles[1].Path))
require.Len(t, s.AdditionalFiles, 3)
require.Equal(t, "header.h", filepath.Base(s.AdditionalFiles[0].Path))
require.Equal(t, "s_file.S", filepath.Base(s.AdditionalFiles[1].Path))
require.Equal(t, "helper.h", filepath.Base(s.AdditionalFiles[2].Path))

// pass the path to the main file
symlinkSketchPath = mainFilePath
s, err = builder.SketchLoad(symlinkSketchPath, "")
require.Nil(t, err)
require.NotNil(t, s)
require.Equal(t, mainFilePath, s.MainFile.Path)
require.Len(t, s.OtherSketchFiles, 2)
require.Equal(t, "old.pde", filepath.Base(s.OtherSketchFiles[0].Path))
require.Equal(t, "other.ino", filepath.Base(s.OtherSketchFiles[1].Path))
require.Len(t, s.AdditionalFiles, 3)
require.Equal(t, "header.h", filepath.Base(s.AdditionalFiles[0].Path))
require.Equal(t, "s_file.S", filepath.Base(s.AdditionalFiles[1].Path))
require.Equal(t, "helper.h", filepath.Base(s.AdditionalFiles[2].Path))
}

func TestLoadSketchFolderIno(t *testing.T) {
// pass the path to the sketch folder
sketchPath := filepath.Join("testdata", t.Name())
_, err := builder.SketchLoad(sketchPath, "")
require.Error(t, err)
require.Contains(t, err.Error(), "sketch must not be a directory")
}

func TestLoadSketchFolderWrongMain(t *testing.T) {
sketchPath := filepath.Join("testdata", t.Name())
_, err := builder.SketchLoad(sketchPath, "")
Expand Down
@@ -0,0 +1,7 @@
void setup() {

}

void loop() {

}
@@ -0,0 +1,2 @@
void setup()
void loop) }
@@ -0,0 +1,7 @@
void setup() {

}

void loop() {

}
Empty file.
@@ -0,0 +1 @@
#define FOO "BAR"
Empty file.
@@ -0,0 +1,3 @@
String hello() {
return "world";
}
Empty file.
@@ -0,0 +1,2 @@
#include <testlib4.h>
#error "Whattya looking at?"
Empty file.
58 changes: 57 additions & 1 deletion test/test_compile.py
Expand Up @@ -39,7 +39,7 @@ def test_compile_with_simple_sketch(run_command, data_dir):
result = run_command("core update-index")
assert result.ok

# # Download latest AVR
# Download latest AVR
result = run_command("core install arduino:avr")
assert result.ok

Expand Down Expand Up @@ -70,6 +70,62 @@ def test_compile_with_simple_sketch(run_command, data_dir):
assert is_message_sequence_in_json_log_traces(expected_trace_sequence, json_log_lines)


def test_compile_with_sketch_with_symlink_selfloop(run_command, data_dir):
# Init the environment explicitly
result = run_command("core update-index")
assert result.ok

# Download latest AVR
result = run_command("core install arduino:avr")
assert result.ok

sketch_name = "CompileIntegrationTestSymlinkSelfLoop"
sketch_path = os.path.join(data_dir, sketch_name)
fqbn = "arduino:avr:uno"

# Create a test sketch
result = run_command("sketch new {}".format(sketch_path))
assert result.ok
assert "Sketch created in: {}".format(sketch_path) in result.stdout

# create a symlink that loops on himself
loop_file_path = os.path.join(sketch_path, "loop")
os.symlink(loop_file_path, loop_file_path)

# Build sketch for arduino:avr:uno
result = run_command(
"compile -b {fqbn} {sketch_path}".format(
fqbn=fqbn, sketch_path=sketch_path))
# The assertion is a bit relaxed in this case because win behaves differently from macOs and linux
# returning a different error detailed message
assert "Error during sketch processing" in result.stderr
assert not result.ok

sketch_name = "CompileIntegrationTestSymlinkDirLoop"
sketch_path = os.path.join(data_dir, sketch_name)
fqbn = "arduino:avr:uno"

# Create a test sketch
result = run_command("sketch new {}".format(sketch_path))
assert result.ok
assert "Sketch created in: {}".format(sketch_path) in result.stdout

# create a symlink that loops on the upper level
loop_dir_path = os.path.join(sketch_path, "loop_dir")
os.mkdir(loop_dir_path)
loop_dir_symlink_path = os.path.join(loop_dir_path, "loop_dir_symlink")
os.symlink(loop_dir_path, loop_dir_symlink_path)

# Build sketch for arduino:avr:uno
result = run_command(
"compile -b {fqbn} {sketch_path}".format(
fqbn=fqbn, sketch_path=sketch_path))
# The assertion is a bit relaxed also in this case because macOS behaves differently from win and linux:
# the cli does not follow recursively the symlink til breaking
assert "Error during sketch processing" in result.stderr
assert not result.ok


@pytest.mark.skipif(running_on_ci(), reason="VMs have no serial ports")
def test_compile_and_compile_combo(run_command, data_dir):
# Init the environment explicitly
Expand Down

0 comments on commit 292277f

Please sign in to comment.