Debian Codegraph @

tests/common/metadata.rs

raw
1 use anyhow::Result;
2 use insta::assert_snapshot;
3 use maturin::{write_dist_info, BuildOptions, CargoOptions, ModuleWriter};
4 use std::collections::HashMap;
5 use std::path::{Path, PathBuf};
6
7 #[derive(Default)]
8 struct MockWriter {
9 files: Vec<String>,
10 contents: HashMap<String, String>,
11 }
12
13 impl ModuleWriter for MockWriter {
14 fn add_directory(&mut self, path: impl AsRef<Path>) -> Result<()> {
15 self.files.push(path.as_ref().to_string_lossy().to_string());
16 Ok(())
17 }
18
19 fn add_bytes_with_permissions(
20 &mut self,
21 target: impl AsRef<Path>,
22 _source: Option<&Path>,
23 bytes: &[u8],
24 _permissions: u32,
25 ) -> Result<()> {
26 self.files
27 .push(target.as_ref().to_string_lossy().to_string());
28 self.contents.insert(
29 target.as_ref().to_string_lossy().into(),
30 std::str::from_utf8(bytes).unwrap().to_string(),
31 );
32 Ok(())
33 }
34 }
35
36 #[test]
37 fn metadata_hello_world_pep639() {
38 let build_options = BuildOptions {
39 cargo: CargoOptions {
40 manifest_path: Some(
41 PathBuf::from("test-crates")
42 .join("hello-world")
43 .join("Cargo.toml"),
44 ),
45 ..CargoOptions::default()
46 },
47 ..BuildOptions::default()
48 };
49 let context = build_options.into_build_context().build().unwrap();
50
51 let mut writer = MockWriter::default();
52 write_dist_info(
53 &mut writer,
54 &context.project_layout.project_root,
55 &context.metadata24,
56 &context.tags_from_bridge().unwrap(),
57 )
58 .unwrap();
59
60 assert_snapshot!(writer.files.join("\n").replace("\\", "/"), @r"
61 hello_world-0.1.0.dist-info
62 hello_world-0.1.0.dist-info/METADATA
63 hello_world-0.1.0.dist-info/WHEEL
64 hello_world-0.1.0.dist-info/licenses
65 hello_world-0.1.0.dist-info/licenses/LICENSE
66 hello_world-0.1.0.dist-info/licenses/licenses/AUTHORS.txt
67 ");
68 let metadata_path = Path::new("hello_world-0.1.0.dist-info")
69 .join("METADATA")
70 .to_str()
71 .unwrap()
72 .to_string();
73 // Remove the README in the body of the email
74 let metadata = writer.contents[&metadata_path]
75 .split_once("\n\n")
76 .unwrap()
77 .0;
78 assert_snapshot!(metadata, @r"
79 Metadata-Version: 2.4
80 Name: hello-world
81 Version: 0.1.0
82 License-File: LICENSE
83 License-File: licenses/AUTHORS.txt
84 Author: konstin <[email protected]>
85 Author-email: konstin <[email protected]>
86 Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
87 ");
88 }