|
| 1 | +// This file is part of arduino-cli. |
| 2 | +// |
| 3 | +// Copyright 2020-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 license@arduino.cc. |
| 15 | + |
| 16 | +package sketch |
| 17 | + |
| 18 | +import ( |
| 19 | + "fmt" |
| 20 | + "strings" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/arduino/go-paths-helper" |
| 24 | + "github.com/stretchr/testify/require" |
| 25 | +) |
| 26 | + |
| 27 | +func TestYamlUpdate(t *testing.T) { |
| 28 | + { |
| 29 | + sample, err := paths.New("testdata", "SketchWithProfiles", "sketch.yml").ReadFile() |
| 30 | + require.NoError(t, err) |
| 31 | + tmp, err := paths.WriteToTempFile(sample, nil, "") |
| 32 | + require.NoError(t, err) |
| 33 | + defer tmp.Remove() |
| 34 | + |
| 35 | + err = updateOrAddYamlRootEntry(tmp, "default_fqbn", "arduino:avr:uno") |
| 36 | + require.NoError(t, err) |
| 37 | + err = updateOrAddYamlRootEntry(tmp, "default_port", "/dev/ttyACM0") |
| 38 | + require.NoError(t, err) |
| 39 | + |
| 40 | + updated, err := tmp.ReadFile() |
| 41 | + require.NoError(t, err) |
| 42 | + expected := string(sample) + fmt.Sprintln() |
| 43 | + expected += fmt.Sprintln("default_fqbn: arduino:avr:uno") |
| 44 | + expected += "default_port: /dev/ttyACM0" |
| 45 | + require.Equal(t, expected, string(updated)) |
| 46 | + } |
| 47 | + { |
| 48 | + sample, err := paths.New("testdata", "SketchWithDefaultFQBNAndPort", "sketch.yml").ReadFile() |
| 49 | + require.NoError(t, err) |
| 50 | + tmp, err := paths.WriteToTempFile(sample, nil, "") |
| 51 | + require.NoError(t, err) |
| 52 | + defer tmp.Remove() |
| 53 | + |
| 54 | + err = updateOrAddYamlRootEntry(tmp, "default_fqbn", "TEST1") |
| 55 | + require.NoError(t, err) |
| 56 | + err = updateOrAddYamlRootEntry(tmp, "default_port", "TEST2") |
| 57 | + require.NoError(t, err) |
| 58 | + |
| 59 | + updated, err := tmp.ReadFile() |
| 60 | + fmt.Print(string(updated)) |
| 61 | + require.NoError(t, err) |
| 62 | + expected := strings.Replace(string(sample), "arduino:avr:uno", "TEST1", 1) |
| 63 | + expected = strings.Replace(expected, "/dev/ttyACM0", "TEST2", 1) |
| 64 | + require.Equal(t, expected, string(updated)) |
| 65 | + } |
| 66 | + { |
| 67 | + tmp, err := paths.WriteToTempFile([]byte{}, nil, "") |
| 68 | + require.NoError(t, err) |
| 69 | + require.NoError(t, tmp.Remove()) |
| 70 | + err = updateOrAddYamlRootEntry(tmp, "default_fqbn", "TEST1") |
| 71 | + require.NoError(t, err) |
| 72 | + |
| 73 | + updated, err := tmp.ReadFile() |
| 74 | + require.NoError(t, err) |
| 75 | + expected := "default_fqbn: TEST1" |
| 76 | + require.Equal(t, expected, string(updated)) |
| 77 | + } |
| 78 | +} |
0 commit comments