Debian Codegraph @

tests/common/errors.rs

raw
1 use anyhow::format_err;
2 use anyhow::{bail, Result};
3 use clap::Parser;
4 use maturin::BuildOptions;
5 use pretty_assertions::assert_eq;
6 use std::path::Path;
7 use std::process::Command;
8 use std::str;
9
10 /// Check that you get a good error message if you forgot to set the extension-module feature
11 pub fn pyo3_no_extension_module() -> Result<()> {
12 // The first argument is ignored by clap
13 let cli = vec![
14 "build",
15 "--manifest-path",
16 "test-crates/pyo3-no-extension-module/Cargo.toml",
17 "--quiet",
18 "--target-dir",
19 "test-crates/targets/pyo3_no_extension_module",
20 "--out",
21 "test-crates/targets/pyo3_no_extension_module",
22 ];
23
24 let options = BuildOptions::try_parse_from(cli)?;
25 let result = options
26 .into_build_context()
27 .release(false)
28 .strip(cfg!(feature = "faster-tests"))
29 .editable(false)
30 .build()?
31 .build_wheels();
32 if let Err(err) = result {
33 if !(err
34 .source()
35 .ok_or_else(|| format_err!("{}", err))?
36 .to_string()
37 .starts_with("Your library links libpython"))
38 {
39 return Err(err);
40 }
41 } else {
42 bail!("Should have errored");
43 }
44
45 Ok(())
46 }
47
48 /// Make sure cargo metadata doesn't create a lock file when --locked was passed
49 ///
50 /// https://github.com/PyO3/maturin/issues/472
51 pub fn locked_doesnt_build_without_cargo_lock() -> Result<()> {
52 // The first argument is ignored by clap
53 let cli = vec![
54 "build",
55 "--manifest-path",
56 "test-crates/lib_with_path_dep/Cargo.toml",
57 "--locked",
58 "-itargetspython",
59 "--target-dir",
60 "test-crates/targets/locked_doesnt_build_without_cargo_lock",
61 ];
62 let options = BuildOptions::try_parse_from(cli)?;
63 let result = options
64 .into_build_context()
65 .release(false)
66 .strip(cfg!(feature = "faster-tests"))
67 .editable(false)
68 .build();
69 if let Err(err) = result {
70 let err_string = err
71 .source()
72 .ok_or_else(|| format_err!("{}", err))?
73 .to_string();
74 if !err_string.starts_with("`cargo metadata` exited with an error:") {
75 bail!("{:?}", err_string);
76 }
77 } else {
78 bail!("Should have errored");
79 }
80
81 Ok(())
82 }
83
84 /// Don't panic if the manylinux version doesn't exit
85 ///
86 /// https://github.com/PyO3/maturin/issues/739
87 pub fn invalid_manylinux_does_not_panic() -> Result<()> {
88 // The first argument is ignored by clap
89 let cli = vec![
90 "build",
91 "-m",
92 "test-crates/pyo3-mixed/Cargo.toml",
93 "--compatibility",
94 "manylinux_2_99",
95 "--target-dir",
96 "test-crates/targets/invalid_manylinux_does_not_panic",
97 "--out",
98 "test-crates/targets/invalid_manylinux_does_not_panic",
99 ];
100 let options: BuildOptions = BuildOptions::try_parse_from(cli)?;
101 let result = options
102 .into_build_context()
103 .release(false)
104 .strip(cfg!(feature = "faster-tests"))
105 .editable(false)
106 .build()?
107 .build_wheels();
108 if let Err(err) = result {
109 assert_eq!(err.to_string(), "Error ensuring manylinux_2_99 compliance");
110 let err_string = err
111 .source()
112 .ok_or_else(|| format_err!("{}", err))?
113 .to_string();
114 assert_eq!(err_string, "manylinux_2_99 compatibility policy is not defined by auditwheel yet, pass `--auditwheel=skip` to proceed anyway");
115 } else {
116 bail!("Should have errored");
117 }
118
119 Ok(())
120 }
121
122 /// The user set `python-source` in pyproject.toml, but there is no python module in there
123 pub fn warn_on_missing_python_source() -> Result<()> {
124 let output = Command::new(env!("CARGO_BIN_EXE_maturin"))
125 .arg("build")
126 .arg("-m")
127 .arg(
128 Path::new("test-crates")
129 .join("wrong-python-source")
130 .join("Cargo.toml"),
131 )
132 .output()
133 .unwrap();
134 if !output.status.success() {
135 bail!(
136 "Failed to run: {}\n---stdout:\n{}---stderr:\n{}",
137 output.status,
138 str::from_utf8(&output.stdout)?,
139 str::from_utf8(&output.stderr)?
140 );
141 }
142
143 assert!(str::from_utf8(&output.stderr)?.contains("Warning: You specified the python source as"));
144 Ok(())
145 }
146
147 /// Check the `--compatibility pypi` error for unsupported targets.
148 ///
149 /// Regression test for <https://github.com/astral-sh/uv/pull/14006>,
150 /// where uv tried to upload a wheel with platform tag `manylinux_2_31_riscv64`
151 /// but PyPI rejected it because RISC-V is not in PyPI's allowed architectures.
152 pub fn pypi_compatibility_unsupported_target() -> Result<()> {
153 // The first argument is ignored by clap
154 let cli = vec![
155 "build",
156 "-m",
157 "test-crates/pyo3-mixed/Cargo.toml",
158 "--compatibility",
159 "pypi",
160 "--target",
161 "riscv32gc-unknown-linux-gnu", // Unsupported by PyPI
162 "--target-dir",
163 "test-crates/targets/pypi_compatibility_unsupported_target",
164 "--out",
165 "test-crates/targets/pypi_compatibility_unsupported_target",
166 "-i",
167 "python3.12", // Add interpreter to bypass interpreter detection
168 ];
169 let options: BuildOptions = BuildOptions::try_parse_from(cli)?;
170 let result = options
171 .into_build_context()
172 .release(false)
173 .strip(cfg!(feature = "faster-tests"))
174 .editable(false)
175 .build();
176
177 if let Err(err) = result {
178 let err_string = err.to_string();
179 assert!(
180 err_string.contains(
181 "Target riscv32gc-unknown-linux-gnu architecture is not supported by PyPI"
182 ),
183 "{err_string}",
184 );
185 } else {
186 bail!("Should have errored");
187 }
188
189 Ok(())
190 }
191
192 /// Test that --compatibility pypi cannot be combined with other platform tags
193 pub fn pypi_compatibility_mixed_tags() -> Result<()> {
194 // The first argument is ignored by clap
195 let cli = vec![
196 "build",
197 "-m",
198 "test-crates/pyo3-mixed/Cargo.toml",
199 "--compatibility",
200 "pypi",
201 "--compatibility",
202 "manylinux2014", // Should fail when combined with pypi
203 "--target-dir",
204 "test-crates/targets/pypi_compatibility_mixed_tags",
205 "--out",
206 "test-crates/targets/pypi_compatibility_mixed_tags",
207 "-i",
208 "python3.12", // Add interpreter to bypass interpreter detection
209 ];
210 let options: BuildOptions = BuildOptions::try_parse_from(cli)?;
211 let result = options
212 .into_build_context()
213 .release(false)
214 .strip(cfg!(feature = "faster-tests"))
215 .editable(false)
216 .build();
217
218 if let Err(err) = result {
219 let err_string = err.to_string();
220 assert!(
221 err_string.contains(
222 "The 'pypi' compatibility option cannot be combined with other platform tags"
223 ),
224 "{err_string}",
225 );
226 } else {
227 bail!("Should have errored");
228 }
229
230 Ok(())
231 }