"Fossies" - the Fresh Open Source Software Archive

Member "cargo-0.61.1/tests/testsuite/directory.rs" (13 Apr 2022, 17558 Bytes) of package /linux/misc/cargo-0.61.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Rust source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 //! Tests for directory sources.
    2 
    3 use std::collections::HashMap;
    4 use std::fs;
    5 use std::str;
    6 
    7 use serde::Serialize;
    8 
    9 use cargo_test_support::cargo_process;
   10 use cargo_test_support::git;
   11 use cargo_test_support::paths;
   12 use cargo_test_support::registry::{cksum, Package};
   13 use cargo_test_support::{basic_manifest, project, t, ProjectBuilder};
   14 
   15 fn setup() {
   16     let root = paths::root();
   17     t!(fs::create_dir(&root.join(".cargo")));
   18     t!(fs::write(
   19         root.join(".cargo/config"),
   20         r#"
   21             [source.crates-io]
   22             replace-with = 'my-awesome-local-registry'
   23 
   24             [source.my-awesome-local-registry]
   25             directory = 'index'
   26         "#
   27     ));
   28 }
   29 
   30 struct VendorPackage {
   31     p: Option<ProjectBuilder>,
   32     cksum: Checksum,
   33 }
   34 
   35 #[derive(Serialize)]
   36 struct Checksum {
   37     package: Option<String>,
   38     files: HashMap<String, String>,
   39 }
   40 
   41 impl VendorPackage {
   42     fn new(name: &str) -> VendorPackage {
   43         VendorPackage {
   44             p: Some(project().at(&format!("index/{}", name))),
   45             cksum: Checksum {
   46                 package: Some(String::new()),
   47                 files: HashMap::new(),
   48             },
   49         }
   50     }
   51 
   52     fn file(&mut self, name: &str, contents: &str) -> &mut VendorPackage {
   53         self.p = Some(self.p.take().unwrap().file(name, contents));
   54         self.cksum
   55             .files
   56             .insert(name.to_string(), cksum(contents.as_bytes()));
   57         self
   58     }
   59 
   60     fn disable_checksum(&mut self) -> &mut VendorPackage {
   61         self.cksum.package = None;
   62         self
   63     }
   64 
   65     fn no_manifest(mut self) -> Self {
   66         self.p = self.p.map(|pb| pb.no_manifest());
   67         self
   68     }
   69 
   70     fn build(&mut self) {
   71         let p = self.p.take().unwrap();
   72         let json = serde_json::to_string(&self.cksum).unwrap();
   73         let p = p.file(".cargo-checksum.json", &json);
   74         let _ = p.build();
   75     }
   76 }
   77 
   78 #[cargo_test]
   79 fn simple() {
   80     setup();
   81 
   82     VendorPackage::new("bar")
   83         .file("Cargo.toml", &basic_manifest("bar", "0.1.0"))
   84         .file("src/lib.rs", "pub fn bar() {}")
   85         .build();
   86 
   87     let p = project()
   88         .file(
   89             "Cargo.toml",
   90             r#"
   91                 [package]
   92                 name = "foo"
   93                 version = "0.1.0"
   94                 authors = []
   95 
   96                 [dependencies]
   97                 bar = "0.1.0"
   98             "#,
   99         )
  100         .file(
  101             "src/lib.rs",
  102             "extern crate bar; pub fn foo() { bar::bar(); }",
  103         )
  104         .build();
  105 
  106     p.cargo("build")
  107         .with_stderr(
  108             "\
  109 [COMPILING] bar v0.1.0
  110 [COMPILING] foo v0.1.0 ([CWD])
  111 [FINISHED] [..]
  112 ",
  113         )
  114         .run();
  115 }
  116 
  117 #[cargo_test]
  118 fn simple_install() {
  119     setup();
  120 
  121     VendorPackage::new("foo")
  122         .file("src/lib.rs", "pub fn foo() {}")
  123         .build();
  124 
  125     VendorPackage::new("bar")
  126         .file(
  127             "Cargo.toml",
  128             r#"
  129                 [package]
  130                 name = "bar"
  131                 version = "0.1.0"
  132                 authors = []
  133 
  134                 [dependencies]
  135                 foo = "0.0.1"
  136             "#,
  137         )
  138         .file(
  139             "src/main.rs",
  140             "extern crate foo; pub fn main() { foo::foo(); }",
  141         )
  142         .build();
  143 
  144     cargo_process("install bar")
  145         .with_stderr(
  146             "\
  147 [INSTALLING] bar v0.1.0
  148 [COMPILING] foo v0.0.1
  149 [COMPILING] bar v0.1.0
  150 [FINISHED] release [optimized] target(s) in [..]s
  151 [INSTALLING] [..]bar[..]
  152 [INSTALLED] package `bar v0.1.0` (executable `bar[EXE]`)
  153 [WARNING] be sure to add `[..]` to your PATH to be able to run the installed binaries
  154 ",
  155         )
  156         .run();
  157 }
  158 
  159 #[cargo_test]
  160 fn simple_install_fail() {
  161     setup();
  162 
  163     VendorPackage::new("foo")
  164         .file("src/lib.rs", "pub fn foo() {}")
  165         .build();
  166 
  167     VendorPackage::new("bar")
  168         .file(
  169             "Cargo.toml",
  170             r#"
  171                 [package]
  172                 name = "bar"
  173                 version = "0.1.0"
  174                 authors = []
  175 
  176                 [dependencies]
  177                 foo = "0.1.0"
  178                 baz = "9.8.7"
  179             "#,
  180         )
  181         .file(
  182             "src/main.rs",
  183             "extern crate foo; pub fn main() { foo::foo(); }",
  184         )
  185         .build();
  186 
  187     cargo_process("install bar")
  188         .with_status(101)
  189         .with_stderr(
  190             "  Installing bar v0.1.0
  191 error: failed to compile `bar v0.1.0`, intermediate artifacts can be found at `[..]`
  192 
  193 Caused by:
  194   no matching package found
  195   searched package name: `baz`
  196   perhaps you meant:      bar or foo
  197   location searched: registry `crates-io`
  198   required by package `bar v0.1.0`
  199 ",
  200         )
  201         .run();
  202 }
  203 
  204 #[cargo_test]
  205 fn install_without_feature_dep() {
  206     setup();
  207 
  208     VendorPackage::new("foo")
  209         .file("src/lib.rs", "pub fn foo() {}")
  210         .build();
  211 
  212     VendorPackage::new("bar")
  213         .file(
  214             "Cargo.toml",
  215             r#"
  216                 [package]
  217                 name = "bar"
  218                 version = "0.1.0"
  219                 authors = []
  220 
  221                 [dependencies]
  222                 foo = "0.0.1"
  223                 baz = { version = "9.8.7", optional = true }
  224 
  225                 [features]
  226                 wantbaz = ["baz"]
  227             "#,
  228         )
  229         .file(
  230             "src/main.rs",
  231             "extern crate foo; pub fn main() { foo::foo(); }",
  232         )
  233         .build();
  234 
  235     cargo_process("install bar")
  236         .with_stderr(
  237             "\
  238 [INSTALLING] bar v0.1.0
  239 [COMPILING] foo v0.0.1
  240 [COMPILING] bar v0.1.0
  241 [FINISHED] release [optimized] target(s) in [..]s
  242 [INSTALLING] [..]bar[..]
  243 [INSTALLED] package `bar v0.1.0` (executable `bar[EXE]`)
  244 [WARNING] be sure to add `[..]` to your PATH to be able to run the installed binaries
  245 ",
  246         )
  247         .run();
  248 }
  249 
  250 #[cargo_test]
  251 fn not_there() {
  252     setup();
  253 
  254     let _ = project().at("index").build();
  255 
  256     let p = project()
  257         .file(
  258             "Cargo.toml",
  259             r#"
  260                 [package]
  261                 name = "foo"
  262                 version = "0.1.0"
  263                 authors = []
  264 
  265                 [dependencies]
  266                 bar = "0.1.0"
  267             "#,
  268         )
  269         .file(
  270             "src/lib.rs",
  271             "extern crate bar; pub fn foo() { bar::bar(); }",
  272         )
  273         .build();
  274 
  275     p.cargo("build")
  276         .with_status(101)
  277         .with_stderr(
  278             "\
  279 error: no matching package named `bar` found
  280 location searched: [..]
  281 required by package `foo v0.1.0 ([..])`
  282 ",
  283         )
  284         .run();
  285 }
  286 
  287 #[cargo_test]
  288 fn multiple() {
  289     setup();
  290 
  291     VendorPackage::new("bar-0.1.0")
  292         .file("Cargo.toml", &basic_manifest("bar", "0.1.0"))
  293         .file("src/lib.rs", "pub fn bar() {}")
  294         .file(".cargo-checksum", "")
  295         .build();
  296 
  297     VendorPackage::new("bar-0.2.0")
  298         .file("Cargo.toml", &basic_manifest("bar", "0.2.0"))
  299         .file("src/lib.rs", "pub fn bar() {}")
  300         .file(".cargo-checksum", "")
  301         .build();
  302 
  303     let p = project()
  304         .file(
  305             "Cargo.toml",
  306             r#"
  307                 [package]
  308                 name = "foo"
  309                 version = "0.1.0"
  310                 authors = []
  311 
  312                 [dependencies]
  313                 bar = "0.1.0"
  314             "#,
  315         )
  316         .file(
  317             "src/lib.rs",
  318             "extern crate bar; pub fn foo() { bar::bar(); }",
  319         )
  320         .build();
  321 
  322     p.cargo("build")
  323         .with_stderr(
  324             "\
  325 [COMPILING] bar v0.1.0
  326 [COMPILING] foo v0.1.0 ([CWD])
  327 [FINISHED] [..]
  328 ",
  329         )
  330         .run();
  331 }
  332 
  333 #[cargo_test]
  334 fn crates_io_then_directory() {
  335     let p = project()
  336         .file(
  337             "Cargo.toml",
  338             r#"
  339                 [package]
  340                 name = "foo"
  341                 version = "0.1.0"
  342                 authors = []
  343 
  344                 [dependencies]
  345                 bar = "0.1.0"
  346             "#,
  347         )
  348         .file(
  349             "src/lib.rs",
  350             "extern crate bar; pub fn foo() { bar::bar(); }",
  351         )
  352         .build();
  353 
  354     let cksum = Package::new("bar", "0.1.0")
  355         .file("src/lib.rs", "pub fn bar() -> u32 { 0 }")
  356         .publish();
  357 
  358     p.cargo("build")
  359         .with_stderr(
  360             "\
  361 [UPDATING] `[..]` index
  362 [DOWNLOADING] crates ...
  363 [DOWNLOADED] bar v0.1.0 ([..])
  364 [COMPILING] bar v0.1.0
  365 [COMPILING] foo v0.1.0 ([CWD])
  366 [FINISHED] [..]
  367 ",
  368         )
  369         .run();
  370 
  371     setup();
  372 
  373     let mut v = VendorPackage::new("bar");
  374     v.file("Cargo.toml", &basic_manifest("bar", "0.1.0"));
  375     v.file("src/lib.rs", "pub fn bar() -> u32 { 1 }");
  376     v.cksum.package = Some(cksum);
  377     v.build();
  378 
  379     p.cargo("build")
  380         .with_stderr(
  381             "\
  382 [COMPILING] bar v0.1.0
  383 [COMPILING] foo v0.1.0 ([CWD])
  384 [FINISHED] [..]
  385 ",
  386         )
  387         .run();
  388 }
  389 
  390 #[cargo_test]
  391 fn crates_io_then_bad_checksum() {
  392     let p = project()
  393         .file(
  394             "Cargo.toml",
  395             r#"
  396                 [package]
  397                 name = "foo"
  398                 version = "0.1.0"
  399                 authors = []
  400 
  401                 [dependencies]
  402                 bar = "0.1.0"
  403             "#,
  404         )
  405         .file("src/lib.rs", "")
  406         .build();
  407 
  408     Package::new("bar", "0.1.0").publish();
  409 
  410     p.cargo("build").run();
  411     setup();
  412 
  413     VendorPackage::new("bar")
  414         .file("Cargo.toml", &basic_manifest("bar", "0.1.0"))
  415         .file("src/lib.rs", "")
  416         .build();
  417 
  418     p.cargo("build")
  419         .with_status(101)
  420         .with_stderr(
  421             "\
  422 error: checksum for `bar v0.1.0` changed between lock files
  423 
  424 this could be indicative of a few possible errors:
  425 
  426     * the lock file is corrupt
  427     * a replacement source in use (e.g., a mirror) returned a different checksum
  428     * the source itself may be corrupt in one way or another
  429 
  430 unable to verify that `bar v0.1.0` is the same as when the lockfile was generated
  431 
  432 ",
  433         )
  434         .run();
  435 }
  436 
  437 #[cargo_test]
  438 fn bad_file_checksum() {
  439     setup();
  440 
  441     VendorPackage::new("bar")
  442         .file("Cargo.toml", &basic_manifest("bar", "0.1.0"))
  443         .file("src/lib.rs", "")
  444         .build();
  445 
  446     t!(fs::write(
  447         paths::root().join("index/bar/src/lib.rs"),
  448         "fn bar() -> u32 { 0 }"
  449     ));
  450 
  451     let p = project()
  452         .file(
  453             "Cargo.toml",
  454             r#"
  455                 [package]
  456                 name = "foo"
  457                 version = "0.1.0"
  458                 authors = []
  459 
  460                 [dependencies]
  461                 bar = "0.1.0"
  462             "#,
  463         )
  464         .file("src/lib.rs", "")
  465         .build();
  466 
  467     p.cargo("build")
  468         .with_status(101)
  469         .with_stderr(
  470             "\
  471 error: the listed checksum of `[..]lib.rs` has changed:
  472 expected: [..]
  473 actual:   [..]
  474 
  475 directory sources are not intended to be edited, if modifications are \
  476 required then it is recommended that `[patch]` is used with a forked copy of \
  477 the source
  478 ",
  479         )
  480         .run();
  481 }
  482 
  483 #[cargo_test]
  484 fn only_dot_files_ok() {
  485     setup();
  486 
  487     VendorPackage::new("bar")
  488         .file("Cargo.toml", &basic_manifest("bar", "0.1.0"))
  489         .file("src/lib.rs", "")
  490         .build();
  491     VendorPackage::new("foo")
  492         .no_manifest()
  493         .file(".bar", "")
  494         .build();
  495 
  496     let p = project()
  497         .file(
  498             "Cargo.toml",
  499             r#"
  500                 [package]
  501                 name = "foo"
  502                 version = "0.1.0"
  503                 authors = []
  504 
  505                 [dependencies]
  506                 bar = "0.1.0"
  507             "#,
  508         )
  509         .file("src/lib.rs", "")
  510         .build();
  511 
  512     p.cargo("build").run();
  513 }
  514 
  515 #[cargo_test]
  516 fn random_files_ok() {
  517     setup();
  518 
  519     VendorPackage::new("bar")
  520         .file("Cargo.toml", &basic_manifest("bar", "0.1.0"))
  521         .file("src/lib.rs", "")
  522         .build();
  523     VendorPackage::new("foo")
  524         .no_manifest()
  525         .file("bar", "")
  526         .file("../test", "")
  527         .build();
  528 
  529     let p = project()
  530         .file(
  531             "Cargo.toml",
  532             r#"
  533                 [package]
  534                 name = "foo"
  535                 version = "0.1.0"
  536                 authors = []
  537 
  538                 [dependencies]
  539                 bar = "0.1.0"
  540             "#,
  541         )
  542         .file("src/lib.rs", "")
  543         .build();
  544 
  545     p.cargo("build").run();
  546 }
  547 
  548 #[cargo_test]
  549 fn git_lock_file_doesnt_change() {
  550     let git = git::new("git", |p| {
  551         p.file("Cargo.toml", &basic_manifest("git", "0.5.0"))
  552             .file("src/lib.rs", "")
  553     });
  554 
  555     VendorPackage::new("git")
  556         .file("Cargo.toml", &basic_manifest("git", "0.5.0"))
  557         .file("src/lib.rs", "")
  558         .disable_checksum()
  559         .build();
  560 
  561     let p = project()
  562         .file(
  563             "Cargo.toml",
  564             &format!(
  565                 r#"
  566                     [package]
  567                     name = "foo"
  568                     version = "0.0.1"
  569                     authors = []
  570 
  571                     [dependencies]
  572                     git = {{ git = '{0}' }}
  573                 "#,
  574                 git.url()
  575             ),
  576         )
  577         .file("src/lib.rs", "")
  578         .build();
  579 
  580     p.cargo("build").run();
  581 
  582     let lock1 = p.read_lockfile();
  583 
  584     let root = paths::root();
  585     t!(fs::create_dir(&root.join(".cargo")));
  586     t!(fs::write(
  587         root.join(".cargo/config"),
  588         format!(
  589             r#"
  590                 [source.my-git-repo]
  591                 git = '{}'
  592                 replace-with = 'my-awesome-local-registry'
  593 
  594                 [source.my-awesome-local-registry]
  595                 directory = 'index'
  596             "#,
  597             git.url()
  598         )
  599     ));
  600 
  601     p.cargo("build")
  602         .with_stderr(
  603             "\
  604 [COMPILING] [..]
  605 [COMPILING] [..]
  606 [FINISHED] [..]
  607 ",
  608         )
  609         .run();
  610 
  611     let lock2 = p.read_lockfile();
  612     assert_eq!(lock1, lock2, "lock files changed");
  613 }
  614 
  615 #[cargo_test]
  616 fn git_override_requires_lockfile() {
  617     VendorPackage::new("git")
  618         .file("Cargo.toml", &basic_manifest("git", "0.5.0"))
  619         .file("src/lib.rs", "")
  620         .disable_checksum()
  621         .build();
  622 
  623     let p = project()
  624         .file(
  625             "Cargo.toml",
  626             r#"
  627                 [package]
  628                 name = "foo"
  629                 version = "0.0.1"
  630                 authors = []
  631 
  632                 [dependencies]
  633                 git = { git = 'https://example.com/' }
  634             "#,
  635         )
  636         .file("src/lib.rs", "")
  637         .build();
  638 
  639     let root = paths::root();
  640     t!(fs::create_dir(&root.join(".cargo")));
  641     t!(fs::write(
  642         root.join(".cargo/config"),
  643         r#"
  644             [source.my-git-repo]
  645             git = 'https://example.com/'
  646             replace-with = 'my-awesome-local-registry'
  647 
  648             [source.my-awesome-local-registry]
  649             directory = 'index'
  650         "#
  651     ));
  652 
  653     p.cargo("build")
  654         .with_status(101)
  655         .with_stderr(
  656             "\
  657 [ERROR] failed to get `git` as a dependency of package `foo v0.0.1 ([..])`
  658 
  659 Caused by:
  660   failed to load source for dependency `git`
  661 
  662 Caused by:
  663   Unable to update [..]
  664 
  665 Caused by:
  666   the source my-git-repo requires a lock file to be present first before it can be
  667   used against vendored source code
  668 
  669   remove the source replacement configuration, generate a lock file, and then
  670   restore the source replacement configuration to continue the build
  671 ",
  672         )
  673         .run();
  674 }
  675 
  676 #[cargo_test]
  677 fn workspace_different_locations() {
  678     let p = project()
  679         .no_manifest()
  680         .file(
  681             "foo/Cargo.toml",
  682             r#"
  683                 [package]
  684                 name = 'foo'
  685                 version = '0.1.0'
  686 
  687                 [dependencies]
  688                 baz = "*"
  689             "#,
  690         )
  691         .file("foo/src/lib.rs", "")
  692         .file("foo/vendor/baz/Cargo.toml", &basic_manifest("baz", "0.1.0"))
  693         .file("foo/vendor/baz/src/lib.rs", "")
  694         .file("foo/vendor/baz/.cargo-checksum.json", "{\"files\":{}}")
  695         .file(
  696             "bar/Cargo.toml",
  697             r#"
  698                 [package]
  699                 name = 'bar'
  700                 version = '0.1.0'
  701 
  702                 [dependencies]
  703                 baz = "*"
  704             "#,
  705         )
  706         .file("bar/src/lib.rs", "")
  707         .file(
  708             ".cargo/config",
  709             r#"
  710                 [build]
  711                 target-dir = './target'
  712 
  713                 [source.crates-io]
  714                 replace-with = 'my-awesome-local-registry'
  715 
  716                 [source.my-awesome-local-registry]
  717                 directory = 'foo/vendor'
  718             "#,
  719         )
  720         .build();
  721 
  722     p.cargo("build").cwd("foo").run();
  723     p.cargo("build")
  724         .cwd("bar")
  725         .with_stderr(
  726             "\
  727 [COMPILING] bar [..]
  728 [FINISHED] [..]
  729 ",
  730         )
  731         .run();
  732 }
  733 
  734 #[cargo_test]
  735 fn version_missing() {
  736     setup();
  737 
  738     VendorPackage::new("foo")
  739         .file("src/lib.rs", "pub fn foo() {}")
  740         .build();
  741 
  742     VendorPackage::new("bar")
  743         .file(
  744             "Cargo.toml",
  745             r#"
  746                 [package]
  747                 name = "bar"
  748                 version = "0.1.0"
  749                 authors = []
  750 
  751                 [dependencies]
  752                 foo = "2"
  753             "#,
  754         )
  755         .file("src/main.rs", "fn main() {}")
  756         .build();
  757 
  758     cargo_process("install bar")
  759         .with_stderr(
  760             "\
  761 [INSTALLING] bar v0.1.0
  762 error: failed to compile [..]
  763 
  764 Caused by:
  765   failed to select a version for the requirement `foo = \"^2\"`
  766   candidate versions found which didn't match: 0.0.1
  767   location searched: directory source `[..] (which is replacing registry `[..]`)
  768   required by package `bar v0.1.0`
  769   perhaps a crate was updated and forgotten to be re-vendored?
  770 ",
  771         )
  772         .with_status(101)
  773         .run();
  774 }