def test_checkout_libraries(self, mock_lib_refs): program = MbedProgram(None, MbedProgramFiles(None, pathlib.Path(), None), MbedOS(pathlib.Path(), None)) program.checkout_libraries() program.lib_references.checkout.assert_called_once()
def test_from_new_returns_valid_program(self, fs): root = pathlib.Path(fs, "foo") root.mkdir() program = MbedProgramFiles.from_new(root) self.assertTrue(program.app_config_file.exists())
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_new(cls, dir_path: Path) -> "MbedProgram": """Create an MbedProgram from an empty directory. Creates the directory if it doesn't exist. Args: dir_path: Directory in which to create the program. Raises: ExistingProgram: An existing program was found in the path. """ if _tree_contains_program(dir_path): raise ExistingProgram( f"An existing Mbed program was found in the directory tree {dir_path}. It is not possible to nest Mbed " "programs. Please ensure there is no .mbed file in the cwd hierarchy." ) logger.info(f"Creating Mbed program at path '{dir_path.resolve()}'") dir_path.mkdir(exist_ok=True) program_files = MbedProgramFiles.from_new(dir_path) logger.info( f"Creating git repository for the Mbed program '{dir_path}'") repo = git_utils.init(dir_path) mbed_os = MbedOS.from_new(dir_path / MBED_OS_DIR_NAME) return cls(repo, program_files, mbed_os)
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}'") try: program = MbedProgramFiles.from_existing(program_root) except ValueError as program_files_err: raise ProgramNotFound( f"{dir_path} doesn't look like a path to a valid program. {program_files_err}" ) repo = git_utils.get_repo(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(repo, 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(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_checks_for_unresolved_libraries(self, fs): root = pathlib.Path(fs, "root") make_mbed_lib_reference(root, resolved=True, ref_url="https://blah") make_mbed_lib_reference(root, name="my-unresolved-lib.lib", resolved=False, ref_url="https://blah") mbed_os_root = root / "mbed-os" mbed_os_root.mkdir() program = MbedProgram( None, MbedProgramFiles(None, pathlib.Path(root / ".mbed"), None), MbedOS(mbed_os_root, None)) self.assertTrue(program.has_unresolved_libraries())
def test_lists_all_known_libraries(self, fs): root = pathlib.Path(fs, "root") lib_ref = make_mbed_lib_reference(root, resolved=True, ref_url="https://blah") lib_ref_unresolved = make_mbed_lib_reference( root, name="my-unresolved-lib.lib", resolved=False, ref_url="https://blah") mbed_os_root = root / "mbed-os" mbed_os_root.mkdir() program = MbedProgram( None, MbedProgramFiles(None, pathlib.Path(root, ".mbed"), None), MbedOS(mbed_os_root, None)) libs = program.list_known_library_dependencies() self.assertEqual(str(lib_ref_unresolved), str(libs[0])) self.assertEqual(str(lib_ref), str(libs[1]))
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)
def test_from_new_raises_if_program_already_exists(self, fs): root = pathlib.Path(fs, "foo") make_mbed_program_files(root) with self.assertRaises(ValueError): MbedProgramFiles.from_new(root)