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
+
1
44
use crate :: git:: repo;
2
45
use crate :: paths;
3
46
use crate :: publish:: { create_index_line, write_to_index} ;
@@ -20,46 +63,72 @@ use time::format_description::well_known::Rfc3339;
20
63
use time:: { Duration , OffsetDateTime } ;
21
64
use url:: Url ;
22
65
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
24
69
/// initialized with a `config.json` file pointing to `dl_path` for downloads
25
70
/// and `api_path` for uploads.
71
+ ///
72
+ /// ex: `$CARGO_TARGET_TMPDIR/cit/t0/registry`
26
73
pub fn registry_path ( ) -> PathBuf {
27
74
generate_path ( "registry" )
28
75
}
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
30
80
/// request here. For example, `api/v1/crates/new` is the result of publishing a crate.
81
+ ///
82
+ /// ex: `$CARGO_TARGET_TMPDIR/cit/t0/api`
31
83
pub fn api_path ( ) -> PathBuf {
32
84
generate_path ( "api" )
33
85
}
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
35
90
/// should be organized as `{name}/{version}/download` to match the web API
36
91
/// 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 {
38
95
generate_path ( "dl" )
39
96
}
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 {
42
102
generate_path ( "alternative-registry" )
43
103
}
44
- /// Gets the alternative-registry version of `registry_url`.
104
+
105
+ /// URL to the alternative-registry version of `registry_url`
45
106
fn alt_registry_url ( ) -> Url {
46
107
generate_url ( "alternative-registry" )
47
108
}
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`
49
113
pub fn alt_dl_path ( ) -> PathBuf {
50
114
generate_path ( "alternative-dl" )
51
115
}
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`
53
120
pub fn alt_api_path ( ) -> PathBuf {
54
121
generate_path ( "alternative-api" )
55
122
}
123
+
56
124
fn generate_path ( name : & str ) -> PathBuf {
57
125
paths:: root ( ) . join ( name)
58
126
}
59
127
fn generate_url ( name : & str ) -> Url {
60
128
Url :: from_file_path ( generate_path ( name) ) . ok ( ) . unwrap ( )
61
129
}
62
130
131
+ /// Auth-token for publishing, see [`RegistryBuilder::token`]
63
132
#[ derive( Clone ) ]
64
133
pub enum Token {
65
134
Plaintext ( String ) ,
@@ -68,6 +137,7 @@ pub enum Token {
68
137
69
138
impl Token {
70
139
/// This is a valid PASETO secret key.
140
+ ///
71
141
/// This one is already publicly available as part of the text of the RFC so is safe to use for tests.
72
142
pub fn rfc_key ( ) -> Token {
73
143
Token :: Keys (
@@ -80,7 +150,9 @@ impl Token {
80
150
81
151
type RequestCallback = Box < dyn Send + Fn ( & Request , & HttpServer ) -> Response > ;
82
152
83
- /// A builder for initializing registries.
153
+ /// Prepare a local [`TestRegistry`] fixture
154
+ ///
155
+ /// See also [`init`] and [`alt_init`]
84
156
pub struct RegistryBuilder {
85
157
/// If set, configures an alternate registry with the given name.
86
158
alternative : Option < String > ,
@@ -108,6 +180,9 @@ pub struct RegistryBuilder {
108
180
credential_provider : Option < String > ,
109
181
}
110
182
183
+ /// A local registry fixture
184
+ ///
185
+ /// Most tests won't need to call this directly but instead interact with [`Package`]
111
186
pub struct TestRegistry {
112
187
server : Option < HttpServerHandle > ,
113
188
index_url : Url ,
@@ -459,71 +534,31 @@ impl RegistryBuilder {
459
534
}
460
535
}
461
536
462
- /// A builder for creating a new package in a registry.
537
+ /// Published package builder for [`TestRegistry`]
463
538
///
464
539
/// This uses "source replacement" using an automatically generated
465
540
/// `.cargo/config` file to ensure that dependencies will use these packages
466
541
/// instead of contacting crates.io. See `source-replacement.md` for more
467
542
/// details on how source replacement works.
468
543
///
469
- /// Call ` publish` to finalize and create the package.
544
+ /// Call [`Package:: publish`] to finalize and create the package.
470
545
///
471
546
/// If no files are specified, an empty `lib.rs` file is automatically created.
472
547
///
473
548
/// 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
475
550
/// `[dependencies]` automatically). You may also specify a `Cargo.toml` file
476
551
/// to override the generated one.
477
552
///
478
553
/// This supports different registry types:
479
554
/// - Regular source replacement that replaces `crates.io` (the default).
480
555
/// - A "local registry" which is a subset for vendoring (see
481
- /// `Package::local`).
556
+ /// [ `Package::local`] ).
482
557
/// - An "alternative registry" which requires specifying the registry name
483
- /// (see `Package::alternative`).
558
+ /// (see [ `Package::alternative`] ).
484
559
///
485
560
/// This does not support "directory sources". See `directory.rs` for
486
561
/// `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
- /// ```
527
562
#[ must_use]
528
563
pub struct Package {
529
564
name : String ,
@@ -544,6 +579,7 @@ pub struct Package {
544
579
545
580
pub ( crate ) type FeatureMap = BTreeMap < String , Vec < String > > ;
546
581
582
+ /// Published package dependency builder, see [`Package::add_dep`]
547
583
#[ derive( Clone ) ]
548
584
pub struct Dependency {
549
585
name : String ,
@@ -582,14 +618,18 @@ struct PackageFile {
582
618
583
619
const DEFAULT_MODE : u32 = 0o644 ;
584
620
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`].
587
626
pub fn init ( ) -> TestRegistry {
588
627
RegistryBuilder :: new ( ) . build ( )
589
628
}
590
629
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`].
593
633
pub fn alt_init ( ) -> TestRegistry {
594
634
init ( ) ;
595
635
RegistryBuilder :: new ( ) . alternative ( ) . build ( )
@@ -1234,6 +1274,8 @@ impl Package {
1234
1274
/// See `src/doc/src/reference/registries.md` for more details on
1235
1275
/// alternative registries. See `alt_registry.rs` for the tests that use
1236
1276
/// this.
1277
+ ///
1278
+ /// **Requires:** [`alt_init`]
1237
1279
pub fn alternative ( & mut self , alternative : bool ) -> & mut Package {
1238
1280
self . alternative = alternative;
1239
1281
self
@@ -1666,6 +1708,7 @@ impl Package {
1666
1708
}
1667
1709
}
1668
1710
1711
+ /// Generate a checksum
1669
1712
pub fn cksum ( s : & [ u8 ] ) -> String {
1670
1713
Sha256 :: new ( ) . update ( s) . finish_hex ( )
1671
1714
}
0 commit comments