|
1 | 1 | //! Tests for dep-info files. This includes the dep-info file Cargo creates in |
2 | 2 | //! the output directory, and the ones stored in the fingerprint. |
3 | 3 |
|
4 | | -use std::fs; |
5 | 4 | use std::path::Path; |
6 | | -use std::str; |
7 | 5 |
|
8 | 6 | use cargo_test_support::compare::assert_e2e; |
9 | 7 | use cargo_test_support::paths; |
10 | 8 | use cargo_test_support::prelude::*; |
11 | 9 | use cargo_test_support::registry::Package; |
12 | 10 | use cargo_test_support::str; |
13 | | -use cargo_test_support::{ |
14 | | - basic_bin_manifest, basic_manifest, main_file, project, rustc_host, Project, |
15 | | -}; |
| 11 | +use cargo_test_support::{assert_deps, assert_deps_contains}; |
| 12 | +use cargo_test_support::{basic_bin_manifest, basic_manifest, main_file, project, rustc_host}; |
16 | 13 | use filetime::FileTime; |
17 | 14 |
|
18 | | -// Helper for testing dep-info files in the fingerprint dir. |
19 | | -#[track_caller] |
20 | | -fn assert_deps(project: &Project, fingerprint: &str, test_cb: impl Fn(&Path, &[(u8, &str)])) { |
21 | | - let mut files = project |
22 | | - .glob(fingerprint) |
23 | | - .map(|f| f.expect("unwrap glob result")) |
24 | | - // Filter out `.json` entries. |
25 | | - .filter(|f| f.extension().is_none()); |
26 | | - let info_path = files |
27 | | - .next() |
28 | | - .unwrap_or_else(|| panic!("expected 1 dep-info file at {}, found 0", fingerprint)); |
29 | | - assert!(files.next().is_none(), "expected only 1 dep-info file"); |
30 | | - let dep_info = fs::read(&info_path).unwrap(); |
31 | | - let dep_info = &mut &dep_info[..]; |
32 | | - let deps = (0..read_usize(dep_info)) |
33 | | - .map(|_| { |
34 | | - let ty = read_u8(dep_info); |
35 | | - let path = str::from_utf8(read_bytes(dep_info)).unwrap(); |
36 | | - let checksum_present = read_bool(dep_info); |
37 | | - if checksum_present { |
38 | | - // Read out the checksum info without using it |
39 | | - let _file_len = read_u64(dep_info); |
40 | | - let _checksum = read_bytes(dep_info); |
41 | | - } |
42 | | - (ty, path) |
43 | | - }) |
44 | | - .collect::<Vec<_>>(); |
45 | | - test_cb(&info_path, &deps); |
46 | | - |
47 | | - fn read_usize(bytes: &mut &[u8]) -> usize { |
48 | | - let ret = &bytes[..4]; |
49 | | - *bytes = &bytes[4..]; |
50 | | - |
51 | | - u32::from_le_bytes(ret.try_into().unwrap()) as usize |
52 | | - } |
53 | | - |
54 | | - fn read_u8(bytes: &mut &[u8]) -> u8 { |
55 | | - let ret = bytes[0]; |
56 | | - *bytes = &bytes[1..]; |
57 | | - ret |
58 | | - } |
59 | | - |
60 | | - fn read_bool(bytes: &mut &[u8]) -> bool { |
61 | | - read_u8(bytes) != 0 |
62 | | - } |
63 | | - |
64 | | - fn read_u64(bytes: &mut &[u8]) -> u64 { |
65 | | - let ret = &bytes[..8]; |
66 | | - *bytes = &bytes[8..]; |
67 | | - |
68 | | - u64::from_le_bytes(ret.try_into().unwrap()) |
69 | | - } |
70 | | - |
71 | | - fn read_bytes<'a>(bytes: &mut &'a [u8]) -> &'a [u8] { |
72 | | - let n = read_usize(bytes); |
73 | | - let ret = &bytes[..n]; |
74 | | - *bytes = &bytes[n..]; |
75 | | - ret |
76 | | - } |
77 | | -} |
78 | | - |
79 | | -fn assert_deps_contains(project: &Project, fingerprint: &str, expected: &[(u8, &str)]) { |
80 | | - assert_deps(project, fingerprint, |info_path, entries| { |
81 | | - for (e_kind, e_path) in expected { |
82 | | - let pattern = glob::Pattern::new(e_path).unwrap(); |
83 | | - let count = entries |
84 | | - .iter() |
85 | | - .filter(|(kind, path)| kind == e_kind && pattern.matches(path)) |
86 | | - .count(); |
87 | | - if count != 1 { |
88 | | - panic!( |
89 | | - "Expected 1 match of {} {} in {:?}, got {}:\n{:#?}", |
90 | | - e_kind, e_path, info_path, count, entries |
91 | | - ); |
92 | | - } |
93 | | - } |
94 | | - }) |
95 | | -} |
96 | | - |
97 | 15 | #[cargo_test] |
98 | 16 | fn build_dep_info() { |
99 | 17 | let p = project() |
|
0 commit comments