Skip to content

Commit

Permalink
When sketch has no functions, don't try generating prototypes. Fixes #1
Browse files Browse the repository at this point in the history
Signed-off-by: Federico Fissore <[email protected]>
  • Loading branch information
Federico Fissore committed Sep 14, 2015
1 parent 2361bc2 commit de283b9
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/arduino.cc/builder/prototypes_adder.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,18 @@ type PrototypesAdder struct{}

func (s *PrototypesAdder) Run(context map[string]interface{}) error {
source := context[constants.CTX_SOURCE].(string)
sourceRows := strings.Split(source, "\n")

if !utils.MapHas(context, constants.CTX_FIRST_FUNCTION_AT_LINE) {
return nil
}

firstFunctionLine := context[constants.CTX_FIRST_FUNCTION_AT_LINE].(int)
firstFunctionChar := len(strings.Join(strings.Split(source, "\n")[:firstFunctionLine-1], "\n")) + 1
if firstFunctionOutsideOfSource(firstFunctionLine, sourceRows) {
return nil
}

firstFunctionChar := len(strings.Join(sourceRows[:firstFunctionLine-1], "\n")) + 1
if firstFunctionLine > 1 {
firstFunctionLine -= context[constants.CTX_LINE_OFFSET].(int)
}
Expand Down Expand Up @@ -82,3 +87,7 @@ func composeIncludeArduinoSection() string {

return str
}

func firstFunctionOutsideOfSource(firstFunctionLine int, sourceRows []string) bool {
return firstFunctionLine > len(sourceRows)-1
}
21 changes: 21 additions & 0 deletions src/arduino.cc/builder/test/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ package test
import (
"arduino.cc/builder"
"arduino.cc/builder/constants"
"github.com/stretchr/testify/require"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -243,3 +244,23 @@ func TestBuilderBridgeRedBearLab(t *testing.T) {
_, err = os.Stat(filepath.Join(buildPath, constants.FOLDER_LIBRARIES, "Bridge", "Mailbox.cpp.o"))
NoError(t, err)
}

func TestBuilderSketchNoFunctions(t *testing.T) {
DownloadCoresAndToolsAndLibraries(t)

context := make(map[string]interface{})

buildPath := SetupBuildPath(t, context)
defer os.RemoveAll(buildPath)

context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware", "downloaded_board_manager_stuff"}
context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools", "downloaded_board_manager_stuff"}
context[constants.CTX_FQBN] = "RedBearLab:avr:blend"
context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch_no_functions", "main.ino")
context[constants.CTX_LIBRARIES_FOLDERS] = []string{"libraries", "downloaded_libraries"}
context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600"

command := builder.Builder{}
err := command.Run(context)
require.Error(t, err)
}
80 changes: 80 additions & 0 deletions src/arduino.cc/builder/test/prototypes_adder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,3 +435,83 @@ func TestPrototypesAdderSketchWithConfig(t *testing.T) {

require.Equal(t, preprocessed, strings.Replace(context[constants.CTX_SOURCE].(string), "\r\n", "\n", -1))
}

func TestPrototypesAdderSketchNoFunctionsTwoFiles(t *testing.T) {
DownloadCoresAndToolsAndLibraries(t)

context := make(map[string]interface{})

buildPath := SetupBuildPath(t, context)
defer os.RemoveAll(buildPath)

context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}
context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"}
context[constants.CTX_FQBN] = "arduino:avr:leonardo"
context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch_no_functions_two_files", "main.ino")
context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600"
context[constants.CTX_LIBRARIES_FOLDERS] = []string{"libraries", "downloaded_libraries"}
context[constants.CTX_VERBOSE] = true

commands := []types.Command{
&builder.SetupHumanLoggerIfMissing{},

&builder.ContainerSetupHardwareToolsLibsSketchAndProps{},

&builder.ContainerMergeCopySketchFiles{},

&builder.ContainerFindIncludes{},

&builder.PrintUsedLibrariesIfVerbose{},
&builder.WarnAboutArchIncompatibleLibraries{},

&builder.ContainerAddPrototypes{},
}

for _, command := range commands {
err := command.Run(context)
NoError(t, err)
}

require.Nil(t, context[constants.CTX_INCLUDE_SECTION])
require.Nil(t, context[constants.CTX_PROTOTYPE_SECTION])
}

func TestPrototypesAdderSketchNoFunctions(t *testing.T) {
DownloadCoresAndToolsAndLibraries(t)

context := make(map[string]interface{})

buildPath := SetupBuildPath(t, context)
defer os.RemoveAll(buildPath)

context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}
context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"}
context[constants.CTX_FQBN] = "arduino:avr:leonardo"
context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch_no_functions", "main.ino")
context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600"
context[constants.CTX_LIBRARIES_FOLDERS] = []string{"libraries", "downloaded_libraries"}
context[constants.CTX_VERBOSE] = true

commands := []types.Command{
&builder.SetupHumanLoggerIfMissing{},

&builder.ContainerSetupHardwareToolsLibsSketchAndProps{},

&builder.ContainerMergeCopySketchFiles{},

&builder.ContainerFindIncludes{},

&builder.PrintUsedLibrariesIfVerbose{},
&builder.WarnAboutArchIncompatibleLibraries{},

&builder.ContainerAddPrototypes{},
}

for _, command := range commands {
err := command.Run(context)
NoError(t, err)
}

require.Nil(t, context[constants.CTX_INCLUDE_SECTION])
require.Nil(t, context[constants.CTX_PROTOTYPE_SECTION])
}
8 changes: 8 additions & 0 deletions src/arduino.cc/builder/test/sketch_no_functions/main.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
Test sketch
*/

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
Test sketch
*/

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#include <SPI.h>

int a = 0;
int b = 0;

0 comments on commit de283b9

Please sign in to comment.