|
13 | 13 | # software without disclosing the source code of your own applications. To purchase
|
14 | 14 | # a commercial license, send an email to [email protected].
|
15 | 15 | from pathlib import Path
|
| 16 | +import json |
| 17 | +import yaml |
16 | 18 |
|
17 | 19 |
|
18 | 20 | def test_init(run_command, data_dir, working_dir):
|
19 | 21 | result = run_command("config init")
|
| 22 | + assert "" == result.stderr |
20 | 23 | assert result.ok
|
21 | 24 | assert data_dir in result.stdout
|
22 | 25 |
|
23 | 26 |
|
24 |
| -def test_init_dest(run_command, working_dir): |
25 |
| - dest = str(Path(working_dir) / "config" / "test") |
| 27 | +def test_init_with_existing_custom_config(run_command, data_dir, working_dir, downloads_dir): |
| 28 | + result = run_command("config init --additional-urls https://example.com") |
| 29 | + assert result.ok |
| 30 | + assert data_dir in result.stdout |
| 31 | + |
| 32 | + config_file = open(Path(data_dir) / "arduino-cli.yaml", "r") |
| 33 | + configs = yaml.load(config_file.read(), Loader=yaml.FullLoader) |
| 34 | + config_file.close() |
| 35 | + assert ["https://example.com"] == configs["board_manager"]["additional_urls"] |
| 36 | + assert "50051" == configs["daemon"]["port"] |
| 37 | + assert data_dir == configs["directories"]["data"] |
| 38 | + assert downloads_dir == configs["directories"]["downloads"] |
| 39 | + assert data_dir == configs["directories"]["user"] |
| 40 | + assert "" == configs["logging"]["file"] |
| 41 | + assert "text" == configs["logging"]["format"] |
| 42 | + assert "info" == configs["logging"]["level"] |
| 43 | + assert ":9090" == configs["telemetry"]["addr"] |
| 44 | + assert configs["telemetry"]["enabled"] |
| 45 | + |
| 46 | + config_file_path = Path(working_dir) / "config" / "test" / "config.yaml" |
| 47 | + assert not config_file_path.exists() |
| 48 | + result = run_command(f'config init --dest-file "{config_file_path}"') |
| 49 | + assert result.ok |
| 50 | + assert str(config_file_path) in result.stdout |
| 51 | + |
| 52 | + config_file = open(config_file_path, "r") |
| 53 | + configs = yaml.load(config_file.read(), Loader=yaml.FullLoader) |
| 54 | + config_file.close() |
| 55 | + assert [] == configs["board_manager"]["additional_urls"] |
| 56 | + assert "50051" == configs["daemon"]["port"] |
| 57 | + assert data_dir == configs["directories"]["data"] |
| 58 | + assert downloads_dir == configs["directories"]["downloads"] |
| 59 | + assert data_dir == configs["directories"]["user"] |
| 60 | + assert "" == configs["logging"]["file"] |
| 61 | + assert "text" == configs["logging"]["format"] |
| 62 | + assert "info" == configs["logging"]["level"] |
| 63 | + assert ":9090" == configs["telemetry"]["addr"] |
| 64 | + assert configs["telemetry"]["enabled"] |
| 65 | + |
| 66 | + |
| 67 | +def test_init_dest_absolute_path(run_command, working_dir): |
| 68 | + dest = Path(working_dir) / "config" / "test" |
| 69 | + expected_config_file = dest / "arduino-cli.yaml" |
| 70 | + assert not expected_config_file.exists() |
| 71 | + result = run_command(f'config init --dest-dir "{dest}"') |
| 72 | + assert result.ok |
| 73 | + assert str(expected_config_file) in result.stdout |
| 74 | + assert expected_config_file.exists() |
| 75 | + |
| 76 | + |
| 77 | +def test_init_dest_relative_path(run_command, working_dir): |
| 78 | + dest = Path(working_dir) / "config" / "test" |
| 79 | + expected_config_file = dest / "arduino-cli.yaml" |
| 80 | + assert not expected_config_file.exists() |
| 81 | + result = run_command('config init --dest-dir "config/test"') |
| 82 | + assert result.ok |
| 83 | + assert str(expected_config_file) in result.stdout |
| 84 | + assert expected_config_file.exists() |
| 85 | + |
| 86 | + |
| 87 | +def test_init_dest_flag_with_overwrite_flag(run_command, working_dir): |
| 88 | + dest = Path(working_dir) / "config" / "test" |
| 89 | + |
| 90 | + expected_config_file = dest / "arduino-cli.yaml" |
| 91 | + assert not expected_config_file.exists() |
| 92 | + |
| 93 | + result = run_command(f'config init --dest-dir "{dest}"') |
| 94 | + assert result.ok |
| 95 | + assert expected_config_file.exists() |
| 96 | + |
26 | 97 | result = run_command(f'config init --dest-dir "{dest}"')
|
| 98 | + assert result.failed |
| 99 | + assert "Config file already exists, use --overwrite to discard the existing one." in result.stderr |
| 100 | + |
| 101 | + result = run_command(f'config init --dest-dir "{dest}" --overwrite') |
| 102 | + assert result.ok |
| 103 | + assert str(expected_config_file) in result.stdout |
| 104 | + |
| 105 | + |
| 106 | +def test_init_dest_and_config_file_flags(run_command, working_dir): |
| 107 | + result = run_command('config init --dest-file "some_other_path" --dest-dir "some_path"') |
| 108 | + assert result.failed |
| 109 | + assert "Can't use both --dest-file and --dest-dir flags at the same time." in result.stderr |
| 110 | + |
| 111 | + |
| 112 | +def test_init_config_file_flag_absolute_path(run_command, working_dir): |
| 113 | + config_file = Path(working_dir) / "config" / "test" / "config.yaml" |
| 114 | + assert not config_file.exists() |
| 115 | + result = run_command(f'config init --dest-file "{config_file}"') |
| 116 | + assert result.ok |
| 117 | + assert str(config_file) in result.stdout |
| 118 | + assert config_file.exists() |
| 119 | + |
| 120 | + |
| 121 | +def test_init_config_file_flag_relative_path(run_command, working_dir): |
| 122 | + config_file = Path(working_dir) / "config.yaml" |
| 123 | + assert not config_file.exists() |
| 124 | + result = run_command('config init --dest-file "config.yaml"') |
| 125 | + assert result.ok |
| 126 | + assert str(config_file) in result.stdout |
| 127 | + assert config_file.exists() |
| 128 | + |
| 129 | + |
| 130 | +def test_init_config_file_flag_with_overwrite_flag(run_command, working_dir): |
| 131 | + config_file = Path(working_dir) / "config" / "test" / "config.yaml" |
| 132 | + assert not config_file.exists() |
| 133 | + |
| 134 | + result = run_command(f'config init --dest-file "{config_file}"') |
| 135 | + assert result.ok |
| 136 | + assert config_file.exists() |
| 137 | + |
| 138 | + result = run_command(f'config init --dest-file "{config_file}"') |
| 139 | + assert result.failed |
| 140 | + assert "Config file already exists, use --overwrite to discard the existing one." in result.stderr |
| 141 | + |
| 142 | + result = run_command(f'config init --dest-file "{config_file}" --overwrite') |
| 143 | + assert result.ok |
| 144 | + assert str(config_file) in result.stdout |
| 145 | + |
| 146 | + |
| 147 | +def test_dump(run_command, data_dir, working_dir): |
| 148 | + # Create a config file first |
| 149 | + config_file = Path(working_dir) / "config" / "test" / "config.yaml" |
| 150 | + assert not config_file.exists() |
| 151 | + result = run_command(f'config init --dest-file "{config_file}"') |
| 152 | + assert result.ok |
| 153 | + assert config_file.exists() |
| 154 | + |
| 155 | + result = run_command(f'config dump --config-file "{config_file}" --format json') |
| 156 | + assert result.ok |
| 157 | + settings_json = json.loads(result.stdout) |
| 158 | + assert [] == settings_json["board_manager"]["additional_urls"] |
| 159 | + |
| 160 | + result = run_command('config init --additional-urls "https://example.com"') |
| 161 | + assert result.ok |
| 162 | + config_file = Path(data_dir) / "arduino-cli.yaml" |
| 163 | + assert str(config_file) in result.stdout |
| 164 | + assert config_file.exists() |
| 165 | + |
| 166 | + result = run_command("config dump --format json") |
| 167 | + assert result.ok |
| 168 | + settings_json = json.loads(result.stdout) |
| 169 | + assert ["https://example.com"] == settings_json["board_manager"]["additional_urls"] |
| 170 | + |
| 171 | + |
| 172 | +def test_dump_with_config_file_flag(run_command, working_dir): |
| 173 | + # Create a config file first |
| 174 | + config_file = Path(working_dir) / "config" / "test" / "config.yaml" |
| 175 | + assert not config_file.exists() |
| 176 | + result = run_command(f'config init --dest-file "{config_file}" --additional-urls=https://example.com') |
| 177 | + assert result.ok |
| 178 | + assert config_file.exists() |
| 179 | + |
| 180 | + result = run_command(f'config dump --config-file "{config_file}" --format json') |
| 181 | + assert result.ok |
| 182 | + settings_json = json.loads(result.stdout) |
| 183 | + assert ["https://example.com"] == settings_json["board_manager"]["additional_urls"] |
| 184 | + |
| 185 | + result = run_command( |
| 186 | + f'config dump --config-file "{config_file}" --additional-urls=https://another-url.com --format json' |
| 187 | + ) |
27 | 188 | assert result.ok
|
28 |
| - assert dest in result.stdout |
| 189 | + settings_json = json.loads(result.stdout) |
| 190 | + assert ["https://another-url.com"] == settings_json["board_manager"]["additional_urls"] |
0 commit comments