]> git.proxmox.com Git - cargo.git/blobdiff - tests/testsuite/cargo_command.rs
Explicitly mark aliases in `cargo list`.
[cargo.git] / tests / testsuite / cargo_command.rs
index fa4e05a74d256a5d41c7a347b97d87e52b5e79a2..b814713f1d2fd09727c5a58c7f53580f96319519 100644 (file)
@@ -1,63 +1,17 @@
 //! Tests for custom cargo commands and other global command features.
 
 use std::env;
-use std::fs::{self, File};
+use std::fs;
 use std::io::Read;
 use std::path::{Path, PathBuf};
 use std::process::Stdio;
 use std::str;
 
-use cargo_test_support::cargo_process;
-use cargo_test_support::paths::{self, CargoPathExt};
 use cargo_test_support::registry::Package;
-use cargo_test_support::{basic_bin_manifest, basic_manifest, cargo_exe, project, Project};
-
-#[cfg_attr(windows, allow(dead_code))]
-enum FakeKind<'a> {
-    Executable,
-    Symlink { target: &'a Path },
-}
-
-/// Adds an empty file with executable flags (and platform-dependent suffix).
-//
-// TODO: move this to `Project` if other cases using this emerge.
-fn fake_file(proj: Project, dir: &Path, name: &str, kind: &FakeKind<'_>) -> Project {
-    let path = proj
-        .root()
-        .join(dir)
-        .join(&format!("{}{}", name, env::consts::EXE_SUFFIX));
-    path.parent().unwrap().mkdir_p();
-    match *kind {
-        FakeKind::Executable => {
-            File::create(&path).unwrap();
-            make_executable(&path);
-        }
-        FakeKind::Symlink { target } => {
-            make_symlink(&path, target);
-        }
-    }
-    return proj;
-
-    #[cfg(unix)]
-    fn make_executable(p: &Path) {
-        use std::os::unix::prelude::*;
-
-        let mut perms = fs::metadata(p).unwrap().permissions();
-        let mode = perms.mode();
-        perms.set_mode(mode | 0o111);
-        fs::set_permissions(p, perms).unwrap();
-    }
-    #[cfg(windows)]
-    fn make_executable(_: &Path) {}
-    #[cfg(unix)]
-    fn make_symlink(p: &Path, t: &Path) {
-        ::std::os::unix::fs::symlink(t, p).expect("Failed to create symlink");
-    }
-    #[cfg(windows)]
-    fn make_symlink(_: &Path, _: &Path) {
-        panic!("Not supported")
-    }
-}
+use cargo_test_support::tools::echo_subcommand;
+use cargo_test_support::{
+    basic_bin_manifest, cargo_exe, cargo_process, paths, project, project_in_home,
+};
 
 fn path() -> Vec<PathBuf> {
     env::split_paths(&env::var_os("PATH").unwrap_or_default()).collect()
@@ -78,15 +32,59 @@ fn list_commands_with_descriptions() {
         .run();
 }
 
+#[cargo_test]
+fn list_builtin_aliases_with_descriptions() {
+    let p = project().build();
+    p.cargo("--list")
+        .with_stdout_contains("    b                    alias: build")
+        .with_stdout_contains("    c                    alias: check")
+        .with_stdout_contains("    r                    alias: run")
+        .with_stdout_contains("    t                    alias: test")
+        .run();
+}
+
+#[cargo_test]
+fn list_custom_aliases_with_descriptions() {
+    let p = project_in_home("proj")
+        .file(
+            &paths::home().join(".cargo").join("config"),
+            r#"
+            [alias]
+            myaliasstr = "foo --bar"
+            myaliasvec = ["foo", "--bar"]
+        "#,
+        )
+        .build();
+
+    p.cargo("--list")
+        .with_stdout_contains("    myaliasstr           alias: foo --bar")
+        .with_stdout_contains("    myaliasvec           alias: foo --bar")
+        .run();
+}
+
+#[cargo_test]
+fn list_dedupe() {
+    let p = project()
+        .executable(Path::new("path-test-1").join("cargo-dupe"), "")
+        .executable(Path::new("path-test-2").join("cargo-dupe"), "")
+        .build();
+
+    let mut path = path();
+    path.push(p.root().join("path-test-1"));
+    path.push(p.root().join("path-test-2"));
+    let path = env::join_paths(path.iter()).unwrap();
+
+    p.cargo("--list")
+        .env("PATH", &path)
+        .with_stdout_contains_n("    dupe", 1)
+        .run();
+}
+
 #[cargo_test]
 fn list_command_looks_at_path() {
-    let proj = project().build();
-    let proj = fake_file(
-        proj,
-        Path::new("path-test"),
-        "cargo-1",
-        &FakeKind::Executable,
-    );
+    let proj = project()
+        .executable(Path::new("path-test").join("cargo-1"), "")
+        .build();
 
     let mut path = path();
     path.push(proj.root().join("path-test"));
@@ -103,19 +101,36 @@ fn list_command_looks_at_path() {
     );
 }
 
-// Windows and symlinks don't currently mix well.
-#[cfg(unix)]
+#[cargo_test]
+fn list_command_handles_known_external_commands() {
+    let p = project()
+        .executable(Path::new("path-test").join("cargo-fmt"), "")
+        .build();
+
+    let fmt_desc = "    fmt                  Formats all bin and lib files of the current crate using rustfmt.";
+
+    // Without path - fmt isn't there
+    p.cargo("--list")
+        .env("PATH", "")
+        .with_stdout_does_not_contain(fmt_desc)
+        .run();
+
+    // With path - fmt is there with known description
+    let mut path = path();
+    path.push(p.root().join("path-test"));
+    let path = env::join_paths(path.iter()).unwrap();
+
+    p.cargo("--list")
+        .env("PATH", &path)
+        .with_stdout_contains(fmt_desc)
+        .run();
+}
+
 #[cargo_test]
 fn list_command_resolves_symlinks() {
-    let proj = project().build();
-    let proj = fake_file(
-        proj,
-        Path::new("path-test"),
-        "cargo-2",
-        &FakeKind::Symlink {
-            target: &cargo_exe(),
-        },
-    );
+    let proj = project()
+        .symlink(cargo_exe(), Path::new("path-test").join("cargo-2"))
+        .build();
 
     let mut path = path();
     path.push(proj.root().join("path-test"));
@@ -151,10 +166,10 @@ error: no such subcommand: `biuld`
         .file(
             "src/main.rs",
             r#"
-            fn main() {
-                println!("Similar, but not identical to, build");
-            }
-        "#,
+                fn main() {
+                    println!("Similar, but not identical to, build");
+                }
+            "#,
         )
         .publish();
 
@@ -243,21 +258,18 @@ fn override_cargo_home() {
         &my_home.join("config"),
         r#"
             [cargo-new]
-            name = "foo"
-            email = "bar"
-            git = false
+            vcs = "none"
         "#,
     )
     .unwrap();
 
-    cargo_process("new foo")
-        .env("USER", "foo")
-        .env("CARGO_HOME", &my_home)
-        .run();
+    cargo_process("new foo").env("CARGO_HOME", &my_home).run();
 
-    let toml = paths::root().join("foo/Cargo.toml");
-    let contents = fs::read_to_string(&toml).unwrap();
-    assert!(contents.contains(r#"authors = ["foo <bar>"]"#));
+    assert!(!paths::root().join("foo/.git").is_dir());
+
+    cargo_process("new foo2").run();
+
+    assert!(paths::root().join("foo2/.git").is_dir());
 }
 
 #[cargo_test]
@@ -297,62 +309,17 @@ fn cargo_subcommand_env() {
 
 #[cargo_test]
 fn cargo_subcommand_args() {
-    let p = project()
-        .at("cargo-foo")
-        .file("Cargo.toml", &basic_manifest("cargo-foo", "0.0.1"))
-        .file(
-            "src/main.rs",
-            r#"
-            fn main() {
-                let args: Vec<_> = ::std::env::args().collect();
-                println!("{:?}", args);
-            }
-        "#,
-        )
-        .build();
-
-    p.cargo("build").run();
-    let cargo_foo_bin = p.bin("cargo-foo");
+    let p = echo_subcommand();
+    let cargo_foo_bin = p.bin("cargo-echo");
     assert!(cargo_foo_bin.is_file());
 
     let mut path = path();
     path.push(p.target_debug_dir());
     let path = env::join_paths(path.iter()).unwrap();
 
-    cargo_process("foo bar -v --help")
+    cargo_process("echo bar -v --help")
         .env("PATH", &path)
-        .with_stdout(
-            r#"["[CWD]/cargo-foo/target/debug/cargo-foo[EXE]", "foo", "bar", "-v", "--help"]"#,
-        )
-        .run();
-}
-
-#[cargo_test]
-fn cargo_help() {
-    cargo_process("").run();
-    cargo_process("help").run();
-    cargo_process("-h").run();
-    cargo_process("help build").run();
-    cargo_process("build -h").run();
-    cargo_process("help help").run();
-}
-
-#[cargo_test]
-fn cargo_help_external_subcommand() {
-    Package::new("cargo-fake-help", "1.0.0")
-        .file(
-            "src/main.rs",
-            r#"
-            fn main() {
-                if ::std::env::args().nth(2) == Some(String::from("--help")) {
-                    println!("fancy help output");
-                }
-            }"#,
-        )
-        .publish();
-    cargo_process("install cargo-fake-help").run();
-    cargo_process("help fake-help")
-        .with_stdout("fancy help output\n")
+        .with_stdout("echo bar -v --help")
         .run();
 }
 
@@ -365,15 +332,6 @@ fn explain() {
         .run();
 }
 
-// Test that the output of `cargo -Z help` shows a different help screen with
-// all the `-Z` flags.
-#[cargo_test]
-fn z_flags_help() {
-    cargo_process("-Z help")
-        .with_stdout_contains("    -Z unstable-options -- Allow the usage of unstable options")
-        .run();
-}
-
 #[cargo_test]
 fn closed_output_ok() {
     // Checks that closed output doesn't cause an error.
@@ -392,5 +350,5 @@ fn closed_output_ok() {
         .unwrap();
     let status = child.wait().unwrap();
     assert!(status.success());
-    assert!(s.is_empty(), s);
+    assert!(s.is_empty(), "{}", s);
 }