2019-11-25 02:42:45 +00:00
|
|
|
|
//! Tests for the `cargo new` command.
|
|
|
|
|
|
2021-03-17 18:56:34 +00:00
|
|
|
|
use cargo_test_support::cargo_process;
|
|
|
|
|
use cargo_test_support::paths;
|
2020-04-17 04:10:11 +00:00
|
|
|
|
use std::env;
|
|
|
|
|
use std::fs::{self, File};
|
2014-07-22 05:19:31 +00:00
|
|
|
|
|
2022-01-19 16:45:03 +00:00
|
|
|
|
fn create_default_gitconfig() {
|
2017-10-10 00:52:51 +00:00
|
|
|
|
// This helps on Windows where libgit2 is very aggressive in attempting to
|
|
|
|
|
// find a git config file.
|
|
|
|
|
let gitconfig = paths::home().join(".gitconfig");
|
|
|
|
|
File::create(gitconfig).unwrap();
|
2022-01-19 16:45:03 +00:00
|
|
|
|
|
|
|
|
|
// If we're running this under a user account that has a different default branch set up
|
|
|
|
|
// then tests that assume the default branch is master will fail. We set the default branch
|
|
|
|
|
// to master explicitly so that tests that rely on this behavior still pass.
|
|
|
|
|
fs::write(
|
|
|
|
|
paths::home().join(".gitconfig"),
|
|
|
|
|
r#"
|
|
|
|
|
[init]
|
|
|
|
|
defaultBranch = master
|
|
|
|
|
"#,
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
2017-10-10 00:52:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
|
#[cargo_test]
|
2016-05-25 20:55:42 +00:00
|
|
|
|
fn simple_lib() {
|
2018-09-07 00:01:20 +00:00
|
|
|
|
cargo_process("new --lib foo --vcs none --edition 2015")
|
2024-01-29 21:38:04 +00:00
|
|
|
|
.with_stderr("\
|
|
|
|
|
[CREATING] library `foo` package
|
|
|
|
|
[NOTE] see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
|
|
|
|
")
|
2018-08-28 09:20:03 +00:00
|
|
|
|
.run();
|
2014-07-22 05:19:31 +00:00
|
|
|
|
|
2018-08-29 05:53:01 +00:00
|
|
|
|
assert!(paths::root().join("foo").is_dir());
|
2018-08-29 06:11:10 +00:00
|
|
|
|
assert!(paths::root().join("foo/Cargo.toml").is_file());
|
|
|
|
|
assert!(paths::root().join("foo/src/lib.rs").is_file());
|
|
|
|
|
assert!(!paths::root().join("foo/.gitignore").is_file());
|
2014-07-22 05:19:31 +00:00
|
|
|
|
|
2017-03-04 20:51:09 +00:00
|
|
|
|
let lib = paths::root().join("foo/src/lib.rs");
|
2020-04-17 04:10:11 +00:00
|
|
|
|
let contents = fs::read_to_string(&lib).unwrap();
|
2018-03-14 15:17:44 +00:00
|
|
|
|
assert_eq!(
|
|
|
|
|
contents,
|
2022-05-27 15:41:37 +00:00
|
|
|
|
r#"pub fn add(left: usize, right: usize) -> usize {
|
|
|
|
|
left + right
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2017-03-04 20:51:09 +00:00
|
|
|
|
mod tests {
|
2022-05-27 15:41:37 +00:00
|
|
|
|
use super::*;
|
|
|
|
|
|
2017-03-04 20:51:09 +00:00
|
|
|
|
#[test]
|
2017-08-10 22:30:33 +00:00
|
|
|
|
fn it_works() {
|
2022-05-27 15:41:37 +00:00
|
|
|
|
let result = add(2, 2);
|
2021-08-17 11:44:10 +00:00
|
|
|
|
assert_eq!(result, 4);
|
2017-08-10 22:30:33 +00:00
|
|
|
|
}
|
2017-03-04 20:51:09 +00:00
|
|
|
|
}
|
2018-03-14 15:17:44 +00:00
|
|
|
|
"#
|
|
|
|
|
);
|
2017-03-04 20:51:09 +00:00
|
|
|
|
|
2018-08-28 09:20:03 +00:00
|
|
|
|
cargo_process("build").cwd(&paths::root().join("foo")).run();
|
2016-05-25 20:55:42 +00:00
|
|
|
|
}
|
2014-07-22 05:19:31 +00:00
|
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
|
#[cargo_test]
|
2016-05-25 20:55:42 +00:00
|
|
|
|
fn simple_bin() {
|
2018-09-07 00:01:20 +00:00
|
|
|
|
cargo_process("new --bin foo --edition 2015")
|
2024-01-29 21:38:04 +00:00
|
|
|
|
.with_stderr("\
|
|
|
|
|
[CREATING] binary (application) `foo` package
|
|
|
|
|
[NOTE] see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
|
|
|
|
")
|
2018-08-28 09:20:03 +00:00
|
|
|
|
.run();
|
2014-07-22 05:19:31 +00:00
|
|
|
|
|
2018-08-29 05:53:01 +00:00
|
|
|
|
assert!(paths::root().join("foo").is_dir());
|
2018-08-29 06:11:10 +00:00
|
|
|
|
assert!(paths::root().join("foo/Cargo.toml").is_file());
|
|
|
|
|
assert!(paths::root().join("foo/src/main.rs").is_file());
|
2014-07-22 05:19:31 +00:00
|
|
|
|
|
2018-08-28 09:20:03 +00:00
|
|
|
|
cargo_process("build").cwd(&paths::root().join("foo")).run();
|
2018-12-08 11:19:47 +00:00
|
|
|
|
assert!(paths::root()
|
|
|
|
|
.join(&format!("foo/target/debug/foo{}", env::consts::EXE_SUFFIX))
|
|
|
|
|
.is_file());
|
2016-05-25 20:55:42 +00:00
|
|
|
|
}
|
2014-07-22 05:19:31 +00:00
|
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
|
#[cargo_test]
|
2016-07-29 23:52:00 +00:00
|
|
|
|
fn both_lib_and_bin() {
|
2018-08-28 09:20:03 +00:00
|
|
|
|
cargo_process("new --lib --bin foo")
|
|
|
|
|
.with_status(101)
|
2024-01-29 21:29:10 +00:00
|
|
|
|
.with_stderr(
|
|
|
|
|
"\
|
|
|
|
|
[ERROR] can't specify both lib and binary outputs",
|
|
|
|
|
)
|
2018-08-28 09:20:03 +00:00
|
|
|
|
.run();
|
2016-07-29 23:52:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
|
#[cargo_test]
|
2016-05-25 20:55:42 +00:00
|
|
|
|
fn simple_git() {
|
2021-03-20 09:45:01 +00:00
|
|
|
|
cargo_process("new --lib foo --edition 2015").run();
|
2014-07-22 05:19:31 +00:00
|
|
|
|
|
2018-08-29 05:53:01 +00:00
|
|
|
|
assert!(paths::root().is_dir());
|
2018-08-29 06:11:10 +00:00
|
|
|
|
assert!(paths::root().join("foo/Cargo.toml").is_file());
|
|
|
|
|
assert!(paths::root().join("foo/src/lib.rs").is_file());
|
2018-08-29 05:53:01 +00:00
|
|
|
|
assert!(paths::root().join("foo/.git").is_dir());
|
2018-08-29 06:11:10 +00:00
|
|
|
|
assert!(paths::root().join("foo/.gitignore").is_file());
|
2014-07-22 05:19:31 +00:00
|
|
|
|
|
2019-01-04 22:47:16 +00:00
|
|
|
|
let fp = paths::root().join("foo/.gitignore");
|
2020-04-17 04:10:11 +00:00
|
|
|
|
let contents = fs::read_to_string(&fp).unwrap();
|
2023-06-16 01:45:43 +00:00
|
|
|
|
assert_eq!(contents, "/target\n",);
|
2019-01-04 22:47:16 +00:00
|
|
|
|
|
2018-08-28 09:20:03 +00:00
|
|
|
|
cargo_process("build").cwd(&paths::root().join("foo")).run();
|
2016-05-25 20:55:42 +00:00
|
|
|
|
}
|
2014-07-22 05:19:31 +00:00
|
|
|
|
|
2023-03-15 14:31:08 +00:00
|
|
|
|
#[cargo_test(requires_hg)]
|
|
|
|
|
fn simple_hg() {
|
|
|
|
|
cargo_process("new --lib foo --edition 2015 --vcs hg").run();
|
|
|
|
|
|
|
|
|
|
assert!(paths::root().is_dir());
|
|
|
|
|
assert!(paths::root().join("foo/Cargo.toml").is_file());
|
|
|
|
|
assert!(paths::root().join("foo/src/lib.rs").is_file());
|
|
|
|
|
assert!(paths::root().join("foo/.hg").is_dir());
|
|
|
|
|
assert!(paths::root().join("foo/.hgignore").is_file());
|
|
|
|
|
|
|
|
|
|
let fp = paths::root().join("foo/.hgignore");
|
|
|
|
|
let contents = fs::read_to_string(&fp).unwrap();
|
2023-06-16 01:45:43 +00:00
|
|
|
|
assert_eq!(contents, "^target$\n",);
|
2023-03-15 14:31:08 +00:00
|
|
|
|
|
|
|
|
|
cargo_process("build").cwd(&paths::root().join("foo")).run();
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
|
#[cargo_test]
|
2016-05-25 20:55:42 +00:00
|
|
|
|
fn no_argument() {
|
2018-08-28 09:20:03 +00:00
|
|
|
|
cargo_process("new")
|
|
|
|
|
.with_status(1)
|
|
|
|
|
.with_stderr_contains(
|
2018-03-14 15:17:44 +00:00
|
|
|
|
"\
|
2023-01-13 18:26:29 +00:00
|
|
|
|
error: the following required arguments were not provided:
|
2023-10-17 20:25:52 +00:00
|
|
|
|
<PATH>
|
2018-03-14 15:17:44 +00:00
|
|
|
|
",
|
2018-12-08 11:19:47 +00:00
|
|
|
|
)
|
|
|
|
|
.run();
|
2016-05-25 20:55:42 +00:00
|
|
|
|
}
|
2014-07-22 05:19:31 +00:00
|
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
|
#[cargo_test]
|
2016-05-25 20:55:42 +00:00
|
|
|
|
fn existing() {
|
2014-07-22 05:19:31 +00:00
|
|
|
|
let dst = paths::root().join("foo");
|
2015-02-27 01:04:25 +00:00
|
|
|
|
fs::create_dir(&dst).unwrap();
|
2018-08-28 09:20:03 +00:00
|
|
|
|
cargo_process("new foo")
|
|
|
|
|
.with_status(101)
|
2018-08-29 17:56:48 +00:00
|
|
|
|
.with_stderr(
|
2024-01-29 21:29:10 +00:00
|
|
|
|
"\
|
|
|
|
|
[CREATING] binary (application) `foo` package
|
|
|
|
|
[ERROR] destination `[CWD]/foo` already exists\n\n\
|
|
|
|
|
Use `cargo init` to initialize the directory",
|
2018-12-08 11:19:47 +00:00
|
|
|
|
)
|
|
|
|
|
.run();
|
2016-05-25 20:55:42 +00:00
|
|
|
|
}
|
2014-07-22 05:19:31 +00:00
|
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
|
#[cargo_test]
|
2016-05-25 20:55:42 +00:00
|
|
|
|
fn invalid_characters() {
|
2018-08-28 09:20:03 +00:00
|
|
|
|
cargo_process("new foo.rs")
|
|
|
|
|
.with_status(101)
|
2020-09-04 00:39:13 +00:00
|
|
|
|
.with_stderr(
|
|
|
|
|
"\
|
2024-01-29 21:29:10 +00:00
|
|
|
|
[CREATING] binary (application) `foo.rs` package
|
2021-01-24 19:05:11 +00:00
|
|
|
|
[ERROR] invalid character `.` in package name: `foo.rs`, [..]
|
|
|
|
|
If you need a package name to not match the directory name, consider using --name flag.
|
|
|
|
|
If you need a binary with the name \"foo.rs\", use a valid package name, \
|
|
|
|
|
and set the binary name to be different from the package. \
|
|
|
|
|
This can be done by setting the binary filename to `src/bin/foo.rs.rs` \
|
|
|
|
|
or change the name in Cargo.toml with:
|
|
|
|
|
|
2021-09-06 07:45:12 +00:00
|
|
|
|
[[bin]]
|
2021-01-24 19:05:11 +00:00
|
|
|
|
name = \"foo.rs\"
|
|
|
|
|
path = \"src/main.rs\"
|
|
|
|
|
|
2020-09-10 12:52:43 +00:00
|
|
|
|
",
|
2020-09-04 00:39:13 +00:00
|
|
|
|
)
|
2018-12-08 11:19:47 +00:00
|
|
|
|
.run();
|
2016-05-25 20:55:42 +00:00
|
|
|
|
}
|
2014-10-13 00:05:41 +00:00
|
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
|
#[cargo_test]
|
2016-05-25 20:55:42 +00:00
|
|
|
|
fn reserved_name() {
|
2018-08-28 09:20:03 +00:00
|
|
|
|
cargo_process("new test")
|
|
|
|
|
.with_status(101)
|
2020-09-04 00:39:13 +00:00
|
|
|
|
.with_stderr(
|
|
|
|
|
"\
|
2024-01-29 21:29:10 +00:00
|
|
|
|
[CREATING] binary (application) `test` package
|
2021-01-24 19:05:11 +00:00
|
|
|
|
[ERROR] the name `test` cannot be used as a package name, it conflicts [..]
|
|
|
|
|
If you need a package name to not match the directory name, consider using --name flag.
|
|
|
|
|
If you need a binary with the name \"test\", use a valid package name, \
|
|
|
|
|
and set the binary name to be different from the package. \
|
|
|
|
|
This can be done by setting the binary filename to `src/bin/test.rs` \
|
|
|
|
|
or change the name in Cargo.toml with:
|
|
|
|
|
|
2021-09-06 07:45:12 +00:00
|
|
|
|
[[bin]]
|
2021-01-24 19:05:11 +00:00
|
|
|
|
name = \"test\"
|
|
|
|
|
path = \"src/main.rs\"
|
|
|
|
|
|
2020-09-10 12:52:43 +00:00
|
|
|
|
",
|
2020-09-04 00:39:13 +00:00
|
|
|
|
)
|
2018-12-08 11:19:47 +00:00
|
|
|
|
.run();
|
2016-05-25 20:55:42 +00:00
|
|
|
|
}
|
2016-03-04 19:32:22 +00:00
|
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
|
#[cargo_test]
|
2017-05-04 12:23:38 +00:00
|
|
|
|
fn reserved_binary_name() {
|
2018-08-28 09:20:03 +00:00
|
|
|
|
cargo_process("new --bin incremental")
|
|
|
|
|
.with_status(101)
|
|
|
|
|
.with_stderr(
|
2020-09-04 00:39:13 +00:00
|
|
|
|
"\
|
2024-01-29 21:29:10 +00:00
|
|
|
|
[CREATING] binary (application) `incremental` package
|
2021-01-24 19:05:11 +00:00
|
|
|
|
[ERROR] the name `incremental` cannot be used as a package name, it conflicts [..]
|
|
|
|
|
If you need a package name to not match the directory name, consider using --name flag.
|
2020-09-10 12:52:43 +00:00
|
|
|
|
",
|
2020-03-02 22:26:21 +00:00
|
|
|
|
)
|
|
|
|
|
.run();
|
|
|
|
|
|
|
|
|
|
cargo_process("new --lib incremental")
|
|
|
|
|
.with_stderr(
|
|
|
|
|
"\
|
2024-01-29 21:29:10 +00:00
|
|
|
|
[CREATING] library `incremental` package
|
2020-03-02 22:26:21 +00:00
|
|
|
|
[WARNING] the name `incremental` will not support binary executables with that name, \
|
|
|
|
|
it conflicts with cargo's build directory names
|
2024-01-29 21:38:04 +00:00
|
|
|
|
[NOTE] see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
2020-03-02 22:26:21 +00:00
|
|
|
|
",
|
2018-12-08 11:19:47 +00:00
|
|
|
|
)
|
|
|
|
|
.run();
|
2017-05-04 12:23:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
|
#[cargo_test]
|
2016-05-25 20:55:42 +00:00
|
|
|
|
fn keyword_name() {
|
2018-08-28 09:20:03 +00:00
|
|
|
|
cargo_process("new pub")
|
|
|
|
|
.with_status(101)
|
2020-09-04 00:39:13 +00:00
|
|
|
|
.with_stderr(
|
|
|
|
|
"\
|
2024-01-29 21:29:10 +00:00
|
|
|
|
[CREATING] binary (application) `pub` package
|
2021-01-24 19:05:11 +00:00
|
|
|
|
[ERROR] the name `pub` cannot be used as a package name, it is a Rust keyword
|
|
|
|
|
If you need a package name to not match the directory name, consider using --name flag.
|
|
|
|
|
If you need a binary with the name \"pub\", use a valid package name, \
|
|
|
|
|
and set the binary name to be different from the package. \
|
|
|
|
|
This can be done by setting the binary filename to `src/bin/pub.rs` \
|
|
|
|
|
or change the name in Cargo.toml with:
|
|
|
|
|
|
2021-09-06 07:45:12 +00:00
|
|
|
|
[[bin]]
|
2021-01-24 19:05:11 +00:00
|
|
|
|
name = \"pub\"
|
|
|
|
|
path = \"src/main.rs\"
|
|
|
|
|
|
2020-09-10 12:52:43 +00:00
|
|
|
|
",
|
2020-09-04 00:39:13 +00:00
|
|
|
|
)
|
2020-03-02 22:26:21 +00:00
|
|
|
|
.run();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cargo_test]
|
|
|
|
|
fn std_name() {
|
|
|
|
|
cargo_process("new core")
|
2018-08-28 09:20:03 +00:00
|
|
|
|
.with_stderr(
|
2020-03-02 22:26:21 +00:00
|
|
|
|
"\
|
2024-01-29 21:29:10 +00:00
|
|
|
|
[CREATING] binary (application) `core` package
|
2020-03-02 22:26:21 +00:00
|
|
|
|
[WARNING] the name `core` is part of Rust's standard library
|
|
|
|
|
It is recommended to use a different name to avoid problems.
|
2021-01-24 19:05:11 +00:00
|
|
|
|
If you need a package name to not match the directory name, consider using --name flag.
|
|
|
|
|
If you need a binary with the name \"core\", use a valid package name, \
|
|
|
|
|
and set the binary name to be different from the package. \
|
|
|
|
|
This can be done by setting the binary filename to `src/bin/core.rs` \
|
|
|
|
|
or change the name in Cargo.toml with:
|
|
|
|
|
|
2021-09-06 07:45:12 +00:00
|
|
|
|
[[bin]]
|
2021-01-24 19:05:11 +00:00
|
|
|
|
name = \"core\"
|
|
|
|
|
path = \"src/main.rs\"
|
|
|
|
|
|
2024-01-29 21:38:04 +00:00
|
|
|
|
[NOTE] see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
2020-03-02 22:26:21 +00:00
|
|
|
|
",
|
2018-12-08 11:19:47 +00:00
|
|
|
|
)
|
|
|
|
|
.run();
|
2016-05-25 20:55:42 +00:00
|
|
|
|
}
|
2016-05-17 05:37:05 +00:00
|
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
|
#[cargo_test]
|
2016-05-25 20:55:42 +00:00
|
|
|
|
fn git_prefers_command_line() {
|
2014-08-28 20:22:36 +00:00
|
|
|
|
let root = paths::root();
|
2015-02-27 01:04:25 +00:00
|
|
|
|
fs::create_dir(&root.join(".cargo")).unwrap();
|
2020-04-17 04:10:11 +00:00
|
|
|
|
fs::write(
|
2024-01-26 19:40:46 +00:00
|
|
|
|
&root.join(".cargo/config.toml"),
|
2020-04-17 04:10:11 +00:00
|
|
|
|
r#"
|
|
|
|
|
[cargo-new]
|
|
|
|
|
vcs = "none"
|
|
|
|
|
name = "foo"
|
|
|
|
|
email = "bar"
|
|
|
|
|
"#,
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
2018-03-14 15:17:44 +00:00
|
|
|
|
|
2021-03-20 09:45:01 +00:00
|
|
|
|
cargo_process("new foo --vcs git").run();
|
2017-10-16 23:41:11 +00:00
|
|
|
|
assert!(paths::root().join("foo/.gitignore").exists());
|
2021-03-20 10:44:25 +00:00
|
|
|
|
assert!(!fs::read_to_string(paths::root().join("foo/Cargo.toml"))
|
|
|
|
|
.unwrap()
|
|
|
|
|
.contains("authors ="));
|
2016-05-25 20:55:42 +00:00
|
|
|
|
}
|
2014-10-23 12:30:36 +00:00
|
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
|
#[cargo_test]
|
2016-05-25 20:55:42 +00:00
|
|
|
|
fn subpackage_no_git() {
|
2021-03-20 09:45:01 +00:00
|
|
|
|
cargo_process("new foo").run();
|
2014-10-23 12:30:36 +00:00
|
|
|
|
|
2018-08-29 05:53:01 +00:00
|
|
|
|
assert!(paths::root().join("foo/.git").is_dir());
|
2018-08-29 06:11:10 +00:00
|
|
|
|
assert!(paths::root().join("foo/.gitignore").is_file());
|
2018-07-26 18:39:10 +00:00
|
|
|
|
|
2014-10-23 12:30:36 +00:00
|
|
|
|
let subpackage = paths::root().join("foo").join("components");
|
2015-02-27 01:04:25 +00:00
|
|
|
|
fs::create_dir(&subpackage).unwrap();
|
2021-03-20 09:45:01 +00:00
|
|
|
|
cargo_process("new foo/components/subcomponent").run();
|
2018-03-14 15:17:44 +00:00
|
|
|
|
|
2018-12-08 11:19:47 +00:00
|
|
|
|
assert!(!paths::root()
|
|
|
|
|
.join("foo/components/subcomponent/.git")
|
|
|
|
|
.is_file());
|
|
|
|
|
assert!(!paths::root()
|
|
|
|
|
.join("foo/components/subcomponent/.gitignore")
|
|
|
|
|
.is_file());
|
2016-05-25 20:55:42 +00:00
|
|
|
|
}
|
2015-02-16 04:45:59 +00:00
|
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
|
#[cargo_test]
|
2018-07-17 06:27:43 +00:00
|
|
|
|
fn subpackage_git_with_gitignore() {
|
2021-03-20 09:45:01 +00:00
|
|
|
|
cargo_process("new foo").run();
|
2018-07-17 06:27:43 +00:00
|
|
|
|
|
2018-08-29 05:53:01 +00:00
|
|
|
|
assert!(paths::root().join("foo/.git").is_dir());
|
2018-08-29 06:11:10 +00:00
|
|
|
|
assert!(paths::root().join("foo/.gitignore").is_file());
|
2018-07-26 18:39:10 +00:00
|
|
|
|
|
|
|
|
|
let gitignore = paths::root().join("foo/.gitignore");
|
2018-07-17 06:27:43 +00:00
|
|
|
|
fs::write(gitignore, b"components").unwrap();
|
|
|
|
|
|
2018-07-26 18:39:10 +00:00
|
|
|
|
let subpackage = paths::root().join("foo/components");
|
2018-07-17 06:27:43 +00:00
|
|
|
|
fs::create_dir(&subpackage).unwrap();
|
2021-03-20 09:45:01 +00:00
|
|
|
|
cargo_process("new foo/components/subcomponent").run();
|
2018-07-17 06:27:43 +00:00
|
|
|
|
|
2018-12-08 11:19:47 +00:00
|
|
|
|
assert!(paths::root()
|
|
|
|
|
.join("foo/components/subcomponent/.git")
|
|
|
|
|
.is_dir());
|
|
|
|
|
assert!(paths::root()
|
|
|
|
|
.join("foo/components/subcomponent/.gitignore")
|
|
|
|
|
.is_file());
|
2018-07-17 06:27:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
|
#[cargo_test]
|
2016-05-25 20:55:42 +00:00
|
|
|
|
fn subpackage_git_with_vcs_arg() {
|
2021-03-20 09:45:01 +00:00
|
|
|
|
cargo_process("new foo").run();
|
2015-09-06 19:52:13 +00:00
|
|
|
|
|
|
|
|
|
let subpackage = paths::root().join("foo").join("components");
|
|
|
|
|
fs::create_dir(&subpackage).unwrap();
|
2021-03-20 09:45:01 +00:00
|
|
|
|
cargo_process("new foo/components/subcomponent --vcs git").run();
|
2018-03-14 15:17:44 +00:00
|
|
|
|
|
2018-12-08 11:19:47 +00:00
|
|
|
|
assert!(paths::root()
|
|
|
|
|
.join("foo/components/subcomponent/.git")
|
|
|
|
|
.is_dir());
|
|
|
|
|
assert!(paths::root()
|
|
|
|
|
.join("foo/components/subcomponent/.gitignore")
|
|
|
|
|
.is_file());
|
2016-05-25 20:55:42 +00:00
|
|
|
|
}
|
2015-09-06 19:52:13 +00:00
|
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
|
#[cargo_test]
|
2016-05-25 20:55:42 +00:00
|
|
|
|
fn unknown_flags() {
|
2018-08-28 09:20:03 +00:00
|
|
|
|
cargo_process("new foo --flag")
|
|
|
|
|
.with_status(1)
|
2023-01-13 18:26:29 +00:00
|
|
|
|
.with_stderr_contains("error: unexpected argument '--flag' found")
|
2018-12-08 11:19:47 +00:00
|
|
|
|
.run();
|
2016-05-25 20:55:42 +00:00
|
|
|
|
}
|
2018-01-09 13:50:45 +00:00
|
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
|
#[cargo_test]
|
2018-01-09 13:50:45 +00:00
|
|
|
|
fn explicit_invalid_name_not_suggested() {
|
2018-08-28 09:20:03 +00:00
|
|
|
|
cargo_process("new --name 10-invalid a")
|
|
|
|
|
.with_status(101)
|
2020-03-02 22:26:21 +00:00
|
|
|
|
.with_stderr(
|
2021-01-24 19:05:11 +00:00
|
|
|
|
"\
|
2024-01-29 21:29:10 +00:00
|
|
|
|
[CREATING] binary (application) `10-invalid` package
|
2023-12-19 08:40:38 +00:00
|
|
|
|
[ERROR] invalid character `1` in package name: `10-invalid`, the name cannot start with a digit
|
2021-01-24 19:05:11 +00:00
|
|
|
|
If you need a binary with the name \"10-invalid\", use a valid package name, \
|
|
|
|
|
and set the binary name to be different from the package. \
|
|
|
|
|
This can be done by setting the binary filename to `src/bin/10-invalid.rs` \
|
|
|
|
|
or change the name in Cargo.toml with:
|
|
|
|
|
|
2021-09-06 07:45:12 +00:00
|
|
|
|
[[bin]]
|
2021-01-24 19:05:11 +00:00
|
|
|
|
name = \"10-invalid\"
|
|
|
|
|
path = \"src/main.rs\"
|
|
|
|
|
|
|
|
|
|
",
|
2020-03-02 22:26:21 +00:00
|
|
|
|
)
|
2018-08-28 09:20:03 +00:00
|
|
|
|
.run();
|
2018-02-05 23:49:49 +00:00
|
|
|
|
}
|
2018-05-01 01:39:22 +00:00
|
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
|
#[cargo_test]
|
2018-05-01 01:39:22 +00:00
|
|
|
|
fn explicit_project_name() {
|
2018-08-28 09:20:03 +00:00
|
|
|
|
cargo_process("new --lib foo --name bar")
|
2024-01-29 21:38:04 +00:00
|
|
|
|
.with_stderr("\
|
|
|
|
|
[CREATING] library `bar` package
|
|
|
|
|
[NOTE] see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
|
|
|
|
")
|
2018-08-28 09:20:03 +00:00
|
|
|
|
.run();
|
2018-05-01 01:39:22 +00:00
|
|
|
|
}
|
2018-09-05 22:29:22 +00:00
|
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
|
#[cargo_test]
|
2018-09-05 22:29:22 +00:00
|
|
|
|
fn new_with_edition_2015() {
|
2021-03-20 09:45:01 +00:00
|
|
|
|
cargo_process("new --edition 2015 foo").run();
|
2018-09-05 22:29:22 +00:00
|
|
|
|
let manifest = fs::read_to_string(paths::root().join("foo/Cargo.toml")).unwrap();
|
|
|
|
|
assert!(manifest.contains("edition = \"2015\""));
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
|
#[cargo_test]
|
2018-09-05 22:29:22 +00:00
|
|
|
|
fn new_with_edition_2018() {
|
2021-03-20 09:45:01 +00:00
|
|
|
|
cargo_process("new --edition 2018 foo").run();
|
2018-09-05 22:29:22 +00:00
|
|
|
|
let manifest = fs::read_to_string(paths::root().join("foo/Cargo.toml")).unwrap();
|
|
|
|
|
assert!(manifest.contains("edition = \"2018\""));
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
|
#[cargo_test]
|
2018-09-07 00:01:20 +00:00
|
|
|
|
fn new_default_edition() {
|
2021-03-20 09:45:01 +00:00
|
|
|
|
cargo_process("new foo").run();
|
2018-09-07 00:01:20 +00:00
|
|
|
|
let manifest = fs::read_to_string(paths::root().join("foo/Cargo.toml")).unwrap();
|
2021-08-17 15:43:11 +00:00
|
|
|
|
assert!(manifest.contains("edition = \"2021\""));
|
2018-09-07 00:01:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
|
#[cargo_test]
|
2018-09-05 22:29:22 +00:00
|
|
|
|
fn new_with_bad_edition() {
|
|
|
|
|
cargo_process("new --edition something_else foo")
|
2023-01-13 18:26:29 +00:00
|
|
|
|
.with_stderr_contains("error: invalid value 'something_else' for '--edition <YEAR>'")
|
2018-09-05 22:29:22 +00:00
|
|
|
|
.with_status(1)
|
|
|
|
|
.run();
|
|
|
|
|
}
|
2019-03-31 21:05:49 +00:00
|
|
|
|
|
2019-11-18 15:27:18 +00:00
|
|
|
|
#[cargo_test]
|
|
|
|
|
fn lockfile_constant_during_new() {
|
2021-03-20 09:45:01 +00:00
|
|
|
|
cargo_process("new foo").run();
|
2019-11-18 15:27:18 +00:00
|
|
|
|
|
|
|
|
|
cargo_process("build").cwd(&paths::root().join("foo")).run();
|
|
|
|
|
let before = fs::read_to_string(paths::root().join("foo/Cargo.lock")).unwrap();
|
|
|
|
|
cargo_process("build").cwd(&paths::root().join("foo")).run();
|
|
|
|
|
let after = fs::read_to_string(paths::root().join("foo/Cargo.lock")).unwrap();
|
|
|
|
|
assert_eq!(before, after);
|
|
|
|
|
}
|
2020-03-02 22:26:21 +00:00
|
|
|
|
|
|
|
|
|
#[cargo_test]
|
|
|
|
|
fn restricted_windows_name() {
|
|
|
|
|
if cfg!(windows) {
|
|
|
|
|
cargo_process("new nul")
|
|
|
|
|
.with_status(101)
|
2020-09-04 01:18:47 +00:00
|
|
|
|
.with_stderr(
|
|
|
|
|
"\
|
2024-01-29 21:29:10 +00:00
|
|
|
|
[CREATING] binary (application) `nul` package
|
2020-09-04 01:18:47 +00:00
|
|
|
|
[ERROR] cannot use name `nul`, it is a reserved Windows filename
|
2021-01-24 19:05:11 +00:00
|
|
|
|
If you need a package name to not match the directory name, consider using --name flag.
|
2020-09-10 12:52:43 +00:00
|
|
|
|
",
|
2020-09-04 01:18:47 +00:00
|
|
|
|
)
|
2020-03-02 22:26:21 +00:00
|
|
|
|
.run();
|
|
|
|
|
} else {
|
|
|
|
|
cargo_process("new nul")
|
|
|
|
|
.with_stderr(
|
|
|
|
|
"\
|
2024-01-29 21:29:10 +00:00
|
|
|
|
[CREATING] binary (application) `nul` package
|
2020-03-02 22:26:21 +00:00
|
|
|
|
[WARNING] the name `nul` is a reserved Windows filename
|
|
|
|
|
This package will not work on Windows platforms.
|
2024-01-29 21:38:04 +00:00
|
|
|
|
[NOTE] see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
2020-03-02 22:26:21 +00:00
|
|
|
|
",
|
|
|
|
|
)
|
|
|
|
|
.run();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cargo_test]
|
|
|
|
|
fn non_ascii_name() {
|
|
|
|
|
cargo_process("new Привет")
|
|
|
|
|
.with_stderr(
|
|
|
|
|
"\
|
2024-01-29 21:29:10 +00:00
|
|
|
|
[CREATING] binary (application) `Привет` package
|
2020-03-02 22:26:21 +00:00
|
|
|
|
[WARNING] the name `Привет` contains non-ASCII characters
|
2022-08-23 19:11:50 +00:00
|
|
|
|
Non-ASCII crate names are not supported by Rust.
|
2023-09-30 08:53:03 +00:00
|
|
|
|
[WARNING] the name `Привет` is not snake_case or kebab-case which is recommended for package names, consider `привет`
|
2024-01-29 21:38:04 +00:00
|
|
|
|
[NOTE] see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
2020-03-02 22:26:21 +00:00
|
|
|
|
",
|
|
|
|
|
)
|
|
|
|
|
.run();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cargo_test]
|
|
|
|
|
fn non_ascii_name_invalid() {
|
|
|
|
|
// These are alphanumeric characters, but not Unicode XID.
|
|
|
|
|
cargo_process("new ⒶⒷⒸ")
|
|
|
|
|
.with_status(101)
|
|
|
|
|
.with_stderr(
|
2020-09-04 00:39:13 +00:00
|
|
|
|
"\
|
2024-01-29 21:29:10 +00:00
|
|
|
|
[CREATING] binary (application) `ⒶⒷⒸ` package
|
2021-01-24 19:05:11 +00:00
|
|
|
|
[ERROR] invalid character `Ⓐ` in package name: `ⒶⒷⒸ`, \
|
2020-09-04 00:39:13 +00:00
|
|
|
|
the first character must be a Unicode XID start character (most letters or `_`)
|
2021-01-24 19:05:11 +00:00
|
|
|
|
If you need a package name to not match the directory name, consider using --name flag.
|
|
|
|
|
If you need a binary with the name \"ⒶⒷⒸ\", use a valid package name, \
|
|
|
|
|
and set the binary name to be different from the package. \
|
|
|
|
|
This can be done by setting the binary filename to `src/bin/ⒶⒷⒸ.rs` \
|
|
|
|
|
or change the name in Cargo.toml with:
|
|
|
|
|
|
2021-09-06 07:45:12 +00:00
|
|
|
|
[[bin]]
|
2021-01-24 19:05:11 +00:00
|
|
|
|
name = \"ⒶⒷⒸ\"
|
|
|
|
|
path = \"src/main.rs\"
|
|
|
|
|
|
2020-09-10 12:52:43 +00:00
|
|
|
|
",
|
2020-03-02 22:26:21 +00:00
|
|
|
|
)
|
|
|
|
|
.run();
|
|
|
|
|
|
|
|
|
|
cargo_process("new a¼")
|
|
|
|
|
.with_status(101)
|
|
|
|
|
.with_stderr(
|
2020-09-04 00:39:13 +00:00
|
|
|
|
"\
|
2024-01-29 21:29:10 +00:00
|
|
|
|
[CREATING] binary (application) `a¼` package
|
2021-01-24 19:05:11 +00:00
|
|
|
|
[ERROR] invalid character `¼` in package name: `a¼`, \
|
2020-09-04 00:39:13 +00:00
|
|
|
|
characters must be Unicode XID characters (numbers, `-`, `_`, or most letters)
|
2021-01-24 19:05:11 +00:00
|
|
|
|
If you need a package name to not match the directory name, consider using --name flag.
|
|
|
|
|
If you need a binary with the name \"a¼\", use a valid package name, \
|
|
|
|
|
and set the binary name to be different from the package. \
|
|
|
|
|
This can be done by setting the binary filename to `src/bin/a¼.rs` \
|
|
|
|
|
or change the name in Cargo.toml with:
|
|
|
|
|
|
2021-09-06 07:45:12 +00:00
|
|
|
|
[[bin]]
|
2021-01-24 19:05:11 +00:00
|
|
|
|
name = \"a¼\"
|
|
|
|
|
path = \"src/main.rs\"
|
|
|
|
|
|
2020-09-10 12:52:43 +00:00
|
|
|
|
",
|
2020-03-02 22:26:21 +00:00
|
|
|
|
)
|
|
|
|
|
.run();
|
|
|
|
|
}
|
2020-10-14 01:13:25 +00:00
|
|
|
|
|
2023-09-30 08:53:03 +00:00
|
|
|
|
#[cargo_test]
|
|
|
|
|
fn non_snake_case_name() {
|
|
|
|
|
cargo_process("new UPPERcase_name")
|
|
|
|
|
.with_stderr(
|
|
|
|
|
"\
|
2024-01-29 21:29:10 +00:00
|
|
|
|
[CREATING] binary (application) `UPPERcase_name` package
|
2023-09-30 08:53:03 +00:00
|
|
|
|
[WARNING] the name `UPPERcase_name` is not snake_case or kebab-case which is recommended for package names, consider `uppercase_name`
|
2024-01-29 21:38:04 +00:00
|
|
|
|
[NOTE] see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
2023-09-30 08:53:03 +00:00
|
|
|
|
",
|
|
|
|
|
)
|
|
|
|
|
.run();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cargo_test]
|
|
|
|
|
fn kebab_case_name_is_accepted() {
|
|
|
|
|
cargo_process("new kebab-case-is-valid")
|
|
|
|
|
.with_stderr(
|
|
|
|
|
"\
|
2024-01-29 21:29:10 +00:00
|
|
|
|
[CREATING] binary (application) `kebab-case-is-valid` package
|
2024-01-29 21:38:04 +00:00
|
|
|
|
[NOTE] see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
2023-09-30 08:53:03 +00:00
|
|
|
|
",
|
|
|
|
|
)
|
|
|
|
|
.run();
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-14 01:13:25 +00:00
|
|
|
|
#[cargo_test]
|
|
|
|
|
fn git_default_branch() {
|
|
|
|
|
// Check for init.defaultBranch support.
|
2022-01-19 16:45:03 +00:00
|
|
|
|
create_default_gitconfig();
|
2022-01-18 22:51:21 +00:00
|
|
|
|
|
2021-03-20 09:45:01 +00:00
|
|
|
|
cargo_process("new foo").run();
|
2020-10-14 01:13:25 +00:00
|
|
|
|
let repo = git2::Repository::open(paths::root().join("foo")).unwrap();
|
|
|
|
|
let head = repo.find_reference("HEAD").unwrap();
|
|
|
|
|
assert_eq!(head.symbolic_target().unwrap(), "refs/heads/master");
|
|
|
|
|
|
|
|
|
|
fs::write(
|
|
|
|
|
paths::home().join(".gitconfig"),
|
|
|
|
|
r#"
|
|
|
|
|
[init]
|
|
|
|
|
defaultBranch = hello
|
|
|
|
|
"#,
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
2021-03-20 09:45:01 +00:00
|
|
|
|
cargo_process("new bar").run();
|
2020-10-14 01:13:25 +00:00
|
|
|
|
let repo = git2::Repository::open(paths::root().join("bar")).unwrap();
|
|
|
|
|
let head = repo.find_reference("HEAD").unwrap();
|
|
|
|
|
assert_eq!(head.symbolic_target().unwrap(), "refs/heads/hello");
|
|
|
|
|
}
|
2022-11-10 15:49:08 +00:00
|
|
|
|
|
|
|
|
|
#[cargo_test]
|
|
|
|
|
fn non_utf8_str_in_ignore_file() {
|
|
|
|
|
let gitignore = paths::home().join(".gitignore");
|
|
|
|
|
File::create(gitignore).unwrap();
|
|
|
|
|
|
|
|
|
|
fs::write(paths::home().join(".gitignore"), &[0xFF, 0xFE]).unwrap();
|
|
|
|
|
|
|
|
|
|
cargo_process(&format!("init {} --vcs git", paths::home().display()))
|
|
|
|
|
.with_status(101)
|
|
|
|
|
.with_stderr(
|
|
|
|
|
"\
|
2024-01-29 21:29:10 +00:00
|
|
|
|
[CREATING] binary (application) package
|
2022-11-10 15:49:08 +00:00
|
|
|
|
error: Failed to create package `home` at `[..]`
|
|
|
|
|
|
|
|
|
|
Caused by:
|
|
|
|
|
Character at line 0 is invalid. Cargo only supports UTF-8.
|
|
|
|
|
",
|
|
|
|
|
)
|
|
|
|
|
.run();
|
|
|
|
|
}
|
2022-10-26 21:27:46 +00:00
|
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
|
#[cargo_test]
|
|
|
|
|
fn path_with_invalid_character() {
|
|
|
|
|
cargo_process("new --name testing test:ing")
|
|
|
|
|
.with_stderr(
|
|
|
|
|
"\
|
2024-01-29 21:29:10 +00:00
|
|
|
|
[CREATING] binary (application) `testing` package
|
2022-10-26 21:27:46 +00:00
|
|
|
|
[WARNING] the path `[CWD]/test:ing` contains invalid PATH characters (usually `:`, `;`, or `\"`)
|
|
|
|
|
It is recommended to use a different name to avoid problems.
|
2024-01-29 21:38:04 +00:00
|
|
|
|
[NOTE] see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
2022-10-26 21:27:46 +00:00
|
|
|
|
",
|
|
|
|
|
)
|
|
|
|
|
.run();
|
|
|
|
|
}
|