Skip to content

Commit

Permalink
Share all code when performing a ref-map test
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Nov 13, 2022
1 parent f3a6424 commit 886c017
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 66 deletions.
56 changes: 41 additions & 15 deletions git-repository/tests/remote/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,53 @@ pub(crate) fn repo(name: &str) -> git::Repository {
git::open_opts(repo_path(name), git::open::Options::isolated()).unwrap()
}

/// Spawn a git-daemon hosting all directories in or below `base_dir` if we are in async mode - currently only TCP is available in async mode,
/// and it's probably going to stay that way as we don't want to chose a particular runtime in lower-level crates which
/// provide the blocking implementation for everything. Maybe this changes one day once we implement other protocols
/// like spawning a process via `tokio` or `async-std`.
#[cfg(any(feature = "blocking-network-client", feature = "async-network-client-async-std"))]
#[cfg_attr(not(feature = "async-network-client-async-std"), allow(unused_variables))]
pub(crate) fn spawn_git_daemon_if_async<'repo, 'a>(
base_dir: impl AsRef<std::path::Path>,
) -> std::io::Result<Option<git_testtools::GitDaemon>> {
#[cfg(feature = "blocking-network-client")]
{
Ok(None)
}
#[cfg(feature = "async-network-client-async-std")]
{
git_testtools::spawn_git_daemon(base_dir).map(Some)
}
}

/// Turn `remote` into a remote that interacts with the git daemon at `daemon_url`, and `repo_path`
/// can be a specific repo, or it can be empty if the repo is hosted at the root.
#[cfg(feature = "async-network-client-async-std")]
pub(crate) fn into_daemon_remote<'repo, 'a>(
#[cfg(any(feature = "blocking-network-client", feature = "async-network-client-async-std"))]
#[cfg_attr(not(feature = "async-network-client-async-std"), allow(unused_variables))]
pub(crate) fn into_daemon_remote_if_async<'repo, 'a>(
remote: git::Remote<'repo>,
daemon_url: &str,
repo_path: impl Into<Option<&'a str>>,
daemon: Option<&git_testtools::GitDaemon>,
) -> git::Remote<'repo> {
let mut new_remote = remote
.repo()
.remote_at(format!("{}/{}", daemon_url, repo_path.into().unwrap_or_default()))
.expect("valid url to create remote at");
for direction in [git::remote::Direction::Fetch, git::remote::Direction::Push] {
#[cfg(feature = "blocking-network-client")]
{
remote
}
#[cfg(feature = "async-network-client-async-std")]
{
let mut new_remote = remote
.repo()
.remote_at(format!("{}/", daemon.expect("daemon is available in async mode").url))
.expect("valid url to create remote at");
for direction in [git::remote::Direction::Fetch, git::remote::Direction::Push] {
new_remote
.replace_refspecs(
remote.refspecs(direction).iter().map(|s| s.to_ref().to_bstring()),
direction,
)
.expect("input refspecs valid");
}
new_remote
.replace_refspecs(
remote.refspecs(direction).iter().map(|s| s.to_ref().to_bstring()),
direction,
)
.expect("input refspecs valid");
}
new_remote
}

pub(crate) fn cow_str(s: &str) -> Cow<str> {
Expand Down
56 changes: 5 additions & 51 deletions git-repository/tests/remote/ref_map.rs
Original file line number Diff line number Diff line change
@@ -1,65 +1,19 @@
#[cfg(feature = "blocking-network-client")]
mod blocking_io {
#[cfg(any(feature = "blocking-network-client", feature = "async-network-client-async-std"))]
mod blocking_and_async_io {
use git_features::progress;
use git_repository as git;
use git_repository::remote::Direction::Fetch;

use crate::remote;

#[test]
fn all() -> crate::Result {
for (version, expected_remote_refs) in [
(None, 11),
(Some(git::protocol::transport::Protocol::V2), 11),
(Some(git::protocol::transport::Protocol::V1), 14), // V1 doesn't support prefiltering.
] {
let mut repo = remote::repo("clone");
if let Some(version) = version {
repo.config_snapshot_mut().set_raw_value(
"protocol",
None,
"version",
(version as u8).to_string().as_str(),
)?;
}

let remote = repo.find_remote("origin")?;
let map = remote.connect(Fetch, progress::Discard)?.ref_map(Default::default())?;
assert_eq!(
map.remote_refs.len(),
expected_remote_refs,
"{:?}: it gets all remote refs, independently of the refspec. But we use a prefix so pre-filter them.",
version
);

assert_eq!(map.fixes.len(), 0);
assert_eq!(
map.mappings.len(),
11,
"mappings are only a sub-set of all remotes due to refspec matching, tags are filtered out."
);
}
Ok(())
}
}

#[cfg(feature = "async-network-client-async-std")]
mod async_io {
use git_features::progress;
use git_repository as git;
use git_repository::remote::Direction::Fetch;
use git_testtools::spawn_git_daemon;

use crate::remote;
use crate::remote::into_daemon_remote;
use crate::remote::{into_daemon_remote_if_async, spawn_git_daemon_if_async};
use git_protocol::maybe_async;

#[maybe_async::test(
feature = "blocking-network-client",
async(feature = "async-network-client-async-std", async_std::test)
)]
async fn all() -> crate::Result {
let daemon = spawn_git_daemon(remote::repo_path("base"))?;
let daemon = spawn_git_daemon_if_async(remote::repo_path("base"))?;
for (version, expected_remote_refs) in [
(None, 11),
(Some(git::protocol::transport::Protocol::V2), 11),
Expand All @@ -75,7 +29,7 @@ mod async_io {
)?;
}

let remote = into_daemon_remote(repo.find_remote("origin")?, &daemon.url, None);
let remote = into_daemon_remote_if_async(repo.find_remote("origin")?, daemon.as_ref());
let map = remote
.connect(Fetch, progress::Discard)
.await?
Expand Down

0 comments on commit 886c017

Please sign in to comment.