Skip to content

Commit ce0c7b6

Browse files
author
rsora
committed
Add test for debug recipe generation
1 parent 439b64b commit ce0c7b6

File tree

7 files changed

+436
-4
lines changed

7 files changed

+436
-4
lines changed

commands/debug/debug.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"time"
2626

2727
"github.com/arduino/arduino-cli/arduino/cores"
28+
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
2829
"github.com/arduino/arduino-cli/arduino/sketches"
2930
"github.com/arduino/arduino-cli/commands"
3031
"github.com/arduino/go-paths-helper"
@@ -44,7 +45,8 @@ import (
4445
func Debug(ctx context.Context, req *dbg.DebugConfigReq, inStream io.Reader, out io.Writer) (*dbg.DebugResp, error) {
4546

4647
// get tool commandLine from core recipe
47-
commandLine, err := getCommandLine(req)
48+
pm := commands.GetPackageManager(req.GetInstance().GetId())
49+
commandLine, err := getCommandLine(req, pm)
4850
if err != nil {
4951
return nil, fmt.Errorf("cannot get command line for tool: %s", err)
5052
}
@@ -90,7 +92,7 @@ func Debug(ctx context.Context, req *dbg.DebugConfigReq, inStream io.Reader, out
9092
}
9193

9294
// getCommandLine compose a debug command represented by a core recipe
93-
func getCommandLine(req *dbg.DebugConfigReq) ([]string, error) {
95+
func getCommandLine(req *dbg.DebugConfigReq, pm *packagemanager.PackageManager) ([]string, error) {
9496
// TODO: make a generic function to extract sketch from request
9597
// and remove duplication in commands/compile.go
9698
if req.GetSketchPath() == "" {
@@ -120,8 +122,6 @@ func getCommandLine(req *dbg.DebugConfigReq) ([]string, error) {
120122
return nil, fmt.Errorf("incorrect FQBN: %s", err)
121123
}
122124

123-
pm := commands.GetPackageManager(req.GetInstance().GetId())
124-
125125
// Find target board and board properties
126126
_, _, board, boardProperties, _, err := pm.ResolveFQBN(fqbn)
127127
if err != nil {

commands/debug/debug_test.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2020 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 license@arduino.cc.
15+
16+
package debug
17+
18+
import (
19+
"fmt"
20+
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
21+
rpc "github.com/arduino/arduino-cli/rpc/common"
22+
dbg "github.com/arduino/arduino-cli/rpc/debug"
23+
"github.com/arduino/go-paths-helper"
24+
"github.com/stretchr/testify/assert"
25+
"strings"
26+
"testing"
27+
)
28+
29+
var customHardware = paths.New("testdata", "custom_hardware")
30+
var dataDir = paths.New("testdata", "data_dir", "packages")
31+
var sketch = "hello"
32+
var sketchPath = paths.New("testdata", sketch).String()
33+
34+
func TestGetCommandLine(t *testing.T) {
35+
pm := packagemanager.NewPackageManager(nil, nil, nil, nil)
36+
pm.LoadHardwareFromDirectory(customHardware)
37+
pm.LoadHardwareFromDirectory(dataDir)
38+
39+
req := &dbg.DebugConfigReq{
40+
Instance: &rpc.Instance{Id: 1},
41+
Fqbn: "arduino-test:samd:arduino_zero_edbg",
42+
SketchPath: sketchPath,
43+
Port: "none",
44+
}
45+
packageName := strings.Split(req.Fqbn, ":")[0]
46+
processor := strings.Split(req.Fqbn, ":")[1]
47+
boardFamily := "arduino_zero"
48+
49+
goldCommand := []string{
50+
fmt.Sprintf("%s/%s/tools/arm-none-eabi-gcc/7-2017q4/bin//arm-none-eabi-gdb", dataDir, packageName),
51+
fmt.Sprintf("-ex"),
52+
fmt.Sprintf("target extended-remote | %s/%s/tools/openocd/0.10.0-arduino7/bin/openocd", dataDir, packageName) + " " +
53+
fmt.Sprintf("-s \"%s/%s/tools/openocd/0.10.0-arduino7/share/openocd/scripts/\"", dataDir, packageName) + " " +
54+
fmt.Sprintf("--file \"%s/%s/%s/variants/%s/openocd_scripts/arduino_zero.cfg\" -c \"gdb_port pipe\" -c \"telnet_port 0\" -c init -c halt", customHardware, packageName, processor, boardFamily),
55+
fmt.Sprintf("%s/%s.%s.elf", sketchPath, sketch, strings.ReplaceAll(req.Fqbn, ":", ".")),
56+
}
57+
58+
command, err := getCommandLine(req, pm)
59+
assert.Nil(t, err)
60+
assert.Equal(t, goldCommand, command)
61+
62+
// for other samd boards
63+
req2 := &dbg.DebugConfigReq{
64+
Instance: &rpc.Instance{Id: 1},
65+
Fqbn: "arduino-test:samd:mkr1000",
66+
SketchPath: sketchPath,
67+
Port: "none",
68+
}
69+
packageName2 := strings.Split(req2.Fqbn, ":")[0]
70+
processor2 := strings.Split(req2.Fqbn, ":")[1]
71+
name2 := strings.Split(req2.Fqbn, ":")[2]
72+
73+
goldCommand2 := []string{
74+
fmt.Sprintf("%s/%s/tools/arm-none-eabi-gcc/7-2017q4/bin//arm-none-eabi-gdb", dataDir, packageName2),
75+
fmt.Sprintf("-ex"),
76+
fmt.Sprintf("target extended-remote | %s/%s/tools/openocd/0.10.0-arduino7/bin/openocd", dataDir, packageName2) + " " +
77+
fmt.Sprintf("-s \"%s/%s/tools/openocd/0.10.0-arduino7/share/openocd/scripts/\"", dataDir, packageName2) + " " +
78+
fmt.Sprintf("--file \"%s/%s/%s/variants/%s/openocd_scripts/arduino_zero.cfg\" -c \"gdb_port pipe\" -c \"telnet_port 0\" -c init -c halt", customHardware, packageName2, processor2, name2),
79+
fmt.Sprintf("%s/%s.%s.elf", sketchPath, sketch, strings.ReplaceAll(req2.Fqbn, ":", ".")),
80+
}
81+
82+
command2, err := getCommandLine(req2, pm)
83+
assert.Nil(t, err)
84+
assert.Equal(t, goldCommand2, command2)
85+
86+
}

commands/debug/testdata/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
!arduino
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# Copyright (c) 2014-2017 Arduino LLC. All right reserved.
2+
#
3+
# This library is free software; you can redistribute it and/or
4+
# modify it under the terms of the GNU Lesser General Public
5+
# License as published by the Free Software Foundation; either
6+
# version 2.1 of the License, or (at your option) any later version.
7+
#
8+
# This library is distributed in the hope that it will be useful,
9+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11+
# See the GNU Lesser General Public License for more details.
12+
#
13+
# You should have received a copy of the GNU Lesser General Public
14+
# License along with this library; if not, write to the Free Software
15+
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16+
17+
# Arduino Zero (Prorgamming Port)
18+
# ---------------------------------------
19+
arduino_zero_edbg.name=Arduino Zero (Programming Port)
20+
arduino_zero_edbg.vid.0=0x03eb
21+
arduino_zero_edbg.pid.0=0x2157
22+
23+
arduino_zero_edbg.debug.tool=gdb
24+
arduino_zero_edbg.upload.tool=openocd
25+
arduino_zero_edbg.upload.protocol=sam-ba
26+
arduino_zero_edbg.upload.maximum_size=262144
27+
arduino_zero_edbg.upload.use_1200bps_touch=false
28+
arduino_zero_edbg.upload.wait_for_upload_port=false
29+
arduino_zero_edbg.upload.native_usb=false
30+
arduino_zero_edbg.build.mcu=cortex-m0plus
31+
arduino_zero_edbg.build.f_cpu=48000000L
32+
arduino_zero_edbg.build.usb_product="Arduino Zero"
33+
arduino_zero_edbg.build.usb_manufacturer="Arduino LLC"
34+
arduino_zero_edbg.build.board=SAMD_ZERO
35+
arduino_zero_edbg.build.core=arduino
36+
arduino_zero_edbg.build.extra_flags=-D__SAMD21G18A__ {build.usb_flags}
37+
arduino_zero_edbg.build.ldscript=linker_scripts/gcc/flash_with_bootloader.ld
38+
arduino_zero_edbg.build.openocdscript=openocd_scripts/arduino_zero.cfg
39+
arduino_zero_edbg.build.variant=arduino_zero
40+
arduino_zero_edbg.build.variant_system_lib=
41+
arduino_zero_edbg.build.vid=0x2341
42+
arduino_zero_edbg.build.pid=0x804d
43+
arduino_zero_edbg.bootloader.tool=openocd
44+
arduino_zero_edbg.bootloader.file=zero/samd21_sam_ba.bin
45+
46+
# Arduino MKR1000
47+
# -----------------------
48+
mkr1000.name=Arduino MKR1000
49+
mkr1000.vid.0=0x2341
50+
mkr1000.pid.0=0x804e
51+
mkr1000.vid.1=0x2341
52+
mkr1000.pid.1=0x004e
53+
mkr1000.vid.2=0x2341
54+
mkr1000.pid.2=0x824e
55+
mkr1000.vid.3=0x2341
56+
mkr1000.pid.3=0x024e
57+
58+
mkr1000.debug.tool=gdb
59+
mkr1000.upload.tool=bossac
60+
mkr1000.upload.protocol=sam-ba
61+
mkr1000.upload.maximum_size=262144
62+
mkr1000.upload.use_1200bps_touch=true
63+
mkr1000.upload.wait_for_upload_port=true
64+
mkr1000.upload.native_usb=true
65+
mkr1000.build.mcu=cortex-m0plus
66+
mkr1000.build.f_cpu=48000000L
67+
mkr1000.build.usb_product="Arduino MKR1000"
68+
mkr1000.build.usb_manufacturer="Arduino LLC"
69+
mkr1000.build.board=SAMD_MKR1000
70+
mkr1000.build.core=arduino
71+
mkr1000.build.extra_flags=-DUSE_ARDUINO_MKR_PIN_LAYOUT -D__SAMD21G18A__ {build.usb_flags}
72+
mkr1000.build.ldscript=linker_scripts/gcc/flash_with_bootloader.ld
73+
mkr1000.build.openocdscript=openocd_scripts/arduino_zero.cfg
74+
mkr1000.build.variant=mkr1000
75+
mkr1000.build.vid=0x2341
76+
mkr1000.build.pid=0x804e
77+
mkr1000.bootloader.tool=openocd
78+
mkr1000.bootloader.file=mkr1000/samd21_sam_ba_arduino_mkr1000.bin
79+
80+
# Arduino Tian (with) Bootloader
81+
# ------------------------------
82+
tian.name=Arduino Tian
83+
tian.upload.via_ssh=true
84+
tian.vid.0=0x10C4
85+
tian.pid.0=0xEA70
86+
tian.descriptor.0=Enhanced Com Port
87+
88+
tian.upload.tool=avrdude
89+
#tian.upload.protocol=stk500v2
90+
tian.upload.protocol=wiring
91+
tian.upload.maximum_size=262144
92+
tian.upload.use_1200bps_touch=true
93+
tian.upload.wait_for_upload_port=true
94+
tian.upload.native_usb=true
95+
tian.upload.speed=57600
96+
tian.build.mcu=cortex-m0plus
97+
tian.build.f_cpu=48000000L
98+
tian.build.usb_product="Arduino Tian"
99+
tian.build.board=SAMD_TIAN
100+
tian.build.core=arduino
101+
tian.build.extra_flags=-D__SAMD21G18A__ -mthumb {build.usb_flags}
102+
tian.build.ldscript=linker_scripts/gcc/flash_with_bootloader.ld
103+
tian.build.openocdscript=openocd_scripts/arduino_zero.cfg
104+
tian.build.variant=arduino_mzero
105+
tian.build.variant_system_lib=
106+
tian.build.vid=0x2a03
107+
tian.build.pid=0x8052
108+
tian.build.preferred_out_format=hex
109+
tian.bootloader.size=0x4000
110+
tian.build.emu.mcu=atmega2560
111+
tian.bootloader.tool=openocd-withbootsize
112+
tian.bootloader.low_fuses=0xff
113+
tian.bootloader.file=sofia/Sofia_Tian_151118.hex
114+
tian.drivers=SiliconLabs-CP2105/Silicon Labs VCP Driver.pkg

0 commit comments

Comments
 (0)