Skip to content

Add --lib and status message for completion #2921

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions src/bin/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub struct Options {
flag_quiet: Option<bool>,
flag_color: Option<String>,
flag_bin: bool,
flag_lib: bool,
arg_path: Option<String>,
flag_name: Option<String>,
flag_vcs: Option<ops::VersionControl>,
Expand All @@ -28,7 +29,8 @@ Options:
--vcs VCS Initialize a new repository for the given version
control system (git or hg) or do not initialize any version
control at all (none) overriding a global configuration.
--bin Use a binary instead of a library template
--bin Use a binary (application) template
--lib Use a library template
--name NAME Set the resulting package name
-v, --verbose ... Use verbose output
-q, --quiet No output printed to stdout
Expand All @@ -45,16 +47,22 @@ pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
options.flag_frozen,
options.flag_locked));

let Options { flag_bin, arg_path, flag_name, flag_vcs, .. } = options;
let Options { flag_bin, flag_lib, arg_path, flag_name, flag_vcs, .. } = options;

let opts = ops::NewOptions {
version_control: flag_vcs,
bin: flag_bin,
path: &arg_path.unwrap_or(format!(".")),
name: flag_name.as_ref().map(|s| s.as_ref()),
};
let tmp = &arg_path.unwrap_or(format!("."));
let opts = ops::NewOptions::new(flag_vcs,
flag_bin,
flag_lib,
tmp,
flag_name.as_ref().map(|s| s.as_ref()));

let opts_lib = opts.lib;
try!(ops::init(opts, config));

try!(config.shell().status("Created", format!("{} project",
if opts_lib { "library" }
else {"binary (application)"})));

Ok(None)
}

24 changes: 16 additions & 8 deletions src/bin/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub struct Options {
flag_quiet: Option<bool>,
flag_color: Option<String>,
flag_bin: bool,
flag_lib: bool,
arg_path: String,
flag_name: Option<String>,
flag_vcs: Option<ops::VersionControl>,
Expand All @@ -28,7 +29,8 @@ Options:
--vcs VCS Initialize a new repository for the given version
control system (git or hg) or do not initialize any version
control at all (none) overriding a global configuration.
--bin Use a binary instead of a library template
--bin Use a binary (application) template
--lib Use a library template
--name NAME Set the resulting package name
-v, --verbose ... Use verbose output
-q, --quiet No output printed to stdout
Expand All @@ -45,16 +47,22 @@ pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
options.flag_frozen,
options.flag_locked));

let Options { flag_bin, arg_path, flag_name, flag_vcs, .. } = options;
let Options { flag_bin, flag_lib, arg_path, flag_name, flag_vcs, .. } = options;

let opts = ops::NewOptions {
version_control: flag_vcs,
bin: flag_bin,
path: &arg_path,
name: flag_name.as_ref().map(|s| s.as_ref()),
};
let opts = ops::NewOptions::new(flag_vcs,
flag_bin,
flag_lib,
&arg_path,
flag_name.as_ref().map(|s| s.as_ref()));

let opts_lib = opts.lib;
try!(ops::new(opts, config));

try!(config.shell().status("Created", format!("{} `{}` project",
if opts_lib { "library" }
else {"binary (application)"},
arg_path)));

Ok(None)
}

34 changes: 34 additions & 0 deletions src/cargo/ops/cargo_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub enum VersionControl { Git, Hg, NoVcs }
pub struct NewOptions<'a> {
pub version_control: Option<VersionControl>,
pub bin: bool,
pub lib: bool,
pub path: &'a str,
pub name: Option<&'a str>,
}
Expand Down Expand Up @@ -53,6 +54,31 @@ impl Decodable for VersionControl {
}
}

