/tests/testsuite/read_manifest.rs

https://gitlab.com/frewsxcv/cargo · Rust · 206 lines · 200 code · 5 blank · 1 comment · 0 complexity · 6d70d4f924e0a414f129e1d58122f3e7 MD5 · raw file

  1. //! Tests for the `cargo read-manifest` command.
  2. use cargo_test_support::{basic_bin_manifest, main_file, project};
  3. fn manifest_output(readme_value: &str) -> String {
  4. format!(
  5. r#"
  6. {{
  7. "authors": [
  8. "wycats@example.com"
  9. ],
  10. "categories": [],
  11. "default_run": null,
  12. "name":"foo",
  13. "readme": {},
  14. "homepage": null,
  15. "documentation": null,
  16. "repository": null,
  17. "rust_version": null,
  18. "version":"0.5.0",
  19. "id":"foo[..]0.5.0[..](path+file://[..]/foo)",
  20. "keywords": [],
  21. "license": null,
  22. "license_file": null,
  23. "links": null,
  24. "description": null,
  25. "edition": "2015",
  26. "source":null,
  27. "dependencies":[],
  28. "targets":[{{
  29. "kind":["bin"],
  30. "crate_types":["bin"],
  31. "doc": true,
  32. "doctest": false,
  33. "test": true,
  34. "edition": "2015",
  35. "name":"foo",
  36. "src_path":"[..]/foo/src/foo.rs"
  37. }}],
  38. "features":{{}},
  39. "manifest_path":"[..]Cargo.toml",
  40. "metadata": null,
  41. "publish": null
  42. }}"#,
  43. readme_value
  44. )
  45. }
  46. fn manifest_output_no_readme() -> String {
  47. manifest_output("null")
  48. }
  49. pub fn basic_bin_manifest_with_readme(name: &str, readme_filename: &str) -> String {
  50. format!(
  51. r#"
  52. [package]
  53. name = "{}"
  54. version = "0.5.0"
  55. authors = ["wycats@example.com"]
  56. readme = {}
  57. [[bin]]
  58. name = "{}"
  59. "#,
  60. name, readme_filename, name
  61. )
  62. }
  63. #[cargo_test]
  64. fn cargo_read_manifest_path_to_cargo_toml_relative() {
  65. let p = project()
  66. .file("Cargo.toml", &basic_bin_manifest("foo"))
  67. .file("src/foo.rs", &main_file(r#""i am foo""#, &[]))
  68. .build();
  69. p.cargo("read-manifest --manifest-path foo/Cargo.toml")
  70. .cwd(p.root().parent().unwrap())
  71. .with_json(&manifest_output_no_readme())
  72. .run();
  73. }
  74. #[cargo_test]
  75. fn cargo_read_manifest_path_to_cargo_toml_absolute() {
  76. let p = project()
  77. .file("Cargo.toml", &basic_bin_manifest("foo"))
  78. .file("src/foo.rs", &main_file(r#""i am foo""#, &[]))
  79. .build();
  80. p.cargo("read-manifest --manifest-path")
  81. .arg(p.root().join("Cargo.toml"))
  82. .cwd(p.root().parent().unwrap())
  83. .with_json(&manifest_output_no_readme())
  84. .run();
  85. }
  86. #[cargo_test]
  87. fn cargo_read_manifest_path_to_cargo_toml_parent_relative() {
  88. let p = project()
  89. .file("Cargo.toml", &basic_bin_manifest("foo"))
  90. .file("src/foo.rs", &main_file(r#""i am foo""#, &[]))
  91. .build();
  92. p.cargo("read-manifest --manifest-path foo")
  93. .cwd(p.root().parent().unwrap())
  94. .with_status(101)
  95. .with_stderr(
  96. "[ERROR] the manifest-path must be \
  97. a path to a Cargo.toml file",
  98. )
  99. .run();
  100. }
  101. #[cargo_test]
  102. fn cargo_read_manifest_path_to_cargo_toml_parent_absolute() {
  103. let p = project()
  104. .file("Cargo.toml", &basic_bin_manifest("foo"))
  105. .file("src/foo.rs", &main_file(r#""i am foo""#, &[]))
  106. .build();
  107. p.cargo("read-manifest --manifest-path")
  108. .arg(p.root())
  109. .cwd(p.root().parent().unwrap())
  110. .with_status(101)
  111. .with_stderr(
  112. "[ERROR] the manifest-path must be \
  113. a path to a Cargo.toml file",
  114. )
  115. .run();
  116. }
  117. #[cargo_test]
  118. fn cargo_read_manifest_cwd() {
  119. let p = project()
  120. .file("Cargo.toml", &basic_bin_manifest("foo"))
  121. .file("src/foo.rs", &main_file(r#""i am foo""#, &[]))
  122. .build();
  123. p.cargo("read-manifest")
  124. .with_json(&manifest_output_no_readme())
  125. .run();
  126. }
  127. #[cargo_test]
  128. fn cargo_read_manifest_with_specified_readme() {
  129. let p = project()
  130. .file(
  131. "Cargo.toml",
  132. &basic_bin_manifest_with_readme("foo", r#""SomeReadme.txt""#),
  133. )
  134. .file("SomeReadme.txt", "Sample Project")
  135. .file("src/foo.rs", &main_file(r#""i am foo""#, &[]))
  136. .build();
  137. p.cargo("read-manifest")
  138. .with_json(&manifest_output(&format!(r#""{}""#, "SomeReadme.txt")))
  139. .run();
  140. }
  141. #[cargo_test]
  142. fn cargo_read_manifest_default_readme() {
  143. let readme_filenames = ["README.md", "README.txt", "README"];
  144. for readme in readme_filenames.iter() {
  145. let p = project()
  146. .file("Cargo.toml", &basic_bin_manifest("foo"))
  147. .file(readme, "Sample project")
  148. .file("src/foo.rs", &main_file(r#""i am foo""#, &[]))
  149. .build();
  150. p.cargo("read-manifest")
  151. .with_json(&manifest_output(&format!(r#""{}""#, readme)))
  152. .run();
  153. }
  154. }
  155. #[cargo_test]
  156. fn cargo_read_manifest_suppress_default_readme() {
  157. let p = project()
  158. .file(
  159. "Cargo.toml",
  160. &basic_bin_manifest_with_readme("foo", "false"),
  161. )
  162. .file("README.txt", "Sample project")
  163. .file("src/foo.rs", &main_file(r#""i am foo""#, &[]))
  164. .build();
  165. p.cargo("read-manifest")
  166. .with_json(&manifest_output_no_readme())
  167. .run();
  168. }
  169. // If a file named README.md exists, and `readme = true`, the value `README.md` should be defaulted in.
  170. #[cargo_test]
  171. fn cargo_read_manifest_defaults_readme_if_true() {
  172. let p = project()
  173. .file("Cargo.toml", &basic_bin_manifest_with_readme("foo", "true"))
  174. .file("README.md", "Sample project")
  175. .file("src/foo.rs", &main_file(r#""i am foo""#, &[]))
  176. .build();
  177. p.cargo("read-manifest")
  178. .with_json(&manifest_output(r#""README.md""#))
  179. .run();
  180. }