Skip to content

Commit ebfa12f

Browse files
authored
test(git): add more fetch-index backend interop (#16162)
### What does this PR try to resolve? Cherry-pick some test generalization from <epage@9af85fd> See <#16156 (comment)> `fetch_dep_then_fetch` is a bit tricky to generalize, so leave it there. ### How to test and review this PR?
2 parents 8120df7 + bd1a02c commit ebfa12f

File tree

1 file changed

+122
-36
lines changed

1 file changed

+122
-36
lines changed

tests/testsuite/git_shallow.rs

Lines changed: 122 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use cargo_test_support::{basic_manifest, git, paths, project};
44

55
use crate::git_gc::find_index;
66

7+
#[derive(Copy, Clone, Debug)]
78
enum Backend {
89
Git2,
910
Gitoxide,
@@ -28,6 +29,7 @@ impl Backend {
2829
}
2930
}
3031

32+
#[derive(Copy, Clone, Debug)]
3133
enum RepoMode {
3234
Shallow,
3335
Complete,
@@ -40,6 +42,34 @@ impl RepoMode {
4042
RepoMode::Shallow => "-Zgit=shallow-deps",
4143
}
4244
}
45+
46+
fn to_index_arg(&self) -> &'static str {
47+
match self {
48+
RepoMode::Complete => "",
49+
RepoMode::Shallow => "-Zgit=shallow-index",
50+
}
51+
}
52+
53+
#[track_caller]
54+
fn assert_index(self, repo: &gix::Repository, shallow_depth: usize, complete_depth: usize) {
55+
let commit_count = repo
56+
.rev_parse_single("origin/HEAD")
57+
.unwrap()
58+
.ancestors()
59+
.all()
60+
.unwrap()
61+
.count();
62+
match self {
63+
RepoMode::Shallow => {
64+
assert_eq!(commit_count, shallow_depth,);
65+
assert!(repo.is_shallow());
66+
}
67+
RepoMode::Complete => {
68+
assert_eq!(commit_count, complete_depth,);
69+
assert!(!repo.is_shallow());
70+
}
71+
}
72+
}
4373
}
4474

