Skip to content

Commit

Permalink
setup extra test run for selective gitoxide-enabled tests
Browse files Browse the repository at this point in the history
Using a rust config flag we are able to selective run only those
tests that have been cleared to run with `gitoxide`, as part of
a separate test run.

That way we will keep testing all `git2` related code as before
but step by step enable more tests to work correctly with `gitoxide`.

For now it's only planned to run git-related tests, but it's possible
to one-day run the entire test suite if oen were to be willing to pay
the cost.

CI is configured to run separate tests for `gitoxide` as well.
  • Loading branch information
Byron committed Dec 1, 2022
1 parent a48f8ee commit 62af2de
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/main.yml
Expand Up @@ -80,6 +80,8 @@ jobs:

# Deny warnings on CI to avoid warnings getting into the codebase.
- run: cargo test --features 'deny-warnings'
- name: gitoxide tests (git-related and enabled tests only)
run: RUSTFLAGS='--cfg test_gitoxide' cargo test git
# The testsuite generates a huge amount of data, and fetch-smoke-test was
# running out of disk space.
- name: Clear test output
Expand Down
34 changes: 33 additions & 1 deletion crates/cargo-test-macro/src/lib.rs
Expand Up @@ -32,6 +32,8 @@ pub fn cargo_test(attr: TokenStream, item: TokenStream) -> TokenStream {
};
}
let is_not_nightly = !version().1;
#[cfg(test_gitoxide)]
let mut enable_gitoxide_test = false;
for rule in split_rules(attr) {
match rule.as_str() {
"build_std_real" => {
Expand Down Expand Up @@ -59,6 +61,12 @@ pub fn cargo_test(attr: TokenStream, item: TokenStream) -> TokenStream {
requires_reason = true;
set_ignore!(is_not_nightly, "requires nightly");
}
"gitoxide" => {
#[cfg(test_gitoxide)]
{
enable_gitoxide_test = true;
}
}
s if s.starts_with("requires_") => {
let command = &s[9..];
set_ignore!(!has_command(command), "{command} not installed");
Expand All @@ -81,6 +89,13 @@ pub fn cargo_test(attr: TokenStream, item: TokenStream) -> TokenStream {
such as #[cargo_test(nightly, reason = \"needs -Z unstable-thing\")]"
);
}
#[cfg(test_gitoxide)]
if !enable_gitoxide_test {
ignore = true;
implicit_reasons.push(
"not yet enabled for using 'gitoxide' and tested in the standard test run".to_string(),
)
}

// Construct the appropriate attributes.
let span = Span::call_site();
Expand Down Expand Up @@ -114,7 +129,8 @@ pub fn cargo_test(attr: TokenStream, item: TokenStream) -> TokenStream {
}

// Find where the function body starts, and add the boilerplate at the start.
for token in item {
let mut item = item.into_iter();
while let Some(token) = item.next() {
let group = match token {
TokenTree::Group(g) => {
if g.delimiter() == Delimiter::Brace {
Expand All @@ -124,6 +140,22 @@ pub fn cargo_test(attr: TokenStream, item: TokenStream) -> TokenStream {
continue;
}
}
// Remove duplicate ignore statements to avoid errors - ours take precedence.
// Only needed for handling `gitoxide` tests.
#[cfg(test_gitoxide)]
TokenTree::Punct(p) if p.as_char() == '#' && ignore => {
let next = item.next().expect("group");
let TokenTree::Group(g) = next else {
ret.extend([TokenTree::Punct(p), next]);
continue
};
if g.delimiter() == Delimiter::Bracket && g.to_string().contains("ignore") {
continue;
}

ret.extend([TokenTree::Punct(p), TokenTree::Group(g)]);
continue;
}
other => {
ret.extend(Some(other));
continue;
Expand Down
23 changes: 23 additions & 0 deletions crates/cargo-test-support/src/git.rs
Expand Up @@ -247,3 +247,26 @@ pub fn tag(repo: &git2::Repository, name: &str) {
false
));
}

/// A utility trait to make enabling `gitoxide` related features easier in select tests.
///
/// Generally this will only affect arguments to `Execs` if we are in a special test run
/// marked with `RUSTFLAGS=--cfg test_gitoxide`.
pub trait Gitoxide {
fn maybe_fetch_with_gitoxide(&mut self) -> &mut Self;
}

impl Gitoxide for crate::Execs {
/// Fetch both the crates-index as well as git dependencies with `gitoxide`.
fn maybe_fetch_with_gitoxide(&mut self) -> &mut Self {
#[cfg(test_gitoxide)]
enable_feature(self, "fetch");
self
}
}

#[cfg(test_gitoxide)]
fn enable_feature(exec: &mut crate::Execs, name: &str) {
exec.masquerade_as_nightly_cargo(&["gitoxide"])
.arg(format!("-Zgitoxide={}", name));
}
4 changes: 3 additions & 1 deletion tests/testsuite/registry.rs
Expand Up @@ -2,6 +2,7 @@

use cargo::core::SourceId;
use cargo_test_support::cargo_process;
use cargo_test_support::git::Gitoxide;
use cargo_test_support::paths::{self, CargoPathExt};
use cargo_test_support::registry::{
self, registry_path, Dependency, Package, RegistryBuilder, TestRegistry,
Expand Down Expand Up @@ -1422,7 +1423,7 @@ fn fetch_downloads_http() {
fetch_downloads(cargo_http);
}

#[cargo_test]
#[cargo_test(gitoxide)]
fn fetch_downloads_git() {
fetch_downloads(cargo_stable);
}
Expand All @@ -1447,6 +1448,7 @@ fn fetch_downloads(cargo: fn(&Project, &str) -> Execs) {
Package::new("a", "0.1.0").publish();

cargo(&p, "fetch")
.maybe_fetch_with_gitoxide()
.with_stderr(
"\
[UPDATING] `[..]` index
Expand Down

0 comments on commit 62af2de

Please sign in to comment.