/tests/test_bad_config.rs

https://gitlab.com/alx741/cargo · Rust · 456 lines · 424 code · 32 blank · 0 comment · 0 complexity · dc9570a2d23d1b459882c50b712c544f MD5 · raw file

  1. use support::{project, execs};
  2. use support::registry::Package;
  3. use hamcrest::assert_that;
  4. fn setup() {}
  5. test!(bad1 {
  6. let foo = project("foo")
  7. .file("Cargo.toml", r#"
  8. [package]
  9. name = "foo"
  10. version = "0.0.0"
  11. authors = []
  12. "#)
  13. .file("src/lib.rs", "")
  14. .file(".cargo/config", r#"
  15. [target]
  16. nonexistent-target = "foo"
  17. "#);
  18. assert_that(foo.cargo_process("build").arg("-v")
  19. .arg("--target=nonexistent-target"),
  20. execs().with_status(101).with_stderr("\
  21. [ERROR] expected table for configuration key `target.nonexistent-target`, \
  22. but found string in [..]config
  23. "));
  24. });
  25. test!(bad2 {
  26. let foo = project("foo")
  27. .file("Cargo.toml", r#"
  28. [package]
  29. name = "foo"
  30. version = "0.0.0"
  31. authors = []
  32. "#)
  33. .file("src/lib.rs", "")
  34. .file(".cargo/config", r#"
  35. [http]
  36. proxy = 3.0
  37. "#);
  38. assert_that(foo.cargo_process("publish").arg("-v"),
  39. execs().with_status(101).with_stderr("\
  40. [ERROR] Couldn't load Cargo configuration
  41. Caused by:
  42. failed to load TOML configuration from `[..]config`
  43. Caused by:
  44. failed to parse key `http`
  45. Caused by:
  46. failed to parse key `proxy`
  47. Caused by:
  48. found TOML configuration value of unknown type `float`
  49. "));
  50. });
  51. test!(bad3 {
  52. let foo = project("foo")
  53. .file("Cargo.toml", r#"
  54. [package]
  55. name = "foo"
  56. version = "0.0.0"
  57. authors = []
  58. "#)
  59. .file("src/lib.rs", "")
  60. .file(".cargo/config", r#"
  61. [http]
  62. proxy = true
  63. "#);
  64. assert_that(foo.cargo_process("publish").arg("-v"),
  65. execs().with_status(101).with_stderr("\
  66. [ERROR] invalid configuration for key `http.proxy`
  67. expected a string, but found a boolean in [..]config
  68. "));
  69. });
  70. test!(bad4 {
  71. let foo = project("foo")
  72. .file(".cargo/config", r#"
  73. [cargo-new]
  74. name = false
  75. "#);
  76. assert_that(foo.cargo_process("new").arg("-v").arg("foo"),
  77. execs().with_status(101).with_stderr("\
  78. [ERROR] Failed to create project `foo` at `[..]`
  79. Caused by:
  80. invalid configuration for key `cargo-new.name`
  81. expected a string, but found a boolean in [..]config
  82. "));
  83. });
  84. test!(bad5 {
  85. let foo = project("foo")
  86. .file(".cargo/config", r#"
  87. foo = ""
  88. "#)
  89. .file("foo/.cargo/config", r#"
  90. foo = 2
  91. "#);
  92. foo.build();
  93. assert_that(foo.cargo("new")
  94. .arg("-v").arg("foo").cwd(&foo.root().join("foo")),
  95. execs().with_status(101).with_stderr("\
  96. [ERROR] Couldn't load Cargo configuration
  97. Caused by:
  98. failed to merge key `foo` between files:
  99. file 1: [..]foo[..]foo[..]config
  100. file 2: [..]foo[..]config
  101. Caused by:
  102. expected integer, but found string
  103. "));
  104. });
  105. test!(bad_cargo_config_jobs {
  106. let foo = project("foo")
  107. .file("Cargo.toml", r#"
  108. [package]
  109. name = "foo"
  110. version = "0.0.0"
  111. authors = []
  112. "#)
  113. .file("src/lib.rs", "")
  114. .file(".cargo/config", r#"
  115. [build]
  116. jobs = -1
  117. "#);
  118. assert_that(foo.cargo_process("build").arg("-v"),
  119. execs().with_status(101).with_stderr("\
  120. [ERROR] build.jobs must be positive, but found -1 in [..]
  121. "));
  122. });
  123. test!(default_cargo_config_jobs {
  124. let foo = project("foo")
  125. .file("Cargo.toml", r#"
  126. [package]
  127. name = "foo"
  128. version = "0.0.0"
  129. authors = []
  130. "#)
  131. .file("src/lib.rs", "")
  132. .file(".cargo/config", r#"
  133. [build]
  134. jobs = 1
  135. "#);
  136. assert_that(foo.cargo_process("build").arg("-v"),
  137. execs().with_status(0));
  138. });
  139. test!(good_cargo_config_jobs {
  140. let foo = project("foo")
  141. .file("Cargo.toml", r#"
  142. [package]
  143. name = "foo"
  144. version = "0.0.0"
  145. authors = []
  146. "#)
  147. .file("src/lib.rs", "")
  148. .file(".cargo/config", r#"
  149. [build]
  150. jobs = 4
  151. "#);
  152. assert_that(foo.cargo_process("build").arg("-v"),
  153. execs().with_status(0));
  154. });
  155. test!(invalid_global_config {
  156. let foo = project("foo")
  157. .file("Cargo.toml", r#"
  158. [package]
  159. name = "foo"
  160. version = "0.0.0"
  161. authors = []
  162. [dependencies]
  163. foo = "0.1.0"
  164. "#)
  165. .file(".cargo/config", "4")
  166. .file("src/lib.rs", "");
  167. assert_that(foo.cargo_process("build").arg("-v"),
  168. execs().with_status(101).with_stderr("\
  169. [ERROR] Couldn't load Cargo configuration
  170. Caused by:
  171. could not parse TOML configuration in `[..]config`
  172. Caused by:
  173. could not parse input as TOML
  174. [..]config:1:2 expected `=`, but found eof
  175. "));
  176. });
  177. test!(bad_cargo_lock {
  178. let foo = project("foo")
  179. .file("Cargo.toml", r#"
  180. [package]
  181. name = "foo"
  182. version = "0.0.0"
  183. authors = []
  184. "#)
  185. .file("Cargo.lock", "")
  186. .file("src/lib.rs", "");
  187. assert_that(foo.cargo_process("build").arg("-v"),
  188. execs().with_status(101).with_stderr("\
  189. [ERROR] failed to parse lock file at: [..]Cargo.lock
  190. Caused by:
  191. expected a section for the key `root`
  192. "));
  193. });
  194. test!(bad_git_dependency {
  195. let foo = project("foo")
  196. .file("Cargo.toml", r#"
  197. [package]
  198. name = "foo"
  199. version = "0.0.0"
  200. authors = []
  201. [dependencies]
  202. foo = { git = "file:.." }
  203. "#)
  204. .file("src/lib.rs", "");
  205. assert_that(foo.cargo_process("build").arg("-v"),
  206. execs().with_status(101).with_stderr("\
  207. [ERROR] Unable to update file:///
  208. Caused by:
  209. failed to clone into: [..]
  210. Caused by:
  211. [[..]] 'file:///' is not a valid local file URI
  212. "));
  213. });
  214. test!(bad_crate_type {
  215. let foo = project("foo")
  216. .file("Cargo.toml", r#"
  217. [package]
  218. name = "foo"
  219. version = "0.0.0"
  220. authors = []
  221. [lib]
  222. crate-type = ["bad_type", "rlib"]
  223. "#)
  224. .file("src/lib.rs", "");
  225. assert_that(foo.cargo_process("build").arg("-v"),
  226. execs().with_status(0).with_stderr("\
  227. warning: crate-type \"bad_type\" was not one of lib|rlib|dylib|staticlib
  228. "));
  229. });
  230. test!(malformed_override {
  231. let foo = project("foo")
  232. .file("Cargo.toml", r#"
  233. [package]
  234. name = "foo"
  235. version = "0.0.0"
  236. authors = []
  237. [target.x86_64-apple-darwin.freetype]
  238. native = {
  239. foo: "bar"
  240. }
  241. "#)
  242. .file("src/lib.rs", "");
  243. assert_that(foo.cargo_process("build"),
  244. execs().with_status(101).with_stderr("\
  245. [ERROR] failed to parse manifest at `[..]`
  246. Caused by:
  247. could not parse input as TOML
  248. Cargo.toml:[..]
  249. "));
  250. });
  251. test!(duplicate_binary_names {
  252. let foo = project("foo")
  253. .file("Cargo.toml", r#"
  254. [package]
  255. name = "qqq"
  256. version = "0.1.0"
  257. authors = ["A <a@a.a>"]
  258. [[bin]]
  259. name = "e"
  260. path = "a.rs"
  261. [[bin]]
  262. name = "e"
  263. path = "b.rs"
  264. "#)
  265. .file("a.rs", r#"fn main() -> () {}"#)
  266. .file("b.rs", r#"fn main() -> () {}"#);
  267. assert_that(foo.cargo_process("build"),
  268. execs().with_status(101).with_stderr("\
  269. [ERROR] failed to parse manifest at `[..]`
  270. Caused by:
  271. found duplicate binary name e, but all binary targets must have a unique name
  272. "));
  273. });
  274. test!(duplicate_example_names {
  275. let foo = project("foo")
  276. .file("Cargo.toml", r#"
  277. [package]
  278. name = "qqq"
  279. version = "0.1.0"
  280. authors = ["A <a@a.a>"]
  281. [[example]]
  282. name = "ex"
  283. path = "examples/ex.rs"
  284. [[example]]
  285. name = "ex"
  286. path = "examples/ex2.rs"
  287. "#)
  288. .file("examples/ex.rs", r#"fn main () -> () {}"#)
  289. .file("examples/ex2.rs", r#"fn main () -> () {}"#);
  290. assert_that(foo.cargo_process("build").arg("--example").arg("ex"),
  291. execs().with_status(101).with_stderr("\
  292. [ERROR] failed to parse manifest at `[..]`
  293. Caused by:
  294. found duplicate example name ex, but all binary targets must have a unique name
  295. "));
  296. });
  297. test!(duplicate_bench_names {
  298. let foo = project("foo")
  299. .file("Cargo.toml", r#"
  300. [package]
  301. name = "qqq"
  302. version = "0.1.0"
  303. authors = ["A <a@a.a>"]
  304. [[bench]]
  305. name = "ex"
  306. path = "benches/ex.rs"
  307. [[bench]]
  308. name = "ex"
  309. path = "benches/ex2.rs"
  310. "#)
  311. .file("benches/ex.rs", r#"fn main () {}"#)
  312. .file("benches/ex2.rs", r#"fn main () {}"#);
  313. assert_that(foo.cargo_process("bench"),
  314. execs().with_status(101).with_stderr("\
  315. [ERROR] failed to parse manifest at `[..]`
  316. Caused by:
  317. found duplicate bench name ex, but all binary targets must have a unique name
  318. "));
  319. });
  320. test!(duplicate_deps {
  321. let foo = project("foo")
  322. .file("shim-bar/Cargo.toml", r#"
  323. [package]
  324. name = "bar"
  325. version = "0.0.1"
  326. authors = []
  327. "#)
  328. .file("shim-bar/src/lib.rs", r#"
  329. pub fn a() {}
  330. "#)
  331. .file("linux-bar/Cargo.toml", r#"
  332. [package]
  333. name = "bar"
  334. version = "0.0.1"
  335. authors = []
  336. "#)
  337. .file("linux-bar/src/lib.rs", r#"
  338. pub fn a() {}
  339. "#)
  340. .file("Cargo.toml", r#"
  341. [package]
  342. name = "qqq"
  343. version = "0.0.1"
  344. authors = []
  345. [dependencies]
  346. bar = { path = "shim-bar" }
  347. [target.x86_64-unknown-linux-gnu.dependencies]
  348. bar = { path = "linux-bar" }
  349. "#)
  350. .file("src/main.rs", r#"fn main () {}"#);
  351. assert_that(foo.cargo_process("build"),
  352. execs().with_status(101).with_stderr("\
  353. [ERROR] failed to parse manifest at `[..]`
  354. Caused by:
  355. found duplicate dependency name bar, but all dependencies must have a unique name
  356. "));
  357. });
  358. test!(unused_keys {
  359. let foo = project("foo")
  360. .file("Cargo.toml", r#"
  361. [package]
  362. name = "foo"
  363. version = "0.1.0"
  364. authors = []
  365. [target.foo]
  366. bar = "3"
  367. "#)
  368. .file("src/lib.rs", "");
  369. assert_that(foo.cargo_process("build"),
  370. execs().with_status(0).with_stderr("\
  371. warning: unused manifest key: target.foo.bar
  372. "));
  373. });
  374. test!(empty_dependencies {
  375. let p = project("empty_deps")
  376. .file("Cargo.toml", r#"
  377. [package]
  378. name = "empty_deps"
  379. version = "0.0.0"
  380. authors = []
  381. [dependencies]
  382. foo = {}
  383. "#)
  384. .file("src/main.rs", "fn main() {}");
  385. Package::new("foo", "0.0.1").publish();
  386. assert_that(p.cargo_process("build"),
  387. execs().with_status(0).with_stderr_contains("\
  388. warning: dependency (foo) specified without providing a local path, Git repository, or version \
  389. to use. This will be considered an error in future versions
  390. "));
  391. });