Skip to content

Commit 0c9d0c6

Browse files
committed
setup extra test run for selective gitoxide-enabled tests
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.
1 parent dda904a commit 0c9d0c6

File tree

4 files changed

+61
-2
lines changed

4 files changed

+61
-2
lines changed

.github/workflows/main.yml

+2
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ jobs:
8080

8181
# Deny warnings on CI to avoid warnings getting into the codebase.
8282
- run: cargo test --features 'deny-warnings'
83+
- name: gitoxide tests (git-related and enabled tests only)
84+
run: RUSTFLAGS='--cfg test_gitoxide' cargo test git
8385
# The testsuite generates a huge amount of data, and fetch-smoke-test was
8486
# running out of disk space.
8587
- name: Clear test output

crates/cargo-test-macro/src/lib.rs

+33-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ pub fn cargo_test(attr: TokenStream, item: TokenStream) -> TokenStream {
3232
};
3333
}
3434
let is_not_nightly = !version().1;
35+
#[cfg(test_gitoxide)]
36+
let mut enable_gitoxide_test = false;
3537
for rule in split_rules(attr) {
3638
match rule.as_str() {
3739
"build_std_real" => {
@@ -59,6 +61,12 @@ pub fn cargo_test(attr: TokenStream, item: TokenStream) -> TokenStream {
5961
requires_reason = true;
6062
set_ignore!(is_not_nightly, "requires nightly");
6163
}
64+
"gitoxide" => {
65+
#[cfg(test_gitoxide)]
66+
{
67+
enable_gitoxide_test = true;
68+
}
69+
}
6270
s if s.starts_with("requires_") => {
6371
let command = &s[9..];
6472
set_ignore!(!has_command(command), "{command} not installed");
@@ -81,6 +89,13 @@ pub fn cargo_test(attr: TokenStream, item: TokenStream) -> TokenStream {
8189
such as #[cargo_test(nightly, reason = \"needs -Z unstable-thing\")]"
8290
);
8391
}
92+
#[cfg(test_gitoxide)]
93+
if !enable_gitoxide_test {
94+
ignore = true;
95+
implicit_reasons.push(
96+
"not yet enabled for using 'gitoxide' and tested in the standard test run".to_string(),
97+
)
98+
}
8499

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

116131
// Find where the function body starts, and add the boilerplate at the start.
117-
for token in item {
132+
let mut item = item.into_iter();
133+
while let Some(token) = item.next() {
118134
let group = match token {
119135
TokenTree::Group(g) => {
120136
if g.delimiter() == Delimiter::Brace {
@@ -124,6 +140,22 @@ pub fn cargo_test(attr: TokenStream, item: TokenStream) -> TokenStream {
124140
continue;
125141
}
126142
}
143+
// Remove duplicate ignore statements to avoid errors - ours take precedence.
144+
// Only needed for handling `gitoxide` tests.
145+
#[cfg(test_gitoxide)]
146+
TokenTree::Punct(p) if p.as_char() == '#' => {
147+
let next = item.next().expect("group");
148+
let TokenTree::Group(g) = next else {
149+
ret.extend([TokenTree::Punct(p), next]);
150+
continue
151+
};
152+
if g.delimiter() == Delimiter::Bracket && g.to_string().contains("ignore") {
153+
continue;
154+
}
155+
156+
ret.extend([TokenTree::Punct(p), TokenTree::Group(g)]);
157+
continue;
158+
}
127159
other => {
128160
ret.extend(Some(other));
129161
continue;

crates/cargo-test-support/src/git.rs

+23
Original file line numberDiff line numberDiff line change
@@ -247,3 +247,26 @@ pub fn tag(repo: &git2::Repository, name: &str) {
247247
false
248248
));
249249
}
250+
251+
/// A utility trait to make enabling `gitoxide` related features easier in select tests.
252+
///
253+
/// Generally this will only affect arguments to `Execs` if we are in a special test run
254+
/// marked with `RUSTFLAGS=--cfg test_gitoxide`.
255+
pub trait Gitoxide {
256+
fn maybe_fetch_with_gitoxide(&mut self) -> &mut Self;
257+
}
258+
259+
impl Gitoxide for crate::Execs {
260+
/// Fetch both the crates-index as well as git dependencies with `gitoxide`.
261+
fn maybe_fetch_with_gitoxide(&mut self) -> &mut Self {
262+
#[cfg(test_gitoxide)]
263+
enable_feature(self, "fetch");
264+
self
265+
}
266+
}
267+
268+
#[cfg(test_gitoxide)]
269+
fn enable_feature(exec: &mut crate::Execs, name: &str) {
270+
exec.masquerade_as_nightly_cargo(&["gitoxide"])
271+
.arg(format!("-Zgitoxide={}", name));
272+
}

tests/testsuite/registry.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use cargo::core::SourceId;
44
use cargo_test_support::cargo_process;
5+
use cargo_test_support::git::Gitoxide;
56
use cargo_test_support::paths::{self, CargoPathExt};
67
use cargo_test_support::registry::{
78
self, registry_path, Dependency, Package, RegistryBuilder, TestRegistry,
@@ -1422,7 +1423,7 @@ fn fetch_downloads_http() {
14221423
fetch_downloads(cargo_http);
14231424
}
14241425

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

14491450
cargo(&p, "fetch")
1451+
.maybe_fetch_with_gitoxide()
14501452
.with_stderr(
14511453
"\
14521454
[UPDATING] `[..]` index

0 commit comments

Comments
 (0)