Unverified Commit 0e0b615d authored by MatteoPologruto's avatar MatteoPologruto Committed by GitHub

[skip-changelog] Migrate tests from `test_config.py` to `config_test.go` (#1898)

* Migrate TestInit from test_config.py to config_test.go

* Migrate TestInitWithExistingCustomConfig from test_config.py to config_test.go

* Migrate TestInitOverwriteExistingCustomFile from test_config.py to config_test.go

* Migrate TestInitDestAbsolutePath from test_config.py to config_test.go

* Migrate TestInitDestRelativePath from test_config.py to config_test.go

* Migrate TestInitDestFlagWithOverwriteFlag from test_config.py to config_test.go

* Migrate TestInitDestAndConfigFileFlags from test_config.py to config_test.go

* Migrate TestInitConfigFileFlagAbsolutePath from test_config.py to config_test.go

* Migrate TestInitConfigFileFlagWithOverwriteFlag from test_config.py to config_test.go

* Migrate TestDump from test_config.py to config_test.go

* Migrate TestDumpWithConfigFileFlag from test_config.py to config_test.go

* Migrate TestInitConfigFileFlagRelativePath from test_config.py to config_test.go

* Migrate TestAddRemoveSetDeleteOnUnexistingKey from test_config.py to config_test.go

* Migrate TestAddSingleArgument from test_config.py to config_test.go

* Migrate TestAddMultipleArguments from test_config.py to config_test.go

* Migrate TestAddOnUnsupportedKey from test_config.py to config_test.go

* Migrate TestRemoveSingleArgument from test_config.py to config_test.go

* Migrate TestRemoveMultipleArguments from test_config.py to config_test.go

* Migrate TestRemoveOnUnsupportedKey from test_config.py to config_test.go

* Migrate TestSetSliceWithSingleArgument from test_config.py to config_test.go

* Migrate TestSetSliceWithMultipleArguments from test_config.py to config_test.go

* Migrate TestSetStringWithSingleArgument from test_config.py to config_test.go

* Migrate TestSetStringWithMultipleArguments from test_config.py to config_test.go

* Migrate TestSetBoolWithSingleArgument from test_config.py to config_test.go

* Migrate TestSetBoolWithMultipleArguments from test_config.py to config_test.go

* Migrate TestDelete to config_test.go and delete config_test.py
parent 2480d93a
// This file is part of arduino-cli.
//
// Copyright 2022 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to license@arduino.cc.
package config_test
import (
"testing"
"github.com/arduino/arduino-cli/internal/integrationtest"
"github.com/stretchr/testify/require"
"go.bug.st/testifyjson/requirejson"
"gopkg.in/yaml.v2"
)
func TestInit(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
stdout, stderr, err := cli.Run("config", "init")
require.Empty(t, stderr)
require.NoError(t, err)
require.Contains(t, string(stdout), cli.DataDir().String())
}
func TestInitWithExistingCustomConfig(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
stdout, _, err := cli.Run("config", "init", "--additional-urls", "https://example.com")
require.NoError(t, err)
require.Contains(t, string(stdout), cli.DataDir().String())
configFile, err := cli.DataDir().Join("arduino-cli.yaml").ReadFile()
require.NoError(t, err)
config := make(map[string]map[string]interface{})
err = yaml.Unmarshal(configFile, config)
require.NoError(t, err)
require.Equal(t, config["board_manager"]["additional_urls"].([]interface{})[0].(string), "https://example.com")
require.Equal(t, config["daemon"]["port"].(string), "50051")
require.Equal(t, config["directories"]["data"].(string), cli.DataDir().String())
require.Equal(t, config["directories"]["downloads"].(string), env.SharedDownloadsDir().String())
require.Equal(t, config["directories"]["user"].(string), cli.SketchbookDir().String())
require.Empty(t, config["logging"]["file"])
require.Equal(t, config["logging"]["format"].(string), "text")
require.Equal(t, config["logging"]["level"].(string), "info")
require.Equal(t, config["metrics"]["addr"].(string), ":9090")
require.True(t, config["metrics"]["enabled"].(bool))
configFilePath := cli.WorkingDir().Join("config", "test", "config.yaml")
require.NoFileExists(t, configFilePath.String())
stdout, _, err = cli.Run("config", "init", "--dest-file", configFilePath.String())
require.NoError(t, err)
require.Contains(t, string(stdout), configFilePath.String())
configFile, err = configFilePath.ReadFile()
require.NoError(t, err)
err = yaml.Unmarshal(configFile, config)
require.NoError(t, err)
require.Empty(t, config["board_manager"]["additional_urls"])
require.Equal(t, config["daemon"]["port"].(string), "50051")
require.Equal(t, config["directories"]["data"].(string), cli.DataDir().String())
require.Equal(t, config["directories"]["downloads"].(string), env.SharedDownloadsDir().String())
require.Equal(t, config["directories"]["user"].(string), cli.SketchbookDir().String())
require.Empty(t, config["logging"]["file"])
require.Equal(t, config["logging"]["format"].(string), "text")
require.Equal(t, config["logging"]["level"].(string), "info")
require.Equal(t, config["metrics"]["addr"].(string), ":9090")
require.True(t, config["metrics"]["enabled"].(bool))
}
func TestInitOverwriteExistingCustomFile(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
stdout, _, err := cli.Run("config", "init", "--additional-urls", "https://example.com")
require.NoError(t, err)
require.Contains(t, string(stdout), cli.DataDir().String())
configFile, err := cli.DataDir().Join("arduino-cli.yaml").ReadFile()
require.NoError(t, err)
config := make(map[string]map[string]interface{})
err = yaml.Unmarshal(configFile, config)
require.NoError(t, err)
require.Equal(t, config["board_manager"]["additional_urls"].([]interface{})[0].(string), "https://example.com")
require.Equal(t, config["daemon"]["port"].(string), "50051")
require.Equal(t, config["directories"]["data"].(string), cli.DataDir().String())
require.Equal(t, config["directories"]["downloads"].(string), env.SharedDownloadsDir().String())
require.Equal(t, config["directories"]["user"].(string), cli.SketchbookDir().String())
require.Empty(t, config["logging"]["file"])
require.Equal(t, config["logging"]["format"].(string), "text")
require.Equal(t, config["logging"]["level"].(string), "info")
require.Equal(t, config["metrics"]["addr"].(string), ":9090")
require.True(t, config["metrics"]["enabled"].(bool))
stdout, _, err = cli.Run("config", "init", "--overwrite")
require.NoError(t, err)
require.Contains(t, string(stdout), cli.DataDir().String())
configFile, err = cli.DataDir().Join("arduino-cli.yaml").ReadFile()
require.NoError(t, err)
err = yaml.Unmarshal(configFile, config)
require.NoError(t, err)
require.Empty(t, config["board_manager"]["additional_urls"])
require.Equal(t, config["daemon"]["port"].(string), "50051")
require.Equal(t, config["directories"]["data"].(string), cli.DataDir().String())
require.Equal(t, config["directories"]["downloads"].(string), env.SharedDownloadsDir().String())
require.Equal(t, config["directories"]["user"].(string), cli.SketchbookDir().String())
require.Empty(t, config["logging"]["file"])
require.Equal(t, config["logging"]["format"].(string), "text")
require.Equal(t, config["logging"]["level"].(string), "info")
require.Equal(t, config["metrics"]["addr"].(string), ":9090")
require.True(t, config["metrics"]["enabled"].(bool))
}
func TestInitDestAbsolutePath(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
dest := cli.WorkingDir().Join("config", "test")
expectedConfigFile := dest.Join("arduino-cli.yaml")
require.NoFileExists(t, expectedConfigFile.String())
stdout, _, err := cli.Run("config", "init", "--dest-dir", dest.String())
require.NoError(t, err)
require.Contains(t, string(stdout), expectedConfigFile.String())
require.FileExists(t, expectedConfigFile.String())
}
func TestInistDestRelativePath(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
dest := cli.WorkingDir().Join("config", "test")
expectedConfigFile := dest.Join("arduino-cli.yaml")
require.NoFileExists(t, expectedConfigFile.String())
stdout, _, err := cli.Run("config", "init", "--dest-dir", "config/test")
require.NoError(t, err)
require.Contains(t, string(stdout), expectedConfigFile.String())
require.FileExists(t, expectedConfigFile.String())
}
func TestInitDestFlagWithOverwriteFlag(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
dest := cli.WorkingDir().Join("config", "test")
expectedConfigFile := dest.Join("arduino-cli.yaml")
require.NoFileExists(t, expectedConfigFile.String())
_, _, err := cli.Run("config", "init", "--dest-dir", dest.String())
require.NoError(t, err)
require.FileExists(t, expectedConfigFile.String())
_, stderr, err := cli.Run("config", "init", "--dest-dir", dest.String())
require.Error(t, err)
require.Contains(t, string(stderr), "Config file already exists, use --overwrite to discard the existing one.")
stdout, _, err := cli.Run("config", "init", "--dest-dir", dest.String(), "--overwrite")
require.NoError(t, err)
require.Contains(t, string(stdout), expectedConfigFile.String())
}
func TestInitDestAndConfigFileFlags(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
_, stderr, err := cli.Run("config", "init", "--dest-file", "some_other_path", "--dest-dir", "some_path")
require.Error(t, err)
require.Contains(t, string(stderr), "Can't use --dest-file and --dest-dir flags at the same time.")
}
func TestInitConfigFileFlagAbsolutePath(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
configFile := cli.WorkingDir().Join("config", "test", "config.yaml")
require.NoFileExists(t, configFile.String())
stdout, _, err := cli.Run("config", "init", "--dest-file", configFile.String())
require.NoError(t, err)
require.Contains(t, string(stdout), configFile.String())
require.FileExists(t, configFile.String())
}
func TestInitConfigFileFlagRelativePath(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
configFile := cli.WorkingDir().Join("config.yaml")
require.NoFileExists(t, configFile.String())
stdout, _, err := cli.Run("config", "init", "--dest-file", "config.yaml")
require.NoError(t, err)
require.Contains(t, string(stdout), configFile.String())
require.FileExists(t, configFile.String())
}
func TestInitConfigFileFlagWithOverwriteFlag(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
configFile := cli.WorkingDir().Join("config", "test", "config.yaml")
require.NoFileExists(t, configFile.String())
_, _, err := cli.Run("config", "init", "--dest-file", configFile.String())
require.NoError(t, err)
require.FileExists(t, configFile.String())
_, stderr, err := cli.Run("config", "init", "--dest-file", configFile.String())
require.Error(t, err)
require.Contains(t, string(stderr), "Config file already exists, use --overwrite to discard the existing one.")
stdout, _, err := cli.Run("config", "init", "--dest-file", configFile.String(), "--overwrite")
require.NoError(t, err)
require.Contains(t, string(stdout), configFile.String())
}
func TestDump(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
// Create a config file first
configFile := cli.WorkingDir().Join("config", "test", "config.yaml")
require.NoFileExists(t, configFile.String())
_, _, err := cli.Run("config", "init", "--dest-file", configFile.String())
require.NoError(t, err)
require.FileExists(t, configFile.String())
stdout, _, err := cli.Run("config", "dump", "--config-file", configFile.String(), "--format", "json")
require.NoError(t, err)
requirejson.Query(t, stdout, ".board_manager | .additional_urls", "[]")
stdout, _, err = cli.Run("config", "init", "--additional-urls", "https://example.com")
require.NoError(t, err)
configFile = cli.DataDir().Join("arduino-cli.yaml")
require.Contains(t, string(stdout), configFile.String())
require.FileExists(t, configFile.String())
stdout, _, err = cli.Run("config", "dump", "--format", "json")
require.NoError(t, err)
requirejson.Query(t, stdout, ".board_manager | .additional_urls", "[\"https://example.com\"]")
}
func TestDumpWithConfigFileFlag(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
// Create a config file first
configFile := cli.WorkingDir().Join("config", "test", "config.yaml")
require.NoFileExists(t, configFile.String())
_, _, err := cli.Run("config", "init", "--dest-file", configFile.String(), "--additional-urls=https://example.com")
require.NoError(t, err)
require.FileExists(t, configFile.String())
stdout, _, err := cli.Run("config", "dump", "--config-file", configFile.String(), "--format", "json")
require.NoError(t, err)
requirejson.Query(t, stdout, ".board_manager | .additional_urls", "[\"https://example.com\"]")
stdout, _, err = cli.Run(
"config",
"dump",
"--config-file",
configFile.String(),
"--additional-urls=https://another-url.com",
"--format",
"json",
)
require.NoError(t, err)
requirejson.Query(t, stdout, ".board_manager | .additional_urls", "[\"https://another-url.com\"]")
}
func TestAddRemoveSetDeleteOnUnexistingKey(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
// Create a config file
_, _, err := cli.Run("config", "init", "--dest-dir", ".")
require.NoError(t, err)
_, stderr, err := cli.Run("config", "add", "some.key", "some_value")
require.Error(t, err)
require.Contains(t, string(stderr), "Settings key doesn't exist")
_, stderr, err = cli.Run("config", "remove", "some.key", "some_value")
require.Error(t, err)
require.Contains(t, string(stderr), "Settings key doesn't exist")
_, stderr, err = cli.Run("config", "set", "some.key", "some_value")
require.Error(t, err)
require.Contains(t, string(stderr), "Settings key doesn't exist")
_, stderr, err = cli.Run("config", "delete", "some.key")
require.Error(t, err)
require.Contains(t, string(stderr), "Settings key doesn't exist")
}
func TestAddSingleArgument(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
// Create a config file
_, _, err := cli.Run("config", "init", "--dest-dir", ".")
require.NoError(t, err)
// Verifies no additional urls are present
stdout, _, err := cli.Run("config", "dump", "--format", "json")
require.NoError(t, err)
requirejson.Query(t, stdout, ".board_manager | .additional_urls", "[]")
// Adds one URL
url := "https://example.com"
_, _, err = cli.Run("config", "add", "board_manager.additional_urls", url)
require.NoError(t, err)
// Verifies URL has been saved
stdout, _, err = cli.Run("config", "dump", "--format", "json")
require.NoError(t, err)
requirejson.Query(t, stdout, ".board_manager | .additional_urls", "[\"https://example.com\"]")
// Adds the same URL (should not error)
_, _, err = cli.Run("config", "add", "board_manager.additional_urls", url)
require.NoError(t, err)
// Verifies a second copy has NOT been added
stdout, _, err = cli.Run("config", "dump", "--format", "json")
require.NoError(t, err)
requirejson.Query(t, stdout, ".board_manager | .additional_urls", "[\"https://example.com\"]")
}
func TestAddMultipleArguments(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
// Create a config file
_, _, err := cli.Run("config", "init", "--dest-dir", ".")
require.NoError(t, err)
// Verifies no additional urls are present
stdout, _, err := cli.Run("config", "dump", "--format", "json")
require.NoError(t, err)
requirejson.Query(t, stdout, ".board_manager | .additional_urls", "[]")
// Adds multiple URLs at the same time
urls := [3]string{
"https://example.com/package_example_index.json",
"https://example.com/yet_another_package_example_index.json",
}
_, _, err = cli.Run("config", "add", "board_manager.additional_urls", urls[0], urls[1])
require.NoError(t, err)
// Verifies URL has been saved
stdout, _, err = cli.Run("config", "dump", "--format", "json")
require.NoError(t, err)
requirejson.Query(t, stdout, ".board_manager | .additional_urls | length", "2")
requirejson.Contains(t, stdout, `
{
"board_manager": {
"additional_urls": [
"https://example.com/package_example_index.json",
"https://example.com/yet_another_package_example_index.json"
]
}
}`)
// Adds both the same URLs a second time
_, _, err = cli.Run("config", "add", "board_manager.additional_urls", urls[0], urls[1])
require.NoError(t, err)
// Verifies no change in result array
stdout, _, err = cli.Run("config", "dump", "--format", "json")
require.NoError(t, err)
requirejson.Query(t, stdout, ".board_manager | .additional_urls | length", "2")
requirejson.Contains(t, stdout, `
{
"board_manager": {
"additional_urls": [
"https://example.com/package_example_index.json",
"https://example.com/yet_another_package_example_index.json"
]
}
}`)
// Adds multiple URLs ... the middle one is the only new URL
urls = [3]string{
"https://example.com/package_example_index.json",
"https://example.com/a_third_package_example_index.json",
"https://example.com/yet_another_package_example_index.json",
}
_, _, err = cli.Run("config", "add", "board_manager.additional_urls", urls[0], urls[1], urls[2])
require.NoError(t, err)
// Verifies URL has been saved
stdout, _, err = cli.Run("config", "dump", "--format", "json")
require.NoError(t, err)
requirejson.Query(t, stdout, ".board_manager | .additional_urls | length", "3")
requirejson.Contains(t, stdout, `
{
"board_manager": {
"additional_urls": [
"https://example.com/package_example_index.json",
"https://example.com/a_third_package_example_index.json",
"https://example.com/yet_another_package_example_index.json"
]
}
}`)
}
func TestAddOnUnsupportedKey(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
// Create a config file
_, _, err := cli.Run("config", "init", "--dest-dir", ".")
require.NoError(t, err)
// Verifies default value
stdout, _, err := cli.Run("config", "dump", "--format", "json")
require.NoError(t, err)
requirejson.Query(t, stdout, ".daemon | .port", "\"50051\"")
// Tries and fails to add a new item
_, stderr, err := cli.Run("config", "add", "daemon.port", "50000")
require.Error(t, err)
require.Contains(t, string(stderr), "The key 'daemon.port' is not a list of items, can't add to it.\nMaybe use 'config set'?")
// Verifies value is not changed
stdout, _, err = cli.Run("config", "dump", "--format", "json")
require.NoError(t, err)
requirejson.Query(t, stdout, ".daemon | .port", "\"50051\"")
}
func TestRemoveSingleArgument(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
// Create a config file
_, _, err := cli.Run("config", "init", "--dest-dir", ".")
require.NoError(t, err)
// Adds URLs
urls := [2]string{
"https://example.com/package_example_index.json",
"https://example.com/yet_another_package_example_index.json",
}
_, _, err = cli.Run("config", "add", "board_manager.additional_urls", urls[0], urls[1])
require.NoError(t, err)
// Verifies default state
stdout, _, err := cli.Run("config", "dump", "--format", "json")
require.NoError(t, err)
requirejson.Query(t, stdout, ".board_manager | .additional_urls | length", "2")
requirejson.Contains(t, stdout, `
{
"board_manager": {
"additional_urls": [
"https://example.com/package_example_index.json",
"https://example.com/yet_another_package_example_index.json"
]
}
}`)
// Remove first URL
_, _, err = cli.Run("config", "remove", "board_manager.additional_urls", urls[0])
require.NoError(t, err)
// Verifies URLs has been removed
stdout, _, err = cli.Run("config", "dump", "--format", "json")
require.NoError(t, err)
requirejson.Query(t, stdout, ".board_manager | .additional_urls", "[\"https://example.com/yet_another_package_example_index.json\"]")
}
func TestRemoveMultipleArguments(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
// Create a config file
_, _, err := cli.Run("config", "init", "--dest-dir", ".")
require.NoError(t, err)
// Adds URLs
urls := [2]string{
"https://example.com/package_example_index.json",
"https://example.com/yet_another_package_example_index.json",
}
_, _, err = cli.Run("config", "add", "board_manager.additional_urls", urls[0], urls[1])
require.NoError(t, err)
// Verifies default state
stdout, _, err := cli.Run("config", "dump", "--format", "json")
require.NoError(t, err)
requirejson.Query(t, stdout, ".board_manager | .additional_urls | length", "2")
requirejson.Contains(t, stdout, `
{
"board_manager": {
"additional_urls": [
"https://example.com/package_example_index.json",
"https://example.com/yet_another_package_example_index.json"
]
}
}`)
// Remove all URLs
_, _, err = cli.Run("config", "remove", "board_manager.additional_urls", urls[0], urls[1])
require.NoError(t, err)
// Verifies all URLs have been removed
stdout, _, err = cli.Run("config", "dump", "--format", "json")
require.NoError(t, err)
requirejson.Query(t, stdout, ".board_manager | .additional_urls", "[]")
}
func TestRemoveOnUnsupportedKey(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
// Create a config file
_, _, err := cli.Run("config", "init", "--dest-dir", ".")
require.NoError(t, err)
// Verifies default value
stdout, _, err := cli.Run("config", "dump", "--format", "json")
require.NoError(t, err)
requirejson.Query(t, stdout, ".daemon | .port", "\"50051\"")
// Tries and fails to remove an item
_, stderr, err := cli.Run("config", "remove", "daemon.port", "50051")
require.Error(t, err)
require.Contains(t, string(stderr), "The key 'daemon.port' is not a list of items, can't remove from it.\nMaybe use 'config delete'?")
// Verifies value is not changed
stdout, _, err = cli.Run("config", "dump", "--format", "json")
require.NoError(t, err)
requirejson.Query(t, stdout, ".daemon | .port", "\"50051\"")
}
func TestSetSliceWithSingleArgument(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
// Create a config file
_, _, err := cli.Run("config", "init", "--dest-dir", ".")
require.NoError(t, err)
// Verifies default state
stdout, _, err := cli.Run("config", "dump", "--format", "json")
require.NoError(t, err)
requirejson.Query(t, stdout, ".board_manager | .additional_urls", "[]")
// Set an URL in the list
url := "https://example.com/package_example_index.json"
_, _, err = cli.Run("config", "set", "board_manager.additional_urls", url)
require.NoError(t, err)
// Verifies value is changed
stdout, _, err = cli.Run("config", "dump", "--format", "json")
require.NoError(t, err)
requirejson.Query(t, stdout, ".board_manager | .additional_urls", "[\"https://example.com/package_example_index.json\"]")
// Set an URL in the list
url = "https://example.com/yet_another_package_example_index.json"
_, _, err = cli.Run("config", "set", "board_manager.additional_urls", url)
require.NoError(t, err)
// Verifies value is changed
stdout, _, err = cli.Run("config", "dump", "--format", "json")
require.NoError(t, err)
requirejson.Query(t, stdout, ".board_manager | .additional_urls", "[\"https://example.com/yet_another_package_example_index.json\"]")
}
func TestSetSliceWithMultipleArguments(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
// Create a config file
_, _, err := cli.Run("config", "init", "--dest-dir", ".")
require.NoError(t, err)
// Verifies default state
stdout, _, err := cli.Run("config", "dump", "--format", "json")
require.NoError(t, err)
requirejson.Query(t, stdout, ".board_manager | .additional_urls", "[]")
// Set some URLs in the list
urls := [7]string{
"https://example.com/first_package_index.json",
"https://example.com/second_package_index.json",
}
_, _, err = cli.Run("config", "set", "board_manager.additional_urls", urls[0], urls[1])
require.NoError(t, err)
// Verifies value is changed
stdout, _, err = cli.Run("config", "dump", "--format", "json")
require.NoError(t, err)
requirejson.Query(t, stdout, ".board_manager | .additional_urls | length", "2")
requirejson.Contains(t, stdout, `
{
"board_manager": {
"additional_urls": [
"https://example.com/first_package_index.json",
"https://example.com/second_package_index.json"
]
}
}`)
// Set some URLs in the list
urls = [7]string{
"https://example.com/third_package_index.json",
"https://example.com/fourth_package_index.json",
}
_, _, err = cli.Run("config", "set", "board_manager.additional_urls", urls[0], urls[1])
require.NoError(t, err)
// Verifies previous value is overwritten
stdout, _, err = cli.Run("config", "dump", "--format", "json")
require.NoError(t, err)
requirejson.Query(t, stdout, ".board_manager | .additional_urls | length", "2")
requirejson.Contains(t, stdout, `
{
"board_manager": {
"additional_urls": [
"https://example.com/third_package_index.json",
"https://example.com/fourth_package_index.json"
]
}
}`)
// Sets a third set of 7 URLs (with only 4 unique values)
urls = [7]string{
"https://example.com/first_package_index.json",
"https://example.com/second_package_index.json",
"https://example.com/first_package_index.json",
"https://example.com/fifth_package_index.json",
"https://example.com/second_package_index.json",
"https://example.com/sixth_package_index.json",
"https://example.com/first_package_index.json",
}
_, _, err = cli.Run("config", "set", "board_manager.additional_urls", urls[0], urls[1], urls[2], urls[3], urls[4], urls[5], urls[6])
require.NoError(t, err)
// Verifies all unique values exist in config
stdout, _, err = cli.Run("config", "dump", "--format", "json")
require.NoError(t, err)
requirejson.Query(t, stdout, ".board_manager | .additional_urls | length", "4")
requirejson.Contains(t, stdout, `
{
"board_manager": {
"additional_urls": [
"https://example.com/first_package_index.json",
"https://example.com/second_package_index.json",
"https://example.com/fifth_package_index.json",
"https://example.com/sixth_package_index.json"
]
}
}`)
}
func TestSetStringWithSingleArgument(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
// Create a config file
_, _, err := cli.Run("config", "init", "--dest-dir", ".")
require.NoError(t, err)
// Verifies default state
stdout, _, err := cli.Run("config", "dump", "--format", "json")
require.NoError(t, err)
requirejson.Query(t, stdout, ".logging | .level", "\"info\"")
// Changes value
_, _, err = cli.Run("config", "set", "logging.level", "trace")
require.NoError(t, err)
// Verifies value is changed
stdout, _, err = cli.Run("config", "dump", "--format", "json")
require.NoError(t, err)
requirejson.Query(t, stdout, ".logging | .level", "\"trace\"")
}
func TestSetStringWithMultipleArguments(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
// Create a config file
_, _, err := cli.Run("config", "init", "--dest-dir", ".")
require.NoError(t, err)
// Verifies default state
stdout, _, err := cli.Run("config", "dump", "--format", "json")
require.NoError(t, err)
requirejson.Query(t, stdout, ".logging | .level", "\"info\"")
// Tries to change value
_, stderr, err := cli.Run("config", "set", "logging.level", "trace", "debug")
require.Error(t, err)
require.Contains(t, string(stderr), "Can't set multiple values in key logging.level")
}
func TestSetBoolWithSingleArgument(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
// Create a config file
_, _, err := cli.Run("config", "init", "--dest-dir", ".")
require.NoError(t, err)
// Verifies default state
stdout, _, err := cli.Run("config", "dump", "--format", "json")
require.NoError(t, err)
requirejson.Query(t, stdout, ".library | .enable_unsafe_install", "false")
// Changes value
_, _, err = cli.Run("config", "set", "library.enable_unsafe_install", "true")
require.NoError(t, err)
// Verifies value is changed
stdout, _, err = cli.Run("config", "dump", "--format", "json")
require.NoError(t, err)
requirejson.Query(t, stdout, ".library | .enable_unsafe_install", "true")
}
func TestSetBoolWithMultipleArguments(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
// Create a config file
_, _, err := cli.Run("config", "init", "--dest-dir", ".")
require.NoError(t, err)
// Verifies default state
stdout, _, err := cli.Run("config", "dump", "--format", "json")
require.NoError(t, err)
requirejson.Query(t, stdout, ".library | .enable_unsafe_install", "false")
// Changes value
_, stderr, err := cli.Run("config", "set", "library.enable_unsafe_install", "true", "foo")
require.Error(t, err)
require.Contains(t, string(stderr), "Can't set multiple values in key library.enable_unsafe_install")
}
func TestDelete(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
// Create a config file
_, _, err := cli.Run("config", "init", "--dest-dir", ".")
require.NoError(t, err)
// Verifies default state
stdout, _, err := cli.Run("config", "dump", "--format", "json")
require.NoError(t, err)
requirejson.Query(t, stdout, ".library | .enable_unsafe_install", "false")
// Delete config key
_, _, err = cli.Run("config", "delete", "library.enable_unsafe_install")
require.NoError(t, err)
// Verifies value is not found, we read directly from file instead of using
// the dump command since that would still print the deleted value if it has
// a default
configFile := cli.WorkingDir().Join("arduino-cli.yaml")
configLines, err := configFile.ReadFileAsLines()
require.NoError(t, err)
require.NotContains(t, configLines, "enable_unsafe_install")
// Verifies default state
stdout, _, err = cli.Run("config", "dump", "--format", "json")
require.NoError(t, err)
requirejson.Query(t, stdout, ".board_manager | .additional_urls", "[]")
// Delete config key and sub keys
_, _, err = cli.Run("config", "delete", "board_manager")
require.NoError(t, err)
// Verifies value is not found, we read directly from file instead of using
// the dump command since that would still print the deleted value if it has
// a default
configFile = cli.WorkingDir().Join("arduino-cli.yaml")
configLines, err = configFile.ReadFileAsLines()
require.NoError(t, err)
require.NotContains(t, configLines, "additional_urls")
require.NotContains(t, configLines, "board_manager")
}
# This file is part of arduino-cli.
#
# Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
#
# This software is released under the GNU General Public License version 3,
# which covers the main part of arduino-cli.
# The terms of this license can be found at:
# https://www.gnu.org/licenses/gpl-3.0.en.html
#
# You can be released from the requirements of the above licenses by purchasing
# a commercial license. Buying such a license is mandatory if you want to modify or
# otherwise use the software for commercial activities involving the Arduino
# software without disclosing the source code of your own applications. To purchase
# a commercial license, send an email to license@arduino.cc.
from pathlib import Path
import json
import yaml
def test_init(run_command, data_dir, working_dir):
result = run_command(["config", "init"])
assert "" == result.stderr
assert result.ok
assert data_dir in result.stdout
def test_init_with_existing_custom_config(run_command, data_dir, working_dir, downloads_dir):
result = run_command(["config", "init", "--additional-urls", "https://example.com"])
assert result.ok
assert data_dir in result.stdout
config_file = open(Path(data_dir) / "arduino-cli.yaml", "r")
configs = yaml.load(config_file.read(), Loader=yaml.FullLoader)
config_file.close()
assert ["https://example.com"] == configs["board_manager"]["additional_urls"]
assert "50051" == configs["daemon"]["port"]
assert data_dir == configs["directories"]["data"]
assert downloads_dir == configs["directories"]["downloads"]
assert data_dir == configs["directories"]["user"]
assert "" == configs["logging"]["file"]
assert "text" == configs["logging"]["format"]
assert "info" == configs["logging"]["level"]
assert ":9090" == configs["metrics"]["addr"]
assert configs["metrics"]["enabled"]
config_file_path = Path(working_dir) / "config" / "test" / "config.yaml"
assert not config_file_path.exists()
result = run_command(["config", "init", "--dest-file", config_file_path])
assert result.ok
assert str(config_file_path) in result.stdout
config_file = open(config_file_path, "r")
configs = yaml.load(config_file.read(), Loader=yaml.FullLoader)
config_file.close()
assert [] == configs["board_manager"]["additional_urls"]
assert "50051" == configs["daemon"]["port"]
assert data_dir == configs["directories"]["data"]
assert downloads_dir == configs["directories"]["downloads"]
assert data_dir == configs["directories"]["user"]
assert "" == configs["logging"]["file"]
assert "text" == configs["logging"]["format"]
assert "info" == configs["logging"]["level"]
assert ":9090" == configs["metrics"]["addr"]
assert configs["metrics"]["enabled"]
def test_init_overwrite_existing_custom_file(run_command, data_dir, working_dir, downloads_dir):
result = run_command(["config", "init", "--additional-urls", "https://example.com"])
assert result.ok
assert data_dir in result.stdout
config_file = open(Path(data_dir) / "arduino-cli.yaml", "r")
configs = yaml.load(config_file.read(), Loader=yaml.FullLoader)
config_file.close()
assert ["https://example.com"] == configs["board_manager"]["additional_urls"]
assert "50051" == configs["daemon"]["port"]
assert data_dir == configs["directories"]["data"]
assert downloads_dir == configs["directories"]["downloads"]
assert data_dir == configs["directories"]["user"]
assert "" == configs["logging"]["file"]
assert "text" == configs["logging"]["format"]
assert "info" == configs["logging"]["level"]
assert ":9090" == configs["metrics"]["addr"]
assert configs["metrics"]["enabled"]
result = run_command(["config", "init", "--overwrite"])
assert result.ok
assert data_dir in result.stdout
config_file = open(Path(data_dir) / "arduino-cli.yaml", "r")
configs = yaml.load(config_file.read(), Loader=yaml.FullLoader)
config_file.close()
assert [] == configs["board_manager"]["additional_urls"]
assert "50051" == configs["daemon"]["port"]
assert data_dir == configs["directories"]["data"]
assert downloads_dir == configs["directories"]["downloads"]
assert data_dir == configs["directories"]["user"]
assert "" == configs["logging"]["file"]
assert "text" == configs["logging"]["format"]
assert "info" == configs["logging"]["level"]
assert ":9090" == configs["metrics"]["addr"]
assert configs["metrics"]["enabled"]
def test_init_dest_absolute_path(run_command, working_dir):
dest = Path(working_dir) / "config" / "test"
expected_config_file = dest / "arduino-cli.yaml"
assert not expected_config_file.exists()
result = run_command(["config", "init", "--dest-dir", dest])
assert result.ok
assert str(expected_config_file) in result.stdout
assert expected_config_file.exists()
def test_init_dest_relative_path(run_command, working_dir):
dest = Path(working_dir) / "config" / "test"
expected_config_file = dest / "arduino-cli.yaml"
assert not expected_config_file.exists()
result = run_command(["config", "init", "--dest-dir", "config/test"])
assert result.ok
assert str(expected_config_file) in result.stdout
assert expected_config_file.exists()
def test_init_dest_flag_with_overwrite_flag(run_command, working_dir):
dest = Path(working_dir) / "config" / "test"
expected_config_file = dest / "arduino-cli.yaml"
assert not expected_config_file.exists()
result = run_command(["config", "init", "--dest-dir", dest])
assert result.ok
assert expected_config_file.exists()
result = run_command(["config", "init", "--dest-dir", dest])
assert result.failed
assert "Config file already exists, use --overwrite to discard the existing one." in result.stderr
result = run_command(["config", "init", "--dest-dir", dest, "--overwrite"])
assert result.ok
assert str(expected_config_file) in result.stdout
def test_init_dest_and_config_file_flags(run_command, working_dir):
result = run_command(["config", "init", "--dest-file", "some_other_path", "--dest-dir", "some_path"])
assert result.failed
assert "Can't use --dest-file and --dest-dir flags at the same time." in result.stderr
def test_init_config_file_flag_absolute_path(run_command, working_dir):
config_file = Path(working_dir) / "config" / "test" / "config.yaml"
assert not config_file.exists()
result = run_command(["config", "init", "--dest-file", config_file])
assert result.ok
assert str(config_file) in result.stdout
assert config_file.exists()
def test_init_config_file_flag_relative_path(run_command, working_dir):
config_file = Path(working_dir) / "config.yaml"
assert not config_file.exists()
result = run_command(["config", "init", "--dest-file", "config.yaml"])
assert result.ok
assert str(config_file) in result.stdout
assert config_file.exists()
def test_init_config_file_flag_with_overwrite_flag(run_command, working_dir):
config_file = Path(working_dir) / "config" / "test" / "config.yaml"
assert not config_file.exists()
result = run_command(["config", "init", "--dest-file", config_file])
assert result.ok
assert config_file.exists()
result = run_command(["config", "init", "--dest-file", config_file])
assert result.failed
assert "Config file already exists, use --overwrite to discard the existing one." in result.stderr
result = run_command(["config", "init", "--dest-file", config_file, "--overwrite"])
assert result.ok
assert str(config_file) in result.stdout
def test_dump(run_command, data_dir, working_dir):
# Create a config file first
config_file = Path(working_dir) / "config" / "test" / "config.yaml"
assert not config_file.exists()
result = run_command(["config", "init", "--dest-file", config_file])
assert result.ok
assert config_file.exists()
result = run_command(["config", "dump", "--config-file", config_file, "--format", "json"])
assert result.ok
settings_json = json.loads(result.stdout)
assert [] == settings_json["board_manager"]["additional_urls"]
result = run_command(["config", "init", "--additional-urls", "https://example.com"])
assert result.ok
config_file = Path(data_dir) / "arduino-cli.yaml"
assert str(config_file) in result.stdout
assert config_file.exists()
result = run_command(["config", "dump", "--format", "json"])
assert result.ok
settings_json = json.loads(result.stdout)
assert ["https://example.com"] == settings_json["board_manager"]["additional_urls"]
def test_dump_with_config_file_flag(run_command, working_dir):
# Create a config file first
config_file = Path(working_dir) / "config" / "test" / "config.yaml"
assert not config_file.exists()
result = run_command(["config", "init", "--dest-file", config_file, "--additional-urls=https://example.com"])
assert result.ok
assert config_file.exists()
result = run_command(["config", "dump", "--config-file", config_file, "--format", "json"])
assert result.ok
settings_json = json.loads(result.stdout)
assert ["https://example.com"] == settings_json["board_manager"]["additional_urls"]
result = run_command(
[
"config",
"dump",
"--config-file",
config_file,
"--additional-urls=https://another-url.com",
"--format",
"json",
]
)
assert result.ok
settings_json = json.loads(result.stdout)
assert ["https://another-url.com"] == settings_json["board_manager"]["additional_urls"]
def test_add_remove_set_delete_on_unexisting_key(run_command):
# Create a config file
assert run_command(["config", "init", "--dest-dir", "."])
res = run_command(["config", "add", "some.key", "some_value"])
assert res.failed
assert "Settings key doesn't exist" in res.stderr
res = run_command(["config", "remove", "some.key", "some_value"])
assert res.failed
assert "Settings key doesn't exist" in res.stderr
res = run_command(["config", "set", "some.key", "some_value"])
assert res.failed
assert "Settings key doesn't exist" in res.stderr
res = run_command(["config", "delete", "some.key"])
assert res.failed
assert "Settings key doesn't exist" in res.stderr
def test_add_single_argument(run_command):
# Create a config file
assert run_command(["config", "init", "--dest-dir", "."])
# Verifies no additional urls are present
result = run_command(["config", "dump", "--format", "json"])
assert result.ok
settings_json = json.loads(result.stdout)
assert [] == settings_json["board_manager"]["additional_urls"]
# Adds one URL
url = "https://example.com"
assert run_command(["config", "add", "board_manager.additional_urls", url])
# Verifies URL has been saved
result = run_command(["config", "dump", "--format", "json"])
assert result.ok
settings_json = json.loads(result.stdout)
assert ["https://example.com"] == settings_json["board_manager"]["additional_urls"]
# Adds the same URL (should not error)
url = "https://example.com"
assert run_command(["config", "add", "board_manager.additional_urls", url])
# Verifies a second copy has NOT been added
result = run_command(["config", "dump", "--format", "json"])
assert result.ok
settings_json = json.loads(result.stdout)
assert ["https://example.com"] == settings_json["board_manager"]["additional_urls"]
def test_add_multiple_arguments(run_command):
# Create a config file
assert run_command(["config", "init", "--dest-dir", "."])
# Verifies no additional urls are present
result = run_command(["config", "dump", "--format", "json"])
assert result.ok
settings_json = json.loads(result.stdout)
assert [] == settings_json["board_manager"]["additional_urls"]
# Adds multiple URLs at the same time
urls = [
"https://example.com/package_example_index.json",
"https://example.com/yet_another_package_example_index.json",
]
assert run_command(["config", "add", "board_manager.additional_urls"] + urls)
# Verifies URL has been saved
result = run_command(["config", "dump", "--format", "json"])
assert result.ok
settings_json = json.loads(result.stdout)
assert 2 == len(settings_json["board_manager"]["additional_urls"])
assert urls[0] in settings_json["board_manager"]["additional_urls"]
assert urls[1] in settings_json["board_manager"]["additional_urls"]
# Adds both the same URLs a second time
assert run_command(["config", "add", "board_manager.additional_urls"] + urls)
# Verifies no change in result array
result = run_command(["config", "dump", "--format", "json"])
assert result.ok
settings_json = json.loads(result.stdout)
assert 2 == len(settings_json["board_manager"]["additional_urls"])
assert urls[0] in settings_json["board_manager"]["additional_urls"]
assert urls[1] in settings_json["board_manager"]["additional_urls"]
# Adds multiple URLs ... the middle one is the only new URL
urls = [
"https://example.com/package_example_index.json",
"https://example.com/a_third_package_example_index.json",
"https://example.com/yet_another_package_example_index.json",
]
assert run_command(["config", "add", "board_manager.additional_urls"] + urls)
# Verifies URL has been saved
result = run_command(["config", "dump", "--format", "json"])
assert result.ok
settings_json = json.loads(result.stdout)
assert 3 == len(settings_json["board_manager"]["additional_urls"])
assert urls[0] in settings_json["board_manager"]["additional_urls"]
assert urls[1] in settings_json["board_manager"]["additional_urls"]
assert urls[2] in settings_json["board_manager"]["additional_urls"]
def test_add_on_unsupported_key(run_command):
# Create a config file
assert run_command(["config", "init", "--dest-dir", "."])
# Verifies default value
result = run_command(["config", "dump", "--format", "json"])
assert result.ok
settings_json = json.loads(result.stdout)
assert "50051" == settings_json["daemon"]["port"]
# Tries and fails to add a new item
result = run_command(["config", "add", "daemon.port", "50000"])
assert result.failed
assert "The key 'daemon.port' is not a list of items, can't add to it.\nMaybe use 'config set'?" in result.stderr
# Verifies value is not changed
result = run_command(["config", "dump", "--format", "json"])
assert result.ok
settings_json = json.loads(result.stdout)
assert "50051" == settings_json["daemon"]["port"]
def test_remove_single_argument(run_command):
# Create a config file
assert run_command(["config", "init", "--dest-dir", "."])
# Adds URLs
urls = [
"https://example.com/package_example_index.json",
"https://example.com/yet_another_package_example_index.json",
]
assert run_command(["config", "add", "board_manager.additional_urls"] + urls)
# Verifies default state
result = run_command(["config", "dump", "--format", "json"])
assert result.ok
settings_json = json.loads(result.stdout)
assert 2 == len(settings_json["board_manager"]["additional_urls"])
assert urls[0] in settings_json["board_manager"]["additional_urls"]
assert urls[1] in settings_json["board_manager"]["additional_urls"]
# Remove first URL
assert run_command(["config", "remove", "board_manager.additional_urls", urls[0]])
# Verifies URLs has been removed
result = run_command(["config", "dump", "--format", "json"])
assert result.ok
settings_json = json.loads(result.stdout)
assert ["https://example.com/yet_another_package_example_index.json"] == settings_json["board_manager"][
"additional_urls"
]
def test_remove_multiple_arguments(run_command):
# Create a config file
assert run_command(["config", "init", "--dest-dir", "."])
# Adds URLs
urls = [
"https://example.com/package_example_index.json",
"https://example.com/yet_another_package_example_index.json",
]
assert run_command(["config", "add", "board_manager.additional_urls"] + urls)
# Verifies default state
result = run_command(["config", "dump", "--format", "json"])
assert result.ok
settings_json = json.loads(result.stdout)
assert 2 == len(settings_json["board_manager"]["additional_urls"])
assert urls[0] in settings_json["board_manager"]["additional_urls"]
assert urls[1] in settings_json["board_manager"]["additional_urls"]
# Remove all URLs
assert run_command(["config", "remove", "board_manager.additional_urls"] + urls)
# Verifies all URLs have been removed
result = run_command(["config", "dump", "--format", "json"])
assert result.ok
settings_json = json.loads(result.stdout)
assert [] == settings_json["board_manager"]["additional_urls"]
def test_remove_on_unsupported_key(run_command):
# Create a config file
assert run_command(["config", "init", "--dest-dir", "."])
# Verifies default value
result = run_command(["config", "dump", "--format", "json"])
assert result.ok
settings_json = json.loads(result.stdout)
assert "50051" == settings_json["daemon"]["port"]
# Tries and fails to add a new item
result = run_command(["config", "remove", "daemon.port", "50051"])
assert result.failed
assert (
"The key 'daemon.port' is not a list of items, can't remove from it.\nMaybe use 'config delete'?"
in result.stderr
)
# Verifies value is not changed
result = run_command(["config", "dump", "--format", "json"])
assert result.ok
settings_json = json.loads(result.stdout)
assert "50051" == settings_json["daemon"]["port"]
def test_set_slice_with_single_argument(run_command):
# Create a config file
assert run_command(["config", "init", "--dest-dir", "."])
# Verifies default state
result = run_command(["config", "dump", "--format", "json"])
assert result.ok
settings_json = json.loads(result.stdout)
assert [] == settings_json["board_manager"]["additional_urls"]
# Set an URL in the list
url = "https://example.com/package_example_index.json"
assert run_command(["config", "set", "board_manager.additional_urls", url])
# Verifies value is changed
result = run_command(["config", "dump", "--format", "json"])
assert result.ok
settings_json = json.loads(result.stdout)
assert [url] == settings_json["board_manager"]["additional_urls"]
# Sets another URL
url = "https://example.com/yet_another_package_example_index.json"
assert run_command(["config", "set", "board_manager.additional_urls", url])
# Verifies previous value is overwritten
result = run_command(["config", "dump", "--format", "json"])
assert result.ok
settings_json = json.loads(result.stdout)
assert [url] == settings_json["board_manager"]["additional_urls"]
def test_set_slice_with_multiple_arguments(run_command):
# Create a config file
assert run_command(["config", "init", "--dest-dir", "."])
# Verifies default state
result = run_command(["config", "dump", "--format", "json"])
assert result.ok
settings_json = json.loads(result.stdout)
assert [] == settings_json["board_manager"]["additional_urls"]
# Set some URLs in the list
urls = [
"https://example.com/first_package_index.json",
"https://example.com/second_package_index.json",
]
assert run_command(["config", "set", "board_manager.additional_urls"] + urls)
# Verifies value is changed
result = run_command(["config", "dump", "--format", "json"])
assert result.ok
settings_json = json.loads(result.stdout)
assert 2 == len(settings_json["board_manager"]["additional_urls"])
assert urls[0] in settings_json["board_manager"]["additional_urls"]
assert urls[1] in settings_json["board_manager"]["additional_urls"]
# Sets another set of URL
urls = [
"https://example.com/third_package_index.json",
"https://example.com/fourth_package_index.json",
]
assert run_command(["config", "set", "board_manager.additional_urls"] + urls)
# Verifies previous value is overwritten
result = run_command(["config", "dump", "--format", "json"])
assert result.ok
settings_json = json.loads(result.stdout)
assert 2 == len(settings_json["board_manager"]["additional_urls"])
assert urls[0] in settings_json["board_manager"]["additional_urls"]
assert urls[1] in settings_json["board_manager"]["additional_urls"]
# Sets a third set of 7 URLs (with only 4 unique values)
urls = [
"https://example.com/first_package_index.json",
"https://example.com/second_package_index.json",
"https://example.com/first_package_index.json",
"https://example.com/fifth_package_index.json",
"https://example.com/second_package_index.json",
"https://example.com/sixth_package_index.json",
"https://example.com/first_package_index.json",
]
assert run_command(["config", "set", "board_manager.additional_urls"] + urls)
# Verifies all unique values exist in config
result = run_command(["config", "dump", "--format", "json"])
assert result.ok
settings_json = json.loads(result.stdout)
assert 4 == len(settings_json["board_manager"]["additional_urls"])
assert urls[0] in settings_json["board_manager"]["additional_urls"]
assert urls[1] in settings_json["board_manager"]["additional_urls"]
assert urls[2] in settings_json["board_manager"]["additional_urls"]
assert urls[3] in settings_json["board_manager"]["additional_urls"]
assert urls[4] in settings_json["board_manager"]["additional_urls"]
assert urls[5] in settings_json["board_manager"]["additional_urls"]
assert urls[6] in settings_json["board_manager"]["additional_urls"]
def test_set_string_with_single_argument(run_command):
# Create a config file
assert run_command(["config", "init", "--dest-dir", "."])
# Verifies default state
result = run_command(["config", "dump", "--format", "json"])
assert result.ok
settings_json = json.loads(result.stdout)
assert "info" == settings_json["logging"]["level"]
# Changes value
assert run_command(["config", "set", "logging.level", "trace"])
# Verifies value is changed
result = run_command(["config", "dump", "--format", "json"])
assert result.ok
settings_json = json.loads(result.stdout)
assert "trace" == settings_json["logging"]["level"]
def test_set_string_with_multiple_arguments(run_command):
# Create a config file
assert run_command(["config", "init", "--dest-dir", "."])
# Verifies default state
result = run_command(["config", "dump", "--format", "json"])
assert result.ok
settings_json = json.loads(result.stdout)
assert "info" == settings_json["logging"]["level"]
# Tries to change value
res = run_command(["config", "set", "logging.level", "trace", "debug"])
assert res.failed
assert "Can't set multiple values in key logging.level" in res.stderr
def test_set_bool_with_single_argument(run_command):
# Create a config file
assert run_command(["config", "init", "--dest-dir", "."])
# Verifies default state
result = run_command(["config", "dump", "--format", "json"])
assert result.ok
settings_json = json.loads(result.stdout)
assert not settings_json["library"]["enable_unsafe_install"]
# Changes value
assert run_command(["config", "set", "library.enable_unsafe_install", "true"])
# Verifies value is changed
result = run_command(["config", "dump", "--format", "json"])
assert result.ok
settings_json = json.loads(result.stdout)
assert settings_json["library"]["enable_unsafe_install"]
def test_set_bool_with_multiple_arguments(run_command):
# Create a config file
assert run_command(["config", "init", "--dest-dir", "."])
# Verifies default state
result = run_command(["config", "dump", "--format", "json"])
assert result.ok
settings_json = json.loads(result.stdout)
assert not settings_json["library"]["enable_unsafe_install"]
# Changes value'
res = run_command(["config", "set", "library.enable_unsafe_install", "true", "foo"])
assert res.failed
assert "Can't set multiple values in key library.enable_unsafe_install" in res.stderr
def test_delete(run_command, working_dir):
# Create a config file
assert run_command(["config", "init", "--dest-dir", "."])
# Verifies default state
result = run_command(["config", "dump", "--format", "json"])
assert result.ok
settings_json = json.loads(result.stdout)
assert not settings_json["library"]["enable_unsafe_install"]
# Delete config key
assert run_command(["config", "delete", "library.enable_unsafe_install"])
# Verifies value is not found, we read directly from file instead of using
# the dump command since that would still print the deleted value if it has
# a default
config_file = Path(working_dir, "arduino-cli.yaml")
config_lines = config_file.open().readlines()
assert "enable_unsafe_install" not in config_lines
# Verifies default state
result = run_command(["config", "dump", "--format", "json"])
assert result.ok
settings_json = json.loads(result.stdout)
assert [] == settings_json["board_manager"]["additional_urls"]
# Delete config key and sub keys
assert run_command(["config", "delete", "board_manager"])
# Verifies value is not found, we read directly from file instead of using
# the dump command since that would still print the deleted value if it has
# a default
config_file = Path(working_dir, "arduino-cli.yaml")
config_lines = config_file.open().readlines()
assert "additional_urls" not in config_lines
assert "board_manager" not in config_lines
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment