|
| 1 | +// This file is part of arduino-cli. |
| 2 | +// |
| 3 | +// Copyright 2022 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 |
| 12 | +// modify or otherwise use the software for commercial activities involving the |
| 13 | +// Arduino software without disclosing the source code of your own applications. |
| 14 | +// To purchase a commercial license, send an email to [email protected]. |
| 15 | + |
| 16 | +package compile_test |
| 17 | + |
| 18 | +import ( |
| 19 | + "encoding/json" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/arduino/arduino-cli/internal/integrationtest" |
| 23 | + "github.com/arduino/go-paths-helper" |
| 24 | + "github.com/stretchr/testify/require" |
| 25 | + "go.bug.st/testifyjson/requirejson" |
| 26 | +) |
| 27 | + |
| 28 | +func TestCompileCommandsJSONGeneration(t *testing.T) { |
| 29 | + // See: https://github.com/arduino/arduino-cli/issues/2401 |
| 30 | + |
| 31 | + env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t) |
| 32 | + defer env.CleanUp() |
| 33 | + |
| 34 | + // Run update-index with our test index |
| 35 | + _, _, err := cli. Run( "core", "install", "arduino:[email protected]") |
| 36 | + require.NoError(t, err) |
| 37 | + |
| 38 | + // Create a test sketch |
| 39 | + out, _, err := cli.Run("sketch", "new", "Test", "--format", "json") |
| 40 | + require.NoError(t, err) |
| 41 | + var s struct { |
| 42 | + Path string `json:"sketch_path"` |
| 43 | + } |
| 44 | + require.NoError(t, json.Unmarshal(out, &s)) |
| 45 | + sketchPath := paths.New(s.Path) |
| 46 | + buildPath := sketchPath.Join("build") |
| 47 | + |
| 48 | + { |
| 49 | + // Normal build |
| 50 | + _, _, err = cli.Run( |
| 51 | + "compile", |
| 52 | + "-b", "arduino:avr:uno", |
| 53 | + "--build-path", buildPath.String(), |
| 54 | + sketchPath.String()) |
| 55 | + require.NoError(t, err) |
| 56 | + |
| 57 | + compileCommandsPath := buildPath.Join("compile_commands.json") |
| 58 | + require.True(t, compileCommandsPath.Exist()) |
| 59 | + compileCommandJson, err := compileCommandsPath.ReadFile() |
| 60 | + require.NoError(t, err) |
| 61 | + compileCommands := requirejson.Parse(t, compileCommandJson) |
| 62 | + // Check that the variant include path is present, one of the arguments must be |
| 63 | + // something like: |
| 64 | + // "-I/home/user/.arduino15/packages/arduino/hardware/avr/1.8.6/variants/standard" |
| 65 | + compileCommands.Query(`[ .[0].arguments[] | contains("standard") ] | any`).MustEqual(`true`) |
| 66 | + } |
| 67 | + |
| 68 | + { |
| 69 | + // Build with skip-library-check |
| 70 | + _, _, err = cli.Run( |
| 71 | + "compile", |
| 72 | + "-b", "arduino:avr:uno", |
| 73 | + "--only-compilation-database", |
| 74 | + "--skip-libraries-discovery", |
| 75 | + "--build-path", buildPath.String(), |
| 76 | + sketchPath.String()) |
| 77 | + require.NoError(t, err) |
| 78 | + |
| 79 | + compileCommandsPath := buildPath.Join("compile_commands.json") |
| 80 | + require.True(t, compileCommandsPath.Exist()) |
| 81 | + compileCommandJson, err := compileCommandsPath.ReadFile() |
| 82 | + require.NoError(t, err) |
| 83 | + compileCommands := requirejson.Parse(t, compileCommandJson) |
| 84 | + // Check that the variant include path is present, one of the arguments must be |
| 85 | + // something like: |
| 86 | + // "-I/home/user/.arduino15/packages/arduino/hardware/avr/1.8.6/variants/standard" |
| 87 | + compileCommands.Query(`[ .[0].arguments[] | contains("standard") ] | any`).MustEqual(`true`) |
| 88 | + } |
| 89 | +} |
0 commit comments