|
| 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 main_test |
| 17 | + |
| 18 | +import ( |
| 19 | + "encoding/json" |
| 20 | + "strings" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/arduino/arduino-cli/internal/integrationtest" |
| 24 | + "github.com/arduino/go-paths-helper" |
| 25 | + "github.com/stretchr/testify/require" |
| 26 | + semver "go.bug.st/relaxed-semver" |
| 27 | + "go.bug.st/testsuite" |
| 28 | + "go.bug.st/testsuite/requirejson" |
| 29 | +) |
| 30 | + |
| 31 | +func TestHelp(t *testing.T) { |
| 32 | + env := testsuite.NewEnvironment(t) |
| 33 | + defer env.CleanUp() |
| 34 | + |
| 35 | + cli := integrationtest.NewArduinoCliWithinEnvironment(env, &integrationtest.ArduinoCLIConfig{ |
| 36 | + ArduinoCLIPath: paths.New("..", "..", "..", "arduino-cli"), |
| 37 | + UseSharedStagingFolder: true, |
| 38 | + }) |
| 39 | + |
| 40 | + // Run help and check the output message |
| 41 | + stdout, stderr, err := cli.Run("help") |
| 42 | + require.NoError(t, err) |
| 43 | + require.Empty(t, stderr) |
| 44 | + require.Contains(t, string(stdout), "Usage") |
| 45 | +} |
| 46 | + |
| 47 | +func TestVersion(t *testing.T) { |
| 48 | + env := testsuite.NewEnvironment(t) |
| 49 | + defer env.CleanUp() |
| 50 | + |
| 51 | + cli := integrationtest.NewArduinoCliWithinEnvironment(env, &integrationtest.ArduinoCLIConfig{ |
| 52 | + ArduinoCLIPath: paths.New("..", "..", "..", "arduino-cli"), |
| 53 | + UseSharedStagingFolder: true, |
| 54 | + }) |
| 55 | + |
| 56 | + // Run version and check the output message |
| 57 | + stdout, stderr, err := cli.Run("version") |
| 58 | + require.NoError(t, err) |
| 59 | + require.Contains(t, string(stdout), "Version:") |
| 60 | + require.Contains(t, string(stdout), "Commit:") |
| 61 | + require.Empty(t, stderr) |
| 62 | + |
| 63 | + // Checks if "version --format json" has a json as an output |
| 64 | + stdout, _, err = cli.Run("version", "--format", "json") |
| 65 | + require.NoError(t, err) |
| 66 | + var jsonMap map[string]string |
| 67 | + err = json.Unmarshal(stdout, &jsonMap) |
| 68 | + require.NoError(t, err) |
| 69 | + |
| 70 | + // Checks if Application's value is arduino-cli |
| 71 | + require.Equal(t, jsonMap["Application"], "arduino-cli") |
| 72 | + |
| 73 | + // Checks if VersionString's value is git-snapshot, nightly or a valid semantic versioning |
| 74 | + switch version := jsonMap["VersionString"]; version { |
| 75 | + case "git-snapshot": |
| 76 | + require.Contains(t, version, "git-snapshot") |
| 77 | + case "nigthly": |
| 78 | + require.Contains(t, version, "nightly") |
| 79 | + default: |
| 80 | + _, err = semver.Parse(version) |
| 81 | + require.NoError(t, err) |
| 82 | + } |
| 83 | + |
| 84 | + // Checks if Commit's value is not empty |
| 85 | + require.NotEmpty(t, jsonMap["Commit"]) |
| 86 | +} |
| 87 | + |
| 88 | +func TestLogOptions(t *testing.T) { |
| 89 | + // Using version as a test command |
| 90 | + env := testsuite.NewEnvironment(t) |
| 91 | + defer env.CleanUp() |
| 92 | + |
| 93 | + cli := integrationtest.NewArduinoCliWithinEnvironment(env, &integrationtest.ArduinoCLIConfig{ |
| 94 | + ArduinoCLIPath: paths.New("..", "..", "..", "arduino-cli"), |
| 95 | + UseSharedStagingFolder: true, |
| 96 | + }) |
| 97 | + |
| 98 | + // No logs |
| 99 | + stdout, _, err := cli.Run("version") |
| 100 | + require.NoError(t, err) |
| 101 | + trimOut := strings.TrimSpace(string(stdout)) |
| 102 | + outLines := strings.Split(trimOut, "\n") |
| 103 | + require.Len(t, outLines, 1) |
| 104 | + |
| 105 | + // Plain text logs on stdout |
| 106 | + stdout, _, err = cli.Run("version", "-v") |
| 107 | + require.NoError(t, err) |
| 108 | + trimOut = strings.TrimSpace(string(stdout)) |
| 109 | + outLines = strings.Split(trimOut, "\n") |
| 110 | + require.Greater(t, len(outLines), 1) |
| 111 | + require.True(t, strings.HasPrefix(outLines[0], "\x1b[36mINFO\x1b[0m")) // account for the colors |
| 112 | + |
| 113 | + // Plain text logs on file |
| 114 | + logFile := cli.DataDir().Join("log.txt") |
| 115 | + _, _, err = cli.Run("version", "--log-file", logFile.String()) |
| 116 | + require.NoError(t, err) |
| 117 | + lines, _ := logFile.ReadFileAsLines() |
| 118 | + require.True(t, strings.HasPrefix(lines[0], "time=\"")) |
| 119 | + |
| 120 | + // json on stdout |
| 121 | + stdout, _, err = cli.Run("version", "-v", "--log-format", "JSON") |
| 122 | + require.NoError(t, err) |
| 123 | + trimOut = strings.TrimSpace(string(stdout)) |
| 124 | + outLines = strings.Split(trimOut, "\n") |
| 125 | + requirejson.Contains(t, []byte(outLines[0]), `{ "level" }`) |
| 126 | + |
| 127 | + // Check if log.json contains readable json in each line |
| 128 | + var v interface{} |
| 129 | + logFileJson := cli.DataDir().Join("log.json") |
| 130 | + _, _, err = cli.Run("version", "--log-format", "JSON", "--log-file", logFileJson.String()) |
| 131 | + require.NoError(t, err) |
| 132 | + fileContent, err := logFileJson.ReadFileAsLines() |
| 133 | + require.NoError(t, err) |
| 134 | + for _, line := range fileContent { |
| 135 | + // exclude empty lines since they are not valid json |
| 136 | + if line == "" { |
| 137 | + continue |
| 138 | + } |
| 139 | + err = json.Unmarshal([]byte(line), &v) |
| 140 | + require.NoError(t, err) |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +func TestInventoryCreation(t *testing.T) { |
| 145 | + // Using version as a test command |
| 146 | + env := testsuite.NewEnvironment(t) |
| 147 | + defer env.CleanUp() |
| 148 | + |
| 149 | + cli := integrationtest.NewArduinoCliWithinEnvironment(env, &integrationtest.ArduinoCLIConfig{ |
| 150 | + ArduinoCLIPath: paths.New("..", "..", "..", "arduino-cli"), |
| 151 | + UseSharedStagingFolder: true, |
| 152 | + }) |
| 153 | + |
| 154 | + // no logs |
| 155 | + stdout, _, err := cli.Run("version") |
| 156 | + require.NoError(t, err) |
| 157 | + line := strings.TrimSpace(string(stdout)) |
| 158 | + outLines := strings.Split(line, "\n") |
| 159 | + require.Len(t, outLines, 1) |
| 160 | + |
| 161 | + // parse inventory file |
| 162 | + inventoryFile := cli.DataDir().Join("inventory.yaml") |
| 163 | + stream, err := inventoryFile.ReadFile() |
| 164 | + require.NoError(t, err) |
| 165 | + require.True(t, strings.Contains(string(stream), "installation")) |
| 166 | +} |
0 commit comments