Skip to content

Commit

Permalink
test: encode URL params correctly for SourceId in Cargo.lock
Browse files Browse the repository at this point in the history
This will happen in lockfile format v4.
  • Loading branch information
weihanglo committed Jun 17, 2023
1 parent de8fce2 commit c1840a7
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions tests/testsuite/lockfile_compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1058,3 +1058,91 @@ fn v3_and_git_url_encoded_tag() {
fn v3_and_git_url_encoded_rev() {
v3_and_git_url_encoded("rev", create_tag);
}

fn v4_and_git_url_encoded(ref_kind: &str, f: impl FnOnce(&git2::Repository, &str, git2::Oid)) {
let (git_project, repo) = git::new_repo("dep1", |project| {
project
.file("Cargo.toml", &basic_lib_manifest("dep1"))
.file("src/lib.rs", "")
});
let url = git_project.url();
let head_id = repo.head().unwrap().target().unwrap();
// Ref name with special characters
let git_ref = "a-_+#$)";
let encoded_ref = "a-_%2B%23%24%29";
f(&repo, git_ref, head_id);

let lockfile = format!(
r#"# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "dep1"
version = "0.5.0"
source = "git+{url}?{ref_kind}={encoded_ref}#{head_id}"
[[package]]
name = "foo"
version = "0.0.1"
dependencies = [
"dep1",
]
"#,
);

let p = project()
.file(
"Cargo.toml",
&format!(
r#"
[package]
name = "foo"
version = "0.0.1"
[dependencies]
dep1 = {{ git = '{url}', {ref_kind} = '{git_ref}' }}
"#,
),
)
.file("src/lib.rs", "")
.file("Cargo.lock", "version = 4")
.build();

p.cargo("check -Znext-lockfile-bump")
.masquerade_as_nightly_cargo(&["-Znext-lockfile-bump"])
.with_stderr(format!(
"\
[UPDATING] git repository `{url}`
[CHECKING] dep1 v0.5.0 ({url}?{ref_kind}={git_ref}#[..])
[CHECKING] foo v0.0.1 ([CWD])
[FINISHED] dev [..]
"
))
.run();

let lock = p.read_lockfile();
assert_match_exact(&lockfile, &lock);

// Unlike v3_and_git_url_encoded, v4 encodes URL parameters so no git
// repository re-clone happen.
p.cargo("check -Znext-lockfile-bump")
.masquerade_as_nightly_cargo(&["-Znext-lockfile-bump"])
.with_stderr("[FINISHED] dev [..]")
.run();
}

#[cargo_test]
fn v4_and_git_url_encoded_branch() {
v4_and_git_url_encoded("branch", create_branch);
}

#[cargo_test]
fn v4_and_git_url_encoded_tag() {
v4_and_git_url_encoded("tag", create_tag);
}

#[cargo_test]
fn v4_and_git_url_encoded_rev() {
v4_and_git_url_encoded("rev", create_tag)
}

0 comments on commit c1840a7

Please sign in to comment.