|
| 1 | +// This file is part of arduino-cli. |
| 2 | +// |
| 3 | +// Copyright 2019 ARDUINO SA (http://www.arduino.cc/) |
| 4 | +// |
| 5 | +// This software is released under the GNU General Public License version 3, |
| 6 | +// which covers the main part of arduino-cli. |
| 7 | +// The terms of this license can be found at: |
| 8 | +// https://www.gnu.org/licenses/gpl-3.0.en.html |
| 9 | +// |
| 10 | +// You can be released from the requirements of the above licenses by purchasing |
| 11 | +// a commercial license. Buying such a license is mandatory if you want to modify or |
| 12 | +// otherwise use the software for commercial activities involving the Arduino |
| 13 | +// software without disclosing the source code of your own applications. To purchase |
| 14 | +// a commercial license, send an email to [email protected]. |
| 15 | + |
| 16 | +package builder_test |
| 17 | + |
| 18 | +import ( |
| 19 | + "io/ioutil" |
| 20 | + "os" |
| 21 | + "path/filepath" |
| 22 | + "strings" |
| 23 | + "testing" |
| 24 | + |
| 25 | + "github.com/arduino/arduino-cli/arduino/builder" |
| 26 | + "github.com/arduino/arduino-cli/arduino/sketch" |
| 27 | + "github.com/stretchr/testify/assert" |
| 28 | +) |
| 29 | + |
| 30 | +func TestSaveSketch(t *testing.T) { |
| 31 | + sketchName := t.Name() + ".ino" |
| 32 | + outName := sketchName + ".cpp" |
| 33 | + sketchFile := filepath.Join("testdata", sketchName) |
| 34 | + tmp := tmpDirOrDie() |
| 35 | + defer os.RemoveAll(tmp) |
| 36 | + source, err := ioutil.ReadFile(sketchFile) |
| 37 | + if err != nil { |
| 38 | + t.Fatalf("unable to read golden file %s: %v", sketchFile, err) |
| 39 | + } |
| 40 | + |
| 41 | + builder.SaveSketchItemCpp(&sketch.Item{Path: sketchName, Source: source}, tmp) |
| 42 | + |
| 43 | + out, err := ioutil.ReadFile(filepath.Join(tmp, outName)) |
| 44 | + if err != nil { |
| 45 | + t.Fatalf("unable to read output file %s: %v", outName, err) |
| 46 | + } |
| 47 | + |
| 48 | + assert.Equal(t, source, out) |
| 49 | +} |
| 50 | + |
| 51 | +func TestLoadSketchFolder(t *testing.T) { |
| 52 | + // pass the path to the sketch folder |
| 53 | + sketchPath := filepath.Join("testdata", t.Name()) |
| 54 | + mainFilePath := filepath.Join(sketchPath, t.Name()+".ino") |
| 55 | + s, err := builder.LoadSketch(sketchPath, "") |
| 56 | + assert.Nil(t, err) |
| 57 | + assert.NotNil(t, s) |
| 58 | + assert.Equal(t, mainFilePath, s.MainFile.Path) |
| 59 | + assert.Equal(t, sketchPath, s.LocationPath) |
| 60 | + assert.Len(t, s.OtherSketchFiles, 2) |
| 61 | + assert.Equal(t, "old.pde", filepath.Base(s.OtherSketchFiles[0].Path)) |
| 62 | + assert.Equal(t, "other.ino", filepath.Base(s.OtherSketchFiles[1].Path)) |
| 63 | + assert.Len(t, s.AdditionalFiles, 3) |
| 64 | + assert.Equal(t, "header.h", filepath.Base(s.AdditionalFiles[0].Path)) |
| 65 | + assert.Equal(t, "s_file.S", filepath.Base(s.AdditionalFiles[1].Path)) |
| 66 | + assert.Equal(t, "helper.h", filepath.Base(s.AdditionalFiles[2].Path)) |
| 67 | + |
| 68 | + // pass the path to the main file |
| 69 | + sketchPath = mainFilePath |
| 70 | + s, err = builder.LoadSketch(sketchPath, "") |
| 71 | + assert.Nil(t, err) |
| 72 | + assert.NotNil(t, s) |
| 73 | + assert.Equal(t, mainFilePath, s.MainFile.Path) |
| 74 | + assert.Len(t, s.OtherSketchFiles, 2) |
| 75 | + assert.Equal(t, "old.pde", filepath.Base(s.OtherSketchFiles[0].Path)) |
| 76 | + assert.Equal(t, "other.ino", filepath.Base(s.OtherSketchFiles[1].Path)) |
| 77 | + assert.Len(t, s.AdditionalFiles, 3) |
| 78 | + assert.Equal(t, "header.h", filepath.Base(s.AdditionalFiles[0].Path)) |
| 79 | + assert.Equal(t, "s_file.S", filepath.Base(s.AdditionalFiles[1].Path)) |
| 80 | + assert.Equal(t, "helper.h", filepath.Base(s.AdditionalFiles[2].Path)) |
| 81 | +} |
| 82 | + |
| 83 | +func TestLoadSketchFolderWrongMain(t *testing.T) { |
| 84 | + sketchPath := filepath.Join("testdata", t.Name()) |
| 85 | + _, err := builder.LoadSketch(sketchPath, "") |
| 86 | + assert.Error(t, err) |
| 87 | + assert.Contains(t, err.Error(), "unable to find the main sketch file") |
| 88 | + |
| 89 | + _, err = builder.LoadSketch("does/not/exist", "") |
| 90 | + assert.Error(t, err) |
| 91 | + assert.Contains(t, err.Error(), "no such file or directory") |
| 92 | +} |
| 93 | + |
| 94 | +func TestMergeSketchSources(t *testing.T) { |
| 95 | + // borrow the sketch from TestLoadSketchFolder to avoid boilerplate |
| 96 | + s, err := builder.LoadSketch(filepath.Join("testdata", "TestLoadSketchFolder"), "") |
| 97 | + assert.Nil(t, err) |
| 98 | + assert.NotNil(t, s) |
| 99 | + |
| 100 | + // load expected result |
| 101 | + mergedPath := filepath.Join("testdata", t.Name()+".txt") |
| 102 | + mergedBytes, err := ioutil.ReadFile(mergedPath) |
| 103 | + if err != nil { |
| 104 | + t.Fatalf("unable to read golden file %s: %v", mergedPath, err) |
| 105 | + } |
| 106 | + |
| 107 | + offset, source := builder.MergeSketchSources(s) |
| 108 | + assert.Equal(t, 2, offset) |
| 109 | + assert.Equal(t, string(mergedBytes), source) |
| 110 | +} |
| 111 | + |
| 112 | +func TestMergeSketchSourcesArduinoIncluded(t *testing.T) { |
| 113 | + s, err := builder.LoadSketch(filepath.Join("testdata", t.Name()), "") |
| 114 | + assert.Nil(t, err) |
| 115 | + assert.NotNil(t, s) |
| 116 | + |
| 117 | + // ensure not to include Arduino.h when it's already there |
| 118 | + _, source := builder.MergeSketchSources(s) |
| 119 | + assert.Equal(t, 1, strings.Count(source, "<Arduino.h>")) |
| 120 | +} |
0 commit comments