4575
#[cargo_test]
@@ -306,17 +336,89 @@ fn fetch_shallow_dep_branch_to_rev(backend: Backend) -> anyhow::Result<()> {
306336

307337
#[cargo_test]
308338
fn gitoxide_fetch_shallow_index_then_git2_fetch_complete() -> anyhow::Result<()> {
309-
fetch_shallow_index_then_fetch_complete(Backend::Gitoxide, Backend::Git2)
339+
fetch_index_then_fetch(
340+
Backend::Gitoxide,
341+
RepoMode::Shallow,
342+
Backend::Git2,
343+
RepoMode::Complete,
344+
)
345+
}
346+
347+
#[cargo_test]
348+
fn gitoxide_fetch_shallow_index_then_git_cli_fetch_shallow() -> anyhow::Result<()> {
349+
fetch_index_then_fetch(
350+
Backend::Gitoxide,
351+
RepoMode::Shallow,
352+
Backend::GitCli,
353+
RepoMode::Shallow,
354+
)
355+
}
356+
357+
#[cargo_test]
358+
fn gitoxide_fetch_complete_index_then_git_cli_fetch_shallow() -> anyhow::Result<()> {
359+
fetch_index_then_fetch(
360+
Backend::Gitoxide,
361+
RepoMode::Complete,
362+
Backend::GitCli,
363+
RepoMode::Shallow,
364+
)
365+
}
366+
367+
#[cargo_test]
368+
fn gitoxide_fetch_shallow_index_then_git_cli_fetch_complete() -> anyhow::Result<()> {
369+
fetch_index_then_fetch(
370+
Backend::Gitoxide,
371+
RepoMode::Shallow,
372+
Backend::GitCli,
373+
RepoMode::Complete,
374+
)
310375
}
311376

312377
#[cargo_test]
313378
fn git_cli_fetch_shallow_index_then_git2_fetch_complete() -> anyhow::Result<()> {
314-
fetch_shallow_index_then_fetch_complete(Backend::GitCli, Backend::Git2)
379+
fetch_index_then_fetch(
380+
Backend::GitCli,
381+
RepoMode::Shallow,
382+
Backend::Git2,
383+
RepoMode::Complete,
384+
)
385+
}
386+
387+
#[cargo_test]
388+
fn git_cli_fetch_shallow_index_then_gitoxide_fetch_shallow() -> anyhow::Result<()> {
389+
fetch_index_then_fetch(
390+
Backend::GitCli,
391+
RepoMode::Shallow,
392+
Backend::Gitoxide,
393+
RepoMode::Shallow,
394+
)
395+
}
396+
397+
#[cargo_test]
398+
fn git_cli_fetch_shallow_complete_then_gitoxide_fetch_complete() -> anyhow::Result<()> {
399+
fetch_index_then_fetch(
400+
Backend::GitCli,
401+
RepoMode::Complete,
402+
Backend::Gitoxide,
403+
RepoMode::Shallow,
404+
)
405+
}
406+
407+
#[cargo_test]
408+
fn git_cli_fetch_shallow_index_then_gitoxide_fetch_complete() -> anyhow::Result<()> {
409+
fetch_index_then_fetch(
410+
Backend::GitCli,
411+
RepoMode::Shallow,
412+
Backend::Gitoxide,
413+
RepoMode::Complete,
414+
)
315415
}
316416

317-
fn fetch_shallow_index_then_fetch_complete(
417+
fn fetch_index_then_fetch(
318418
backend_1st: Backend,
419+
mode_1st: RepoMode,
319420
backend_2nd: Backend,
421+
mode_2nd: RepoMode,
320422
) -> anyhow::Result<()> {
321423
Package::new("bar", "1.0.0").publish();
322424
let p = project()
@@ -335,47 +437,31 @@ fn fetch_shallow_index_then_fetch_complete(
335437
.build();
336438
p.cargo("fetch")
337439
.arg_line(backend_1st.to_arg())
338-
.arg("-Zgit=shallow-index")
440+
.arg_line(mode_1st.to_index_arg())
339441
.env("__CARGO_USE_GITOXIDE_INSTEAD_OF_GIT2", "0")
340442
.masquerade_as_nightly_cargo(&["gitoxide=fetch", "git=shallow-index"])
341443
.env("CARGO_LOG", "git-fetch=debug")
342444
.with_stderr_contains(backend_1st.to_trace_log())
343445
.run();
344446

345-
let shallow_repo = gix::open_opts(find_index(), gix::open::Options::isolated())?;
346-
assert_eq!(
347-
shallow_repo
348-
.rev_parse_single("origin/HEAD")?
349-
.ancestors()
350-
.all()?
351-
.count(),
352-
1,
353-
"shallow fetch always start at depth of 1 to minimize download size"
354-
);
355-
assert!(shallow_repo.is_shallow());
447+
let repo = gix::open_opts(find_remote_index(mode_1st), gix::open::Options::isolated())?;
448+
let complete_depth = 2; // initial commmit, bar@1.0.0
449+
mode_1st.assert_index(&repo, 1, complete_depth);
356450

357451
Package::new("bar", "1.1.0").publish();
358452
p.cargo("update")
359453
.arg_line(backend_2nd.to_arg())
454+
.arg_line(mode_2nd.to_index_arg())
360455
.env("__CARGO_USE_GITOXIDE_INSTEAD_OF_GIT2", "0")
361-
.masquerade_as_nightly_cargo(&["gitoxide=fetch"])
456+
.masquerade_as_nightly_cargo(&["gitoxide=fetch", "git=shallow-index"])
362457
.env("CARGO_LOG", "git-fetch=debug")
363458
.with_stderr_contains(backend_2nd.to_trace_log())
364459
.run();
365460

366-
let repo = gix::open_opts(
367-
find_remote_index(RepoMode::Complete),
368-
gix::open::Options::isolated(),
369-
)?;
370-
assert_eq!(
371-
repo.rev_parse_single("origin/HEAD")?
372-
.ancestors()
373-
.all()?
374-
.count(),
375-
3,
376-
"an entirely new repo was fetched which is never shallow"
377-
);
378-
assert!(!repo.is_shallow());
461+
let repo = gix::open_opts(find_remote_index(mode_2nd), gix::open::Options::isolated())?;
462+
let complete_depth = 3; // initial commmit, bar@1.0.0, and bar@1.1.0
463+
mode_2nd.assert_index(&repo, 1, complete_depth);
464+
379465
Ok(())
380466
}
381467

@@ -583,7 +669,7 @@ fn fetch_shallow_index_then_preserve_shallow(backend: Backend) -> anyhow::Result
583669
.build();
584670
p.cargo("fetch")
585671
.arg_line(backend.to_arg())
586-
.arg("-Zgit=shallow-index")
672+
.arg(RepoMode::Shallow.to_index_arg())
587673
.env("__CARGO_USE_GITOXIDE_INSTEAD_OF_GIT2", "0")
588674
.masquerade_as_nightly_cargo(&["gitoxide=fetch", "git=shallow-index"])
589675
.env("CARGO_LOG", "git-fetch=debug")
@@ -604,7 +690,7 @@ fn fetch_shallow_index_then_preserve_shallow(backend: Backend) -> anyhow::Result
604690
Package::new("bar", "1.1.0").publish();
605691
p.cargo("update")
606692
.arg_line(backend.to_arg())
607-
.arg("-Zgit=shallow-index") // NOTE: the flag needs to be consistent or else a different index is created
693+
.arg(RepoMode::Shallow.to_index_arg()) // NOTE: the flag needs to be consistent or else a different index is created
608694
.env("__CARGO_USE_GITOXIDE_INSTEAD_OF_GIT2", "0")
609695
.masquerade_as_nightly_cargo(&["gitoxide=fetch", "git=shallow-index"])
610696
.env("CARGO_LOG", "git-fetch=debug")
@@ -625,7 +711,7 @@ fn fetch_shallow_index_then_preserve_shallow(backend: Backend) -> anyhow::Result
625711
Package::new("bar", "1.3.0").publish();
626712
p.cargo("update")
627713
.arg_line(backend.to_arg())
628-
.arg("-Zgit=shallow-index")
714+
.arg(RepoMode::Shallow.to_index_arg())
629715
.env("__CARGO_USE_GITOXIDE_INSTEAD_OF_GIT2", "0")
630716
.masquerade_as_nightly_cargo(&["gitoxide=fetch", "git=shallow-index"])
631717
.env("CARGO_LOG", "git-fetch=debug")
@@ -694,7 +780,7 @@ fn fetch_complete_index_then_shallow(backend: Backend) -> anyhow::Result<()> {
694780
Package::new("bar", "1.1.0").publish();
695781
p.cargo("update")
696782
.arg_line(backend.to_arg())
697-
.arg("-Zgit=shallow-index")
783+
.arg(RepoMode::Shallow.to_index_arg())
698784
.env("__CARGO_USE_GITOXIDE_INSTEAD_OF_GIT2", "0")
699785
.masquerade_as_nightly_cargo(&["gitoxide=fetch", "git=shallow-index"])
700786
.env("CARGO_LOG", "git-fetch=debug")
@@ -720,7 +806,7 @@ fn fetch_complete_index_then_shallow(backend: Backend) -> anyhow::Result<()> {
720806
Package::new("bar", "1.3.0").publish();
721807
p.cargo("update")
722808
.arg_line(backend.to_arg())
723-
.arg("-Zgit=shallow-index")
809+
.arg(RepoMode::Shallow.to_index_arg())
724810
.env("__CARGO_USE_GITOXIDE_INSTEAD_OF_GIT2", "0")
725811
.masquerade_as_nightly_cargo(&["gitoxide=fetch", "git=shallow-index"])
726812
.env("CARGO_LOG", "git-fetch=debug")
@@ -800,7 +886,7 @@ fn fetch_shallow_index_then_abort_and_update(backend: Backend) -> anyhow::Result
800886
.build();
801887
p.cargo("fetch")
802888
.arg_line(backend.to_arg())
803-
.arg("-Zgit=shallow-index")
889+
.arg(RepoMode::Shallow.to_index_arg())
804890
.env("__CARGO_USE_GITOXIDE_INSTEAD_OF_GIT2", "0")
805891
.masquerade_as_nightly_cargo(&["gitoxide=fetch", "git=shallow-index"])
806892
.env("CARGO_LOG", "git-fetch=debug")
@@ -826,7 +912,7 @@ fn fetch_shallow_index_then_abort_and_update(backend: Backend) -> anyhow::Result
826912
Package::new("bar", "1.1.0").publish();
827913
p.cargo("update")
828914
.arg_line(backend.to_arg())
829-
.arg("-Zgit=shallow-index")
915+
.arg(RepoMode::Shallow.to_index_arg())
830916
.env("__CARGO_USE_GITOXIDE_INSTEAD_OF_GIT2", "0")
831917
.masquerade_as_nightly_cargo(&["gitoxide=fetch", "git=shallow-index"])
832918
.env("CARGO_LOG", "git-fetch=debug")

0 commit comments

Comments
 (0)