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
use crate::types::errors::Error;
use crate::utils::paths::{artifacts_dir, test_target_dir};
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use std::thread;
/// Manages a pipeline of target directories for sequential test execution
pub struct TargetPipeline {
master_target_dir: PathBuf,
output_dir: PathBuf,
shared_target_dir: PathBuf,
staging_dir: PathBuf,
prepare_thread: Option<thread::JoinHandle<Result<(), Error>>>,
next_test: Arc<Mutex<Option<String>>>,
is_running: Arc<Mutex<bool>>,
}
impl TargetPipeline {
/// Creates a new target pipeline manager
pub fn new(master_target_dir: &Path, output_dir: &Path) -> Result<Self, Error> {
// Create the artifacts directory
let artifacts = artifacts_dir(output_dir);
fs::create_dir_all(&artifacts)?;
// Create shared and staging directories
let shared_target_dir = artifacts.join("shared_target");
let staging_dir = artifacts.join("staging_target");
// Create directories if they don't exist
if !shared_target_dir.exists() {
fs::create_dir_all(&shared_target_dir)?;
}
if !staging_dir.exists() {
fs::create_dir_all(&staging_dir)?;
}
// Prepare the initial shared target directory (minimal setup)
Self::setup_minimal_target_dir(&shared_target_dir)?;
Ok(TargetPipeline {
master_target_dir: master_target_dir.to_path_buf(),
output_dir: output_dir.to_path_buf(),
shared_target_dir,
staging_dir,
prepare_thread: None,
next_test: Arc::new(Mutex::new(None)),
is_running: Arc::new(Mutex::new(true)),
})
}
/// Set up a minimal target directory structure
fn setup_minimal_target_dir(target_dir: &Path) -> Result<(), Error> {
// Create the debug directory
let debug_dir = target_dir.join("debug");
fs::create_dir_all(&debug_dir)?;
// Create basic structure
for dir in &[
"debug/.fingerprint",
"debug/deps",
"debug/build",
"debug/incremental",
] {
fs::create_dir_all(target_dir.join(dir))?;
}
// Create an empty .cargo-lock file
let cargo_lock_file = debug_dir.join(".cargo-lock");
if !cargo_lock_file.exists() {
fs::write(&cargo_lock_file, "")?;
}
Ok(())
}
/// Start preparing the target directory for the next test
pub fn prepare_next(&mut self, test_name: &str) -> Result<(), Error> {
// Stop any existing preparation thread
self.stop_preparation();
// Set the next test name
{
let mut next = self.next_test.lock().unwrap();
*next = Some(test_name.to_string());
}
// Clone necessary data for the thread
let master_dir = self.master_target_dir.clone();
let staging = self.staging_dir.clone();
let next_test = Arc::clone(&self.next_test);
let is_running = Arc::clone(&self.is_running);
let output_dir = self.output_dir.clone();
// Start a new thread to prepare the next target directory
let handle = thread::spawn(move || -> Result<(), Error> {
// Clean the staging directory first
if staging.exists() {
fs::remove_dir_all(&staging)?;
}
fs::create_dir_all(&staging)?;
// Set up the minimal directory structure
Self::setup_minimal_target_dir(&staging)?;
// Get the test-specific target directory path (for reference)
let test_name_str = {
let next = next_test.lock().unwrap();
match &*next {
Some(name) => name.clone(),
None => return Ok(()),
}
};
let _test_dir = test_target_dir(&output_dir, &test_name_str);
// Copy selected files from the master target directory
// This is the main work of preparing for the next test
println!(
"Preparing target directory for test '{}' in the background",
test_name_str
);
// Copy executable files from the master to the staging directory
let master_deps = master_dir.join("debug/deps");
let staging_deps = staging.join("debug/deps");
if master_deps.exists() {
for entry in fs::read_dir(&master_deps)? {
let entry = entry?;
let path = entry.path();
// Only copy files (not directories)
if path.is_file() {
// Check if preparation should continue
if !*is_running.lock().unwrap() {
return Ok(());
}
let dest = staging_deps.join(path.file_name().unwrap());
fs::copy(&path, &dest)?;
}
}
}
println!(
"Background preparation complete for test '{}'",
test_name_str
);
Ok(())
});
self.prepare_thread = Some(handle);
Ok(())
}
/// Wait for the preparation to complete and swap in the new directory
pub fn get_ready_target_dir(&mut self) -> Result<PathBuf, Error> {
// Wait for preparation to complete if there's a thread running
if let Some(thread) = self.prepare_thread.take() {
match thread.join() {
Ok(result) => {
if let Err(e) = result {
eprintln!("Error preparing target directory: {}", e);
// Fall back to using the shared directory as-is
}
}
Err(_) => {
eprintln!("Background preparation thread panicked");
// Fall back to using the shared directory as-is
}
}
}
// Get the current test name
let current_test = {
let mut next = self.next_test.lock().unwrap();
next.take()
};
if let Some(test_name) = current_test {
println!(
"Swapping prepared target directory for test '{}'",
test_name
);
// Swap the staging directory with the shared directory
if self.staging_dir.exists() {
// Remove the old shared directory
if self.shared_target_dir.exists() {
fs::remove_dir_all(&self.shared_target_dir)?;
}
// Rename the staging directory to the shared directory
fs::rename(&self.staging_dir, &self.shared_target_dir)?;
// Create a new empty staging directory
fs::create_dir_all(&self.staging_dir)?;
}
}
Ok(self.shared_target_dir.clone())
}
/// Stop background preparation
fn stop_preparation(&mut self) {
// Signal the thread to stop
{
let mut running = self.is_running.lock().unwrap();
*running = false;
}
// Wait for the thread to finish
if let Some(thread) = self.prepare_thread.take() {
let _ = thread.join();
}
// Reset the running flag
{
let mut running = self.is_running.lock().unwrap();
*running = true;
}
}
/// Clean up resources
pub fn cleanup(&mut self) -> Result<(), Error> {
self.stop_preparation();
// Clean up directories
if self.shared_target_dir.exists() {
fs::remove_dir_all(&self.shared_target_dir)?;
}
if self.staging_dir.exists() {
fs::remove_dir_all(&self.staging_dir)?;
}
Ok(())
}
}
impl Drop for TargetPipeline {
fn drop(&mut self) {
self.stop_preparation();
let _ = self.cleanup();
}
}