impl<'a> NewOptions<'a> {
pub fn new(version_control: Option<VersionControl>,
bin: bool,
lib: bool,
path: &'a str,
name: Option<&'a str>) -> NewOptions<'a> {

// default to lib
let is_lib = if !bin {
true
}
else {
lib
};

NewOptions {
version_control: version_control,
bin: bin,
lib: is_lib,
path: path,
name: name,
}
}
}

struct CargoNewConfig {
name: Option<String>,
email: Option<String>,
Expand Down Expand Up @@ -235,6 +261,10 @@ pub fn new(opts: NewOptions, config: &Config) -> CargoResult<()> {
path.display())
}

if opts.lib && opts.bin {
bail!("can't specify both lib and binary outputs");
}

let name = try!(get_name(&path, &opts, config));
try!(check_name(name));

Expand All @@ -260,6 +290,10 @@ pub fn init(opts: NewOptions, config: &Config) -> CargoResult<()> {
bail!("`cargo init` cannot be run on existing Cargo projects")
}

if opts.lib && opts.bin {
bail!("can't specify both lib and binary outputs");
}

let name = try!(get_name(&path, &opts, config));
try!(check_name(name));

Expand Down
1 change: 1 addition & 0 deletions tests/cargotest/support/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,7 @@ fn substitute_macros(input: &str) -> String {
let macros = [
("[RUNNING]", " Running"),
("[COMPILING]", " Compiling"),
("[CREATED]", " Created"),
("[FINISHED]", " Finished"),
("[ERROR]", "error:"),
("[WARNING]", "warning:"),
Expand Down
40 changes: 28 additions & 12 deletions tests/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ fn cargo_process(s: &str) -> ProcessBuilder {

#[test]
fn simple_lib() {
assert_that(cargo_process("init").arg("--vcs").arg("none")
assert_that(cargo_process("init").arg("--lib").arg("--vcs").arg("none")
.env("USER", "foo"),
execs().with_status(0));
execs().with_status(0).with_stderr("\
[CREATED] library project
"));

assert_that(&paths::root().join("Cargo.toml"), existing_file());
assert_that(&paths::root().join("src/lib.rs"), existing_file());
Expand All @@ -38,7 +40,9 @@ fn simple_bin() {
fs::create_dir(&path).unwrap();
assert_that(cargo_process("init").arg("--bin").arg("--vcs").arg("none")
.env("USER", "foo").cwd(&path),
execs().with_status(0));
execs().with_status(0).with_stderr("\
[CREATED] binary (application) project
"));

assert_that(&paths::root().join("foo/Cargo.toml"), existing_file());
assert_that(&paths::root().join("foo/src/main.rs"), existing_file());
Expand All @@ -50,6 +54,15 @@ fn simple_bin() {
existing_file());
}

#[test]
fn both_lib_and_bin() {
let td = TempDir::new("cargo").unwrap();
assert_that(cargo_process("init").arg("--lib").arg("--bin").cwd(td.path().clone())
.env("USER", "foo"),
execs().with_status(101).with_stderr(
"[ERROR] can't specify both lib and binary outputs"));
}

fn bin_already_exists(explicit: bool, rellocation: &str) {
let path = paths::root().join("foo");
fs::create_dir_all(&path.join("src")).unwrap();
Expand Down Expand Up @@ -165,7 +178,7 @@ fn multibin_project_name_clash() {
}
"#).unwrap();

assert_that(cargo_process("init").arg("--vcs").arg("none")
assert_that(cargo_process("init").arg("--lib").arg("--vcs").arg("none")
.env("USER", "foo").cwd(&path),
execs().with_status(101).with_stderr("\
[ERROR] multiple possible binary sources found:
Expand Down Expand Up @@ -214,8 +227,10 @@ fn lib_already_exists_nosrc() {

#[test]
fn simple_git() {
assert_that(cargo_process("init").arg("--vcs").arg("git")
.env("USER", "foo"),
assert_that(cargo_process("init").arg("--lib")
.arg("--vcs")
.arg("git")
.env("USER", "foo"),
execs().with_status(0));

assert_that(&paths::root().join("Cargo.toml"), existing_file());
Expand All @@ -229,7 +244,8 @@ fn auto_git() {
let td = TempDir::new("cargo").unwrap();
let foo = &td.path().join("foo");
fs::create_dir_all(&foo).unwrap();
assert_that(cargo_process("init").cwd(foo.clone())
assert_that(cargo_process("init").arg("--lib")
.cwd(foo.clone())
.env("USER", "foo"),
execs().with_status(0));

Expand Down Expand Up @@ -271,7 +287,7 @@ use --name to override crate name
fn git_autodetect() {
fs::create_dir(&paths::root().join(".git")).unwrap();

assert_that(cargo_process("init")
assert_that(cargo_process("init").arg("--lib")
.env("USER", "foo"),
execs().with_status(0));

Expand All @@ -287,7 +303,7 @@ fn git_autodetect() {
fn mercurial_autodetect() {
fs::create_dir(&paths::root().join(".hg")).unwrap();

assert_that(cargo_process("init")
assert_that(cargo_process("init").arg("--lib")
.env("USER", "foo"),
execs().with_status(0));

Expand All @@ -304,8 +320,8 @@ fn gitignore_appended_not_replaced() {

File::create(&paths::root().join(".gitignore")).unwrap().write_all(b"qqqqqq\n").unwrap();

assert_that(cargo_process("init")
.env("USER", "foo"),
assert_that(cargo_process("init").arg("--lib")
.env("USER", "foo"),
execs().with_status(0));


Expand All @@ -323,7 +339,7 @@ fn gitignore_appended_not_replaced() {
fn cargo_lock_gitignored_if_lib1() {
fs::create_dir(&paths::root().join(".git")).unwrap();

assert_that(cargo_process("init").arg("--vcs").arg("git")
assert_that(cargo_process("init").arg("--lib").arg("--vcs").arg("git")
.env("USER", "foo"),
execs().with_status(0));

Expand Down
25 changes: 19 additions & 6 deletions tests/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ fn cargo_process(s: &str) -> ProcessBuilder {

#[test]
fn simple_lib() {
assert_that(cargo_process("new").arg("foo").arg("--vcs").arg("none")
assert_that(cargo_process("new").arg("--lib").arg("foo").arg("--vcs").arg("none")
.env("USER", "foo"),
execs().with_status(0));
execs().with_status(0).with_stderr("\
[CREATED] library `foo` project
"));

assert_that(&paths::root().join("foo"), existing_dir());
assert_that(&paths::root().join("foo/Cargo.toml"), existing_file());
Expand All @@ -36,9 +38,11 @@ fn simple_lib() {

#[test]
fn simple_bin() {
assert_that(cargo_process("new").arg("foo").arg("--bin")
assert_that(cargo_process("new").arg("--bin").arg("foo")
.env("USER", "foo"),
execs().with_status(0));
execs().with_status(0).with_stderr("\
[CREATED] binary (application) `foo` project
"));

assert_that(&paths::root().join("foo"), existing_dir());
assert_that(&paths::root().join("foo/Cargo.toml"), existing_file());
Expand All @@ -51,10 +55,19 @@ fn simple_bin() {
existing_file());
}

#[test]
fn both_lib_and_bin() {
let td = TempDir::new("cargo").unwrap();
assert_that(cargo_process("new").arg("--lib").arg("--bin").arg("foo").cwd(td.path().clone())
.env("USER", "foo"),
execs().with_status(101).with_stderr(
"[ERROR] can't specify both lib and binary outputs"));
}

#[test]
fn simple_git() {
let td = TempDir::new("cargo").unwrap();
assert_that(cargo_process("new").arg("foo").cwd(td.path().clone())
assert_that(cargo_process("new").arg("--lib").arg("foo").cwd(td.path().clone())
.env("USER", "foo"),
execs().with_status(0));

Expand Down Expand Up @@ -120,7 +133,7 @@ use --name to override crate name"));

#[test]
fn rust_prefix_stripped() {
assert_that(cargo_process("new").arg("rust-foo").env("USER", "foo"),
assert_that(cargo_process("new").arg("--lib").arg("rust-foo").env("USER", "foo"),
execs().with_status(0)
.with_stdout("note: package will be named `foo`; use --name to override"));
let toml = paths::root().join("rust-foo/Cargo.toml");
Expand Down
3 changes: 2 additions & 1 deletion tests/workspaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@ fn new_warns_you_this_will_not_work() {
.file("src/lib.rs", "");
p.build();

assert_that(p.cargo("new").arg("bar").env("USER", "foo"),
assert_that(p.cargo("new").arg("--lib").arg("bar").env("USER", "foo"),
execs().with_status(0)
.with_stderr("\
warning: compiling this new crate may not work due to invalid workspace \
Expand All @@ -780,6 +780,7 @@ workspace: [..]

this may be fixable by ensuring that this crate is depended on by the workspace \
root: [..]
[CREATED] library `bar` project
"));
}

Expand Down