@@ -58,28 +58,30 @@ pub fn version() -> VersionInfo {
58
58
} ;
59
59
}
60
60
61
- // This is the version set in bootstrap, which we use to match rustc.
62
- let version = option_env_str ! ( "CFG_RELEASE" ) . unwrap_or_else ( || {
63
- // If cargo is not being built by bootstrap, then we just use the
64
- // version from cargo's own `Cargo.toml`.
65
- //
66
- // There are two versions at play here:
67
- // - version of cargo-the-binary, which you see when you type `cargo --version`
68
- // - version of cargo-the-library, which you download from crates.io for use
69
- // in your packages.
70
- //
71
- // The library is permanently unstable, so it always has a 0 major
72
- // version. However, the CLI now reports a stable 1.x version
73
- // (starting in 1.26) which stays in sync with rustc's version.
74
- //
75
- // Coincidentally, the minor version for cargo-the-library is always
76
- // +1 of rustc's minor version (that is, `rustc 1.11.0` corresponds to
77
- // `cargo `0.12.0`). The versions always get bumped in lockstep, so
78
- // this should continue to hold.
79
- let minor = env ! ( "CARGO_PKG_VERSION_MINOR" ) . parse :: < u8 > ( ) . unwrap ( ) - 1 ;
80
- let patch = env ! ( "CARGO_PKG_VERSION_PATCH" ) . parse :: < u8 > ( ) . unwrap ( ) ;
81
- format ! ( "1.{}.{}" , minor, patch)
82
- } ) ;
61
+ let version = version_for_testing ( )
62
+ // This is the version set in bootstrap, which we use to match rustc.
63
+ . or_else ( || option_env_str ! ( "CFG_RELEASE" ) )
64
+ . unwrap_or_else ( || {
65
+ // If cargo is not being built by bootstrap, then we just use the
66
+ // version from cargo's own `Cargo.toml`.
67
+ //
68
+ // There are two versions at play here:
69
+ // - version of cargo-the-binary, which you see when you type `cargo --version`
70
+ // - version of cargo-the-library, which you download from crates.io for use
71
+ // in your packages.
72
+ //
73
+ // The library is permanently unstable, so it always has a 0 major
74
+ // version. However, the CLI now reports a stable 1.x version
75
+ // (starting in 1.26) which stays in sync with rustc's version.
76
+ //
77
+ // Coincidentally, the minor version for cargo-the-library is always
78
+ // +1 of rustc's minor version (that is, `rustc 1.11.0` corresponds to
79
+ // `cargo `0.12.0`). The versions always get bumped in lockstep, so
80
+ // this should continue to hold.
81
+ let minor = env ! ( "CARGO_PKG_VERSION_MINOR" ) . parse :: < u8 > ( ) . unwrap ( ) - 1 ;
82
+ let patch = env ! ( "CARGO_PKG_VERSION_PATCH" ) . parse :: < u8 > ( ) . unwrap ( ) ;
83
+ format ! ( "1.{}.{}" , minor, patch)
84
+ } ) ;
83
85
84
86
let release_channel = option_env_str ! ( "CFG_RELEASE_CHANNEL" ) ;
85
87
let commit_info = option_env_str ! ( "CARGO_COMMIT_HASH" ) . map ( |commit_hash| CommitInfo {
@@ -96,3 +98,11 @@ pub fn version() -> VersionInfo {
96
98
description,
97
99
}
98
100
}
101
+
102
+ /// Provides a way for tests to inject an abitrary version for testing purposes.
103
+ ///
104
+ // __CARGO_TEST_CARGO_VERSION should not be relied on outside of tests.
105
+ #[ allow( clippy:: disallowed_methods) ]
106
+ fn version_for_testing ( ) -> Option < String > {
107
+ std:: env:: var ( "__CARGO_TEST_CARGO_VERSION" ) . ok ( )
108
+ }
0 commit comments