-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathproject.rs
More file actions
200 lines (174 loc) · 6.36 KB
/
project.rs
File metadata and controls
200 lines (174 loc) · 6.36 KB
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
// internal helper code
#![allow(dead_code)]
use super::utils::assert_matches_regex;
use std::path::PathBuf;
use std::process::{Command, ExitStatus};
use std::sync::LazyLock;
use std::sync::atomic::{AtomicU64, Ordering};
use tempfile::TempDir;
// Cache build output from TempRustProject across tests within the same process.
//
// Under coverage (CARGO_LLVM_COV=1), we reuse the main project's llvm-cov target
// dir so that subprocess builds share the same instrumented artifacts. This lets
// cargo-llvm-cov's report phase map subprocess profraw data back to source files.
// Cargo's internal file locks prevent corruption from concurrent access.
//
// Outside coverage, each test binary gets its own target dir (via PID) to avoid
// heavy lock contention when multiple test binaries build in parallel.
static SHARED_TARGET_DIR: LazyLock<PathBuf> = LazyLock::new(|| {
if std::env::var("CARGO_LLVM_COV").is_ok() {
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("target/llvm-cov-target");
return path;
}
let path = std::env::temp_dir().join(format!("hegel-test-cargo-target-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&path);
std::fs::create_dir_all(&path).unwrap();
path
});
// use a unique package name in our Cargo.toml to avoid lock contention of parallel
// cargo builds within the shared target dir.
static PACKAGE_NAME_ID: AtomicU64 = AtomicU64::new(0);
pub struct TempRustProject {
_temp_dir: TempDir,
project_path: PathBuf,
crate_name: String,
env_vars: Vec<(String, String)>,
env_removes: Vec<String>,
features: Vec<String>,
expect_failure: Option<String>,
}
pub struct RunOutput {
pub status: ExitStatus,
#[allow(dead_code)]
pub stdout: String,
pub stderr: String,
}
impl TempRustProject {
pub fn new() -> Self {
let temp_dir = TempDir::new().unwrap();
let project_path = temp_dir.path().to_path_buf();
let id = PACKAGE_NAME_ID.fetch_add(1, Ordering::Relaxed);
let crate_name = format!("temp_hegel_test_{}", id);
// Copy the main project's Cargo.lock so the temp project uses the same
// pinned dependency versions. Without this, cargo resolves fresh and may
// pull in crates (e.g. getrandom 0.4+) that require a newer Rust edition
// than our MSRV supports.
let lock_src = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("Cargo.lock");
if lock_src.exists() {
std::fs::copy(&lock_src, project_path.join("Cargo.lock")).unwrap();
}
Self {
_temp_dir: temp_dir,
project_path,
crate_name,
env_vars: Vec::new(),
env_removes: Vec::new(),
features: Vec::new(),
expect_failure: None,
}
}
pub fn main_file(self, code: &str) -> Self {
let src_dir = self.project_path.join("src");
std::fs::create_dir_all(&src_dir).unwrap();
std::fs::write(src_dir.join("main.rs"), code).unwrap();
self
}
pub fn test_file(self, name: &str, content: &str) -> Self {
let tests_dir = self.project_path.join("tests");
std::fs::create_dir_all(&tests_dir).unwrap();
std::fs::write(tests_dir.join(name), content).unwrap();
self
}
pub fn feature(mut self, feature: &str) -> Self {
self.features.push(feature.to_string());
self
}
pub fn env(mut self, key: &str, value: &str) -> Self {
self.env_vars.push((key.to_string(), value.to_string()));
self
}
pub fn expect_failure(mut self, pattern: &str) -> Self {
self.expect_failure = Some(pattern.to_string());
self
}
pub fn env_remove(mut self, key: &str) -> Self {
self.env_removes.push(key.to_string());
self
}
fn cargo(&self, args: &[&str]) -> RunOutput {
let cached_target = &*SHARED_TARGET_DIR;
let hegel_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let features = if self.features.is_empty() {
String::new()
} else {
format!(
", features = [{}]",
self.features
.iter()
.map(|f| format!("\"{}\"", f))
.collect::<Vec<_>>()
.join(", ")
)
};
let cargo_toml = format!(
r#"[package]
name = "{crate_name}"
version = "0.1.0"
edition = "2021"
[dependencies]
hegeltest = {{ path = "{path}"{features} }}
"#,
crate_name = self.crate_name,
path = hegel_path.display(),
features = features,
);
std::fs::write(self.project_path.join("Cargo.toml"), cargo_toml).unwrap();
let mut cmd = Command::new(env!("CARGO"));
cmd.args(args)
.current_dir(&self.project_path)
.env("CARGO_TARGET_DIR", cached_target);
for key in &self.env_removes {
cmd.env_remove(key);
}
for (key, value) in &self.env_vars {
cmd.env(key, value);
}
let output = cmd.output().unwrap();
let run_output = RunOutput {
status: output.status,
stdout: String::from_utf8_lossy(&output.stdout).trim().to_string(),
stderr: String::from_utf8_lossy(&output.stderr).trim().to_string(),
};
match &self.expect_failure {
None => {
assert!(
run_output.status.success(),
"Expected command to succeed.\nstdout:\n{}\nstderr:\n{}",
run_output.stdout,
run_output.stderr
);
}
Some(pattern) => {
assert!(
!run_output.status.success(),
"Expected command to fail.\nstdout:\n{}\nstderr:\n{}",
run_output.stdout,
run_output.stderr
);
let combined = format!("{}\n{}", run_output.stdout, run_output.stderr);
assert_matches_regex(&combined, pattern);
}
}
run_output
}
pub fn cargo_run(&self, args: &[&str]) -> RunOutput {
let mut all = vec!["run", "--quiet"];
all.extend(args);
self.cargo(&all)
}
pub fn cargo_test(&self, args: &[&str]) -> RunOutput {
let mut all = vec!["test", "--quiet"];
all.extend(args);
self.cargo(&all)
}
}