def test_from_existing_skips_rendering_cmake_template_if_file_exists( self, fs): with mock.patch( "mbed_tools.project._internal.project_data.render_cmakelists_template" ) as render_cmake: root = pathlib.Path(fs, "foo") make_mbed_program_files(root) (root / CMAKELISTS_FILE_NAME).touch() MbedProgramFiles.from_existing(root) render_cmake.assert_not_called()
def test_from_existing_finds_existing_program_data(self, fs): root = pathlib.Path(fs, "foo") make_mbed_program_files(root) program = MbedProgramFiles.from_existing(root) self.assertTrue(program.app_config_file.exists())
def from_existing(cls, dir_path: Path, check_mbed_os: bool = True) -> "MbedProgram": """Create an MbedProgram from an existing program directory. Args: dir_path: Directory containing an Mbed program. check_mbed_os: If True causes an exception to be raised if the Mbed OS source directory does not exist. Raises: ProgramNotFound: An existing program was not found in the path. """ program_root = _find_program_root(dir_path) logger.info(f"Found existing Mbed program at path '{program_root}'") program = MbedProgramFiles.from_existing(program_root) try: mbed_os = MbedOS.from_existing(program_root / MBED_OS_DIR_NAME, check_mbed_os) except ValueError as mbed_os_err: raise MbedOSNotFound( f"Mbed OS was not found due to the following error: {mbed_os_err}" "\nYou may need to resolve the mbed-os.lib reference. You can do this by performing a `checkout`." ) return cls(program, mbed_os)
def test_from_url_returns_valid_program(self, mock_clone, fs): fs_root = pathlib.Path(fs, "foo") url = "https://valid" mock_clone.side_effect = lambda *args: make_mbed_program_files(fs_root) program = MbedProgram.from_url(url, fs_root, False) self.assertEqual(program.files, MbedProgramFiles.from_existing(fs_root)) mock_clone.assert_called_once_with(url, fs_root)
def test_from_new_local_dir_generates_valid_program_creating_directory(self, fs): fs_root = pathlib.Path(fs, "foo") fs_root.mkdir() program_root = fs_root / "programfoo" program = from_new_set_target_toolchain(program_root) self.assertEqual(program.files, MbedProgramFiles.from_existing(program_root, DEFAULT_BUILD_SUBDIR))
def test_from_new_local_dir_generates_valid_program_creating_directory(self, fs): fs_root = pathlib.Path(fs, "foo") fs_root.mkdir() program_root = fs_root / "programfoo" program = MbedProgram.from_new(program_root) self.assertEqual(program.files, MbedProgramFiles.from_existing(program_root))
def test_from_new_local_dir_generates_valid_program(self, mock_init, fs): fs_root = pathlib.Path(fs, "foo") fs_root.mkdir() program_root = fs_root / "programfoo" program = MbedProgram.from_new(program_root) self.assertEqual(program.files, MbedProgramFiles.from_existing(program_root)) self.assertEqual(program.repo, mock_init.return_value) mock_init.assert_called_once_with(program_root)
def test_from_existing_finds_existing_program_data(self, tmp_path): root = pathlib.Path(tmp_path, "foo") make_mbed_program_files(root) program = MbedProgramFiles.from_existing( root, pathlib.Path("K64F", "develop", "GCC_ARM")) assert program.app_config_file.exists() assert program.mbed_os_ref.exists() assert program.cmakelists_file.exists()
def test_from_new_local_dir_generates_valid_program_existing_directory( self, tmp_path): fs_root = pathlib.Path(tmp_path, "foo") fs_root.mkdir() program_root = fs_root / "programfoo" program_root.mkdir() program = from_new_set_target_toolchain(program_root) assert program.files == MbedProgramFiles.from_existing( program_root, DEFAULT_BUILD_SUBDIR)
def test_from_existing_calls_render_cmakelists_template_with_program_name_if_file_nonexistent( self, fs): with mock.patch( "mbed_tools.project._internal.project_data.render_cmakelists_template" ) as render_cmake: root = pathlib.Path(fs, "foo") make_mbed_program_files(root) render_cmake.return_value = "foo" program_files = MbedProgramFiles.from_existing(root) render_cmake.assert_called_with(program_files.cmakelists_file, root.stem)
def test_from_new_local_dir_generates_valid_program_creating_directory_in_cwd(self, fs): old_cwd = os.getcwd() try: fs_root = pathlib.Path(fs, "foo") fs_root.mkdir() os.chdir(fs_root) program_root = pathlib.Path("programfoo") program = from_new_set_target_toolchain(program_root) self.assertEqual(program.files, MbedProgramFiles.from_existing(program_root, DEFAULT_BUILD_SUBDIR)) finally: os.chdir(old_cwd)
def from_url(cls, url: str, dst_path: Path, check_mbed_os: bool = True) -> "MbedProgram": """Fetch an Mbed program from a remote URL. Args: url: URL of the remote program repository. dst_path: Destination path for the cloned program. Raises: ExistingProgram: `dst_path` already contains an Mbed program. """ if _tree_contains_program(dst_path): raise ExistingProgram( f"The destination path '{dst_path}' already contains an Mbed program. Please set the destination path " "to an empty directory.") logger.info(f"Cloning Mbed program from URL '{url}'.") repo = git_utils.clone(url, dst_path) try: program_files = MbedProgramFiles.from_existing(dst_path) except ValueError as e: raise ProgramNotFound( f"This repository does not contain a valid Mbed program at the top level. {e} " "Cloned programs must contain an mbed-os.lib file containing the URL to the Mbed OS repository. It is " "possible you have cloned a repository containing multiple mbed-programs. If this is the case, you " "should cd to a directory containing a program before performing any other operations." ) try: mbed_os = MbedOS.from_existing(dst_path / MBED_OS_DIR_NAME, check_mbed_os) except ValueError as mbed_err: raise MbedOSNotFound(f"{mbed_err}") return cls(repo, program_files, mbed_os)
def test_from_existing_raises_if_program_doesnt_exist(self, fs): root = pathlib.Path(fs, "foo") root.mkdir() with self.assertRaises(ValueError): MbedProgramFiles.from_existing(root)