]> git.proxmox.com Git - cargo.git/blobdiff - tests/testsuite/directory.rs
Add RegistryBuilder to help initializing test registries.
[cargo.git] / tests / testsuite / directory.rs
index 081e0be0e270d378bd2c57acb8cea7a8e6321029..85a5dd5842c61cf2fb9d3d068f17b77f12d944f5 100644 (file)
@@ -1,21 +1,23 @@
+//! Tests for directory sources.
+
 use std::collections::HashMap;
-use std::fs::{self, File};
-use std::io::prelude::*;
+use std::fs;
 use std::str;
 
 use serde::Serialize;
 
-use crate::support::cargo_process;
-use crate::support::git;
-use crate::support::paths;
-use crate::support::registry::{cksum, Package};
-use crate::support::{basic_manifest, project, ProjectBuilder};
+use cargo_test_support::cargo_process;
+use cargo_test_support::git;
+use cargo_test_support::paths;
+use cargo_test_support::registry::{cksum, Package};
+use cargo_test_support::{basic_manifest, project, t, ProjectBuilder};
 
 fn setup() {
     let root = paths::root();
     t!(fs::create_dir(&root.join(".cargo")));
-    t!(t!(File::create(root.join(".cargo/config"))).write_all(
-        br#"
+    t!(fs::write(
+        root.join(".cargo/config"),
+        r#"
             [source.crates-io]
             replace-with = 'my-awesome-local-registry'
 
@@ -73,7 +75,7 @@ impl VendorPackage {
     }
 }
 
-#[test]
+#[cargo_test]
 fn simple() {
     setup();
 
@@ -86,14 +88,14 @@ fn simple() {
         .file(
             "Cargo.toml",
             r#"
-            [package]
-            name = "foo"
-            version = "0.1.0"
-            authors = []
+                [package]
+                name = "foo"
+                version = "0.1.0"
+                authors = []
 
-            [dependencies]
-            bar = "0.1.0"
-        "#,
+                [dependencies]
+                bar = "0.1.0"
+            "#,
         )
         .file(
             "src/lib.rs",
@@ -112,7 +114,7 @@ fn simple() {
         .run();
 }
 
-#[test]
+#[cargo_test]
 fn simple_install() {
     setup();
 
@@ -124,14 +126,14 @@ fn simple_install() {
         .file(
             "Cargo.toml",
             r#"
-            [package]
-            name = "bar"
-            version = "0.1.0"
-            authors = []
+                [package]
+                name = "bar"
+                version = "0.1.0"
+                authors = []
 
-            [dependencies]
-            foo = "0.0.1"
-        "#,
+                [dependencies]
+                foo = "0.0.1"
+            "#,
         )
         .file(
             "src/main.rs",
@@ -141,18 +143,20 @@ fn simple_install() {
 
     cargo_process("install bar")
         .with_stderr(
-            "  Installing bar v0.1.0
-   Compiling foo v0.0.1
-   Compiling bar v0.1.0
-    Finished release [optimized] target(s) in [..]s
-  Installing [..]bar[..]
-warning: be sure to add `[..]` to your PATH to be able to run the installed binaries
+            "\
+[INSTALLING] bar v0.1.0
+[COMPILING] foo v0.0.1
+[COMPILING] bar v0.1.0
+[FINISHED] release [optimized] target(s) in [..]s
+[INSTALLING] [..]bar[..]
+[INSTALLED] package `bar v0.1.0` (executable `bar[EXE]`)
+[WARNING] be sure to add `[..]` to your PATH to be able to run the installed binaries
 ",
         )
         .run();
 }
 
-#[test]
+#[cargo_test]
 fn simple_install_fail() {
     setup();
 
@@ -164,15 +168,15 @@ fn simple_install_fail() {
         .file(
             "Cargo.toml",
             r#"
-            [package]
-            name = "bar"
-            version = "0.1.0"
-            authors = []
+                [package]
+                name = "bar"
+                version = "0.1.0"
+                authors = []
 
-            [dependencies]
-            foo = "0.1.0"
-            baz = "9.8.7"
-        "#,
+                [dependencies]
+                foo = "0.1.0"
+                baz = "9.8.7"
+            "#,
         )
         .file(
             "src/main.rs",
@@ -188,15 +192,15 @@ error: failed to compile `bar v0.1.0`, intermediate artifacts can be found at `[
 
 Caused by:
   no matching package named `baz` found
-location searched: registry `https://github.com/rust-lang/crates.io-index`
-perhaps you meant: bar or foo
-required by package `bar v0.1.0`
+  location searched: registry `https://github.com/rust-lang/crates.io-index`
+  perhaps you meant: bar or foo
+  required by package `bar v0.1.0`
 ",
         )
         .run();
 }
 
-#[test]
+#[cargo_test]
 fn install_without_feature_dep() {
     setup();
 
@@ -208,18 +212,18 @@ fn install_without_feature_dep() {
         .file(
             "Cargo.toml",
             r#"
-            [package]
-            name = "bar"
-            version = "0.1.0"
-            authors = []
+                [package]
+                name = "bar"
+                version = "0.1.0"
+                authors = []
 
-            [dependencies]
-            foo = "0.0.1"
-            baz = { version = "9.8.7", optional = true }
+                [dependencies]
+                foo = "0.0.1"
+                baz = { version = "9.8.7", optional = true }
 
-            [features]
-            wantbaz = ["baz"]
-        "#,
+                [features]
+                wantbaz = ["baz"]
+            "#,
         )
         .file(
             "src/main.rs",
@@ -229,18 +233,20 @@ fn install_without_feature_dep() {
 
     cargo_process("install bar")
         .with_stderr(
-            "  Installing bar v0.1.0
-   Compiling foo v0.0.1
-   Compiling bar v0.1.0
-    Finished release [optimized] target(s) in [..]s
-  Installing [..]bar[..]
-warning: be sure to add `[..]` to your PATH to be able to run the installed binaries
+            "\
+[INSTALLING] bar v0.1.0
+[COMPILING] foo v0.0.1
+[COMPILING] bar v0.1.0
+[FINISHED] release [optimized] target(s) in [..]s
+[INSTALLING] [..]bar[..]
+[INSTALLED] package `bar v0.1.0` (executable `bar[EXE]`)
+[WARNING] be sure to add `[..]` to your PATH to be able to run the installed binaries
 ",
         )
         .run();
 }
 
-#[test]
+#[cargo_test]
 fn not_there() {
     setup();
 
@@ -250,14 +256,14 @@ fn not_there() {
         .file(
             "Cargo.toml",
             r#"
-            [package]
-            name = "foo"
-            version = "0.1.0"
-            authors = []
+                [package]
+                name = "foo"
+                version = "0.1.0"
+                authors = []
 
-            [dependencies]
-            bar = "0.1.0"
-        "#,
+                [dependencies]
+                bar = "0.1.0"
+            "#,
         )
         .file(
             "src/lib.rs",
@@ -277,7 +283,7 @@ required by package `foo v0.1.0 ([..])`
         .run();
 }
 
-#[test]
+#[cargo_test]
 fn multiple() {
     setup();
 
@@ -297,14 +303,14 @@ fn multiple() {
         .file(
             "Cargo.toml",
             r#"
-            [package]
-            name = "foo"
-            version = "0.1.0"
-            authors = []
+                [package]
+                name = "foo"
+                version = "0.1.0"
+                authors = []
 
-            [dependencies]
-            bar = "0.1.0"
-        "#,
+                [dependencies]
+                bar = "0.1.0"
+            "#,
         )
         .file(
             "src/lib.rs",
@@ -323,20 +329,20 @@ fn multiple() {
         .run();
 }
 
-#[test]
+#[cargo_test]
 fn crates_io_then_directory() {
     let p = project()
         .file(
             "Cargo.toml",
             r#"
-            [package]
-            name = "foo"
-            version = "0.1.0"
-            authors = []
+                [package]
+                name = "foo"
+                version = "0.1.0"
+                authors = []
 
-            [dependencies]
-            bar = "0.1.0"
-        "#,
+                [dependencies]
+                bar = "0.1.0"
+            "#,
         )
         .file(
             "src/lib.rs",
@@ -380,20 +386,20 @@ fn crates_io_then_directory() {
         .run();
 }
 
-#[test]
+#[cargo_test]
 fn crates_io_then_bad_checksum() {
     let p = project()
         .file(
             "Cargo.toml",
             r#"
-            [package]
-            name = "foo"
-            version = "0.1.0"
-            authors = []
+                [package]
+                name = "foo"
+                version = "0.1.0"
+                authors = []
 
-            [dependencies]
-            bar = "0.1.0"
-        "#,
+                [dependencies]
+                bar = "0.1.0"
+            "#,
         )
         .file("src/lib.rs", "")
         .build();
@@ -417,7 +423,7 @@ error: checksum for `bar v0.1.0` changed between lock files
 this could be indicative of a few possible errors:
 
     * the lock file is corrupt
-    * a replacement source in use (e.g. a mirror) returned a different checksum
+    * a replacement source in use (e.g., a mirror) returned a different checksum
     * the source itself may be corrupt in one way or another
 
 unable to verify that `bar v0.1.0` is the same as when the lockfile was generated
@@ -427,7 +433,7 @@ unable to verify that `bar v0.1.0` is the same as when the lockfile was generate
         .run();
 }
 
-#[test]
+#[cargo_test]
 fn bad_file_checksum() {
     setup();
 
@@ -436,21 +442,23 @@ fn bad_file_checksum() {
         .file("src/lib.rs", "")
         .build();
 
-    let mut f = t!(File::create(paths::root().join("index/bar/src/lib.rs")));
-    t!(f.write_all(b"fn bar() -> u32 { 0 }"));
+    t!(fs::write(
+        paths::root().join("index/bar/src/lib.rs"),
+        "fn bar() -> u32 { 0 }"
+    ));
 
     let p = project()
         .file(
             "Cargo.toml",
             r#"
-            [package]
-            name = "foo"
-            version = "0.1.0"
-            authors = []
+                [package]
+                name = "foo"
+                version = "0.1.0"
+                authors = []
 
-            [dependencies]
-            bar = "0.1.0"
-        "#,
+                [dependencies]
+                bar = "0.1.0"
+            "#,
         )
         .file("src/lib.rs", "")
         .build();
@@ -471,7 +479,7 @@ the source
         .run();
 }
 
-#[test]
+#[cargo_test]
 fn only_dot_files_ok() {
     setup();
 
@@ -488,14 +496,14 @@ fn only_dot_files_ok() {
         .file(
             "Cargo.toml",
             r#"
-            [package]
-            name = "foo"
-            version = "0.1.0"
-            authors = []
+                [package]
+                name = "foo"
+                version = "0.1.0"
+                authors = []
 
-            [dependencies]
-            bar = "0.1.0"
-        "#,
+                [dependencies]
+                bar = "0.1.0"
+            "#,
         )
         .file("src/lib.rs", "")
         .build();
@@ -503,7 +511,7 @@ fn only_dot_files_ok() {
     p.cargo("build").run();
 }
 
-#[test]
+#[cargo_test]
 fn random_files_ok() {
     setup();
 
@@ -521,14 +529,14 @@ fn random_files_ok() {
         .file(
             "Cargo.toml",
             r#"
-            [package]
-            name = "foo"
-            version = "0.1.0"
-            authors = []
+                [package]
+                name = "foo"
+                version = "0.1.0"
+                authors = []
 
-            [dependencies]
-            bar = "0.1.0"
-        "#,
+                [dependencies]
+                bar = "0.1.0"
+            "#,
         )
         .file("src/lib.rs", "")
         .build();
@@ -536,13 +544,12 @@ fn random_files_ok() {
     p.cargo("build").run();
 }
 
-#[test]
+#[cargo_test]
 fn git_lock_file_doesnt_change() {
     let git = git::new("git", |p| {
         p.file("Cargo.toml", &basic_manifest("git", "0.5.0"))
             .file("src/lib.rs", "")
-    })
-    .unwrap();
+    });
 
     VendorPackage::new("git")
         .file("Cargo.toml", &basic_manifest("git", "0.5.0"))
@@ -555,14 +562,14 @@ fn git_lock_file_doesnt_change() {
             "Cargo.toml",
             &format!(
                 r#"
-            [package]
-            name = "foo"
-            version = "0.0.1"
-            authors = []
-
-            [dependencies]
-            git = {{ git = '{0}' }}
-        "#,
+                    [package]
+                    name = "foo"
+                    version = "0.0.1"
+                    authors = []
+
+                    [dependencies]
+                    git = {{ git = '{0}' }}
+                "#,
                 git.url()
             ),
         )
@@ -571,24 +578,23 @@ fn git_lock_file_doesnt_change() {
 
     p.cargo("build").run();
 
-    let mut lock1 = String::new();
-    t!(t!(File::open(p.root().join("Cargo.lock"))).read_to_string(&mut lock1));
+    let lock1 = p.read_lockfile();
 
     let root = paths::root();
     t!(fs::create_dir(&root.join(".cargo")));
-    t!(t!(File::create(root.join(".cargo/config"))).write_all(
-        &format!(
+    t!(fs::write(
+        root.join(".cargo/config"),
+        format!(
             r#"
-        [source.my-git-repo]
-        git = '{}'
-        replace-with = 'my-awesome-local-registry'
+                [source.my-git-repo]
+                git = '{}'
+                replace-with = 'my-awesome-local-registry'
 
-        [source.my-awesome-local-registry]
-        directory = 'index'
-    "#,
+                [source.my-awesome-local-registry]
+                directory = 'index'
+            "#,
             git.url()
         )
-        .as_bytes()
     ));
 
     p.cargo("build")
@@ -601,12 +607,11 @@ fn git_lock_file_doesnt_change() {
         )
         .run();
 
-    let mut lock2 = String::new();
-    t!(t!(File::open(p.root().join("Cargo.lock"))).read_to_string(&mut lock2));
+    let lock2 = p.read_lockfile();
     assert_eq!(lock1, lock2, "lock files changed");
 }
 
-#[test]
+#[cargo_test]
 fn git_override_requires_lockfile() {
     VendorPackage::new("git")
         .file("Cargo.toml", &basic_manifest("git", "0.5.0"))
@@ -618,53 +623,56 @@ fn git_override_requires_lockfile() {
         .file(
             "Cargo.toml",
             r#"
-            [package]
-            name = "foo"
-            version = "0.0.1"
-            authors = []
+                [package]
+                name = "foo"
+                version = "0.0.1"
+                authors = []
 
-            [dependencies]
-            git = { git = 'https://example.com/' }
-        "#,
+                [dependencies]
+                git = { git = 'https://example.com/' }
+            "#,
         )
         .file("src/lib.rs", "")
         .build();
 
     let root = paths::root();
     t!(fs::create_dir(&root.join(".cargo")));
-    t!(t!(File::create(root.join(".cargo/config"))).write_all(
-        br#"
-        [source.my-git-repo]
-        git = 'https://example.com/'
-        replace-with = 'my-awesome-local-registry'
-
-        [source.my-awesome-local-registry]
-        directory = 'index'
-    "#
+    t!(fs::write(
+        root.join(".cargo/config"),
+        r#"
+            [source.my-git-repo]
+            git = 'https://example.com/'
+            replace-with = 'my-awesome-local-registry'
+
+            [source.my-awesome-local-registry]
+            directory = 'index'
+        "#
     ));
 
     p.cargo("build")
         .with_status(101)
         .with_stderr(
             "\
-error: failed to load source for a dependency on `git`
+[ERROR] failed to get `git` as a dependency of package `foo v0.0.1 ([..])`
+
+Caused by:
+  failed to load source for dependency `git`
 
 Caused by:
   Unable to update [..]
 
 Caused by:
   the source my-git-repo requires a lock file to be present first before it can be
-used against vendored source code
-
-remove the source replacement configuration, generate a lock file, and then
-restore the source replacement configuration to continue the build
+  used against vendored source code
 
+  remove the source replacement configuration, generate a lock file, and then
+  restore the source replacement configuration to continue the build
 ",
         )
         .run();
 }
 
-#[test]
+#[cargo_test]
 fn workspace_different_locations() {
     let p = project()
         .no_manifest()
@@ -710,10 +718,9 @@ fn workspace_different_locations() {
         )
         .build();
 
-    p.cargo("build").cwd(p.root().join("foo")).run();
+    p.cargo("build").cwd("foo").run();
     p.cargo("build")
-        .cwd(p.root().join("bar"))
-        .with_status(0)
+        .cwd("bar")
         .with_stderr(
             "\
 [COMPILING] bar [..]
@@ -723,7 +730,7 @@ fn workspace_different_locations() {
         .run();
 }
 
-#[test]
+#[cargo_test]
 fn version_missing() {
     setup();
 
@@ -757,8 +764,8 @@ Caused by:
   failed to select a version for the requirement `foo = \"^2\"`
   candidate versions found which didn't match: 0.0.1
   location searched: directory source `[..] (which is replacing registry `[..]`)
-required by package `bar v0.1.0`
-perhaps a crate was updated and forgotten to be re-vendored?
+  required by package `bar v0.1.0`
+  perhaps a crate was updated and forgotten to be re-vendored?
 ",
         )
         .with_status(101)