def test_raises_when_no_such_ref(self, mock_repo, mock_clone, mock_fetch, mock_checkout, tmp_path): num_times_called = 0 def clone_side_effect(url, dst_dir, *args): nonlocal num_times_called if num_times_called == 0: num_times_called += 1 raise VersionControlError("Failed to clone") elif num_times_called == 1: num_times_called += 1 dst_dir.mkdir() else: assert False fs_root = pathlib.Path(tmp_path, "foo") make_mbed_lib_reference(fs_root, ref_url="https://git#lajdhalk234") mock_clone.side_effect = clone_side_effect mock_fetch.side_effect = None mock_checkout.side_effect = VersionControlError("Failed to checkout") with pytest.raises(VersionControlError, match="Failed to checkout"): lib_refs = LibraryReferences(fs_root, ignore_paths=["mbed-os"]) lib_refs.fetch()
def test_does_not_resolve_references_in_ignore_paths(self, mock_get_repo, mock_checkout, mock_clone, tmp_path): fs_root = pathlib.Path(tmp_path, "mbed-os") make_mbed_lib_reference(fs_root, ref_url="https://git#lajdhalk234") lib_refs = LibraryReferences(fs_root, ignore_paths=["mbed-os"]) lib_refs.fetch() mock_clone.assert_not_called()
def test_does_not_perform_checkout_if_no_git_ref_exists(self, mock_init, mock_checkout, mock_clone, fs): fs_root = pathlib.Path(fs, "foo") make_mbed_lib_reference(fs_root, ref_url="https://git", resolved=True) lib_refs = LibraryReferences(fs_root, ignore_paths=[fs_root / "mbed-os"]) lib_refs.checkout(force=False) mock_checkout.assert_not_called()
def test_checks_for_unresolved_libraries(self, fs): root = pathlib.Path(fs, "root").absolute().resolve() make_mbed_program_files(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") program = MbedProgram.from_existing(root, check_mbed_os=False) self.assertTrue(program.has_unresolved_libraries())
def test_resolve_does_not_perform_checkout_if_no_git_ref_exists(self, mock_init, mock_checkout, mock_clone, fs): fs_root = pathlib.Path(fs, "foo") make_mbed_lib_reference(fs_root, ref_url="https://git") mock_clone.side_effect = lambda url, dst_dir: dst_dir.mkdir() lib_refs = LibraryReferences(fs_root, ignore_paths=[fs_root / "mbed-os"]) lib_refs.resolve() mock_checkout.assert_not_called()
def test_fetches_only_requested_ref(self, mock_repo, tmp_path): fs_root = pathlib.Path(tmp_path, "foo") fake_ref = "28eeee2b4c169739192600b92e7970dbbcabd8d0" make_mbed_lib_reference(fs_root, ref_url=f"https://git#{fake_ref}", resolved=True) lib_refs = LibraryReferences(fs_root, ignore_paths=["mbed-os"]) lib_refs.checkout(force=False) mock_repo().git.fetch.assert_called_once_with("origin", fake_ref)
def test_fetch_does_not_perform_checkout_if_no_git_ref_exists( self, mock_get_repo, mock_checkout, mock_clone, tmp_path): fs_root = pathlib.Path(tmp_path, "foo") make_mbed_lib_reference(fs_root, ref_url="https://git") mock_clone.side_effect = lambda url, dst_dir: dst_dir.mkdir() lib_refs = LibraryReferences(fs_root, ignore_paths=["mbed-os"]) lib_refs.fetch() mock_checkout.assert_not_called()
def test_does_perform_checkout_of_default_repo_branch_if_no_git_ref_exists( self, mock_get_repo, mock_checkout, mock_get_default_branch, mock_clone, tmp_path ): fs_root = pathlib.Path(tmp_path, "foo") make_mbed_lib_reference(fs_root, ref_url="https://git", resolved=True) lib_refs = LibraryReferences(fs_root, ignore_paths=["mbed-os"]) lib_refs.checkout(force=False) mock_checkout.assert_called_once_with(mock_get_repo(), mock_get_default_branch(), force=False)
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_doesnt_fetch_for_branch_or_tag(self, mock_clone, mock_fetch, mock_checkout, tmp_path): fs_root = pathlib.Path(tmp_path, "foo") make_mbed_lib_reference(fs_root, ref_url="https://git#lajdhalk234") mock_clone.side_effect = lambda url, dst_dir, *args: dst_dir.mkdir() lib_refs = LibraryReferences(fs_root, ignore_paths=["mbed-os"]) lib_refs.fetch() mock_fetch.assert_not_called() mock_checkout.assert_not_called()
def test_lists_all_known_libraries(self, fs): root = pathlib.Path(fs, "root").absolute().resolve() make_mbed_program_files(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" ) program = MbedProgram.from_existing(root, check_mbed_os=False) libs = program.list_known_library_dependencies() self.assertEqual(str(lib_ref_unresolved), str(libs[1])) self.assertEqual(str(lib_ref), str(libs[2]))
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_fetch_performs_checkout_if_ref_is_hash(self, mock_get_repo, mock_clone, mock_fetch, mock_checkout, tmp_path): num_times_called = 0 def clone_side_effect(url, dst_dir, *args): nonlocal num_times_called if num_times_called == 0: num_times_called += 1 raise VersionControlError("Failed to clone") elif num_times_called == 1: num_times_called += 1 dst_dir.mkdir() else: assert False fs_root = pathlib.Path(tmp_path, "foo") lib = make_mbed_lib_reference(fs_root, ref_url="https://git#398bc1a63370") mock_clone.side_effect = clone_side_effect lib_refs = LibraryReferences(fs_root, ignore_paths=["mbed-os"]) lib_refs.fetch() mock_clone.assert_called_with(lib.get_git_reference().repo_url, lib.source_code_path) mock_fetch.assert_called_once_with(None, lib.get_git_reference().ref) mock_checkout.assert_called_once_with(None, "FETCH_HEAD")
def test_performs_checkout_if_git_ref_exists(self, mock_init, mock_checkout, mock_clone, fs): fs_root = pathlib.Path(fs, "foo") lib = make_mbed_lib_reference(fs_root, ref_url="https://git#lajdhalk234", resolved=True) lib_refs = LibraryReferences(fs_root, ignore_paths=[fs_root / "mbed-os"]) lib_refs.checkout(force=False) mock_checkout.assert_called_once_with(mock_init.return_value, lib.get_git_reference().ref, force=False)
def test_resolve_performs_checkout_if_git_ref_exists(self, mock_init, mock_checkout, mock_clone, fs): fs_root = pathlib.Path(fs, "foo") lib = make_mbed_lib_reference(fs_root, ref_url="https://git#lajdhalk234") mock_clone.side_effect = lambda url, dst_dir: dst_dir.mkdir() lib_refs = LibraryReferences(fs_root, ignore_paths=[fs_root / "mbed-os"]) lib_refs.resolve() mock_checkout.assert_called_once_with(None, lib.get_git_reference().ref)
def test_hydrates_top_level_library_references(self, mock_clone, fs): fs_root = pathlib.Path(fs, "foo") lib = make_mbed_lib_reference(fs_root, ref_url="https://git") mock_clone.side_effect = lambda url, dst_dir: dst_dir.mkdir() lib_refs = LibraryReferences(fs_root, ignore_paths=[fs_root / "mbed-os"]) lib_refs.resolve() mock_clone.assert_called_once_with(lib.get_git_reference().repo_url, lib.source_code_path) self.assertTrue(lib.is_resolved())
def test_hydrates_recursive_dependencies(self, mock_clone, fs): fs_root = pathlib.Path(fs, "foo") lib = make_mbed_lib_reference(fs_root, ref_url="https://git") # Create a lib reference without touching the fs at this point, we want to mock the effects of a recursive # reference lookup and we need to assert the reference was resolved. lib2 = MbedLibReference( reference_file=(lib.source_code_path / "lib2.lib"), source_code_path=(lib.source_code_path / "lib2") ) # Here we mock the effects of a recursive reference lookup. We create a new lib reference as a side effect of # the first call to the mock. Then we create the src dir, thus resolving the lib, on the second call. mock_clone.side_effect = lambda url, dst_dir: ( make_mbed_lib_reference(pathlib.Path(dst_dir), name=lib2.reference_file.name, ref_url="https://valid2"), lib2.source_code_path.mkdir(), ) lib_refs = LibraryReferences(fs_root, ignore_paths=[fs_root / "mbed-os"]) lib_refs.resolve() self.assertTrue(lib.is_resolved()) self.assertTrue(lib2.is_resolved())
def test_get_git_reference_returns_lib_file_contents(self, fs): root = pathlib.Path(fs, "foo") url = "https://github.com/mylibrepo" ref = "latest" full_ref = f"{url}#{ref}" lib = make_mbed_lib_reference(root, ref_url=full_ref) reference = lib.get_git_reference() self.assertEqual(reference.repo_url, url) self.assertEqual(reference.ref, ref)
def test_get_git_reference_returns_lib_file_contents(self, tmp_path): root = pathlib.Path(tmp_path, "foo") url = "https://github.com/mylibrepo" ref = "latest" references = [f"{url}#{ref}", f"{url}/#{ref}"] for full_ref in references: lib = make_mbed_lib_reference(root, ref_url=full_ref) reference = lib.get_git_reference() assert reference.repo_url == url assert reference.ref == ref
def test_performs_checkout_if_git_ref_exists(self, mock_get_repo, mock_checkout, mock_fetch, mock_clone, tmp_path): fs_root = pathlib.Path(tmp_path, "foo") lib = make_mbed_lib_reference(fs_root, ref_url="https://git#lajdhalk234", resolved=True) lib_refs = LibraryReferences(fs_root, ignore_paths=["mbed-os"]) lib_refs.checkout(force=False) mock_fetch.assert_called_once_with(mock_get_repo(), lib.get_git_reference().ref) mock_checkout.assert_called_once_with(mock_get_repo.return_value, "FETCH_HEAD", force=False)
def test_is_resolved_returns_false_if_source_code_dir_doesnt_exist( self, fs): root = pathlib.Path(fs, "foo") lib = make_mbed_lib_reference(root) self.assertFalse(lib.is_resolved())
def test_is_resolved_returns_true_if_source_code_dir_exists(self, fs): root = pathlib.Path(fs, "foo") lib = make_mbed_lib_reference(root, resolved=True) self.assertTrue(lib.is_resolved())
def test_is_resolved_returns_false_if_source_code_dir_doesnt_exist( self, tmp_path): root = pathlib.Path(tmp_path, "foo") lib = make_mbed_lib_reference(root) assert not lib.is_resolved()
def test_is_resolved_returns_true_if_source_code_dir_exists( self, tmp_path): root = pathlib.Path(tmp_path, "foo") lib = make_mbed_lib_reference(root, resolved=True) assert lib.is_resolved()