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_returns_valid_program_file_set(self, fs): root = pathlib.Path(fs, "foo") root.mkdir() program = MbedProgramFiles.from_new(root) self.assertTrue(program.app_config_file.exists()) self.assertTrue(program.mbed_os_ref.exists()) self.assertTrue(program.cmakelists_file.exists())
def test_from_new_returns_valid_program_file_set(self, tmp_path): root = pathlib.Path(tmp_path, "foo") root.mkdir() program = MbedProgramFiles.from_new(root) 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, fs): fs_root = pathlib.Path(fs, "foo") fs_root.mkdir() program_root = fs_root / "programfoo" program_root.mkdir() program = from_new_set_target_toolchain(program_root) self.assertEqual(program.files, MbedProgramFiles.from_existing(program_root, DEFAULT_BUILD_SUBDIR))
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()) self.assertTrue(program.mbed_os_ref.exists()) self.assertTrue(program.cmakelists_file.exists())
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_creating_directory( self, tmp_path): fs_root = pathlib.Path(tmp_path, "foo") fs_root.mkdir() program_root = fs_root / "programfoo" program = from_new_set_target_toolchain(program_root) assert program.files == MbedProgramFiles.from_existing( program_root, DEFAULT_BUILD_SUBDIR)
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_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 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 test_from_new_calls_render_template_for_gitignore_and_main( self, tmp_path): with mock.patch( "mbed_tools.project._internal.project_data.render_cmakelists_template" ) as render_cmakelists_template, mock.patch( "mbed_tools.project._internal.project_data.render_main_cpp_template" ) as render_main_cpp_template, mock.patch( "mbed_tools.project._internal.project_data.render_gitignore_template" ) as render_gitignore_template: root = pathlib.Path(tmp_path, "foo") root.mkdir() program_files = MbedProgramFiles.from_new(root) render_cmakelists_template.assert_called_once_with( program_files.cmakelists_file, "foo") render_main_cpp_template.assert_called_once_with( root / MAIN_CPP_FILE_NAME) render_gitignore_template.assert_called_once_with(root / ".gitignore")
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-os.lib 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}'") mbed_os = MbedOS.from_new(dir_path / MBED_OS_DIR_NAME) return cls(program_files, mbed_os)
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 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 `deploy`." ) return cls(program, mbed_os)
def test_from_new_raises_if_program_files_already_exist(self, fs): root = pathlib.Path(fs, "foo") make_mbed_program_files(root) with self.assertRaises(ValueError): MbedProgramFiles.from_new(root)
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_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_raises_if_program_files_already_exist(self, tmp_path): root = pathlib.Path(tmp_path, "foo") make_mbed_program_files(root) with pytest.raises(ValueError): MbedProgramFiles.from_new(root)