cargo/tests/testsuite/git.rs

3691 lines
97 KiB
Rust
Raw Normal View History

2019-11-25 02:42:45 +00:00
//! Tests for git support.
use std::fs;
use std::io::prelude::*;
use std::net::{TcpListener, TcpStream};
use std::path::Path;
use std::str;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::thread;
use cargo_test_support::git::cargo_uses_gitoxide;
use cargo_test_support::paths::{self, CargoPathExt};
2022-10-28 02:56:41 +00:00
use cargo_test_support::registry::Package;
use cargo_test_support::{basic_lib_manifest, basic_manifest, git, main_file, path2url, project};
2019-09-12 19:52:46 +00:00
use cargo_test_support::{sleep_ms, t, Project};
#[cargo_test]
fn cargo_compile_simple_git_dep() {
let project = project();
let git_project = git::new("dep1", |project| {
2014-06-12 22:51:16 +00:00
project
2018-07-24 22:35:01 +00:00
.file("Cargo.toml", &basic_lib_manifest("dep1"))
2018-03-14 15:17:44 +00:00
.file(
"src/dep1.rs",
r#"
2020-09-27 00:59:58 +00:00
pub fn hello() -> &'static str {
"hello world"
}
"#,
2018-03-14 15:17:44 +00:00
)
});
2014-06-12 22:51:16 +00:00
let project = project
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
&format!(
r#"
[package]
2014-06-12 22:51:16 +00:00
2020-09-27 00:59:58 +00:00
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
2014-06-12 22:51:16 +00:00
2020-09-27 00:59:58 +00:00
[dependencies.dep1]
2014-06-12 22:51:16 +00:00
2020-09-27 00:59:58 +00:00
git = '{}'
"#,
2018-03-14 15:17:44 +00:00
git_project.url()
),
2018-12-08 11:19:47 +00:00
)
.file(
"src/main.rs",
&main_file(r#""{}", dep1::hello()"#, &["dep1"]),
2018-12-08 11:19:47 +00:00
)
.build();
2014-06-12 22:51:16 +00:00
let git_root = git_project.root();
project
.cargo("build")
.with_stderr(&format!(
2018-03-14 15:17:44 +00:00
"[UPDATING] git repository `{}`\n\
[COMPILING] dep1 v0.5.0 ({}#[..])\n\
[COMPILING] foo v0.5.0 ([CWD])\n\
2018-03-14 15:17:44 +00:00
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]\n",
path2url(&git_root),
path2url(&git_root),
2018-12-08 11:19:47 +00:00
))
.run();
2014-06-12 22:51:16 +00:00
2018-08-29 06:11:10 +00:00
assert!(project.bin("foo").is_file());
2014-06-12 22:51:16 +00:00
project
.process(&project.bin("foo"))
.with_stdout("hello world\n")
.run();
}
#[cargo_test]
fn cargo_compile_git_dep_branch() {
let project = project();
let git_project = git::new("dep1", |project| {
2014-06-24 22:23:03 +00:00
project
2018-07-24 22:35:01 +00:00
.file("Cargo.toml", &basic_lib_manifest("dep1"))
2018-03-14 15:17:44 +00:00
.file(
"src/dep1.rs",
r#"
2020-09-27 00:59:58 +00:00
pub fn hello() -> &'static str {
"hello world"
}
"#,
2018-03-14 15:17:44 +00:00
)
});
2014-06-24 22:23:03 +00:00
// Make a new branch based on the current HEAD commit
let repo = git2::Repository::open(&git_project.root()).unwrap();
let head = repo.head().unwrap().target().unwrap();
let head = repo.find_commit(head).unwrap();
2015-03-16 13:27:58 +00:00
repo.branch("branchy", &head, true).unwrap();
2014-06-24 22:23:03 +00:00
let project = project
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
&format!(
r#"
[package]
2014-06-24 22:23:03 +00:00
2020-09-27 00:59:58 +00:00
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
2014-06-24 22:23:03 +00:00
2020-09-27 00:59:58 +00:00
[dependencies.dep1]
2014-06-24 22:23:03 +00:00
2020-09-27 00:59:58 +00:00
git = '{}'
branch = "branchy"
2014-06-24 22:23:03 +00:00
2020-09-27 00:59:58 +00:00
"#,
2018-03-14 15:17:44 +00:00
git_project.url()
),
2018-12-08 11:19:47 +00:00
)
.file(
"src/main.rs",
&main_file(r#""{}", dep1::hello()"#, &["dep1"]),
2018-12-08 11:19:47 +00:00
)
.build();
2014-06-24 22:23:03 +00:00
let git_root = git_project.root();
project
.cargo("build")
.with_stderr(&format!(
2018-03-14 15:17:44 +00:00
"[UPDATING] git repository `{}`\n\
[COMPILING] dep1 v0.5.0 ({}?branch=branchy#[..])\n\
[COMPILING] foo v0.5.0 ([CWD])\n\
2018-03-14 15:17:44 +00:00
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]\n",
path2url(&git_root),
path2url(&git_root),
2018-12-08 11:19:47 +00:00
))
.run();
2014-06-24 22:23:03 +00:00
2018-08-29 06:11:10 +00:00
assert!(project.bin("foo").is_file());
2014-06-24 22:23:03 +00:00
project
.process(&project.bin("foo"))
.with_stdout("hello world\n")
.run();
}
2014-06-24 22:23:03 +00:00
#[cargo_test]
fn cargo_compile_git_dep_tag() {
let project = project();
let git_project = git::new("dep1", |project| {
2014-06-24 22:23:03 +00:00
project
2018-07-24 22:35:01 +00:00
.file("Cargo.toml", &basic_lib_manifest("dep1"))
2018-03-14 15:17:44 +00:00
.file(
"src/dep1.rs",
r#"
2020-09-27 00:59:58 +00:00
pub fn hello() -> &'static str {
"hello world"
}
"#,
2018-03-14 15:17:44 +00:00
)
});
2014-06-24 22:23:03 +00:00
2015-05-06 00:05:41 +00:00
// Make a tag corresponding to the current HEAD
let repo = git2::Repository::open(&git_project.root()).unwrap();
let head = repo.head().unwrap().target().unwrap();
2018-03-14 15:17:44 +00:00
repo.tag(
"v0.1.0",
&repo.find_object(head, None).unwrap(),
&repo.signature().unwrap(),
"make a new tag",
false,
2018-12-08 11:19:47 +00:00
)
.unwrap();
2014-06-24 22:23:03 +00:00
let project = project
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
&format!(
r#"
[package]
2014-06-24 22:23:03 +00:00
2020-09-27 00:59:58 +00:00
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
2014-06-24 22:23:03 +00:00
2020-09-27 00:59:58 +00:00
[dependencies.dep1]
2014-06-24 22:23:03 +00:00
2020-09-27 00:59:58 +00:00
git = '{}'
tag = "v0.1.0"
"#,
2018-03-14 15:17:44 +00:00
git_project.url()
),
2018-12-08 11:19:47 +00:00
)
.file(
"src/main.rs",
&main_file(r#""{}", dep1::hello()"#, &["dep1"]),
2018-12-08 11:19:47 +00:00
)
.build();
2014-06-24 22:23:03 +00:00
let git_root = git_project.root();
project
.cargo("build")
.with_stderr(&format!(
2018-03-14 15:17:44 +00:00
"[UPDATING] git repository `{}`\n\
[COMPILING] dep1 v0.5.0 ({}?tag=v0.1.0#[..])\n\
[COMPILING] foo v0.5.0 ([CWD])\n\
2018-03-14 15:17:44 +00:00
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]\n",
path2url(&git_root),
path2url(&git_root),
2018-12-08 11:19:47 +00:00
))
.run();
2014-06-24 22:23:03 +00:00
2018-08-29 06:11:10 +00:00
assert!(project.bin("foo").is_file());
2014-06-24 22:23:03 +00:00
project
.process(&project.bin("foo"))
.with_stdout("hello world\n")
.run();
project.cargo("build").run();
}
2014-07-01 01:36:27 +00:00
#[cargo_test]
fn cargo_compile_git_dep_pull_request() {
let project = project();
let git_project = git::new("dep1", |project| {
project
.file("Cargo.toml", &basic_lib_manifest("dep1"))
.file(
"src/dep1.rs",
r#"
pub fn hello() -> &'static str {
"hello world"
}
"#,
)
});
// Make a reference in GitHub's pull request ref naming convention.
let repo = git2::Repository::open(&git_project.root()).unwrap();
let oid = repo.refname_to_id("HEAD").unwrap();
let force = false;
let log_message = "open pull request";
repo.reference("refs/pull/330/head", oid, force, log_message)
.unwrap();
let project = project
.file(
"Cargo.toml",
&format!(
r#"
[package]
name = "foo"
version = "0.0.0"
[dependencies]
dep1 = {{ git = "{}", rev = "refs/pull/330/head" }}
"#,
git_project.url()
),
)
.file(
"src/main.rs",
&main_file(r#""{}", dep1::hello()"#, &["dep1"]),
)
.build();
let git_root = git_project.root();
project
.cargo("build")
.with_stderr(&format!(
"[UPDATING] git repository `{}`\n\
[COMPILING] dep1 v0.5.0 ({}?rev=refs/pull/330/head#[..])\n\
[COMPILING] foo v0.0.0 ([CWD])\n\
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]\n",
path2url(&git_root),
path2url(&git_root),
))
.run();
assert!(project.bin("foo").is_file());
}
#[cargo_test]
fn cargo_compile_with_nested_paths() {
let git_project = git::new("dep1", |project| {
project
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
[package]
2020-09-27 00:59:58 +00:00
name = "dep1"
version = "0.5.0"
authors = ["carlhuda@example.com"]
2020-09-27 00:59:58 +00:00
[dependencies.dep2]
2020-09-27 00:59:58 +00:00
version = "0.5.0"
path = "vendor/dep2"
2020-09-27 00:59:58 +00:00
[lib]
2020-09-27 00:59:58 +00:00
name = "dep1"
"#,
2018-12-08 11:19:47 +00:00
)
.file(
2018-03-14 15:17:44 +00:00
"src/dep1.rs",
r#"
2020-09-27 00:59:58 +00:00
extern crate dep2;
2020-09-27 00:59:58 +00:00
pub fn hello() -> &'static str {
dep2::hello()
}
"#,
2018-12-08 11:19:47 +00:00
)
.file("vendor/dep2/Cargo.toml", &basic_lib_manifest("dep2"))
2018-03-14 15:17:44 +00:00
.file(
"vendor/dep2/src/dep2.rs",
r#"
2020-09-27 00:59:58 +00:00
pub fn hello() -> &'static str {
"hello world"
}
"#,
2018-03-14 15:17:44 +00:00
)
});
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
&format!(
r#"
[package]
2020-09-27 00:59:58 +00:00
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
2020-09-27 00:59:58 +00:00
[dependencies.dep1]
2020-09-27 00:59:58 +00:00
version = "0.5.0"
git = '{}'
2020-09-27 00:59:58 +00:00
[[bin]]
2020-09-27 00:59:58 +00:00
name = "foo"
"#,
2018-03-14 15:17:44 +00:00
git_project.url()
),
2018-12-08 11:19:47 +00:00
)
.file(
"src/foo.rs",
&main_file(r#""{}", dep1::hello()"#, &["dep1"]),
2018-12-08 11:19:47 +00:00
)
.build();
p.cargo("build").run();
2018-08-29 06:11:10 +00:00
assert!(p.bin("foo").is_file());
p.process(&p.bin("foo")).with_stdout("hello world\n").run();
2017-05-06 12:44:41 +00:00
}
#[cargo_test]
2017-05-06 12:44:41 +00:00
fn cargo_compile_with_malformed_nested_paths() {
let git_project = git::new("dep1", |project| {
project
2018-07-24 22:35:01 +00:00
.file("Cargo.toml", &basic_lib_manifest("dep1"))
2018-03-14 15:17:44 +00:00
.file(
"src/dep1.rs",
r#"
2020-09-27 00:59:58 +00:00
pub fn hello() -> &'static str {
"hello world"
}
"#,
2018-12-08 11:19:47 +00:00
)
.file("vendor/dep2/Cargo.toml", "!INVALID!")
.file(
"vendor/dep3/Cargo.toml",
r#"
[package]
name = "dep3"
version = "0.5.0"
[dependencies]
2020-09-27 00:59:58 +00:00
subdep1 = { path = "../require-extra-build-step" }
"#,
)
.file("vendor/dep3/src/lib.rs", "")
});
2017-05-06 12:44:41 +00:00
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
&format!(
r#"
[package]
2017-05-06 12:44:41 +00:00
2020-09-27 00:59:58 +00:00
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
2017-05-06 12:44:41 +00:00
2020-09-27 00:59:58 +00:00
[dependencies.dep1]
2017-05-06 12:44:41 +00:00
2020-09-27 00:59:58 +00:00
version = "0.5.0"
git = '{}'
2017-05-06 12:44:41 +00:00
2020-09-27 00:59:58 +00:00
[[bin]]
2017-05-06 12:44:41 +00:00
2020-09-27 00:59:58 +00:00
name = "foo"
"#,
2018-03-14 15:17:44 +00:00
git_project.url()
),
2018-12-08 11:19:47 +00:00
)
.file(
"src/foo.rs",
&main_file(r#""{}", dep1::hello()"#, &["dep1"]),
2018-12-08 11:19:47 +00:00
)
.build();
2017-05-06 12:44:41 +00:00
p.cargo("build").run();
2017-05-06 12:44:41 +00:00
2018-08-29 06:11:10 +00:00
assert!(p.bin("foo").is_file());
2017-05-06 12:44:41 +00:00
p.process(&p.bin("foo")).with_stdout("hello world\n").run();
}
#[cargo_test]
fn cargo_compile_with_meta_package() {
let git_project = git::new("meta-dep", |project| {
2014-07-01 01:36:27 +00:00
project
2018-07-24 22:35:01 +00:00
.file("dep1/Cargo.toml", &basic_lib_manifest("dep1"))
2018-03-14 15:17:44 +00:00
.file(
"dep1/src/dep1.rs",
r#"
2020-09-27 00:59:58 +00:00
pub fn hello() -> &'static str {
"this is dep1"
}
"#,
2018-12-08 11:19:47 +00:00
)
.file("dep2/Cargo.toml", &basic_lib_manifest("dep2"))
2018-03-14 15:17:44 +00:00
.file(
"dep2/src/dep2.rs",
r#"
2020-09-27 00:59:58 +00:00
pub fn hello() -> &'static str {
"this is dep2"
}
"#,
2018-03-14 15:17:44 +00:00
)
});
2014-07-01 01:36:27 +00:00
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
&format!(
r#"
[package]
2014-07-01 01:36:27 +00:00
2020-09-27 00:59:58 +00:00
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
2014-07-01 01:36:27 +00:00
2020-09-27 00:59:58 +00:00
[dependencies.dep1]
2014-07-01 01:36:27 +00:00
2020-09-27 00:59:58 +00:00
version = "0.5.0"
git = '{}'
2014-07-01 01:36:27 +00:00
2020-09-27 00:59:58 +00:00
[dependencies.dep2]
2014-07-01 01:36:27 +00:00
2020-09-27 00:59:58 +00:00
version = "0.5.0"
git = '{}'
2014-07-01 01:36:27 +00:00
2020-09-27 00:59:58 +00:00
[[bin]]
2014-07-01 01:36:27 +00:00
2020-09-27 00:59:58 +00:00
name = "foo"
"#,
2018-03-14 15:17:44 +00:00
git_project.url(),
git_project.url()
),
2018-12-08 11:19:47 +00:00
)
.file(
"src/foo.rs",
2018-03-14 15:17:44 +00:00
&main_file(
r#""{} {}", dep1::hello(), dep2::hello()"#,
&["dep1", "dep2"],
),
2018-12-08 11:19:47 +00:00
)
.build();
2014-07-01 01:36:27 +00:00
p.cargo("build").run();
2014-07-01 01:36:27 +00:00
2018-08-29 06:11:10 +00:00
assert!(p.bin("foo").is_file());
2014-07-01 01:36:27 +00:00
p.process(&p.bin("foo"))
.with_stdout("this is dep1 this is dep2\n")
.run();
}
2014-07-01 01:36:27 +00:00
#[cargo_test]
fn cargo_compile_with_short_ssh_git() {
let url = "git@github.com:a/dep";
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
&format!(
r#"
[package]
2020-09-27 00:59:58 +00:00
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
2020-09-27 00:59:58 +00:00
[dependencies.dep]
2020-09-27 00:59:58 +00:00
git = "{}"
2020-09-27 00:59:58 +00:00
[[bin]]
2020-09-27 00:59:58 +00:00
name = "foo"
"#,
2018-03-14 15:17:44 +00:00
url
),
2018-12-08 11:19:47 +00:00
)
.file(
"src/foo.rs",
&main_file(r#""{}", dep1::hello()"#, &["dep1"]),
2018-12-08 11:19:47 +00:00
)
.build();
2023-02-15 23:51:46 +00:00
p.cargo("check")
.with_status(101)
.with_stdout("")
.with_stderr(&format!(
2018-03-14 15:17:44 +00:00
"\
[ERROR] failed to parse manifest at `[..]`
Caused by:
invalid url `{}`: relative URL without a base
2018-03-14 15:17:44 +00:00
",
url
2018-12-08 11:19:47 +00:00
))
.run();
}
#[cargo_test]
fn recompilation() {
let git_project = git::new("bar", |project| {
project
2018-07-24 13:01:56 +00:00
.file("Cargo.toml", &basic_lib_manifest("bar"))
.file("src/bar.rs", "pub fn bar() {}")
});
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
&format!(
r#"
[package]
2020-09-27 00:59:58 +00:00
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
2020-09-27 00:59:58 +00:00
[dependencies.bar]
2020-09-27 00:59:58 +00:00
version = "0.5.0"
git = '{}'
"#,
2018-03-14 15:17:44 +00:00
git_project.url()
),
2018-12-08 11:19:47 +00:00
)
.file("src/main.rs", &main_file(r#""{:?}", bar::bar()"#, &["bar"]))
.build();
// First time around we should compile both foo and bar
2023-02-15 23:51:46 +00:00
p.cargo("check")
.with_stderr(&format!(
2018-03-14 15:17:44 +00:00
"[UPDATING] git repository `{}`\n\
2023-02-15 23:51:46 +00:00
[CHECKING] bar v0.5.0 ({}#[..])\n\
[CHECKING] foo v0.5.0 ([CWD])\n\
2018-03-14 15:17:44 +00:00
[FINISHED] dev [unoptimized + debuginfo] target(s) \
in [..]\n",
git_project.url(),
git_project.url(),
2018-12-08 11:19:47 +00:00
))
.run();
// Don't recompile the second time
2023-11-15 22:33:27 +00:00
p.cargo("check").with_stderr("[FINISHED] [..]").run();
// Modify a file manually, shouldn't trigger a recompile
git_project.change_file("src/bar.rs", r#"pub fn bar() { println!("hello!"); }"#);
2023-11-15 22:33:27 +00:00
p.cargo("check").with_stderr("[FINISHED] [..]").run();
p.cargo("update")
.with_stderr(&format!(
2018-03-14 15:17:44 +00:00
"[UPDATING] git repository `{}`",
git_project.url()
2018-12-08 11:19:47 +00:00
))
.run();
2014-07-31 22:19:20 +00:00
2023-11-15 22:33:27 +00:00
p.cargo("check").with_stderr("[FINISHED] [..]").run();
// Commit the changes and make sure we don't trigger a recompile because the
2019-02-03 04:01:23 +00:00
// lock file says not to change
let repo = git2::Repository::open(&git_project.root()).unwrap();
git::add(&repo);
git::commit(&repo);
2014-07-31 22:19:20 +00:00
println!("compile after commit");
2023-11-15 22:33:27 +00:00
p.cargo("check").with_stderr("[FINISHED] [..]").run();
p.root().move_into_the_past();
2014-07-31 22:19:20 +00:00
// Update the dependency and carry on!
p.cargo("update")
.with_stderr(&format!(
2018-03-14 15:17:44 +00:00
"[UPDATING] git repository `{}`\n\
[UPDATING] bar v0.5.0 ([..]) -> #[..]\n\
",
git_project.url()
2018-12-08 11:19:47 +00:00
))
.run();
2014-07-31 22:19:20 +00:00
println!("going for the last compile");
2023-02-15 23:51:46 +00:00
p.cargo("check")
.with_stderr(&format!(
2023-02-15 23:51:46 +00:00
"[CHECKING] bar v0.5.0 ({}#[..])\n\
[CHECKING] foo v0.5.0 ([CWD])\n\
2018-03-14 15:17:44 +00:00
[FINISHED] dev [unoptimized + debuginfo] target(s) \
in [..]\n",
git_project.url(),
2018-12-08 11:19:47 +00:00
))
.run();
// Make sure clean only cleans one dep
2023-11-15 22:33:27 +00:00
p.cargo("clean -p foo").with_stderr("[REMOVED] [..]").run();
2023-02-15 23:51:46 +00:00
p.cargo("check")
.with_stderr(
2023-02-15 23:51:46 +00:00
"[CHECKING] foo v0.5.0 ([CWD])\n\
2018-03-14 15:17:44 +00:00
[FINISHED] dev [unoptimized + debuginfo] target(s) \
2018-12-08 11:19:47 +00:00
in [..]",
)
.run();
}
2014-08-01 17:39:11 +00:00
#[cargo_test]
fn update_with_shared_deps() {
let git_project = git::new("bar", |project| {
2014-08-01 17:39:11 +00:00
project
2018-07-24 13:01:56 +00:00
.file("Cargo.toml", &basic_lib_manifest("bar"))
.file("src/bar.rs", "pub fn bar() {}")
});
2014-08-01 17:39:11 +00:00
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
2020-09-27 00:59:58 +00:00
[package]
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
2014-08-01 17:39:11 +00:00
2020-09-27 00:59:58 +00:00
[dependencies.dep1]
path = "dep1"
[dependencies.dep2]
path = "dep2"
"#,
2018-12-08 11:19:47 +00:00
)
.file(
2018-03-14 15:17:44 +00:00
"src/main.rs",
r#"
2020-09-27 00:59:58 +00:00
#[allow(unused_extern_crates)]
extern crate dep1;
#[allow(unused_extern_crates)]
extern crate dep2;
fn main() {}
"#,
2018-12-08 11:19:47 +00:00
)
.file(
2018-03-14 15:17:44 +00:00
"dep1/Cargo.toml",
&format!(
r#"
2020-09-27 00:59:58 +00:00
[package]
name = "dep1"
version = "0.5.0"
authors = ["wycats@example.com"]
[dependencies.bar]
version = "0.5.0"
git = '{}'
"#,
2018-03-14 15:17:44 +00:00
git_project.url()
),
2018-12-08 11:19:47 +00:00
)
.file("dep1/src/lib.rs", "")
2018-03-14 15:17:44 +00:00
.file(
"dep2/Cargo.toml",
&format!(
r#"
2020-09-27 00:59:58 +00:00
[package]
name = "dep2"
version = "0.5.0"
authors = ["wycats@example.com"]
[dependencies.bar]
version = "0.5.0"
git = '{}'
"#,
2018-03-14 15:17:44 +00:00
git_project.url()
),
2018-12-08 11:19:47 +00:00
)
.file("dep2/src/lib.rs", "")
.build();
2014-08-01 17:39:11 +00:00
// First time around we should compile both foo and bar
2023-02-15 23:51:46 +00:00
p.cargo("check")
.with_stderr(&format!(
2018-03-14 15:17:44 +00:00
"\
[UPDATING] git repository `{git}`
2023-02-15 23:51:46 +00:00
[CHECKING] bar v0.5.0 ({git}#[..])
[CHECKING] [..] v0.5.0 ([..])
[CHECKING] [..] v0.5.0 ([..])
[CHECKING] foo v0.5.0 ([CWD])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]\n",
2018-03-14 15:17:44 +00:00
git = git_project.url(),
2018-12-08 11:19:47 +00:00
))
.run();
2014-08-01 17:39:11 +00:00
// Modify a file manually, and commit it
git_project.change_file("src/bar.rs", r#"pub fn bar() { println!("hello!"); }"#);
let repo = git2::Repository::open(&git_project.root()).unwrap();
let old_head = repo.head().unwrap().target().unwrap();
git::add(&repo);
git::commit(&repo);
sleep_ms(1000);
// By default, not transitive updates
println!("dep1 update");
2023-11-15 22:33:27 +00:00
p.cargo("update dep1")
.with_stderr(
"\
[UPDATING] git repository [..]
[UPDATING] bar v0.5.0 [..]
",
)
.run();
// Don't do anything bad on a weird --precise argument
println!("bar bad precise update");
p.cargo("update bar --precise 0.1.2")
.with_status(101)
.with_stderr(
2018-03-14 15:17:44 +00:00
"\
[UPDATING] git repository [..]
[ERROR] Unable to update [..]
2018-03-02 17:44:47 +00:00
Caused by:
revspec '0.1.2' not found; class=Reference (4); code=NotFound (-3)
2018-03-14 15:17:44 +00:00
",
2018-12-08 11:19:47 +00:00
)
.run();
// Specifying a precise rev to the old rev shouldn't actually update
// anything because we already have the rev in the db.
println!("bar precise update");
p.cargo("update bar --precise")
.arg(&old_head.to_string())
2023-11-15 22:33:27 +00:00
.with_stderr("[UPDATING] bar v0.5.0 [..]")
.run();
// Updating recursively should, however, update the repo.
println!("dep1 recursive update");
p.cargo("update dep1 --recursive")
.with_stderr(&format!(
2018-03-14 15:17:44 +00:00
"[UPDATING] git repository `{}`\n\
[UPDATING] bar v0.5.0 ([..]) -> #[..]\n\
",
git_project.url()
2018-12-08 11:19:47 +00:00
))
.run();
2014-08-01 17:39:11 +00:00
// Make sure we still only compile one version of the git repo
println!("build");
2023-02-15 23:51:46 +00:00
p.cargo("check")
.with_stderr(&format!(
2018-03-14 15:17:44 +00:00
"\
2023-02-15 23:51:46 +00:00
[CHECKING] bar v0.5.0 ({git}#[..])
[CHECKING] [..] v0.5.0 ([CWD][..]dep[..])
[CHECKING] [..] v0.5.0 ([CWD][..]dep[..])
[CHECKING] foo v0.5.0 ([CWD])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]\n",
2018-03-14 15:17:44 +00:00
git = git_project.url(),
2018-12-08 11:19:47 +00:00
))
.run();
// We should be able to update transitive deps
p.cargo("update bar")
.with_stderr(&format!(
2018-03-14 15:17:44 +00:00
"[UPDATING] git repository `{}`",
git_project.url()
2018-12-08 11:19:47 +00:00
))
.run();
}
#[cargo_test]
fn dep_with_submodule() {
let project = project();
let git_project = git::new("dep1", |project| {
2018-07-24 22:35:01 +00:00
project.file("Cargo.toml", &basic_manifest("dep1", "0.5.0"))
});
let git_project2 = git::new("dep2", |project| project.file("lib.rs", "pub fn dep() {}"));
let repo = git2::Repository::open(&git_project.root()).unwrap();
let url = path2url(git_project2.root()).to_string();
git::add_submodule(&repo, &url, Path::new("src"));
git::commit(&repo);
let project = project
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
&format!(
r#"
[package]
2020-09-27 00:59:58 +00:00
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
2020-09-27 00:59:58 +00:00
[dependencies.dep1]
2020-09-27 00:59:58 +00:00
git = '{}'
"#,
2018-03-14 15:17:44 +00:00
git_project.url()
),
2018-12-08 11:19:47 +00:00
)
.file(
"src/lib.rs",
"extern crate dep1; pub fn foo() { dep1::dep() }",
2018-12-08 11:19:47 +00:00
)
.build();
project
2023-02-15 23:51:46 +00:00
.cargo("check")
.with_stderr(
"\
2016-05-20 13:39:58 +00:00
[UPDATING] git repository [..]
[UPDATING] git submodule `file://[..]/dep2`
2023-02-15 23:51:46 +00:00
[CHECKING] dep1 [..]
[CHECKING] foo [..]
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]\n",
)
.run();
}
#[cargo_test]
fn dep_with_relative_submodule() {
let foo = project();
let base = git::new("base", |project| {
project
.file(
"Cargo.toml",
r#"
[package]
name = "base"
version = "0.5.0"
[dependencies]
deployment.path = "deployment"
"#,
)
.file(
"src/lib.rs",
r#"
pub fn dep() {
deployment::deployment_func();
}
"#,
)
});
let _deployment = git::new("deployment", |project| {
project
.file("src/lib.rs", "pub fn deployment_func() {}")
.file("Cargo.toml", &basic_lib_manifest("deployment"))
});
let base_repo = git2::Repository::open(&base.root()).unwrap();
git::add_submodule(&base_repo, "../deployment", Path::new("deployment"));
git::commit(&base_repo);
let project = foo
.file(
"Cargo.toml",
&format!(
r#"
[package]
name = "foo"
version = "0.5.0"
[dependencies.base]
git = '{}'
"#,
base.url()
),
)
.file("src/lib.rs", "pub fn foo() { }")
.build();
project
2023-02-15 23:51:46 +00:00
.cargo("check")
.with_stderr(
"\
[UPDATING] git repository [..]
[UPDATING] git submodule `file://[..]/deployment`
2023-02-15 23:51:46 +00:00
[CHECKING] deployment [..]
[CHECKING] base [..]
[CHECKING] foo [..]
2018-03-14 15:17:44 +00:00
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]\n",
2018-12-08 11:19:47 +00:00
)
.run();
}
#[cargo_test]
2017-04-14 03:58:02 +00:00
fn dep_with_bad_submodule() {
let project = project();
2017-04-14 03:58:02 +00:00
let git_project = git::new("dep1", |project| {
2018-07-24 22:35:01 +00:00
project.file("Cargo.toml", &basic_manifest("dep1", "0.5.0"))
});
let git_project2 = git::new("dep2", |project| project.file("lib.rs", "pub fn dep() {}"));
2017-04-14 03:58:02 +00:00
let repo = git2::Repository::open(&git_project.root()).unwrap();
let url = path2url(git_project2.root()).to_string();
git::add_submodule(&repo, &url, Path::new("src"));
git::commit(&repo);
// now amend the first commit on git_project2 to make submodule ref point to not-found
// commit
let repo = git2::Repository::open(&git_project2.root()).unwrap();
let original_submodule_ref = repo.refname_to_id("refs/heads/master").unwrap();
let commit = repo.find_commit(original_submodule_ref).unwrap();
2018-03-14 15:17:44 +00:00
commit
.amend(
Some("refs/heads/master"),
None,
None,
None,
Some("something something"),
None,
2018-12-08 11:19:47 +00:00
)
.unwrap();
2017-04-14 03:58:02 +00:00
let p = project
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
&format!(
r#"
[package]
2017-04-14 03:58:02 +00:00
2020-09-27 00:59:58 +00:00
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
2017-04-14 03:58:02 +00:00
2020-09-27 00:59:58 +00:00
[dependencies.dep1]
2017-04-14 03:58:02 +00:00
2020-09-27 00:59:58 +00:00
git = '{}'
"#,
2018-03-14 15:17:44 +00:00
git_project.url()
),
2018-12-08 11:19:47 +00:00
)
.file(
"src/lib.rs",
"extern crate dep1; pub fn foo() { dep1::dep() }",
2018-12-08 11:19:47 +00:00
)
.build();
2017-04-14 03:58:02 +00:00
let expected = format!(
"\
2017-04-14 03:58:02 +00:00
[UPDATING] git repository [..]
[UPDATING] git submodule `file://[..]/dep2`
[ERROR] failed to get `dep1` as a dependency of package `foo v0.5.0 [..]`
Caused by:
failed to load source for dependency `dep1`
2017-04-14 03:58:02 +00:00
Caused by:
Unable to update {}
2017-04-14 03:58:02 +00:00
Caused by:
2017-04-17 12:08:51 +00:00
failed to update submodule `src`
Caused by:
object not found - no match for id [..]
2018-03-14 15:17:44 +00:00
",
path2url(git_project.root())
);
2017-04-14 07:55:26 +00:00
2023-02-15 23:51:46 +00:00
p.cargo("check")
.with_stderr(expected)
.with_status(101)
.run();
2017-04-14 03:58:02 +00:00
}
Respect submodule update=none strategy in .gitmodules Git lets users define the default update/checkout strategy for a submodule by setting the `submodule.<name>.update` key in `.gitmodules` file. If the update strategy is `none`, the submodule will be skipped during update. It will not be fetched and checked out: 1. *foo* is a big git repo ``` /tmp $ git init foo Initialized empty Git repository in /tmp/foo/.git/ /tmp $ dd if=/dev/zero of=foo/big bs=1000M count=1 1+0 records in 1+0 records out 1048576000 bytes (1.0 GB, 1000 MiB) copied, 0.482087 s, 2.2 GB/s /tmp $ git -C foo add big /tmp $ git -C foo commit -m 'I am big' [main (root-commit) 84fb533] I am big 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 big ``` 2. *bar* is a repo with a big submodule with `update=none` ``` /tmp $ git init bar Initialized empty Git repository in /tmp/bar/.git/ /tmp $ git -C bar submodule add file:///tmp/foo foo Cloning into '/tmp/bar/foo'... remote: Enumerating objects: 3, done. remote: Counting objects: 100% (3/3), done. remote: Total 3 (delta 0), reused 1 (delta 0), pack-reused 0 Receiving objects: 100% (3/3), 995.50 KiB | 338.00 KiB/s, done. /tmp $ git -C bar config --file .gitmodules submodule.foo.update none /tmp $ cat bar/.gitmodules [submodule "foo"] path = foo url = file:///tmp/foo update = none /tmp $ git -C bar commit --all -m 'I have a big submodule with update=none' [main (root-commit) 6c355ea] I have a big submodule not updated by default 2 files changed, 4 insertions(+) create mode 100644 .gitmodules create mode 160000 foo ``` 3. *baz* is a clone of *bar*, notice *foo* submodule gets skipped ``` /tmp $ git clone --recurse-submodules file:///tmp/bar baz Cloning into 'baz'... remote: Enumerating objects: 3, done. remote: Counting objects: 100% (3/3), done. remote: Compressing objects: 100% (3/3), done. remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 Receiving objects: 100% (3/3), done. Submodule 'foo' (file:///tmp/foo) registered for path 'foo' Skipping submodule 'foo' /tmp $ git -C baz submodule update --init Skipping submodule 'foo' /tmp $ ``` Cargo, on the other hand, ignores the submodule update strategy set in `.gitmodules` properties when updating dependencies. Such behavior can be considered against the wish of the crate publisher. 4. *bar* is now a lib with a big submodule with update disabled ``` /tmp $ cargo init --lib bar Created library package /tmp $ git -C bar add . /tmp $ git -C bar commit -m 'I am a lib with a big submodule but update=none' [main eb07cf7] I am a lib with a big submodule but update=none 3 files changed, 18 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 src/lib.rs /tmp $ ``` 5. *qux* depends on *bar*, notice *bar*'s submodules are fetched ``` /tmp $ cargo init qux && cd qux Created binary (application) package /tmp/qux $ echo -e '[dependencies.bar]\ngit = "file:///tmp/bar"' >> Cargo.toml /tmp/qux $ time cargo update Updating git repository `file:///tmp/bar` Updating git submodule `file:///tmp/foo` real 0m22.182s user 0m20.402s sys 0m1.714s /tmp/qux $ ``` Fix it by checking if a Git repository submodule should be updated when cargo processes dependencies. 6. With the change applied, submodules with `update=none` are skipped ``` /tmp/qux $ cargo cache -a > /dev/null /tmp/qux $ time ~/src/cargo/target/debug/cargo update Updating git repository `file:///tmp/bar` Skipping git submodule `file:///tmp/foo` real 0m0.029s user 0m0.021s sys 0m0.008s /tmp/qux $ ``` Fixes #4247. Signed-off-by: Jakub Sitnicki <jakub@cloudflare.com>
2022-05-31 08:36:50 +00:00
#[cargo_test]
fn dep_with_skipped_submodule() {
// Ensure we skip dependency submodules if their update strategy is `none`.
let qux = git::new("qux", |project| {
project.no_manifest().file("README", "skip me")
});
let bar = git::new("bar", |project| {
project
.file("Cargo.toml", &basic_manifest("bar", "0.0.0"))
.file("src/lib.rs", "")
});
// `qux` is a submodule of `bar`, but we don't want to update it.
let repo = git2::Repository::open(&bar.root()).unwrap();
git::add_submodule(&repo, qux.url().as_str(), Path::new("qux"));
let mut conf = git2::Config::open(&bar.root().join(".gitmodules")).unwrap();
conf.set_str("submodule.qux.update", "none").unwrap();
git::add(&repo);
git::commit(&repo);
let foo = project()
.file(
"Cargo.toml",
&format!(
r#"
[package]
Respect submodule update=none strategy in .gitmodules Git lets users define the default update/checkout strategy for a submodule by setting the `submodule.<name>.update` key in `.gitmodules` file. If the update strategy is `none`, the submodule will be skipped during update. It will not be fetched and checked out: 1. *foo* is a big git repo ``` /tmp $ git init foo Initialized empty Git repository in /tmp/foo/.git/ /tmp $ dd if=/dev/zero of=foo/big bs=1000M count=1 1+0 records in 1+0 records out 1048576000 bytes (1.0 GB, 1000 MiB) copied, 0.482087 s, 2.2 GB/s /tmp $ git -C foo add big /tmp $ git -C foo commit -m 'I am big' [main (root-commit) 84fb533] I am big 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 big ``` 2. *bar* is a repo with a big submodule with `update=none` ``` /tmp $ git init bar Initialized empty Git repository in /tmp/bar/.git/ /tmp $ git -C bar submodule add file:///tmp/foo foo Cloning into '/tmp/bar/foo'... remote: Enumerating objects: 3, done. remote: Counting objects: 100% (3/3), done. remote: Total 3 (delta 0), reused 1 (delta 0), pack-reused 0 Receiving objects: 100% (3/3), 995.50 KiB | 338.00 KiB/s, done. /tmp $ git -C bar config --file .gitmodules submodule.foo.update none /tmp $ cat bar/.gitmodules [submodule "foo"] path = foo url = file:///tmp/foo update = none /tmp $ git -C bar commit --all -m 'I have a big submodule with update=none' [main (root-commit) 6c355ea] I have a big submodule not updated by default 2 files changed, 4 insertions(+) create mode 100644 .gitmodules create mode 160000 foo ``` 3. *baz* is a clone of *bar*, notice *foo* submodule gets skipped ``` /tmp $ git clone --recurse-submodules file:///tmp/bar baz Cloning into 'baz'... remote: Enumerating objects: 3, done. remote: Counting objects: 100% (3/3), done. remote: Compressing objects: 100% (3/3), done. remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 Receiving objects: 100% (3/3), done. Submodule 'foo' (file:///tmp/foo) registered for path 'foo' Skipping submodule 'foo' /tmp $ git -C baz submodule update --init Skipping submodule 'foo' /tmp $ ``` Cargo, on the other hand, ignores the submodule update strategy set in `.gitmodules` properties when updating dependencies. Such behavior can be considered against the wish of the crate publisher. 4. *bar* is now a lib with a big submodule with update disabled ``` /tmp $ cargo init --lib bar Created library package /tmp $ git -C bar add . /tmp $ git -C bar commit -m 'I am a lib with a big submodule but update=none' [main eb07cf7] I am a lib with a big submodule but update=none 3 files changed, 18 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 src/lib.rs /tmp $ ``` 5. *qux* depends on *bar*, notice *bar*'s submodules are fetched ``` /tmp $ cargo init qux && cd qux Created binary (application) package /tmp/qux $ echo -e '[dependencies.bar]\ngit = "file:///tmp/bar"' >> Cargo.toml /tmp/qux $ time cargo update Updating git repository `file:///tmp/bar` Updating git submodule `file:///tmp/foo` real 0m22.182s user 0m20.402s sys 0m1.714s /tmp/qux $ ``` Fix it by checking if a Git repository submodule should be updated when cargo processes dependencies. 6. With the change applied, submodules with `update=none` are skipped ``` /tmp/qux $ cargo cache -a > /dev/null /tmp/qux $ time ~/src/cargo/target/debug/cargo update Updating git repository `file:///tmp/bar` Skipping git submodule `file:///tmp/foo` real 0m0.029s user 0m0.021s sys 0m0.008s /tmp/qux $ ``` Fixes #4247. Signed-off-by: Jakub Sitnicki <jakub@cloudflare.com>
2022-05-31 08:36:50 +00:00
name = "foo"
version = "0.0.0"
authors = []
[dependencies.bar]
git = "{}"
"#,
bar.url()
),
)
.file("src/main.rs", "fn main() {}")
.build();
2023-02-15 23:51:46 +00:00
foo.cargo("check")
Respect submodule update=none strategy in .gitmodules Git lets users define the default update/checkout strategy for a submodule by setting the `submodule.<name>.update` key in `.gitmodules` file. If the update strategy is `none`, the submodule will be skipped during update. It will not be fetched and checked out: 1. *foo* is a big git repo ``` /tmp $ git init foo Initialized empty Git repository in /tmp/foo/.git/ /tmp $ dd if=/dev/zero of=foo/big bs=1000M count=1 1+0 records in 1+0 records out 1048576000 bytes (1.0 GB, 1000 MiB) copied, 0.482087 s, 2.2 GB/s /tmp $ git -C foo add big /tmp $ git -C foo commit -m 'I am big' [main (root-commit) 84fb533] I am big 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 big ``` 2. *bar* is a repo with a big submodule with `update=none` ``` /tmp $ git init bar Initialized empty Git repository in /tmp/bar/.git/ /tmp $ git -C bar submodule add file:///tmp/foo foo Cloning into '/tmp/bar/foo'... remote: Enumerating objects: 3, done. remote: Counting objects: 100% (3/3), done. remote: Total 3 (delta 0), reused 1 (delta 0), pack-reused 0 Receiving objects: 100% (3/3), 995.50 KiB | 338.00 KiB/s, done. /tmp $ git -C bar config --file .gitmodules submodule.foo.update none /tmp $ cat bar/.gitmodules [submodule "foo"] path = foo url = file:///tmp/foo update = none /tmp $ git -C bar commit --all -m 'I have a big submodule with update=none' [main (root-commit) 6c355ea] I have a big submodule not updated by default 2 files changed, 4 insertions(+) create mode 100644 .gitmodules create mode 160000 foo ``` 3. *baz* is a clone of *bar*, notice *foo* submodule gets skipped ``` /tmp $ git clone --recurse-submodules file:///tmp/bar baz Cloning into 'baz'... remote: Enumerating objects: 3, done. remote: Counting objects: 100% (3/3), done. remote: Compressing objects: 100% (3/3), done. remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 Receiving objects: 100% (3/3), done. Submodule 'foo' (file:///tmp/foo) registered for path 'foo' Skipping submodule 'foo' /tmp $ git -C baz submodule update --init Skipping submodule 'foo' /tmp $ ``` Cargo, on the other hand, ignores the submodule update strategy set in `.gitmodules` properties when updating dependencies. Such behavior can be considered against the wish of the crate publisher. 4. *bar* is now a lib with a big submodule with update disabled ``` /tmp $ cargo init --lib bar Created library package /tmp $ git -C bar add . /tmp $ git -C bar commit -m 'I am a lib with a big submodule but update=none' [main eb07cf7] I am a lib with a big submodule but update=none 3 files changed, 18 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 src/lib.rs /tmp $ ``` 5. *qux* depends on *bar*, notice *bar*'s submodules are fetched ``` /tmp $ cargo init qux && cd qux Created binary (application) package /tmp/qux $ echo -e '[dependencies.bar]\ngit = "file:///tmp/bar"' >> Cargo.toml /tmp/qux $ time cargo update Updating git repository `file:///tmp/bar` Updating git submodule `file:///tmp/foo` real 0m22.182s user 0m20.402s sys 0m1.714s /tmp/qux $ ``` Fix it by checking if a Git repository submodule should be updated when cargo processes dependencies. 6. With the change applied, submodules with `update=none` are skipped ``` /tmp/qux $ cargo cache -a > /dev/null /tmp/qux $ time ~/src/cargo/target/debug/cargo update Updating git repository `file:///tmp/bar` Skipping git submodule `file:///tmp/foo` real 0m0.029s user 0m0.021s sys 0m0.008s /tmp/qux $ ``` Fixes #4247. Signed-off-by: Jakub Sitnicki <jakub@cloudflare.com>
2022-05-31 08:36:50 +00:00
.with_stderr(
"\
[UPDATING] git repository `file://[..]/bar`
[SKIPPING] git submodule `file://[..]/qux` [..]
2023-02-15 23:51:46 +00:00
[CHECKING] bar [..]
[CHECKING] foo [..]
Respect submodule update=none strategy in .gitmodules Git lets users define the default update/checkout strategy for a submodule by setting the `submodule.<name>.update` key in `.gitmodules` file. If the update strategy is `none`, the submodule will be skipped during update. It will not be fetched and checked out: 1. *foo* is a big git repo ``` /tmp $ git init foo Initialized empty Git repository in /tmp/foo/.git/ /tmp $ dd if=/dev/zero of=foo/big bs=1000M count=1 1+0 records in 1+0 records out 1048576000 bytes (1.0 GB, 1000 MiB) copied, 0.482087 s, 2.2 GB/s /tmp $ git -C foo add big /tmp $ git -C foo commit -m 'I am big' [main (root-commit) 84fb533] I am big 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 big ``` 2. *bar* is a repo with a big submodule with `update=none` ``` /tmp $ git init bar Initialized empty Git repository in /tmp/bar/.git/ /tmp $ git -C bar submodule add file:///tmp/foo foo Cloning into '/tmp/bar/foo'... remote: Enumerating objects: 3, done. remote: Counting objects: 100% (3/3), done. remote: Total 3 (delta 0), reused 1 (delta 0), pack-reused 0 Receiving objects: 100% (3/3), 995.50 KiB | 338.00 KiB/s, done. /tmp $ git -C bar config --file .gitmodules submodule.foo.update none /tmp $ cat bar/.gitmodules [submodule "foo"] path = foo url = file:///tmp/foo update = none /tmp $ git -C bar commit --all -m 'I have a big submodule with update=none' [main (root-commit) 6c355ea] I have a big submodule not updated by default 2 files changed, 4 insertions(+) create mode 100644 .gitmodules create mode 160000 foo ``` 3. *baz* is a clone of *bar*, notice *foo* submodule gets skipped ``` /tmp $ git clone --recurse-submodules file:///tmp/bar baz Cloning into 'baz'... remote: Enumerating objects: 3, done. remote: Counting objects: 100% (3/3), done. remote: Compressing objects: 100% (3/3), done. remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 Receiving objects: 100% (3/3), done. Submodule 'foo' (file:///tmp/foo) registered for path 'foo' Skipping submodule 'foo' /tmp $ git -C baz submodule update --init Skipping submodule 'foo' /tmp $ ``` Cargo, on the other hand, ignores the submodule update strategy set in `.gitmodules` properties when updating dependencies. Such behavior can be considered against the wish of the crate publisher. 4. *bar* is now a lib with a big submodule with update disabled ``` /tmp $ cargo init --lib bar Created library package /tmp $ git -C bar add . /tmp $ git -C bar commit -m 'I am a lib with a big submodule but update=none' [main eb07cf7] I am a lib with a big submodule but update=none 3 files changed, 18 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 src/lib.rs /tmp $ ``` 5. *qux* depends on *bar*, notice *bar*'s submodules are fetched ``` /tmp $ cargo init qux && cd qux Created binary (application) package /tmp/qux $ echo -e '[dependencies.bar]\ngit = "file:///tmp/bar"' >> Cargo.toml /tmp/qux $ time cargo update Updating git repository `file:///tmp/bar` Updating git submodule `file:///tmp/foo` real 0m22.182s user 0m20.402s sys 0m1.714s /tmp/qux $ ``` Fix it by checking if a Git repository submodule should be updated when cargo processes dependencies. 6. With the change applied, submodules with `update=none` are skipped ``` /tmp/qux $ cargo cache -a > /dev/null /tmp/qux $ time ~/src/cargo/target/debug/cargo update Updating git repository `file:///tmp/bar` Skipping git submodule `file:///tmp/foo` real 0m0.029s user 0m0.021s sys 0m0.008s /tmp/qux $ ``` Fixes #4247. Signed-off-by: Jakub Sitnicki <jakub@cloudflare.com>
2022-05-31 08:36:50 +00:00
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]\n",
)
.run();
}
#[cargo_test]
fn ambiguous_published_deps() {
let project = project();
let git_project = git::new("dep", |project| {
project
.file(
"aaa/Cargo.toml",
&format!(
r#"
[package]
name = "bar"
version = "0.5.0"
publish = true
"#
),
)
.file("aaa/src/lib.rs", "")
.file(
"bbb/Cargo.toml",
&format!(
r#"
[package]
name = "bar"
version = "0.5.0"
publish = true
"#
),
)
.file("bbb/src/lib.rs", "")
});
let p = project
.file(
"Cargo.toml",
&format!(
r#"
[package]
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
[dependencies.bar]
git = '{}'
"#,
git_project.url()
),
)
.file("src/main.rs", "fn main() { }")
.build();
p.cargo("build").run();
p.cargo("run")
.with_stderr(
"\
[WARNING] skipping duplicate package `bar` found at `[..]`
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
[RUNNING] `target/debug/foo[EXE]`
",
)
.run();
}
#[cargo_test]
fn two_deps_only_update_one() {
let project = project();
let git1 = git::new("dep1", |project| {
project
2018-07-24 22:35:01 +00:00
.file("Cargo.toml", &basic_manifest("dep1", "0.5.0"))
.file("src/lib.rs", "")
});
let git2 = git::new("dep2", |project| {
project
2018-07-24 22:35:01 +00:00
.file("Cargo.toml", &basic_manifest("dep2", "0.5.0"))
.file("src/lib.rs", "")
});
let p = project
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
&format!(
r#"
[package]
2020-09-27 00:59:58 +00:00
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
2020-09-27 00:59:58 +00:00
[dependencies.dep1]
git = '{}'
[dependencies.dep2]
git = '{}'
"#,
2018-03-14 15:17:44 +00:00
git1.url(),
git2.url()
),
2018-12-08 11:19:47 +00:00
)
.file("src/main.rs", "fn main() {}")
.build();
fn oid_to_short_sha(oid: git2::Oid) -> String {
oid.to_string()[..8].to_string()
}
fn git_repo_head_sha(p: &Project) -> String {
let repo = git2::Repository::open(p.root()).unwrap();
let head = repo.head().unwrap().target().unwrap();
oid_to_short_sha(head)
}
println!("dep1 head sha: {}", git_repo_head_sha(&git1));
println!("dep2 head sha: {}", git_repo_head_sha(&git2));
2023-02-15 23:51:46 +00:00
p.cargo("check")
.with_stderr(
2018-03-14 15:17:44 +00:00
"[UPDATING] git repository `[..]`\n\
[UPDATING] git repository `[..]`\n\
2023-02-15 23:51:46 +00:00
[CHECKING] [..] v0.5.0 ([..])\n\
[CHECKING] [..] v0.5.0 ([..])\n\
[CHECKING] foo v0.5.0 ([CWD])\n\
2018-03-14 15:17:44 +00:00
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]\n",
2018-12-08 11:19:47 +00:00
)
.run();
2018-03-14 15:17:44 +00:00
git1.change_file("src/lib.rs", "pub fn foo() {}");
let repo = git2::Repository::open(&git1.root()).unwrap();
git::add(&repo);
let oid = git::commit(&repo);
println!("dep1 head sha: {}", oid_to_short_sha(oid));
p.cargo("update dep1")
.with_stderr(&format!(
2018-03-14 15:17:44 +00:00
"[UPDATING] git repository `{}`\n\
[UPDATING] dep1 v0.5.0 ([..]) -> #[..]\n\
",
git1.url()
2018-12-08 11:19:47 +00:00
))
.run();
}
#[cargo_test]
fn stale_cached_version() {
let bar = git::new("meta-dep", |project| {
2018-03-14 15:17:44 +00:00
project
2018-07-24 22:35:01 +00:00
.file("Cargo.toml", &basic_manifest("bar", "0.0.0"))
2018-03-14 15:17:44 +00:00
.file("src/lib.rs", "pub fn bar() -> i32 { 1 }")
});
// Update the git database in the cache with the current state of the git
// repo
let foo = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
&format!(
r#"
[package]
2020-09-27 00:59:58 +00:00
name = "foo"
version = "0.0.0"
authors = []
[dependencies.bar]
git = '{}'
"#,
2018-03-14 15:17:44 +00:00
bar.url()
),
2018-12-08 11:19:47 +00:00
)
.file(
2018-03-14 15:17:44 +00:00
"src/main.rs",
r#"
2020-09-27 00:59:58 +00:00
extern crate bar;
2020-09-27 00:59:58 +00:00
fn main() { assert_eq!(bar::bar(), 1) }
"#,
2018-12-08 11:19:47 +00:00
)
.build();
foo.cargo("build").run();
2018-08-28 20:38:26 +00:00
foo.process(&foo.bin("foo")).run();
2019-02-03 04:01:23 +00:00
// Update the repo, and simulate someone else updating the lock file and then
// us pulling it down.
bar.change_file("src/lib.rs", "pub fn bar() -> i32 { 1 + 0 }");
let repo = git2::Repository::open(&bar.root()).unwrap();
git::add(&repo);
git::commit(&repo);
sleep_ms(1000);
let rev = repo.revparse_single("HEAD").unwrap().id();
foo.change_file(
"Cargo.lock",
&format!(
r#"
[[package]]
name = "foo"
version = "0.0.0"
dependencies = [
'bar 0.0.0 (git+{url}#{hash})'
]
[[package]]
name = "bar"
version = "0.0.0"
source = 'git+{url}#{hash}'
"#,
url = bar.url(),
hash = rev
),
);
// Now build!
foo.cargo("build")
.with_stderr(&format!(
2018-03-14 15:17:44 +00:00
"\
[UPDATING] git repository `{bar}`
[COMPILING] bar v0.0.0 ({bar}#[..])
[COMPILING] foo v0.0.0 ([CWD])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
2018-03-14 15:17:44 +00:00
",
bar = bar.url(),
2018-12-08 11:19:47 +00:00
))
.run();
2018-08-28 20:38:26 +00:00
foo.process(&foo.bin("foo")).run();
}
2014-08-16 18:20:47 +00:00
#[cargo_test]
fn dep_with_changed_submodule() {
let project = project();
let git_project = git::new("dep1", |project| {
2018-07-24 22:35:01 +00:00
project.file("Cargo.toml", &basic_manifest("dep1", "0.5.0"))
});
2014-08-16 18:20:47 +00:00
let git_project2 = git::new("dep2", |project| {
2018-03-14 15:17:44 +00:00
project.file("lib.rs", "pub fn dep() -> &'static str { \"project2\" }")
});
2014-08-16 18:20:47 +00:00
let git_project3 = git::new("dep3", |project| {
2018-03-14 15:17:44 +00:00
project.file("lib.rs", "pub fn dep() -> &'static str { \"project3\" }")
});
2014-08-16 18:20:47 +00:00
let repo = git2::Repository::open(&git_project.root()).unwrap();
2018-03-14 15:17:44 +00:00
let mut sub = git::add_submodule(&repo, &git_project2.url().to_string(), Path::new("src"));
git::commit(&repo);
2014-08-16 18:20:47 +00:00
let p = project
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
&format!(
r#"
[package]
2020-09-27 00:59:58 +00:00
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
[dependencies.dep1]
git = '{}'
"#,
2018-03-14 15:17:44 +00:00
git_project.url()
),
2018-12-08 11:19:47 +00:00
)
.file(
2018-03-14 15:17:44 +00:00
"src/main.rs",
"
2014-08-16 18:20:47 +00:00
extern crate dep1;
pub fn main() { println!(\"{}\", dep1::dep()) }
2018-03-14 15:17:44 +00:00
",
2018-12-08 11:19:47 +00:00
)
.build();
2014-08-16 18:20:47 +00:00
2014-08-27 05:23:01 +00:00
println!("first run");
p.cargo("run")
.with_stderr(
"[UPDATING] git repository `[..]`\n\
[UPDATING] git submodule `file://[..]/dep2`\n\
[COMPILING] dep1 v0.5.0 ([..])\n\
[COMPILING] foo v0.5.0 ([..])\n\
[FINISHED] dev [unoptimized + debuginfo] target(s) in \
[..]\n\
[RUNNING] `target/debug/foo[EXE]`\n",
2018-12-08 11:19:47 +00:00
)
.with_stdout("project2\n")
.run();
2018-03-14 15:17:44 +00:00
git_project.change_file(
".gitmodules",
&format!(
"[submodule \"src\"]\n\tpath = src\n\turl={}",
git_project3.url()
),
);
2014-08-16 18:20:47 +00:00
// Sync the submodule and reset it to the new remote.
sub.sync().unwrap();
{
let subrepo = sub.open().unwrap();
2018-03-14 15:17:44 +00:00
subrepo
.remote_add_fetch("origin", "refs/heads/*:refs/heads/*")
.unwrap();
subrepo
.remote_set_url("origin", &git_project3.url().to_string())
.unwrap();
let mut origin = subrepo.find_remote("origin").unwrap();
2019-12-13 11:40:31 +00:00
origin.fetch(&Vec::<String>::new(), None, None).unwrap();
let id = subrepo.refname_to_id("refs/remotes/origin/master").unwrap();
let obj = subrepo.find_object(id, None).unwrap();
2015-03-16 13:27:58 +00:00
subrepo.reset(&obj, git2::ResetType::Hard, None).unwrap();
}
sub.add_to_index(true).unwrap();
git::add(&repo);
git::commit(&repo);
2014-08-16 18:20:47 +00:00
sleep_ms(1000);
2014-08-16 18:20:47 +00:00
// Update the dependency and carry on!
2014-08-27 05:23:01 +00:00
println!("update");
p.cargo("update -v")
.with_stderr("")
.with_stderr(&format!(
2018-03-14 15:17:44 +00:00
"[UPDATING] git repository `{}`\n\
[UPDATING] git submodule `file://[..]/dep3`\n\
2018-03-14 15:17:44 +00:00
[UPDATING] dep1 v0.5.0 ([..]) -> #[..]\n\
",
git_project.url()
2018-12-08 11:19:47 +00:00
))
.run();
2014-08-16 18:20:47 +00:00
2014-08-27 05:23:01 +00:00
println!("last run");
p.cargo("run")
.with_stderr(
"[COMPILING] dep1 v0.5.0 ([..])\n\
[COMPILING] foo v0.5.0 ([..])\n\
[FINISHED] dev [unoptimized + debuginfo] target(s) in \
[..]\n\
[RUNNING] `target/debug/foo[EXE]`\n",
2018-12-08 11:19:47 +00:00
)
.with_stdout("project3\n")
.run();
}
#[cargo_test]
fn dev_deps_with_testing() {
let p2 = git::new("bar", |project| {
2018-03-14 15:17:44 +00:00
project
2018-07-24 22:35:01 +00:00
.file("Cargo.toml", &basic_manifest("bar", "0.5.0"))
2018-03-14 15:17:44 +00:00
.file(
"src/lib.rs",
r#"
2020-09-27 00:59:58 +00:00
pub fn gimme() -> &'static str { "zoidberg" }
"#,
2018-03-14 15:17:44 +00:00
)
});
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
&format!(
r#"
[package]
2020-09-27 00:59:58 +00:00
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
2020-09-27 00:59:58 +00:00
[dev-dependencies.bar]
version = "0.5.0"
git = '{}'
"#,
2018-03-14 15:17:44 +00:00
p2.url()
),
2018-12-08 11:19:47 +00:00
)
.file(
2018-03-14 15:17:44 +00:00
"src/main.rs",
r#"
2020-09-27 00:59:58 +00:00
fn main() {}
2020-09-27 00:59:58 +00:00
#[cfg(test)]
mod tests {
extern crate bar;
#[test] fn foo() { bar::gimme(); }
}
"#,
2018-12-08 11:19:47 +00:00
)
.build();
2019-02-03 04:01:23 +00:00
// Generate a lock file which did not use `bar` to compile, but had to update
// `bar` to generate the lock file
2023-02-15 23:51:46 +00:00
p.cargo("check")
.with_stderr(&format!(
2018-03-14 15:17:44 +00:00
"\
[UPDATING] git repository `{bar}`
2023-02-15 23:51:46 +00:00
[CHECKING] foo v0.5.0 ([CWD])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
2018-03-14 15:17:44 +00:00
",
bar = p2.url()
2018-12-08 11:19:47 +00:00
))
.run();
// Make sure we use the previous resolution of `bar` instead of updating it
// a second time.
p.cargo("test")
.with_stderr(
"\
[COMPILING] [..] v0.5.0 ([..])
[COMPILING] [..] v0.5.0 ([..]
[FINISHED] test [unoptimized + debuginfo] target(s) in [..]
2021-02-21 22:37:42 +00:00
[RUNNING] [..] (target/debug/deps/foo-[..][EXE])",
2018-12-08 11:19:47 +00:00
)
.with_stdout_contains("test tests::foo ... ok")
.run();
}
#[cargo_test]
fn git_build_cmd_freshness() {
let foo = git::new("foo", |project| {
2018-03-14 15:17:44 +00:00
project
.file(
"Cargo.toml",
r#"
2020-09-27 00:59:58 +00:00
[package]
name = "foo"
version = "0.0.0"
authors = []
build = "build.rs"
"#,
2018-12-08 11:19:47 +00:00
)
.file("build.rs", "fn main() {}")
2018-03-14 15:17:44 +00:00
.file("src/lib.rs", "pub fn bar() -> i32 { 1 }")
.file(".gitignore", "src/bar.rs")
});
foo.root().move_into_the_past();
sleep_ms(1000);
2014-08-19 05:51:14 +00:00
2023-02-15 23:51:46 +00:00
foo.cargo("check")
.with_stderr(
2018-03-14 15:17:44 +00:00
"\
[COMPILING] foo v0.0.0 ([CWD])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
2018-03-14 15:17:44 +00:00
",
2018-12-08 11:19:47 +00:00
)
.run();
// Smoke test to make sure it doesn't compile again
println!("first pass");
2023-11-15 22:33:27 +00:00
foo.cargo("check").with_stderr("[FINISHED] [..]").run();
// Modify an ignored file and make sure we don't rebuild
println!("second pass");
foo.change_file("src/bar.rs", "");
2023-11-15 22:33:27 +00:00
foo.cargo("check").with_stderr("[FINISHED] [..]").run();
}
#[cargo_test]
fn git_name_not_always_needed() {
let p2 = git::new("bar", |project| {
2018-03-14 15:17:44 +00:00
project
2018-07-24 22:35:01 +00:00
.file("Cargo.toml", &basic_manifest("bar", "0.5.0"))
2018-03-14 15:17:44 +00:00
.file(
"src/lib.rs",
r#"
2020-09-27 00:59:58 +00:00
pub fn gimme() -> &'static str { "zoidberg" }
"#,
2018-03-14 15:17:44 +00:00
)
});
let repo = git2::Repository::open(&p2.root()).unwrap();
let mut cfg = repo.config().unwrap();
let _ = cfg.remove("user.name");
let _ = cfg.remove("user.email");
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
&format!(
r#"
[package]
2020-09-27 00:59:58 +00:00
name = "foo"
version = "0.5.0"
authors = []
[dev-dependencies.bar]
git = '{}'
"#,
2018-03-14 15:17:44 +00:00
p2.url()
),
2018-12-08 11:19:47 +00:00
)
.file("src/main.rs", "fn main() {}")
.build();
2019-02-03 04:01:23 +00:00
// Generate a lock file which did not use `bar` to compile, but had to update
// `bar` to generate the lock file
2023-02-15 23:51:46 +00:00
p.cargo("check")
.with_stderr(&format!(
2018-03-14 15:17:44 +00:00
"\
[UPDATING] git repository `{bar}`
2023-02-15 23:51:46 +00:00
[CHECKING] foo v0.5.0 ([CWD])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
2018-03-14 15:17:44 +00:00
",
bar = p2.url()
2018-12-08 11:19:47 +00:00
))
.run();
}
#[cargo_test]
fn git_repo_changing_no_rebuild() {
let bar = git::new("bar", |project| {
2018-03-14 15:17:44 +00:00
project
2018-07-24 22:35:01 +00:00
.file("Cargo.toml", &basic_manifest("bar", "0.5.0"))
2018-03-14 15:17:44 +00:00
.file("src/lib.rs", "pub fn bar() -> i32 { 1 }")
});
// Lock p1 to the first rev in the git repo
let p1 = project()
.at("p1")
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
&format!(
r#"
[package]
2020-09-27 00:59:58 +00:00
name = "p1"
version = "0.5.0"
authors = []
build = 'build.rs'
[dependencies.bar]
git = '{}'
"#,
2018-03-14 15:17:44 +00:00
bar.url()
),
2018-12-08 11:19:47 +00:00
)
.file("src/main.rs", "fn main() {}")
.file("build.rs", "fn main() {}")
.build();
p1.root().move_into_the_past();
2023-02-15 23:51:46 +00:00
p1.cargo("check")
.with_stderr(&format!(
2018-03-14 15:17:44 +00:00
"\
[UPDATING] git repository `{bar}`
[COMPILING] [..]
2023-02-15 23:51:46 +00:00
[CHECKING] [..]
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
2018-03-14 15:17:44 +00:00
",
bar = bar.url()
2018-12-08 11:19:47 +00:00
))
.run();
// Make a commit to lock p2 to a different rev
bar.change_file("src/lib.rs", "pub fn bar() -> i32 { 2 }");
let repo = git2::Repository::open(&bar.root()).unwrap();
git::add(&repo);
git::commit(&repo);
// Lock p2 to the second rev
let p2 = project()
.at("p2")
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
&format!(
r#"
[package]
2020-09-27 00:59:58 +00:00
name = "p2"
version = "0.5.0"
authors = []
[dependencies.bar]
git = '{}'
"#,
2018-03-14 15:17:44 +00:00
bar.url()
),
2018-12-08 11:19:47 +00:00
)
.file("src/main.rs", "fn main() {}")
.build();
2023-02-15 23:51:46 +00:00
p2.cargo("check")
.with_stderr(&format!(
2018-03-14 15:17:44 +00:00
"\
[UPDATING] git repository `{bar}`
2023-02-15 23:51:46 +00:00
[CHECKING] [..]
[CHECKING] [..]
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
2018-03-14 15:17:44 +00:00
",
bar = bar.url()
2018-12-08 11:19:47 +00:00
))
.run();
// And now for the real test! Make sure that p1 doesn't get rebuilt
// even though the git repo has changed.
2023-11-15 22:33:27 +00:00
p1.cargo("check").with_stderr("[FINISHED] [..]").run();
}
2014-09-12 22:21:23 +00:00
#[cargo_test]
fn git_dep_build_cmd() {
let p = git::new("foo", |project| {
2018-03-14 15:17:44 +00:00
project
.file(
"Cargo.toml",
r#"
[package]
2014-09-12 22:21:23 +00:00
2020-09-27 00:59:58 +00:00
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
2014-09-12 22:21:23 +00:00
2020-09-27 00:59:58 +00:00
[dependencies.bar]
2014-09-12 22:21:23 +00:00
2020-09-27 00:59:58 +00:00
version = "0.5.0"
path = "bar"
2014-09-12 22:21:23 +00:00
2020-09-27 00:59:58 +00:00
[[bin]]
2014-09-12 22:21:23 +00:00
2020-09-27 00:59:58 +00:00
name = "foo"
"#,
2018-12-08 11:19:47 +00:00
)
.file("src/foo.rs", &main_file(r#""{}", bar::gimme()"#, &["bar"]))
2018-03-14 15:17:44 +00:00
.file(
"bar/Cargo.toml",
r#"
[package]
2014-09-12 22:21:23 +00:00
2020-09-27 00:59:58 +00:00
name = "bar"
version = "0.5.0"
authors = ["wycats@example.com"]
build = "build.rs"
2014-09-12 22:21:23 +00:00
2020-09-27 00:59:58 +00:00
[lib]
name = "bar"
path = "src/bar.rs"
"#,
2018-12-08 11:19:47 +00:00
)
.file(
2018-03-14 15:17:44 +00:00
"bar/src/bar.rs.in",
r#"
2020-09-27 00:59:58 +00:00
pub fn gimme() -> i32 { 0 }
"#,
2018-12-08 11:19:47 +00:00
)
.file(
2018-03-14 15:17:44 +00:00
"bar/build.rs",
r#"
2020-09-27 00:59:58 +00:00
use std::fs;
fn main() {
fs::copy("src/bar.rs.in", "src/bar.rs").unwrap();
}
"#,
2018-03-14 15:17:44 +00:00
)
});
2014-09-12 22:21:23 +00:00
p.root().join("bar").move_into_the_past();
p.cargo("build").run();
2014-09-12 22:21:23 +00:00
p.process(&p.bin("foo")).with_stdout("0\n").run();
2014-09-12 22:21:23 +00:00
// Touching bar.rs.in should cause the `build` command to run again.
p.change_file("bar/src/bar.rs.in", "pub fn gimme() -> i32 { 1 }");
2014-09-12 22:21:23 +00:00
p.cargo("build").run();
2014-09-12 22:21:23 +00:00
p.process(&p.bin("foo")).with_stdout("1\n").run();
}
2014-09-12 22:21:23 +00:00
#[cargo_test]
fn fetch_downloads() {
let bar = git::new("bar", |project| {
2018-03-14 15:17:44 +00:00
project
2018-07-24 22:35:01 +00:00
.file("Cargo.toml", &basic_manifest("bar", "0.5.0"))
2018-03-14 15:17:44 +00:00
.file("src/lib.rs", "pub fn bar() -> i32 { 1 }")
});
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
&format!(
r#"
[package]
2020-09-27 00:59:58 +00:00
name = "foo"
version = "0.5.0"
authors = []
[dependencies.bar]
git = '{}'
"#,
2018-03-14 15:17:44 +00:00
bar.url()
),
2018-12-08 11:19:47 +00:00
)
.file("src/main.rs", "fn main() {}")
.build();
p.cargo("fetch")
.with_stderr(&format!(
2018-03-14 15:43:41 +00:00
"[UPDATING] git repository `{url}`",
2018-03-14 15:17:44 +00:00
url = bar.url()
2018-12-08 11:19:47 +00:00
))
.run();
2023-11-15 22:33:27 +00:00
p.cargo("fetch").with_stderr("").run();
}
#[cargo_test]
fn fetch_downloads_with_git2_first_then_with_gitoxide_and_vice_versa() {
let bar = git::new("bar", |project| {
project
.file("Cargo.toml", &basic_manifest("bar", "0.5.0"))
.file("src/lib.rs", "pub fn bar() -> i32 { 1 }")
});
let feature_configuration = if cargo_uses_gitoxide() {
// When we are always using `gitoxide` by default, create the registry with git2 as well as the download…
"-Zgitoxide=internal-use-git2"
} else {
// …otherwise create the registry and the git download with `gitoxide`.
"-Zgitoxide=fetch"
};
let p = project()
.file(
"Cargo.toml",
&format!(
r#"
[package]
name = "foo"
version = "0.5.0"
authors = []
[dependencies.bar]
git = '{url}'
"#,
url = bar.url()
),
)
.file("src/main.rs", "fn main() {}")
.build();
p.cargo("fetch")
.arg(feature_configuration)
.masquerade_as_nightly_cargo(&["unstable features must be available for -Z gitoxide"])
.with_stderr(&format!(
"[UPDATING] git repository `{url}`",
url = bar.url()
))
.run();
Package::new("bar", "1.0.0").publish(); // trigger a crates-index change.
2023-11-15 22:33:27 +00:00
p.cargo("fetch").with_stderr("").run();
}
#[cargo_test]
fn warnings_in_git_dep() {
let bar = git::new("bar", |project| {
2018-03-14 15:17:44 +00:00
project
2018-07-24 22:35:01 +00:00
.file("Cargo.toml", &basic_manifest("bar", "0.5.0"))
2018-03-14 15:17:44 +00:00
.file("src/lib.rs", "fn unused() {}")
});
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
&format!(
r#"
[package]
2020-09-27 00:59:58 +00:00
name = "foo"
version = "0.5.0"
authors = []
[dependencies.bar]
git = '{}'
"#,
2018-03-14 15:17:44 +00:00
bar.url()
),
2018-12-08 11:19:47 +00:00
)
.file("src/main.rs", "fn main() {}")
.build();
2023-02-15 23:51:46 +00:00
p.cargo("check")
.with_stderr(&format!(
2018-03-14 15:17:44 +00:00
"[UPDATING] git repository `{}`\n\
2023-02-15 23:51:46 +00:00
[CHECKING] bar v0.5.0 ({}#[..])\n\
[CHECKING] foo v0.5.0 ([CWD])\n\
2018-03-14 15:17:44 +00:00
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]\n",
bar.url(),
bar.url(),
2018-12-08 11:19:47 +00:00
))
.run();
}
#[cargo_test]
fn update_ambiguous() {
let bar1 = git::new("bar1", |project| {
2018-03-14 15:17:44 +00:00
project
2018-07-24 22:35:01 +00:00
.file("Cargo.toml", &basic_manifest("bar", "0.5.0"))
2018-03-14 15:17:44 +00:00
.file("src/lib.rs", "")
});
let bar2 = git::new("bar2", |project| {
2018-03-14 15:17:44 +00:00
project
2018-07-24 22:35:01 +00:00
.file("Cargo.toml", &basic_manifest("bar", "0.6.0"))
2018-03-14 15:17:44 +00:00
.file("src/lib.rs", "")
});
let baz = git::new("baz", |project| {
2018-03-14 15:17:44 +00:00
project
.file(
"Cargo.toml",
&format!(
r#"
2020-09-27 00:59:58 +00:00
[package]
name = "baz"
version = "0.5.0"
authors = ["wycats@example.com"]
[dependencies.bar]
git = '{}'
"#,
bar2.url()
2018-03-14 15:17:44 +00:00
),
2018-12-08 11:19:47 +00:00
)
.file("src/lib.rs", "")
});
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
&format!(
r#"
[package]
2020-09-27 00:59:58 +00:00
name = "foo"
version = "0.5.0"
authors = []
[dependencies.bar]
git = '{}'
[dependencies.baz]
git = '{}'
"#,
bar1.url(),
baz.url()
2018-03-14 15:17:44 +00:00
),
2018-12-08 11:19:47 +00:00
)
.file("src/main.rs", "fn main() {}")
.build();
p.cargo("generate-lockfile").run();
p.cargo("update bar")
.with_status(101)
.with_stderr(
2018-03-14 15:17:44 +00:00
"\
[ERROR] There are multiple `bar` packages in your project, and the specification `bar` \
is ambiguous.
Please re-run this command with one of the \
following specifications:
bar@0.[..].0
bar@0.[..].0
2018-03-14 15:17:44 +00:00
",
2018-12-08 11:19:47 +00:00
)
.run();
}
#[cargo_test]
fn update_one_dep_in_repo_with_many_deps() {
let bar = git::new("bar", |project| {
2018-03-14 15:17:44 +00:00
project
2018-07-24 22:35:01 +00:00
.file("Cargo.toml", &basic_manifest("bar", "0.5.0"))
2018-03-14 15:17:44 +00:00
.file("src/lib.rs", "")
2018-07-24 22:35:01 +00:00
.file("a/Cargo.toml", &basic_manifest("a", "0.5.0"))
2018-03-14 15:17:44 +00:00
.file("a/src/lib.rs", "")
});
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
&format!(
r#"
[package]
2020-09-27 00:59:58 +00:00
name = "foo"
version = "0.5.0"
authors = []
[dependencies.bar]
git = '{}'
[dependencies.a]
git = '{}'
"#,
bar.url(),
bar.url()
2018-03-14 15:17:44 +00:00
),
2018-12-08 11:19:47 +00:00
)
.file("src/main.rs", "fn main() {}")
.build();
p.cargo("generate-lockfile").run();
p.cargo("update bar")
.with_stderr(&format!("[UPDATING] git repository `{}`", bar.url()))
.run();
}
#[cargo_test]
fn switch_deps_does_not_update_transitive() {
let transitive = git::new("transitive", |project| {
2018-03-14 15:17:44 +00:00
project
2018-07-24 22:35:01 +00:00
.file("Cargo.toml", &basic_manifest("transitive", "0.5.0"))
2018-03-14 15:17:44 +00:00
.file("src/lib.rs", "")
});
let dep1 = git::new("dep1", |project| {
2018-03-14 15:17:44 +00:00
project
.file(
"Cargo.toml",
&format!(
r#"
2020-09-27 00:59:58 +00:00
[package]
name = "dep"
version = "0.5.0"
authors = ["wycats@example.com"]
[dependencies.transitive]
git = '{}'
"#,
2018-03-14 15:17:44 +00:00
transitive.url()
),
2018-12-08 11:19:47 +00:00
)
.file("src/lib.rs", "")
});
let dep2 = git::new("dep2", |project| {
2018-03-14 15:17:44 +00:00
project
.file(
"Cargo.toml",
&format!(
r#"
2020-09-27 00:59:58 +00:00
[package]
name = "dep"
version = "0.5.0"
authors = ["wycats@example.com"]
[dependencies.transitive]
git = '{}'
"#,
2018-03-14 15:17:44 +00:00
transitive.url()
),
2018-12-08 11:19:47 +00:00
)
.file("src/lib.rs", "")
});
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
&format!(
r#"
[package]
2020-09-27 00:59:58 +00:00
name = "foo"
version = "0.5.0"
authors = []
[dependencies.dep]
git = '{}'
"#,
2018-03-14 15:17:44 +00:00
dep1.url()
),
2018-12-08 11:19:47 +00:00
)
.file("src/main.rs", "fn main() {}")
.build();
2023-02-15 23:51:46 +00:00
p.cargo("check")
.with_stderr(&format!(
2018-03-14 15:17:44 +00:00
"\
[UPDATING] git repository `{}`
[UPDATING] git repository `{}`
2023-02-15 23:51:46 +00:00
[CHECKING] transitive [..]
[CHECKING] dep [..]
[CHECKING] foo [..]
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
2018-03-14 15:17:44 +00:00
",
dep1.url(),
transitive.url()
2018-12-08 11:19:47 +00:00
))
.run();
// Update the dependency to point to the second repository, but this
// shouldn't update the transitive dependency which is the same.
p.change_file(
"Cargo.toml",
&format!(
r#"
[package]
name = "foo"
version = "0.5.0"
authors = []
[dependencies.dep]
git = '{}'
"#,
dep2.url()
),
);
2023-02-15 23:51:46 +00:00
p.cargo("check")
.with_stderr(&format!(
2018-03-14 15:17:44 +00:00
"\
[UPDATING] git repository `{}`
2023-02-15 23:51:46 +00:00
[CHECKING] dep [..]
[CHECKING] foo [..]
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
2018-03-14 15:17:44 +00:00
",
dep2.url()
2018-12-08 11:19:47 +00:00
))
.run();
}
#[cargo_test]
fn update_one_source_updates_all_packages_in_that_git_source() {
let dep = git::new("dep", |project| {
2018-03-14 15:17:44 +00:00
project
.file(
"Cargo.toml",
r#"
2020-09-27 00:59:58 +00:00
[package]
name = "dep"
version = "0.5.0"
authors = []
[dependencies.a]
path = "a"
"#,
2018-12-08 11:19:47 +00:00
)
.file("src/lib.rs", "")
2018-07-24 22:35:01 +00:00
.file("a/Cargo.toml", &basic_manifest("a", "0.5.0"))
2018-03-14 15:17:44 +00:00
.file("a/src/lib.rs", "")
});
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
&format!(
r#"
[package]
2020-09-27 00:59:58 +00:00
name = "foo"
version = "0.5.0"
authors = []
[dependencies.dep]
git = '{}'
"#,
2018-03-14 15:17:44 +00:00
dep.url()
),
2018-12-08 11:19:47 +00:00
)
.file("src/main.rs", "fn main() {}")
.build();
2023-02-15 23:51:46 +00:00
p.cargo("check").run();
let repo = git2::Repository::open(&dep.root()).unwrap();
let rev1 = repo.revparse_single("HEAD").unwrap().id();
// Just be sure to change a file
dep.change_file("src/lib.rs", "pub fn bar() -> i32 { 2 }");
git::add(&repo);
git::commit(&repo);
p.cargo("update dep").run();
let lockfile = p.read_lockfile();
2018-03-14 15:17:44 +00:00
assert!(
!lockfile.contains(&rev1.to_string()),
"{} in {}",
rev1,
lockfile
);
}
#[cargo_test]
fn switch_sources() {
let a1 = git::new("a1", |project| {
2018-03-14 15:17:44 +00:00
project
2018-07-24 22:35:01 +00:00
.file("Cargo.toml", &basic_manifest("a", "0.5.0"))
2018-03-14 15:17:44 +00:00
.file("src/lib.rs", "")
});
let a2 = git::new("a2", |project| {
2018-03-14 15:17:44 +00:00
project
2018-07-24 22:35:01 +00:00
.file("Cargo.toml", &basic_manifest("a", "0.5.1"))
2018-03-14 15:17:44 +00:00
.file("src/lib.rs", "")
});
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
[package]
2020-09-27 00:59:58 +00:00
name = "foo"
version = "0.5.0"
authors = []
[dependencies.b]
path = "b"
"#,
2018-12-08 11:19:47 +00:00
)
.file("src/main.rs", "fn main() {}")
2018-03-14 15:17:44 +00:00
.file(
"b/Cargo.toml",
&format!(
r#"
[package]
2020-09-27 00:59:58 +00:00
name = "b"
version = "0.5.0"
authors = []
[dependencies.a]
git = '{}'
"#,
2018-03-14 15:17:44 +00:00
a1.url()
),
2018-12-08 11:19:47 +00:00
)
.file("b/src/lib.rs", "pub fn main() {}")
.build();
2023-02-15 23:51:46 +00:00
p.cargo("check")
.with_stderr(
2018-03-14 15:17:44 +00:00
"\
[UPDATING] git repository `file://[..]a1`
2023-02-15 23:51:46 +00:00
[CHECKING] a v0.5.0 ([..]a1#[..]
[CHECKING] b v0.5.0 ([..])
[CHECKING] foo v0.5.0 ([..])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
2018-03-14 15:17:44 +00:00
",
2018-12-08 11:19:47 +00:00
)
.run();
2018-03-14 15:17:44 +00:00
p.change_file(
"b/Cargo.toml",
&format!(
r#"
[package]
name = "b"
version = "0.5.0"
authors = []
[dependencies.a]
git = '{}'
"#,
a2.url()
),
);
2023-02-15 23:51:46 +00:00
p.cargo("check")
.with_stderr(
2018-03-14 15:17:44 +00:00
"\
[UPDATING] git repository `file://[..]a2`
2023-02-15 23:51:46 +00:00
[CHECKING] a v0.5.1 ([..]a2#[..]
[CHECKING] b v0.5.0 ([..])
[CHECKING] foo v0.5.0 ([..])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
2018-03-14 15:17:44 +00:00
",
2018-12-08 11:19:47 +00:00
)
.run();
}
#[cargo_test]
fn dont_require_submodules_are_checked_out() {
let p = project().build();
let git1 = git::new("dep1", |p| {
2018-03-14 15:17:44 +00:00
p.file(
"Cargo.toml",
r#"
[package]
2020-09-27 00:59:58 +00:00
name = "foo"
version = "0.5.0"
authors = []
build = "build.rs"
"#,
2018-12-08 11:19:47 +00:00
)
.file("build.rs", "fn main() {}")
.file("src/lib.rs", "")
.file("a/foo", "")
});
let git2 = git::new("dep2", |p| p);
let repo = git2::Repository::open(&git1.root()).unwrap();
let url = path2url(git2.root()).to_string();
2017-02-18 12:01:10 +00:00
git::add_submodule(&repo, &url, Path::new("a/submodule"));
git::commit(&repo);
git2::Repository::init(&p.root()).unwrap();
let url = path2url(git1.root()).to_string();
let dst = paths::home().join("foo");
2015-02-22 23:06:12 +00:00
git2::Repository::clone(&url, &dst).unwrap();
2023-02-15 23:51:46 +00:00
git1.cargo("check -v").cwd(&dst).run();
}
#[cargo_test]
fn doctest_same_name() {
let a2 = git::new("a2", |p| {
2018-07-24 22:35:01 +00:00
p.file("Cargo.toml", &basic_manifest("a", "0.5.0"))
.file("src/lib.rs", "pub fn a2() {}")
});
let a1 = git::new("a1", |p| {
2018-03-14 15:17:44 +00:00
p.file(
"Cargo.toml",
&format!(
r#"
[package]
2020-09-27 00:59:58 +00:00
name = "a"
version = "0.5.0"
authors = []
[dependencies]
a = {{ git = '{}' }}
"#,
2018-03-14 15:17:44 +00:00
a2.url()
),
2018-12-08 11:19:47 +00:00
)
.file("src/lib.rs", "extern crate a; pub fn a1() {}")
});
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
&format!(
r#"
2020-09-27 00:59:58 +00:00
[package]
name = "foo"
version = "0.0.1"
authors = []
[dependencies]
a = {{ git = '{}' }}
"#,
2018-03-14 15:17:44 +00:00
a1.url()
),
2018-12-08 11:19:47 +00:00
)
.file(
2018-03-14 15:17:44 +00:00
"src/lib.rs",
r#"
2020-09-27 00:59:58 +00:00
#[macro_use]
extern crate a;
"#,
2018-12-08 11:19:47 +00:00
)
.build();
p.cargo("test -v").run();
}
#[cargo_test]
fn lints_are_suppressed() {
let a = git::new("a", |p| {
p.file("Cargo.toml", &basic_manifest("a", "0.5.0")).file(
2018-03-14 15:17:44 +00:00
"src/lib.rs",
"
use std::option;
2018-03-14 15:17:44 +00:00
",
)
});
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
&format!(
r#"
2020-09-27 00:59:58 +00:00
[package]
name = "foo"
version = "0.0.1"
authors = []
[dependencies]
a = {{ git = '{}' }}
"#,
2018-03-14 15:17:44 +00:00
a.url()
),
2018-12-08 11:19:47 +00:00
)
.file("src/lib.rs", "")
.build();
2023-02-15 23:51:46 +00:00
p.cargo("check")
.with_stderr(
2018-03-14 15:17:44 +00:00
"\
[UPDATING] git repository `[..]`
2023-02-15 23:51:46 +00:00
[CHECKING] a v0.5.0 ([..])
[CHECKING] foo v0.0.1 ([..])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
2018-03-14 15:17:44 +00:00
",
2018-12-08 11:19:47 +00:00
)
.run();
}
#[cargo_test]
fn denied_lints_are_allowed() {
let a = git::new("a", |p| {
p.file("Cargo.toml", &basic_manifest("a", "0.5.0")).file(
2018-03-14 15:17:44 +00:00
"src/lib.rs",
"
#![deny(warnings)]
use std::option;
2018-03-14 15:17:44 +00:00
",
)
});
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
&format!(
r#"
2020-09-27 00:59:58 +00:00
[package]
name = "foo"
version = "0.0.1"
authors = []
[dependencies]
a = {{ git = '{}' }}
"#,
2018-03-14 15:17:44 +00:00
a.url()
),
2018-12-08 11:19:47 +00:00
)
.file("src/lib.rs", "")
.build();
2023-02-15 23:51:46 +00:00
p.cargo("check")
.with_stderr(
2018-03-14 15:17:44 +00:00
"\
[UPDATING] git repository `[..]`
2023-02-15 23:51:46 +00:00
[CHECKING] a v0.5.0 ([..])
[CHECKING] foo v0.0.1 ([..])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
2018-03-14 15:17:44 +00:00
",
2018-12-08 11:19:47 +00:00
)
.run();
}
#[cargo_test]
fn add_a_git_dep() {
let git = git::new("git", |p| {
2018-07-24 22:35:01 +00:00
p.file("Cargo.toml", &basic_manifest("git", "0.5.0"))
.file("src/lib.rs", "")
});
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
&format!(
r#"
2020-09-27 00:59:58 +00:00
[package]
name = "foo"
version = "0.0.1"
authors = []
[dependencies]
a = {{ path = 'a' }}
git = {{ git = '{}' }}
"#,
2018-03-14 15:17:44 +00:00
git.url()
),
2018-12-08 11:19:47 +00:00
)
.file("src/lib.rs", "")
2018-07-24 22:35:01 +00:00
.file("a/Cargo.toml", &basic_manifest("a", "0.0.1"))
.file("a/src/lib.rs", "")
.build();
2023-02-15 23:51:46 +00:00
p.cargo("check").run();
assert!(paths::home().join(".cargo/git/CACHEDIR.TAG").is_file());
p.change_file(
"a/Cargo.toml",
&format!(
r#"
[package]
name = "a"
version = "0.0.1"
authors = []
[dependencies]
git = {{ git = '{}' }}
"#,
git.url()
),
);
2023-02-15 23:51:46 +00:00
p.cargo("check").run();
}
#[cargo_test]
fn two_at_rev_instead_of_tag() {
let git = git::new("git", |p| {
2018-07-24 22:35:01 +00:00
p.file("Cargo.toml", &basic_manifest("git1", "0.5.0"))
.file("src/lib.rs", "")
.file("a/Cargo.toml", &basic_manifest("git2", "0.5.0"))
.file("a/src/lib.rs", "")
});
// Make a tag corresponding to the current HEAD
let repo = git2::Repository::open(&git.root()).unwrap();
let head = repo.head().unwrap().target().unwrap();
2018-03-14 15:17:44 +00:00
repo.tag(
"v0.1.0",
&repo.find_object(head, None).unwrap(),
&repo.signature().unwrap(),
"make a new tag",
false,
2018-12-08 11:19:47 +00:00
)
.unwrap();
let p = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
&format!(
r#"
2020-09-27 00:59:58 +00:00
[package]
name = "foo"
version = "0.0.1"
authors = []
[dependencies]
git1 = {{ git = '{0}', rev = 'v0.1.0' }}
git2 = {{ git = '{0}', rev = 'v0.1.0' }}
"#,
2018-03-14 15:17:44 +00:00
git.url()
),
2018-12-08 11:19:47 +00:00
)
.file("src/lib.rs", "")
.build();
p.cargo("generate-lockfile").run();
2023-02-15 23:51:46 +00:00
p.cargo("check -v").run();
}
2017-06-16 15:54:59 +00:00
#[cargo_test]
2017-06-16 15:54:59 +00:00
fn include_overrides_gitignore() {
2019-04-14 18:50:41 +00:00
// Make sure that `package.include` takes precedence over .gitignore.
let p = git::new("foo", |repo| {
2018-03-14 15:17:44 +00:00
repo.file(
"Cargo.toml",
r#"
2020-09-27 00:59:58 +00:00
[package]
name = "foo"
version = "0.5.0"
include = ["src/lib.rs", "ignored.txt", "Cargo.toml"]
"#,
2018-12-08 11:19:47 +00:00
)
.file(
".gitignore",
r#"
2020-09-27 00:59:58 +00:00
/target
Cargo.lock
ignored.txt
"#,
)
2019-04-14 18:50:41 +00:00
.file("src/lib.rs", "")
.file("ignored.txt", "")
.file("build.rs", "fn main() {}")
});
2017-06-16 15:54:59 +00:00
2023-02-15 23:51:46 +00:00
p.cargo("check").run();
2019-04-14 18:50:41 +00:00
p.change_file("ignored.txt", "Trigger rebuild.");
2023-02-15 23:51:46 +00:00
p.cargo("check -v")
.with_stderr(
2018-03-14 15:17:44 +00:00
"\
[DIRTY] foo v0.5.0 ([..]): the precalculated components changed
2019-04-14 18:50:41 +00:00
[COMPILING] foo v0.5.0 ([..])
[RUNNING] `[..]build-script-build[..]`
[RUNNING] `rustc --crate-name foo src/lib.rs [..]`
2017-06-16 15:54:59 +00:00
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
2018-03-14 15:17:44 +00:00
",
2018-12-08 11:19:47 +00:00
)
.run();
2019-04-14 18:50:41 +00:00
p.cargo("package --list --allow-dirty")
.with_stdout(
2018-03-14 15:17:44 +00:00
"\
2019-04-14 18:50:41 +00:00
Cargo.toml
Cargo.toml.orig
2019-04-14 18:50:41 +00:00
ignored.txt
src/lib.rs
2018-03-14 15:17:44 +00:00
",
2018-12-08 11:19:47 +00:00
)
.run();
2017-06-16 15:54:59 +00:00
}
#[cargo_test]
fn invalid_git_dependency_manifest() {
let project = project();
let git_project = git::new("dep1", |project| {
project
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
[package]
2020-09-27 00:59:58 +00:00
name = "dep1"
version = "0.5.0"
authors = ["carlhuda@example.com"]
categories = ["algorithms"]
categories = ["algorithms"]
2020-09-27 00:59:58 +00:00
[lib]
2020-09-27 00:59:58 +00:00
name = "dep1"
"#,
2018-12-08 11:19:47 +00:00
)
.file(
2018-03-14 15:17:44 +00:00
"src/dep1.rs",
r#"
2020-09-27 00:59:58 +00:00
pub fn hello() -> &'static str {
"hello world"
}
"#,
2018-03-14 15:17:44 +00:00
)
});
let project = project
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
&format!(
r#"
[package]
2020-09-27 00:59:58 +00:00
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
2020-09-27 00:59:58 +00:00
[dependencies.dep1]
2020-09-27 00:59:58 +00:00
git = '{}'
"#,
2018-03-14 15:17:44 +00:00
git_project.url()
),
2018-12-08 11:19:47 +00:00
)
.file(
"src/main.rs",
&main_file(r#""{}", dep1::hello()"#, &["dep1"]),
2018-12-08 11:19:47 +00:00
)
.build();
let git_root = git_project.root();
project
2023-02-15 23:51:46 +00:00
.cargo("check")
.with_status(101)
.with_stderr(&format!(
"\
[UPDATING] git repository `{}`
[ERROR] duplicate key `categories` in table `package`
--> [..]/Cargo.toml:8:21
|
8 | categories = [\"algorithms\"]
| ^
|
[ERROR] failed to get `dep1` as a dependency of package `foo v0.5.0 ([..])`
Caused by:
failed to load source for dependency `dep1`
Caused by:
Unable to update {}
",
path2url(&git_root),
path2url(&git_root),
2018-12-08 11:19:47 +00:00
))
.run();
}
#[cargo_test]
fn failed_submodule_checkout() {
let project = project();
let git_project = git::new("dep1", |project| {
2018-07-24 22:35:01 +00:00
project.file("Cargo.toml", &basic_manifest("dep1", "0.5.0"))
});
let git_project2 = git::new("dep2", |project| project.file("lib.rs", ""));
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
let addr = listener.local_addr().unwrap();
let done = Arc::new(AtomicBool::new(false));
let done2 = done.clone();
let t = thread::spawn(move || {
while !done2.load(Ordering::SeqCst) {
if let Ok((mut socket, _)) = listener.accept() {
drop(socket.write_all(b"foo\r\n"));
}
}
});
let repo = git2::Repository::open(&git_project2.root()).unwrap();
2019-01-30 20:34:37 +00:00
let url = format!("https://{}:{}/", addr.ip(), addr.port());
{
let mut s = repo.submodule(&url, Path::new("bar"), false).unwrap();
let subrepo = s.open().unwrap();
let mut cfg = subrepo.config().unwrap();
cfg.set_str("user.email", "foo@bar.com").unwrap();
cfg.set_str("user.name", "Foo Bar").unwrap();
git::commit(&subrepo);
s.add_finalize().unwrap();
}
git::commit(&repo);
drop((repo, url));
let repo = git2::Repository::open(&git_project.root()).unwrap();
let url = path2url(git_project2.root()).to_string();
git::add_submodule(&repo, &url, Path::new("src"));
git::commit(&repo);
drop(repo);
let project = project
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
&format!(
r#"
[package]
2020-09-27 00:59:58 +00:00
name = "foo"
version = "0.5.0"
authors = []
[dependencies]
dep1 = {{ git = '{}' }}
"#,
2018-03-14 15:17:44 +00:00
git_project.url()
),
2018-12-08 11:19:47 +00:00
)
.file("src/lib.rs", "")
.build();
project
2023-02-15 23:51:46 +00:00
.cargo("check")
.with_status(101)
.with_stderr_contains(" failed to update submodule `src`")
.with_stderr_contains(" failed to update submodule `bar`")
.run();
project
2023-02-15 23:51:46 +00:00
.cargo("check")
.with_status(101)
.with_stderr_contains(" failed to update submodule `src`")
.with_stderr_contains(" failed to update submodule `bar`")
.run();
done.store(true, Ordering::SeqCst);
drop(TcpStream::connect(&addr));
t.join().unwrap();
}
#[cargo_test(requires_git)]
fn use_the_cli() {
let project = project();
let git_project = git::new("dep1", |project| {
project
.file("Cargo.toml", &basic_manifest("dep1", "0.5.0"))
.file("src/lib.rs", "")
});
let project = project
.file(
"Cargo.toml",
&format!(
r#"
[package]
name = "foo"
version = "0.5.0"
authors = []
[dependencies]
dep1 = {{ git = '{}' }}
"#,
git_project.url()
),
2018-12-08 11:19:47 +00:00
)
.file("src/lib.rs", "")
.file(
2024-01-26 19:40:46 +00:00
".cargo/config.toml",
"
[net]
git-fetch-with-cli = true
",
2018-12-08 11:19:47 +00:00
)
.build();
let stderr = "\
[UPDATING] git repository `[..]`
[RUNNING] `git fetch [..]`
2023-01-14 15:18:12 +00:00
From [..]
* [new ref] [..] -> origin/HEAD[..]
2023-02-15 23:51:46 +00:00
[CHECKING] dep1 [..]
[RUNNING] `rustc [..]`
2023-02-15 23:51:46 +00:00
[CHECKING] foo [..]
[RUNNING] `rustc [..]`
[FINISHED] [..]
";
2023-02-15 23:51:46 +00:00
project.cargo("check -v").with_stderr(stderr).run();
assert!(paths::home().join(".cargo/git/CACHEDIR.TAG").is_file());
}
#[cargo_test]
fn templatedir_doesnt_cause_problems() {
let git_project2 = git::new("dep2", |project| {
project
.file("Cargo.toml", &basic_manifest("dep2", "0.5.0"))
.file("src/lib.rs", "")
});
let git_project = git::new("dep1", |project| {
project
.file("Cargo.toml", &basic_manifest("dep1", "0.5.0"))
.file("src/lib.rs", "")
});
let p = project()
.file(
"Cargo.toml",
2018-12-08 11:19:47 +00:00
&format!(
r#"
[package]
2020-09-27 00:59:58 +00:00
name = "fo"
version = "0.5.0"
authors = []
2020-09-27 00:59:58 +00:00
[dependencies]
dep1 = {{ git = '{}' }}
"#,
2018-12-08 11:19:47 +00:00
git_project.url()
),
)
.file("src/main.rs", "fn main() {}")
.build();
fs::write(
paths::home().join(".gitconfig"),
format!(
r#"
[init]
templatedir = {}
2018-12-08 11:19:47 +00:00
"#,
git_project2
.url()
.to_file_path()
.unwrap()
.to_str()
.unwrap()
.replace("\\", "/")
),
)
.unwrap();
2023-02-15 23:51:46 +00:00
p.cargo("check").run();
}
#[cargo_test(requires_git)]
fn git_with_cli_force() {
// Supports a force-pushed repo.
let git_project = git::new("dep1", |project| {
project
.file("Cargo.toml", &basic_lib_manifest("dep1"))
.file("src/lib.rs", r#"pub fn f() { println!("one"); }"#)
});
let p = project()
.file(
"Cargo.toml",
&format!(
r#"
[package]
name = "foo"
version = "0.0.1"
edition = "2018"
[dependencies]
dep1 = {{ git = "{}" }}
"#,
git_project.url()
),
)
.file("src/main.rs", "fn main() { dep1::f(); }")
.file(
2024-01-26 19:40:46 +00:00
".cargo/config.toml",
"
[net]
git-fetch-with-cli = true
",
)
.build();
p.cargo("build").run();
p.rename_run("foo", "foo1").with_stdout("one").run();
// commit --amend a change that will require a force fetch.
let repo = git2::Repository::open(&git_project.root()).unwrap();
git_project.change_file("src/lib.rs", r#"pub fn f() { println!("two"); }"#);
git::add(&repo);
let id = repo.refname_to_id("HEAD").unwrap();
let commit = repo.find_commit(id).unwrap();
let tree_id = t!(t!(repo.index()).write_tree());
t!(commit.amend(
Some("HEAD"),
None,
None,
None,
None,
Some(&t!(repo.find_tree(tree_id)))
));
// Perform the fetch.
p.cargo("update").run();
p.cargo("build").run();
p.rename_run("foo", "foo2").with_stdout("two").run();
}
#[cargo_test(requires_git)]
fn git_fetch_cli_env_clean() {
// This tests that git-fetch-with-cli works when GIT_DIR environment
// variable is set (for whatever reason).
let git_dep = git::new("dep1", |project| {
project
.file("Cargo.toml", &basic_manifest("dep1", "0.5.0"))
.file("src/lib.rs", "")
});
let git_proj = git::new("foo", |project| {
project
.file(
"Cargo.toml",
&format!(
r#"
[package]
name = "foo"
version = "0.1.0"
[dependencies]
dep1 = {{ git = '{}' }}
"#,
git_dep.url()
),
)
.file("src/lib.rs", "pub extern crate dep1;")
.file(
2024-01-26 19:40:46 +00:00
".cargo/config.toml",
"
[net]
git-fetch-with-cli = true
",
)
});
// The directory set here isn't too important. Pointing to our own git
// directory causes git to be confused and fail. Can also point to an
// empty directory, or a nonexistent one.
git_proj
.cargo("fetch")
.env("GIT_DIR", git_proj.root().join(".git"))
.run();
}
#[cargo_test]
fn dirty_submodule() {
// `cargo package` warns for dirty file in submodule.
let (git_project, repo) = git::new_repo("foo", |project| {
project
.file("Cargo.toml", &basic_manifest("foo", "0.5.0"))
// This is necessary because `git::add` is too eager.
.file(".gitignore", "/target")
});
let git_project2 = git::new("src", |project| {
project.no_manifest().file("lib.rs", "pub fn f() {}")
});
let url = path2url(git_project2.root()).to_string();
git::add_submodule(&repo, &url, Path::new("src"));
// Submodule added, but not committed.
git_project
.cargo("package --no-verify")
.with_status(101)
.with_stderr(
"\
[WARNING] manifest has no [..]
See [..]
[ERROR] 1 files in the working directory contain changes that were not yet committed into git:
.gitmodules
to proceed despite [..]
",
)
.run();
git::commit(&repo);
git_project.cargo("package --no-verify").run();
// Modify file, check for warning.
git_project.change_file("src/lib.rs", "");
git_project
.cargo("package --no-verify")
.with_status(101)
.with_stderr(
"\
[WARNING] manifest has no [..]
See [..]
[ERROR] 1 files in the working directory contain changes that were not yet committed into git:
src/lib.rs
to proceed despite [..]
",
)
.run();
// Commit the change.
let sub_repo = git2::Repository::open(git_project.root().join("src")).unwrap();
git::add(&sub_repo);
git::commit(&sub_repo);
git::add(&repo);
git::commit(&repo);
git_project.cargo("package --no-verify").run();
// Try with a nested submodule.
let git_project3 = git::new("bar", |project| project.no_manifest().file("mod.rs", ""));
let url = path2url(git_project3.root()).to_string();
git::add_submodule(&sub_repo, &url, Path::new("bar"));
git_project
.cargo("package --no-verify")
.with_status(101)
.with_stderr(
"\
[WARNING] manifest has no [..]
See [..]
[ERROR] 1 files in the working directory contain changes that were not yet committed into git:
src/.gitmodules
to proceed despite [..]
",
)
.run();
// Commit the submodule addition.
git::commit(&sub_repo);
git::add(&repo);
git::commit(&repo);
git_project.cargo("package --no-verify").run();
// Modify within nested submodule.
git_project.change_file("src/bar/new_file.rs", "//test");
git_project
.cargo("package --no-verify")
.with_status(101)
.with_stderr(
"\
[WARNING] manifest has no [..]
See [..]
[ERROR] 1 files in the working directory contain changes that were not yet committed into git:
src/bar/new_file.rs
to proceed despite [..]
",
)
.run();
// And commit the change.
let sub_sub_repo = git2::Repository::open(git_project.root().join("src/bar")).unwrap();
git::add(&sub_sub_repo);
git::commit(&sub_sub_repo);
git::add(&sub_repo);
git::commit(&sub_repo);
git::add(&repo);
git::commit(&repo);
git_project.cargo("package --no-verify").run();
}
2021-02-09 15:29:01 +00:00
#[cargo_test]
fn default_not_master() {
let project = project();
// Create a repository with a `master` branch, but switch the head to a
// branch called `main` at the same time.
let (git_project, repo) = git::new_repo("dep1", |project| {
project
.file("Cargo.toml", &basic_lib_manifest("dep1"))
.file("src/lib.rs", "pub fn foo() {}")
});
let head_id = repo.head().unwrap().target().unwrap();
let head = repo.find_commit(head_id).unwrap();
repo.branch("main", &head, false).unwrap();
repo.set_head("refs/heads/main").unwrap();
// Then create a commit on the new `main` branch so `master` and `main`
// differ.
git_project.change_file("src/lib.rs", "pub fn bar() {}");
git::add(&repo);
git::commit(&repo);
let project = project
.file(
"Cargo.toml",
&format!(
r#"
[package]
2021-02-09 15:29:01 +00:00
name = "foo"
version = "0.5.0"
[dependencies]
dep1 = {{ git = '{}' }}
"#,
git_project.url()
),
)
.file("src/lib.rs", "pub fn foo() { dep1::bar() }")
.build();
project
2023-02-15 23:51:46 +00:00
.cargo("check")
2021-02-09 15:29:01 +00:00
.with_stderr(
"\
[UPDATING] git repository `[..]`
2023-02-15 23:51:46 +00:00
[CHECKING] dep1 v0.5.0 ([..])
[CHECKING] foo v0.5.0 ([..])
2021-02-09 15:29:01 +00:00
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]",
)
.run();
}
#[cargo_test]
fn historical_lockfile_works() {
let project = project();
let (git_project, repo) = git::new_repo("dep1", |project| {
project
.file("Cargo.toml", &basic_lib_manifest("dep1"))
.file("src/lib.rs", "")
});
let head_id = repo.head().unwrap().target().unwrap();
let project = project
.file(
"Cargo.toml",
&format!(
r#"
[package]
name = "foo"
version = "0.5.0"
[dependencies]
dep1 = {{ git = '{}', branch = 'master' }}
"#,
git_project.url()
),
)
.file("src/lib.rs", "")
.build();
2023-02-15 23:51:46 +00:00
project.cargo("check").run();
project.change_file(
"Cargo.lock",
&format!(
r#"# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "dep1"
version = "0.5.0"
source = "git+{}#{}"
[[package]]
name = "foo"
version = "0.5.0"
dependencies = [
"dep1",
]
"#,
git_project.url(),
head_id
),
);
project
2023-02-15 23:51:46 +00:00
.cargo("check")
.with_stderr("[FINISHED] [..]\n")
.run();
}
#[cargo_test]
fn historical_lockfile_works_with_vendor() {
let project = project();
let (git_project, repo) = git::new_repo("dep1", |project| {
project
.file("Cargo.toml", &basic_lib_manifest("dep1"))
.file("src/lib.rs", "")
});
let head_id = repo.head().unwrap().target().unwrap();
let project = project
.file(
"Cargo.toml",
&format!(
r#"
[package]
name = "foo"
version = "0.5.0"
[dependencies]
dep1 = {{ git = '{}', branch = 'master' }}
"#,
git_project.url()
),
)
.file("src/lib.rs", "")
.build();
let output = project.cargo("vendor").exec_with_output().unwrap();
2024-01-26 19:40:46 +00:00
project.change_file(
".cargo/config.toml",
str::from_utf8(&output.stdout).unwrap(),
);
project.change_file(
"Cargo.lock",
&format!(
r#"# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "dep1"
version = "0.5.0"
source = "git+{}#{}"
[[package]]
name = "foo"
version = "0.5.0"
dependencies = [
"dep1",
]
"#,
git_project.url(),
head_id
),
);
2023-02-15 23:51:46 +00:00
project.cargo("check").run();
}
2021-02-09 15:29:01 +00:00
#[cargo_test]
fn two_dep_forms() {
let project = project();
let (git_project, _repo) = git::new_repo("dep1", |project| {
project
.file("Cargo.toml", &basic_lib_manifest("dep1"))
.file("src/lib.rs", "")
});
let project = project
.file(
"Cargo.toml",
&format!(
r#"
[package]
2021-02-09 15:29:01 +00:00
name = "foo"
version = "0.5.0"
[dependencies]
dep1 = {{ git = '{}', branch = 'master' }}
a = {{ path = 'a' }}
"#,
git_project.url()
),
)
.file("src/lib.rs", "")
.file(
"a/Cargo.toml",
&format!(
r#"
[package]
2021-02-09 15:29:01 +00:00
name = "a"
version = "0.5.0"
[dependencies]
dep1 = {{ git = '{}' }}
"#,
git_project.url()
),
)
.file("a/src/lib.rs", "")
.build();
// This'll download the git repository twice, one with HEAD and once with
// the master branch. Then it'll compile 4 crates, the 2 git deps, then
// the two local deps.
project
2023-02-15 23:51:46 +00:00
.cargo("check")
2021-02-09 15:29:01 +00:00
.with_stderr(
"\
[UPDATING] [..]
[UPDATING] [..]
2023-02-15 23:51:46 +00:00
[CHECKING] [..]
[CHECKING] [..]
[CHECKING] [..]
[CHECKING] [..]
2021-02-09 15:29:01 +00:00
[FINISHED] [..]
",
)
.run();
}
#[cargo_test]
fn metadata_master_consistency() {
// SourceId consistency in the `cargo metadata` output when `master` is
// explicit or implicit, using new or old Cargo.lock.
let (git_project, git_repo) = git::new_repo("bar", |project| {
project
.file("Cargo.toml", &basic_manifest("bar", "1.0.0"))
.file("src/lib.rs", "")
});
let bar_hash = git_repo.head().unwrap().target().unwrap().to_string();
// Explicit branch="master" with a lock file created before 1.47 (does not contain ?branch=master).
let p = project()
.file(
"Cargo.toml",
&format!(
r#"
[package]
name = "foo"
version = "0.1.0"
[dependencies]
bar = {{ git = "{}", branch = "master" }}
"#,
git_project.url()
),
)
.file(
"Cargo.lock",
&format!(
r#"
[[package]]
name = "bar"
version = "1.0.0"
source = "git+{}#{}"
[[package]]
name = "foo"
version = "0.1.0"
dependencies = [
"bar",
]
"#,
git_project.url(),
bar_hash,
),
)
.file("src/lib.rs", "")
.build();
let metadata = |bar_source| -> String {
r#"
{
"packages": [
{
"name": "bar",
"version": "1.0.0",
"id": "__BAR_SOURCE__#1.0.0",
"license": null,
"license_file": null,
"description": null,
"source": "__BAR_SOURCE__#__BAR_HASH__",
"dependencies": [],
"targets": "{...}",
"features": {},
"manifest_path": "[..]",
"metadata": null,
"publish": null,
"authors": [],
"categories": [],
"default_run": null,
"keywords": [],
"readme": null,
"repository": null,
"rust_version": null,
"homepage": null,
"documentation": null,
"edition": "2015",
"links": null
},
{
"name": "foo",
"version": "0.1.0",
"id": "[..]foo#0.1.0",
"license": null,
"license_file": null,
"description": null,
"source": null,
"dependencies": [
{
"name": "bar",
"source": "__BAR_SOURCE__",
"req": "*",
"kind": null,
"rename": null,
"optional": false,
"uses_default_features": true,
"features": [],
"target": null,
"registry": null
}
],
"targets": "{...}",
"features": {},
"manifest_path": "[..]",
"metadata": null,
"publish": null,
"authors": [],
"categories": [],
"default_run": null,
"keywords": [],
"readme": null,
"repository": null,
"rust_version": null,
"homepage": null,
"documentation": null,
"edition": "2015",
"links": null
}
],
"workspace_members": [
"[..]foo#0.1.0"
],
"workspace_default_members": [
"[..]foo#0.1.0"
],
"resolve": {
"nodes": [
{
"id": "__BAR_SOURCE__#1.0.0",
"dependencies": [],
"deps": [],
"features": []
},
{
"id": "[..]foo#0.1.0",
"dependencies": [
"__BAR_SOURCE__#1.0.0"
],
"deps": [
{
"name": "bar",
"pkg": "__BAR_SOURCE__#1.0.0",
"dep_kinds": [
{
"kind": null,
"target": null
}
]
}
],
"features": []
}
],
"root": "[..]foo#0.1.0"
},
"target_directory": "[..]",
"version": 1,
"workspace_root": "[..]",
"metadata": null
}
"#
.replace("__BAR_SOURCE__", bar_source)
.replace("__BAR_HASH__", &bar_hash)
};
let bar_source = format!("git+{}?branch=master", git_project.url());
p.cargo("metadata").with_json(&metadata(&bar_source)).run();
// Conversely, remove branch="master" from Cargo.toml, but use a new Cargo.lock that has ?branch=master.
let p = project()
.file(
"Cargo.toml",
&format!(
r#"
[package]
name = "foo"
version = "0.1.0"
[dependencies]
bar = {{ git = "{}" }}
"#,
git_project.url()
),
)
.file(
"Cargo.lock",
&format!(
r#"
[[package]]
name = "bar"
version = "1.0.0"
source = "git+{}?branch=master#{}"
[[package]]
name = "foo"
version = "0.1.0"
dependencies = [
"bar",
]
"#,
git_project.url(),
bar_hash
),
)
.file("src/lib.rs", "")
.build();
// No ?branch=master!
let bar_source = format!("git+{}", git_project.url());
p.cargo("metadata").with_json(&metadata(&bar_source)).run();
}
#[cargo_test]
fn git_with_force_push() {
// Checks that cargo can handle force-pushes to git repos.
// This works by having a git dependency that is updated with an amend
// commit, and tries with various forms (default branch, branch, rev,
// tag).
let main = |text| format!(r#"pub fn f() {{ println!("{}"); }}"#, text);
let (git_project, repo) = git::new_repo("dep1", |project| {
project
.file("Cargo.toml", &basic_lib_manifest("dep1"))
.file("src/lib.rs", &main("one"))
});
let manifest = |extra| {
format!(
r#"
[package]
name = "foo"
version = "0.0.1"
edition = "2018"
[dependencies]
dep1 = {{ git = "{}"{} }}
"#,
git_project.url(),
extra
)
};
let p = project()
.file("Cargo.toml", &manifest(""))
.file("src/main.rs", "fn main() { dep1::f(); }")
.build();
// Download the original and make sure it is OK.
p.cargo("build").run();
p.rename_run("foo", "foo1").with_stdout("one").run();
let find_head = || t!(t!(repo.head()).peel_to_commit());
let amend_commit = |text| {
// commit --amend a change that will require a force fetch.
git_project.change_file("src/lib.rs", &main(text));
git::add(&repo);
let commit = find_head();
let tree_id = t!(t!(repo.index()).write_tree());
t!(commit.amend(
Some("HEAD"),
None,
None,
None,
None,
Some(&t!(repo.find_tree(tree_id)))
));
};
let mut rename_annoyance = 1;
let mut verify = |text: &str| {
// Perform the fetch.
p.cargo("update").run();
p.cargo("build").run();
rename_annoyance += 1;
p.rename_run("foo", &format!("foo{}", rename_annoyance))
.with_stdout(text)
.run();
};
amend_commit("two");
verify("two");
// Try with a rev.
let head1 = find_head().id().to_string();
let extra = format!(", rev = \"{}\"", head1);
p.change_file("Cargo.toml", &manifest(&extra));
verify("two");
amend_commit("three");
let head2 = find_head().id().to_string();
assert_ne!(&head1, &head2);
let extra = format!(", rev = \"{}\"", head2);
p.change_file("Cargo.toml", &manifest(&extra));
verify("three");
// Try with a tag.
git::tag(&repo, "my-tag");
p.change_file("Cargo.toml", &manifest(", tag = \"my-tag\""));
verify("three");
amend_commit("tag-three");
let head = t!(t!(repo.head()).peel(git2::ObjectType::Commit));
t!(repo.tag("my-tag", &head, &t!(repo.signature()), "move tag", true));
verify("tag-three");
// Try with a branch.
let br = t!(repo.branch("awesome-stuff", &find_head(), false));
t!(repo.checkout_tree(&t!(br.get().peel(git2::ObjectType::Tree)), None));
t!(repo.set_head("refs/heads/awesome-stuff"));
git_project.change_file("src/lib.rs", &main("awesome-three"));
git::add(&repo);
git::commit(&repo);
p.change_file("Cargo.toml", &manifest(", branch = \"awesome-stuff\""));
verify("awesome-three");
amend_commit("awesome-four");
verify("awesome-four");
}
2022-07-06 04:02:08 +00:00
#[cargo_test]
fn corrupted_checkout() {
// Test what happens if the checkout is corrupted somehow.
_corrupted_checkout(false);
}
#[cargo_test]
2022-07-06 04:02:08 +00:00
fn corrupted_checkout_with_cli() {
// Test what happens if the checkout is corrupted somehow with git cli.
_corrupted_checkout(true);
}
fn _corrupted_checkout(with_cli: bool) {
let git_project = git::new("dep1", |project| {
project
.file("Cargo.toml", &basic_manifest("dep1", "0.5.0"))
.file("src/lib.rs", "")
});
let p = project()
.file(
"Cargo.toml",
&format!(
r#"
[package]
name = "foo"
version = "0.1.0"
[dependencies]
dep1 = {{ git = "{}" }}
"#,
git_project.url()
),
)
.file("src/lib.rs", "")
.build();
p.cargo("fetch").run();
let mut paths = t!(glob::glob(
paths::home()
.join(".cargo/git/checkouts/dep1-*/*")
.to_str()
.unwrap()
));
let path = paths.next().unwrap().unwrap();
let ok = path.join(".cargo-ok");
// Deleting this file simulates an interrupted checkout.
t!(fs::remove_file(&ok));
// This should refresh the checkout.
let mut e = p.cargo("fetch");
if with_cli {
e.env("CARGO_NET_GIT_FETCH_WITH_CLI", "true");
}
e.run();
assert!(ok.exists());
}
2022-10-28 02:56:41 +00:00
#[cargo_test]
fn cleans_temp_pack_files() {
// Checks that cargo removes temp files left by libgit2 when it is
// interrupted (see clean_repo_temp_files).
Package::new("bar", "1.0.0").publish();
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
[dependencies]
bar = "1.0"
"#,
)
.file("src/lib.rs", "")
.build();
p.cargo("fetch").run();
// Simulate what happens when libgit2 is interrupted while indexing a pack file.
let tmp_path = super::git_gc::find_index().join(".git/objects/pack/pack_git2_91ab40da04fdc2e7");
fs::write(&tmp_path, "test").unwrap();
let mut perms = fs::metadata(&tmp_path).unwrap().permissions();
perms.set_readonly(true);
fs::set_permissions(&tmp_path, perms).unwrap();
// Trigger an index update.
p.cargo("generate-lockfile").run();
assert!(!tmp_path.exists());
}
2023-06-08 15:27:23 +00:00
#[cargo_test]
fn different_user_relative_submodules() {
let user1_git_project = git::new("user1/dep1", |project| {
project
.file("Cargo.toml", &basic_lib_manifest("dep1"))
.file("src/lib.rs", "")
});
let user2_git_project = git::new("user2/dep1", |project| {
project
.file("Cargo.toml", &basic_lib_manifest("dep1"))
.file("src/lib.rs", "")
});
let user2_git_project2 = git::new("user2/dep2", |project| {
2023-06-08 15:27:23 +00:00
project
.file("Cargo.toml", &basic_lib_manifest("dep1"))
.file("src/lib.rs", "")
});
let user2_repo = git2::Repository::open(&user2_git_project.root()).unwrap();
let url = "../dep2";
git::add_submodule(&user2_repo, url, Path::new("dep2"));
git::commit(&user2_repo);
let user1_repo = git2::Repository::open(&user1_git_project.root()).unwrap();
let url = user2_git_project.url();
git::add_submodule(&user1_repo, url.as_str(), Path::new("user2/dep1"));
git::commit(&user1_repo);
let project = project()
.file(
"Cargo.toml",
&format!(
r#"
[package]
name = "foo"
version = "0.5.0"
[dependencies.dep1]
git = '{}'
"#,
user1_git_project.url()
),
)
.file("src/main.rs", &main_file(r#""hello""#, &[]))
.build();
project
.cargo("build")
.with_stderr(&format!(
"\
[UPDATING] git repository `{}`
[UPDATING] git submodule `{}`
[UPDATING] git submodule `{}`
[COMPILING] dep1 v0.5.0 ({}#[..])
[COMPILING] foo v0.5.0 ([CWD])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
",
2023-06-08 15:27:23 +00:00
path2url(&user1_git_project.root()),
path2url(&user2_git_project.root()),
path2url(&user2_git_project2.root()),
2023-06-08 15:27:23 +00:00
path2url(&user1_git_project.root()),
))
.run();
assert!(project.bin("foo").is_file());
}