]> git.proxmox.com Git - cargo.git/blob - tests/testsuite/rust_version.rs
Add RegistryBuilder to help initializing test registries.
[cargo.git] / tests / testsuite / rust_version.rs
1 //! Tests for targets with `rust-version`.
2
3 use cargo_test_support::{project, registry::Package};
4
5 #[cargo_test]
6 fn rust_version_gated() {
7 project()
8 .file(
9 "Cargo.toml",
10 r#"
11 [package]
12 name = "foo"
13 version = "0.0.1"
14 rust-version = "1.9999"
15 "#,
16 )
17 .file("src/lib.rs", "")
18 .build()
19 .cargo("build")
20 .masquerade_as_nightly_cargo()
21 .with_stderr_contains(
22 "warning: `rust-version` is not supported on this version of Cargo and will be ignored\
23 \n\nconsider adding `cargo-features = [\"rust-version\"]` to the manifest",
24 )
25 .run();
26
27 project()
28 .file(
29 "Cargo.toml",
30 r#"
31 [package]
32 name = "foo"
33 version = "0.0.1"
34 rust-version = "1.9999"
35 "#,
36 )
37 .file("src/lib.rs", "")
38 .build()
39 .cargo("build")
40 .with_stderr_contains(
41 "warning: `rust-version` is not supported on this version of Cargo and will be ignored\
42 \n\nthis Cargo does not support nightly features, but if you\n\
43 switch to nightly channel you can add\n\
44 `cargo-features = [\"rust-version\"]` to enable this feature",
45 )
46 .run();
47 }
48
49 #[cargo_test]
50 fn rust_version_satisfied() {
51 let p = project()
52 .file(
53 "Cargo.toml",
54 r#"
55 cargo-features = ["rust-version"]
56
57 [project]
58 name = "foo"
59 version = "0.0.1"
60 authors = []
61 rust-version = "1.1.1"
62 [[bin]]
63 name = "foo"
64 "#,
65 )
66 .file("src/main.rs", "fn main() {}")
67 .build();
68
69 p.cargo("build").masquerade_as_nightly_cargo().run();
70 p.cargo("build --ignore-rust-version -Zunstable-options")
71 .masquerade_as_nightly_cargo()
72 .run();
73 }
74
75 #[cargo_test]
76 fn rust_version_bad_caret() {
77 project()
78 .file(
79 "Cargo.toml",
80 r#"
81 cargo-features = ["rust-version"]
82
83 [project]
84 name = "foo"
85 version = "0.0.1"
86 authors = []
87 rust-version = "^1.43"
88 [[bin]]
89 name = "foo"
90 "#,
91 )
92 .file("src/main.rs", "fn main() {}")
93 .build()
94 .cargo("build")
95 .masquerade_as_nightly_cargo()
96 .with_status(101)
97 .with_stderr(
98 "error: failed to parse manifest at `[..]`\n\n\
99 Caused by:\n `rust-version` must be a value like \"1.32\"",
100 )
101 .run();
102 }
103
104 #[cargo_test]
105 fn rust_version_bad_pre_release() {
106 project()
107 .file(
108 "Cargo.toml",
109 r#"
110 cargo-features = ["rust-version"]
111
112 [project]
113 name = "foo"
114 version = "0.0.1"
115 authors = []
116 rust-version = "1.43-beta.1"
117 [[bin]]
118 name = "foo"
119 "#,
120 )
121 .file("src/main.rs", "fn main() {}")
122 .build()
123 .cargo("build")
124 .masquerade_as_nightly_cargo()
125 .with_status(101)
126 .with_stderr(
127 "error: failed to parse manifest at `[..]`\n\n\
128 Caused by:\n `rust-version` must be a value like \"1.32\"",
129 )
130 .run();
131 }
132
133 #[cargo_test]
134 fn rust_version_bad_nonsense() {
135 project()
136 .file(
137 "Cargo.toml",
138 r#"
139 cargo-features = ["rust-version"]
140
141 [project]
142 name = "foo"
143 version = "0.0.1"
144 authors = []
145 rust-version = "foodaddle"
146 [[bin]]
147 name = "foo"
148 "#,
149 )
150 .file("src/main.rs", "fn main() {}")
151 .build()
152 .cargo("build")
153 .masquerade_as_nightly_cargo()
154 .with_status(101)
155 .with_stderr(
156 "error: failed to parse manifest at `[..]`\n\n\
157 Caused by:\n `rust-version` must be a value like \"1.32\"",
158 )
159 .run();
160 }
161
162 #[cargo_test]
163 fn rust_version_too_high() {
164 let p = project()
165 .file(
166 "Cargo.toml",
167 r#"
168 cargo-features = ["rust-version"]
169
170 [project]
171 name = "foo"
172 version = "0.0.1"
173 authors = []
174 rust-version = "1.9876.0"
175 [[bin]]
176 name = "foo"
177 "#,
178 )
179 .file("src/main.rs", "fn main() {}")
180 .build();
181
182 p.cargo("build")
183 .masquerade_as_nightly_cargo()
184 .with_status(101)
185 .with_stderr(
186 "error: package `foo v0.0.1 ([..])` cannot be built because it requires \
187 rustc 1.9876.0 or newer, while the currently active rustc version is [..]",
188 )
189 .run();
190 p.cargo("build --ignore-rust-version -Zunstable-options")
191 .masquerade_as_nightly_cargo()
192 .run();
193 }
194
195 #[cargo_test]
196 fn rust_version_dependency_fails() {
197 Package::new("bar", "0.0.1")
198 .cargo_feature("rust-version")
199 .rust_version("1.2345.0")
200 .file("src/lib.rs", "fn other_stuff() {}")
201 .publish();
202
203 let p = project()
204 .file(
205 "Cargo.toml",
206 r#"
207 [package]
208 name = "foo"
209 version = "0.0.1"
210 authors = []
211 [dependencies]
212 bar = "0.0.1"
213 "#,
214 )
215 .file("src/main.rs", "fn main(){}")
216 .build();
217
218 p.cargo("build")
219 .masquerade_as_nightly_cargo()
220 .with_status(101)
221 .with_stderr(
222 " Updating `[..]` index\n \
223 Downloading crates ...\n \
224 Downloaded bar v0.0.1 (registry `[..]`)\n\
225 error: package `bar v0.0.1` cannot be built because it requires \
226 rustc 1.2345.0 or newer, while the currently active rustc version is [..]",
227 )
228 .run();
229 p.cargo("build --ignore-rust-version -Zunstable-options")
230 .masquerade_as_nightly_cargo()
231 .run();
232 }
233
234 #[cargo_test]
235 fn rust_version_older_than_edition() {
236 project()
237 .file(
238 "Cargo.toml",
239 r#"
240 cargo-features = ["rust-version"]
241
242 [project]
243 name = "foo"
244 version = "0.0.1"
245 authors = []
246 rust-version = "1.1"
247 edition = "2018"
248 [[bin]]
249 name = "foo"
250 "#,
251 )
252 .file("src/main.rs", "fn main() {}")
253 .build()
254 .cargo("build")
255 .masquerade_as_nightly_cargo()
256 .with_status(101)
257 .with_stderr_contains(" rust-version 1.1 is older than first version (1.31.0) required by the specified edition (2018)",
258 )
259 .run();
260 }