1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
use crate::{
errors::{
ArchiveExtractError, ArchiveReadError, PathMapperConstructError, PathMapperConstructKind,
},
list::BinaryList,
};
use camino::{Utf8Path, Utf8PathBuf};
use guppy::graph::PackageGraph;
use std::{fs, io, sync::Arc};
use tempfile::TempDir;
mod archive_reporter;
mod archiver;
mod unarchiver;
pub use archive_reporter::*;
pub use archiver::*;
pub use unarchiver::*;
pub const CARGO_METADATA_FILE_NAME: &str = "target/nextest/cargo-metadata.json";
pub const BINARIES_METADATA_FILE_NAME: &str = "target/nextest/binaries-metadata.json";
#[derive(Debug, Default)]
pub struct ReuseBuildInfo {
pub cargo_metadata: Option<MetadataWithRemap<(String, PackageGraph)>>,
pub binaries_metadata: Option<MetadataWithRemap<BinaryList>>,
_temp_dir: Option<TempDir>,
}
impl ReuseBuildInfo {
pub fn new(
cargo_metadata: Option<MetadataWithRemap<(String, PackageGraph)>>,
binaries_metadata: Option<MetadataWithRemap<BinaryList>>,
) -> Self {
Self {
cargo_metadata,
binaries_metadata,
_temp_dir: None,
}
}
pub fn extract_archive<F>(
archive_file: &Utf8Path,
format: ArchiveFormat,
dest: ExtractDestination,
callback: F,
workspace_remap: Option<&Utf8Path>,
) -> Result<Self, ArchiveExtractError>
where
F: for<'e> FnMut(ArchiveEvent<'e>) -> io::Result<()>,
{
let mut file = fs::File::open(archive_file)
.map_err(|err| ArchiveExtractError::Read(ArchiveReadError::Io(err)))?;
let mut unarchiver = Unarchiver::new(&mut file, format);
let ExtractInfo {
dest_dir,
temp_dir,
binary_list,
cargo_metadata_json,
graph,
} = unarchiver.extract(dest, callback)?;
let cargo_metadata = MetadataWithRemap {
metadata: MetadataOrPath::metadata((cargo_metadata_json, graph)),
remap: workspace_remap.map(|p| p.to_owned()),
};
let binaries_metadata = MetadataWithRemap {
metadata: MetadataOrPath::metadata(binary_list),
remap: Some(dest_dir.join("target")),
};
Ok(Self {
cargo_metadata: Some(cargo_metadata),
binaries_metadata: Some(binaries_metadata),
_temp_dir: temp_dir,
})
}
pub fn cargo_metadata(&self) -> Option<&MetadataOrPath<(String, PackageGraph)>> {
self.cargo_metadata.as_ref().map(|m| &m.metadata)
}
pub fn binaries_metadata(&self) -> Option<&MetadataOrPath<BinaryList>> {
self.binaries_metadata.as_ref().map(|m| &m.metadata)
}
#[inline]
pub fn is_active(&self) -> bool {
self.cargo_metadata.is_some() || self.binaries_metadata.is_some()
}
pub fn workspace_remap(&self) -> Option<&Utf8Path> {
self.cargo_metadata
.as_ref()
.and_then(|m| m.remap.as_deref())
}
pub fn target_dir_remap(&self) -> Option<&Utf8Path> {
self.binaries_metadata
.as_ref()
.and_then(|m| m.remap.as_deref())
}
}
#[derive(Clone, Debug)]
pub struct MetadataWithRemap<T> {
pub metadata: MetadataOrPath<T>,
pub remap: Option<Utf8PathBuf>,
}
#[derive(Clone, Debug)]
pub enum MetadataOrPath<T> {
Metadata(Arc<T>),
Path(Utf8PathBuf),
}
impl<T> MetadataOrPath<T> {
#[inline]
pub fn metadata(metadata: T) -> Self {
Self::Metadata(Arc::new(metadata))
}
}
impl<T> From<Utf8PathBuf> for MetadataOrPath<T> {
#[inline]
fn from(path: Utf8PathBuf) -> Self {
Self::Path(path)
}
}
#[derive(Clone, Debug, Default)]
pub struct PathMapper {
workspace: Option<(Utf8PathBuf, Utf8PathBuf)>,
target_dir: Option<(Utf8PathBuf, Utf8PathBuf)>,
}
impl PathMapper {
pub fn new(
orig_workspace_root: impl Into<Utf8PathBuf>,
workspace_remap: Option<&Utf8Path>,
orig_target_dir: impl Into<Utf8PathBuf>,
target_dir_remap: Option<&Utf8Path>,
) -> Result<Self, PathMapperConstructError> {
let workspace_root = workspace_remap
.map(|root| Self::canonicalize_dir(root, PathMapperConstructKind::WorkspaceRoot))
.transpose()?;
let target_dir = target_dir_remap
.map(|dir| Self::canonicalize_dir(dir, PathMapperConstructKind::WorkspaceRoot))
.transpose()?;
Ok(Self {
workspace: workspace_root.map(|w| (orig_workspace_root.into(), w)),
target_dir: target_dir.map(|d| (orig_target_dir.into(), d)),
})
}
pub fn noop() -> Self {
Self {
workspace: None,
target_dir: None,
}
}
fn canonicalize_dir(
input: &Utf8Path,
kind: PathMapperConstructKind,
) -> Result<Utf8PathBuf, PathMapperConstructError> {
let canonicalized_path =
input
.canonicalize()
.map_err(|err| PathMapperConstructError::Canonicalization {
kind,
input: input.into(),
err,
})?;
let canonicalized_path: Utf8PathBuf =
canonicalized_path
.try_into()
.map_err(|err| PathMapperConstructError::NonUtf8Path {
kind,
input: input.into(),
err,
})?;
if !canonicalized_path.is_dir() {
return Err(PathMapperConstructError::NotADirectory {
kind,
input: input.into(),
canonicalized_path,
});
}
Ok(canonicalized_path)
}
pub(super) fn new_target_dir(&self) -> Option<&Utf8Path> {
self.target_dir.as_ref().map(|(_, new)| new.as_path())
}
pub(crate) fn map_cwd(&self, path: Utf8PathBuf) -> Utf8PathBuf {
match &self.workspace {
Some((from, to)) => match path.strip_prefix(from) {
Ok(p) => to.join(p),
Err(_) => path,
},
None => path,
}
}
pub(crate) fn map_binary(&self, path: Utf8PathBuf) -> Utf8PathBuf {
match &self.target_dir {
Some((from, to)) => match path.strip_prefix(from) {
Ok(p) => to.join(p),
Err(_) => path,
},
None => path,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use tempfile::TempDir;
#[test]
fn test_path_mapper_relative() {
let current_dir: Utf8PathBuf = std::env::current_dir()
.expect("current dir obtained")
.try_into()
.expect("current dir is valid UTF-8");
let temp_workspace_root = TempDir::new().expect("new temp dir created");
let workspace_root_path: Utf8PathBuf = temp_workspace_root
.path()
.canonicalize()
.expect("workspace root canonicalized correctly")
.try_into()
.expect("workspace root is valid UTF-8");
let rel_workspace_root = pathdiff::diff_utf8_paths(&workspace_root_path, ¤t_dir)
.expect("abs to abs diff is non-None");
let temp_target_dir = TempDir::new().expect("new temp dir created");
let target_dir_path: Utf8PathBuf = temp_target_dir
.path()
.canonicalize()
.expect("target dir canonicalized correctly")
.try_into()
.expect("target dir is valid UTF-8");
let rel_target_dir = pathdiff::diff_utf8_paths(&target_dir_path, ¤t_dir)
.expect("abs to abs diff is non-None");
let orig_workspace_root = Utf8Path::new(env!("CARGO_MANIFEST_DIR"));
let orig_target_dir = orig_workspace_root.join("target");
let path_mapper = PathMapper::new(
orig_workspace_root,
Some(&rel_workspace_root),
&orig_target_dir,
Some(&rel_target_dir),
)
.expect("remapped paths exist");
assert_eq!(
path_mapper.map_cwd(orig_workspace_root.join("foobar")),
workspace_root_path.join("foobar")
);
assert_eq!(
path_mapper.map_binary(orig_target_dir.join("foobar")),
target_dir_path.join("foobar")
);
}
}