1+ //! Interact with the [`TestRegistry`]
2+ //!
3+ //! # Example
4+ //!
5+ //! ```no_run
6+ //! use cargo_test_support::registry::Package;
7+ //! use cargo_test_support::project;
8+ //!
9+ //! // Publish package "a" depending on "b".
10+ //! Package::new("a", "1.0.0")
11+ //! .dep("b", "1.0.0")
12+ //! .file("src/lib.rs", r#"
13+ //! extern crate b;
14+ //! pub fn f() -> i32 { b::f() * 2 }
15+ //! "#)
16+ //! .publish();
17+ //!
18+ //! // Publish package "b".
19+ //! Package::new("b", "1.0.0")
20+ //! .file("src/lib.rs", r#"
21+ //! pub fn f() -> i32 { 12 }
22+ //! "#)
23+ //! .publish();
24+ //!
25+ //! // Create a project that uses package "a".
26+ //! let p = project()
27+ //! .file("Cargo.toml", r#"
28+ //! [package]
29+ //! name = "foo"
30+ //! version = "0.0.1"
31+ //!
32+ //! [dependencies]
33+ //! a = "1.0"
34+ //! "#)
35+ //! .file("src/main.rs", r#"
36+ //! extern crate a;
37+ //! fn main() { println!("{}", a::f()); }
38+ //! "#)
39+ //! .build();
40+ //!
41+ //! p.cargo("run").with_stdout("24").run();
42+ //! ```
43+
144use crate :: git:: repo;
245use crate :: paths;
346use crate :: publish:: { create_index_line, write_to_index} ;
@@ -20,46 +63,72 @@ use time::format_description::well_known::Rfc3339;
2063use time:: { Duration , OffsetDateTime } ;
2164use url:: Url ;
2265
23- /// Gets the path to the local index pretending to be crates.io. This is a Git repo
66+ /// Path to the local index for psuedo-crates.io.
67+ ///
68+ /// This is a Git repo
2469/// initialized with a `config.json` file pointing to `dl_path` for downloads
2570/// and `api_path` for uploads.
71+ ///
72+ /// ex: `$CARGO_TARGET_TMPDIR/cit/t0/registry`
2673pub fn registry_path ( ) -> PathBuf {
2774 generate_path ( "registry" )
2875}
29- /// Gets the path for local web API uploads. Cargo will place the contents of a web API
76+
77+ /// Path to the local web API uploads
78+ ///
79+ /// Cargo will place the contents of a web API
3080/// request here. For example, `api/v1/crates/new` is the result of publishing a crate.
81+ ///
82+ /// ex: `$CARGO_TARGET_TMPDIR/cit/t0/api`
3183pub fn api_path ( ) -> PathBuf {
3284 generate_path ( "api" )
3385}
34- /// Gets the path where crates can be downloaded using the web API endpoint. Crates
86+
87+ /// Path to download `.crate` files using the web API endpoint.
88+ ///
89+ /// Crates
3590/// should be organized as `{name}/{version}/download` to match the web API
3691/// endpoint. This is rarely used and must be manually set up.
37- fn dl_path ( ) -> PathBuf {
92+ ///
93+ /// ex: `$CARGO_TARGET_TMPDIR/cit/t0/dl`
94+ pub fn dl_path ( ) -> PathBuf {
3895 generate_path ( "dl" )
3996}
40- /// Gets the alternative-registry version of `registry_path`.
41- fn alt_registry_path ( ) -> PathBuf {
97+
98+ /// Path to the alternative-registry version of [`registry_path`]
99+ ///
100+ /// ex: `$CARGO_TARGET_TMPDIR/cit/t0/alternative-registry`
101+ pub fn alt_registry_path ( ) -> PathBuf {
42102 generate_path ( "alternative-registry" )
43103}
44- /// Gets the alternative-registry version of `registry_url`.
104+
105+ /// URL to the alternative-registry version of `registry_url`
45106fn alt_registry_url ( ) -> Url {
46107 generate_url ( "alternative-registry" )
47108}
48- /// Gets the alternative-registry version of `dl_path`.
109+
110+ /// Path to the alternative-registry version of [`dl_path`]
111+ ///
112+ /// ex: `$CARGO_TARGET_TMPDIR/cit/t0/alternative-dl`
49113pub fn alt_dl_path ( ) -> PathBuf {
50114 generate_path ( "alternative-dl" )
51115}
52- /// Gets the alternative-registry version of `api_path`.
116+
117+ /// Path to the alternative-registry version of [`api_path`]
118+ ///
119+ /// ex: `$CARGO_TARGET_TMPDIR/cit/t0/alternative-api`
53120pub fn alt_api_path ( ) -> PathBuf {
54121 generate_path ( "alternative-api" )
55122}
123+
56124fn generate_path ( name : & str ) -> PathBuf {
57125 paths:: root ( ) . join ( name)
58126}
59127fn generate_url ( name : & str ) -> Url {
60128 Url :: from_file_path ( generate_path ( name) ) . ok ( ) . unwrap ( )
61129}
62130
131+ /// Auth-token for publishing, see [`RegistryBuilder::token`]
63132#[ derive( Clone ) ]
64133pub enum Token {
65134 Plaintext ( String ) ,
@@ -68,6 +137,7 @@ pub enum Token {
68137
69138impl Token {
70139 /// This is a valid PASETO secret key.
140+ ///
71141 /// This one is already publicly available as part of the text of the RFC so is safe to use for tests.
72142 pub fn rfc_key ( ) -> Token {
73143 Token :: Keys (
@@ -80,7 +150,9 @@ impl Token {
80150
81151type RequestCallback = Box < dyn Send + Fn ( & Request , & HttpServer ) -> Response > ;
82152
83- /// A builder for initializing registries.
153+ /// Prepare a local [`TestRegistry`] fixture
154+ ///
155+ /// See also [`init`] and [`alt_init`]
84156pub struct RegistryBuilder {
85157 /// If set, configures an alternate registry with the given name.
86158 alternative : Option < String > ,
@@ -108,6 +180,9 @@ pub struct RegistryBuilder {
108180 credential_provider : Option < String > ,
109181}
110182
183+ /// A local registry fixture
184+ ///
185+ /// Most tests won't need to call this directly but instead interact with [`Package`]
111186pub struct TestRegistry {
112187 server : Option < HttpServerHandle > ,
113188 index_url : Url ,
@@ -459,71 +534,31 @@ impl RegistryBuilder {
459534 }
460535}
461536
462- /// A builder for creating a new package in a registry.
537+ /// Published package builder for [`TestRegistry`]
463538///
464539/// This uses "source replacement" using an automatically generated
465540/// `.cargo/config` file to ensure that dependencies will use these packages
466541/// instead of contacting crates.io. See `source-replacement.md` for more
467542/// details on how source replacement works.
468543///
469- /// Call ` publish` to finalize and create the package.
544+ /// Call [`Package:: publish`] to finalize and create the package.
470545///
471546/// If no files are specified, an empty `lib.rs` file is automatically created.
472547///
473548/// The `Cargo.toml` file is automatically generated based on the methods
474- /// called on `Package` (for example, calling ` dep()` will add to the
549+ /// called on `Package` (for example, calling [`Package:: dep()`] will add to the
475550/// `[dependencies]` automatically). You may also specify a `Cargo.toml` file
476551/// to override the generated one.
477552///
478553/// This supports different registry types:
479554/// - Regular source replacement that replaces `crates.io` (the default).
480555/// - A "local registry" which is a subset for vendoring (see
481- /// `Package::local`).
556+ /// [ `Package::local`] ).
482557/// - An "alternative registry" which requires specifying the registry name
483- /// (see `Package::alternative`).
558+ /// (see [ `Package::alternative`] ).
484559///
485560/// This does not support "directory sources". See `directory.rs` for
486561/// `VendorPackage` which implements directory sources.
487- ///
488- /// # Example
489- /// ```no_run
490- /// use cargo_test_support::registry::Package;
491- /// use cargo_test_support::project;
492- ///
493- /// // Publish package "a" depending on "b".
494- /// Package::new("a", "1.0.0")
495- /// .dep("b", "1.0.0")
496- /// .file("src/lib.rs", r#"
497- /// extern crate b;
498- /// pub fn f() -> i32 { b::f() * 2 }
499- /// "#)
500- /// .publish();
501- ///
502- /// // Publish package "b".
503- /// Package::new("b", "1.0.0")
504- /// .file("src/lib.rs", r#"
505- /// pub fn f() -> i32 { 12 }
506- /// "#)
507- /// .publish();
508- ///
509- /// // Create a project that uses package "a".
510- /// let p = project()
511- /// .file("Cargo.toml", r#"
512- /// [package]
513- /// name = "foo"
514- /// version = "0.0.1"
515- ///
516- /// [dependencies]
517- /// a = "1.0"
518- /// "#)
519- /// .file("src/main.rs", r#"
520- /// extern crate a;
521- /// fn main() { println!("{}", a::f()); }
522- /// "#)
523- /// .build();
524- ///
525- /// p.cargo("run").with_stdout("24").run();
526- /// ```
527562#[ must_use]
528563pub struct Package {
529564 name : String ,
@@ -544,6 +579,7 @@ pub struct Package {
544579
545580pub ( crate ) type FeatureMap = BTreeMap < String , Vec < String > > ;
546581
582+ /// Published package dependency builder, see [`Package::add_dep`]
547583#[ derive( Clone ) ]
548584pub struct Dependency {
549585 name : String ,
@@ -582,14 +618,18 @@ struct PackageFile {
582618
583619const DEFAULT_MODE : u32 = 0o644 ;
584620
585- /// Initializes the on-disk registry and sets up the config so that crates.io
586- /// is replaced with the one on disk.
621+ /// Setup a local psuedo-crates.io [`TestRegistry`]
622+ ///
623+ /// This is implicitly called by [`Package::new`].
624+ ///
625+ /// When calling `cargo publish`, see instead [`crate::publish`].
587626pub fn init ( ) -> TestRegistry {
588627 RegistryBuilder :: new ( ) . build ( )
589628}
590629
591- /// Variant of `init` that initializes the "alternative" registry and crates.io
592- /// replacement.
630+ /// Setup a local "alternative" [`TestRegistry`]
631+ ///
632+ /// When calling `cargo publish`, see instead [`crate::publish`].
593633pub fn alt_init ( ) -> TestRegistry {
594634 init ( ) ;
595635 RegistryBuilder :: new ( ) . alternative ( ) . build ( )
@@ -1234,6 +1274,8 @@ impl Package {
12341274 /// See `src/doc/src/reference/registries.md` for more details on
12351275 /// alternative registries. See `alt_registry.rs` for the tests that use
12361276 /// this.
1277+ ///
1278+ /// **Requires:** [`alt_init`]
12371279 pub fn alternative ( & mut self , alternative : bool ) -> & mut Package {
12381280 self . alternative = alternative;
12391281 self
@@ -1666,6 +1708,7 @@ impl Package {
16661708 }
16671709}
16681710
1711+ /// Generate a checksum
16691712pub fn cksum ( s : & [ u8 ] ) -> String {
16701713 Sha256 :: new ( ) . update ( s) . finish_hex ( )
16711714}
0 commit comments