2017-06-05 16:02:39 +03:00
|
|
|
# Copyright (c) 2014-present PlatformIO <contact@platformio.org>
|
2015-11-18 17:16:17 +02:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2015-02-20 13:48:17 +02:00
|
|
|
|
2016-05-28 22:51:33 +03:00
|
|
|
import json
|
2016-08-02 19:10:29 +03:00
|
|
|
from os import getcwd, makedirs
|
2015-02-20 13:48:17 +02:00
|
|
|
from os.path import getsize, isdir, isfile, join
|
|
|
|
|
2016-08-02 19:10:29 +03:00
|
|
|
from platformio.commands.boards import cli as cmd_boards
|
2020-02-06 23:32:43 +02:00
|
|
|
from platformio.commands.project import project_init as cmd_init
|
2019-05-27 22:25:22 +03:00
|
|
|
from platformio.project.config import ProjectConfig
|
2019-11-28 18:14:10 +02:00
|
|
|
from platformio.project.exception import ProjectEnvsNotAvailableError
|
2015-02-20 13:48:17 +02:00
|
|
|
|
|
|
|
|
|
|
|
def validate_pioproject(pioproject_dir):
|
|
|
|
pioconf_path = join(pioproject_dir, "platformio.ini")
|
|
|
|
assert isfile(pioconf_path) and getsize(pioconf_path) > 0
|
2019-09-23 23:13:48 +03:00
|
|
|
assert isdir(join(pioproject_dir, "src")) and isdir(join(pioproject_dir, "lib"))
|
2015-02-20 13:48:17 +02:00
|
|
|
|
|
|
|
|
2016-08-10 21:58:12 +03:00
|
|
|
def test_init_default(clirunner, validate_cliresult):
|
2015-02-21 15:15:00 +02:00
|
|
|
with clirunner.isolated_filesystem():
|
2016-08-10 21:58:12 +03:00
|
|
|
result = clirunner.invoke(cmd_init)
|
2015-02-21 15:15:00 +02:00
|
|
|
validate_cliresult(result)
|
2015-02-20 13:48:17 +02:00
|
|
|
validate_pioproject(getcwd())
|
|
|
|
|
|
|
|
|
2016-08-10 21:58:12 +03:00
|
|
|
def test_init_ext_folder(clirunner, validate_cliresult):
|
2015-02-21 15:15:00 +02:00
|
|
|
with clirunner.isolated_filesystem():
|
2015-02-20 13:48:17 +02:00
|
|
|
ext_folder_name = "ext_folder"
|
|
|
|
makedirs(ext_folder_name)
|
2016-08-10 21:58:12 +03:00
|
|
|
result = clirunner.invoke(cmd_init, ["-d", ext_folder_name])
|
2015-02-21 15:15:00 +02:00
|
|
|
validate_cliresult(result)
|
2015-02-20 13:48:17 +02:00
|
|
|
validate_pioproject(join(getcwd(), ext_folder_name))
|
|
|
|
|
|
|
|
|
2016-08-10 21:58:12 +03:00
|
|
|
def test_init_duplicated_boards(clirunner, validate_cliresult, tmpdir):
|
|
|
|
with tmpdir.as_cwd():
|
|
|
|
for _ in range(2):
|
|
|
|
result = clirunner.invoke(cmd_init, ["-b", "uno", "-b", "uno"])
|
|
|
|
validate_cliresult(result)
|
|
|
|
validate_pioproject(str(tmpdir))
|
2019-05-27 22:25:22 +03:00
|
|
|
config = ProjectConfig(join(getcwd(), "platformio.ini"))
|
|
|
|
config.validate()
|
2016-08-10 21:58:12 +03:00
|
|
|
assert set(config.sections()) == set(["env:uno"])
|
|
|
|
|
|
|
|
|
2018-01-13 01:19:41 +02:00
|
|
|
def test_init_ide_without_board(clirunner, tmpdir):
|
2016-08-10 21:58:12 +03:00
|
|
|
with tmpdir.as_cwd():
|
|
|
|
result = clirunner.invoke(cmd_init, ["--ide", "atom"])
|
2019-05-09 00:51:28 +03:00
|
|
|
assert result.exit_code != 0
|
2019-11-28 18:14:10 +02:00
|
|
|
assert isinstance(result.exception, ProjectEnvsNotAvailableError)
|
2016-08-10 21:58:12 +03:00
|
|
|
|
|
|
|
|
2020-06-12 23:47:12 +03:00
|
|
|
def test_init_ide_vscode(clirunner, validate_cliresult, tmpdir):
|
2016-08-10 21:58:12 +03:00
|
|
|
with tmpdir.as_cwd():
|
|
|
|
result = clirunner.invoke(
|
2020-06-12 23:47:12 +03:00
|
|
|
cmd_init, ["--ide", "vscode", "-b", "uno", "-b", "teensy31"]
|
2019-09-23 23:13:48 +03:00
|
|
|
)
|
2016-08-10 21:58:12 +03:00
|
|
|
validate_cliresult(result)
|
|
|
|
validate_pioproject(str(tmpdir))
|
2019-09-23 23:13:48 +03:00
|
|
|
assert all(
|
2021-02-27 17:13:30 +02:00
|
|
|
tmpdir.join(".vscode").join(f).check()
|
|
|
|
for f in ("c_cpp_properties.json", "launch.json")
|
2020-06-12 23:47:12 +03:00
|
|
|
)
|
|
|
|
assert (
|
|
|
|
"framework-arduino-avr"
|
|
|
|
in tmpdir.join(".vscode").join("c_cpp_properties.json").read()
|
2019-09-23 23:13:48 +03:00
|
|
|
)
|
2016-08-10 21:58:12 +03:00
|
|
|
|
|
|
|
# switch to NodeMCU
|
2020-06-12 23:47:12 +03:00
|
|
|
result = clirunner.invoke(cmd_init, ["--ide", "vscode", "-b", "nodemcuv2"])
|
|
|
|
validate_cliresult(result)
|
|
|
|
validate_pioproject(str(tmpdir))
|
|
|
|
assert (
|
|
|
|
"framework-arduinoespressif8266"
|
|
|
|
in tmpdir.join(".vscode").join("c_cpp_properties.json").read()
|
|
|
|
)
|
|
|
|
|
|
|
|
# switch to teensy31 via env name
|
|
|
|
result = clirunner.invoke(cmd_init, ["--ide", "vscode", "-e", "teensy31"])
|
2016-08-10 21:58:12 +03:00
|
|
|
validate_cliresult(result)
|
|
|
|
validate_pioproject(str(tmpdir))
|
2020-06-12 23:47:12 +03:00
|
|
|
assert (
|
|
|
|
"framework-arduinoteensy"
|
|
|
|
in tmpdir.join(".vscode").join("c_cpp_properties.json").read()
|
|
|
|
)
|
2016-08-10 21:58:12 +03:00
|
|
|
|
|
|
|
# switch to the first board
|
2020-06-12 23:47:12 +03:00
|
|
|
result = clirunner.invoke(cmd_init, ["--ide", "vscode"])
|
2016-08-10 21:58:12 +03:00
|
|
|
validate_cliresult(result)
|
|
|
|
validate_pioproject(str(tmpdir))
|
2020-06-12 23:47:12 +03:00
|
|
|
assert (
|
|
|
|
"framework-arduino-avr"
|
|
|
|
in tmpdir.join(".vscode").join("c_cpp_properties.json").read()
|
|
|
|
)
|
2016-08-10 21:58:12 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_init_ide_eclipse(clirunner, validate_cliresult):
|
2016-06-15 18:42:25 +03:00
|
|
|
with clirunner.isolated_filesystem():
|
2016-08-10 21:58:12 +03:00
|
|
|
result = clirunner.invoke(cmd_init, ["-b", "uno", "--ide", "eclipse"])
|
2016-06-15 18:42:25 +03:00
|
|
|
validate_cliresult(result)
|
|
|
|
validate_pioproject(getcwd())
|
2021-02-27 17:13:30 +02:00
|
|
|
assert all(isfile(f) for f in (".cproject", ".project"))
|
2016-06-15 18:42:25 +03:00
|
|
|
|
|
|
|
|
2016-08-10 21:58:12 +03:00
|
|
|
def test_init_special_board(clirunner, validate_cliresult):
|
2015-02-21 15:15:00 +02:00
|
|
|
with clirunner.isolated_filesystem():
|
2016-08-10 21:58:12 +03:00
|
|
|
result = clirunner.invoke(cmd_init, ["-b", "uno"])
|
2015-02-21 15:15:00 +02:00
|
|
|
validate_cliresult(result)
|
2015-02-20 13:48:17 +02:00
|
|
|
validate_pioproject(getcwd())
|
|
|
|
|
2016-05-28 22:51:33 +03:00
|
|
|
result = clirunner.invoke(cmd_boards, ["Arduino Uno", "--json-output"])
|
|
|
|
validate_cliresult(result)
|
|
|
|
boards = json.loads(result.output)
|
|
|
|
|
2019-05-27 22:25:22 +03:00
|
|
|
config = ProjectConfig(join(getcwd(), "platformio.ini"))
|
|
|
|
config.validate()
|
2015-02-23 11:50:14 +02:00
|
|
|
|
2019-09-23 23:13:48 +03:00
|
|
|
expected_result = dict(
|
|
|
|
platform=str(boards[0]["platform"]),
|
|
|
|
board="uno",
|
|
|
|
framework=[str(boards[0]["frameworks"][0])],
|
|
|
|
)
|
2015-05-07 15:53:06 +01:00
|
|
|
assert config.has_section("env:uno")
|
2019-05-30 21:27:12 +03:00
|
|
|
assert sorted(config.items(env="uno", as_dict=True).items()) == sorted(
|
2019-09-23 23:13:48 +03:00
|
|
|
expected_result.items()
|
|
|
|
)
|
2015-02-20 13:48:17 +02:00
|
|
|
|
|
|
|
|
2016-08-10 21:58:12 +03:00
|
|
|
def test_init_enable_auto_uploading(clirunner, validate_cliresult):
|
2015-02-21 15:15:00 +02:00
|
|
|
with clirunner.isolated_filesystem():
|
2016-09-01 16:05:02 +03:00
|
|
|
result = clirunner.invoke(
|
2019-09-23 23:13:48 +03:00
|
|
|
cmd_init, ["-b", "uno", "--project-option", "targets=upload"]
|
|
|
|
)
|
2015-02-21 15:15:00 +02:00
|
|
|
validate_cliresult(result)
|
2015-02-20 13:48:17 +02:00
|
|
|
validate_pioproject(getcwd())
|
2019-05-27 22:25:22 +03:00
|
|
|
config = ProjectConfig(join(getcwd(), "platformio.ini"))
|
|
|
|
config.validate()
|
2019-09-23 23:13:48 +03:00
|
|
|
expected_result = dict(
|
|
|
|
targets=["upload"], platform="atmelavr", board="uno", framework=["arduino"]
|
|
|
|
)
|
2015-05-07 15:53:06 +01:00
|
|
|
assert config.has_section("env:uno")
|
2019-05-30 21:27:12 +03:00
|
|
|
assert sorted(config.items(env="uno", as_dict=True).items()) == sorted(
|
2019-09-23 23:13:48 +03:00
|
|
|
expected_result.items()
|
|
|
|
)
|
2015-02-20 13:48:17 +02:00
|
|
|
|
|
|
|
|
2016-09-13 19:22:19 +03:00
|
|
|
def test_init_custom_framework(clirunner, validate_cliresult):
|
|
|
|
with clirunner.isolated_filesystem():
|
|
|
|
result = clirunner.invoke(
|
2019-09-23 23:13:48 +03:00
|
|
|
cmd_init, ["-b", "teensy31", "--project-option", "framework=mbed"]
|
|
|
|
)
|
2016-09-13 19:22:19 +03:00
|
|
|
validate_cliresult(result)
|
|
|
|
validate_pioproject(getcwd())
|
2019-05-27 22:25:22 +03:00
|
|
|
config = ProjectConfig(join(getcwd(), "platformio.ini"))
|
|
|
|
config.validate()
|
2019-09-23 23:13:48 +03:00
|
|
|
expected_result = dict(platform="teensy", board="teensy31", framework=["mbed"])
|
2016-09-13 19:22:19 +03:00
|
|
|
assert config.has_section("env:teensy31")
|
2019-09-23 23:13:48 +03:00
|
|
|
assert sorted(config.items(env="teensy31", as_dict=True).items()) == sorted(
|
|
|
|
expected_result.items()
|
|
|
|
)
|
2016-09-13 19:22:19 +03:00
|
|
|
|
|
|
|
|
2015-02-21 15:15:00 +02:00
|
|
|
def test_init_incorrect_board(clirunner):
|
2016-08-10 21:58:12 +03:00
|
|
|
result = clirunner.invoke(cmd_init, ["-b", "missed_board"])
|
2015-04-22 15:24:45 +01:00
|
|
|
assert result.exit_code == 2
|
2020-04-08 17:18:59 +03:00
|
|
|
assert "Error: Invalid value for" in result.output
|
2015-04-22 15:24:45 +01:00
|
|
|
assert isinstance(result.exception, SystemExit)
|