Skip to content

Commit cdb8c3a

Browse files
committed
Fix tests being non-deterministic
1 parent 0485a85 commit cdb8c3a

File tree

10 files changed

+209
-163
lines changed

10 files changed

+209
-163
lines changed

crates/base-db/src/input.rs

+30-3
Original file line numberDiff line numberDiff line change
@@ -295,11 +295,30 @@ pub struct CrateData {
295295
pub is_proc_macro: bool,
296296
}
297297

298-
#[derive(Default, Debug, Clone, PartialEq, Eq)]
298+
#[derive(Default, Clone, PartialEq, Eq)]
299299
pub struct Env {
300300
entries: FxHashMap<String, String>,
301301
}
302302

303+
impl fmt::Debug for Env {
304+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
305+
struct EnvDebug<'s>(Vec<(&'s String, &'s String)>);
306+
307+
impl fmt::Debug for EnvDebug<'_> {
308+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
309+
f.debug_map().entries(self.0.iter().copied()).finish()
310+
}
311+
}
312+
f.debug_struct("Env")
313+
.field("entries", &{
314+
let mut entries: Vec<_> = self.entries.iter().collect();
315+
entries.sort();
316+
EnvDebug(entries)
317+
})
318+
.finish()
319+
}
320+
}
321+
303322
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
304323
pub struct Dependency {
305324
pub crate_id: CrateId,
@@ -660,8 +679,16 @@ impl Env {
660679
self.entries.get(env).cloned()
661680
}
662681

663-
pub fn iter(&self) -> impl Iterator<Item = (&str, &str)> {
664-
self.entries.iter().map(|(k, v)| (k.as_str(), v.as_str()))
682+
pub fn extend_from_other(&mut self, other: &Env) {
683+
self.entries.extend(other.entries.iter().map(|(x, y)| (x.to_owned(), y.to_owned())));
684+
}
685+
}
686+
687+
impl From<Env> for Vec<(String, String)> {
688+
fn from(env: Env) -> Vec<(String, String)> {
689+
let mut entries: Vec<_> = env.entries.into_iter().collect();
690+
entries.sort();
691+
entries
665692
}
666693
}
667694

crates/cfg/src/lib.rs

-7
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,6 @@ impl CfgOptions {
5858
self.enabled.insert(CfgAtom::KeyValue { key, value });
5959
}
6060

61-
pub fn difference<'a>(
62-
&'a self,
63-
other: &'a CfgOptions,
64-
) -> impl Iterator<Item = &'a CfgAtom> + 'a {
65-
self.enabled.difference(&other.enabled)
66-
}
67-
6861
pub fn apply_diff(&mut self, diff: CfgDiff) {
6962
for atom in diff.enable {
7063
self.enabled.insert(atom);

crates/load-cargo/src/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -407,8 +407,7 @@ impl ProcMacroExpander for Expander {
407407
call_site: Span,
408408
mixed_site: Span,
409409
) -> Result<tt::Subtree<Span>, ProcMacroExpansionError> {
410-
let env = env.iter().map(|(k, v)| (k.to_owned(), v.to_owned())).collect();
411-
match self.0.expand(subtree, attrs, env, def_site, call_site, mixed_site) {
410+
match self.0.expand(subtree, attrs, env.clone(), def_site, call_site, mixed_site) {
412411
Ok(Ok(subtree)) => Ok(subtree),
413412
Ok(Err(err)) => Err(ProcMacroExpansionError::Panic(err.0)),
414413
Err(err) => Err(ProcMacroExpansionError::System(err.to_string())),

crates/proc-macro-api/src/lib.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub mod msg;
1111
mod process;
1212
mod version;
1313

14+
use base_db::Env;
1415
use indexmap::IndexSet;
1516
use paths::AbsPathBuf;
1617
use rustc_hash::FxHashMap;
@@ -152,16 +153,13 @@ impl ProcMacro {
152153
&self,
153154
subtree: &tt::Subtree<Span>,
154155
attr: Option<&tt::Subtree<Span>>,
155-
env: Vec<(String, String)>,
156+
env: Env,
156157
def_site: Span,
157158
call_site: Span,
158159
mixed_site: Span,
159160
) -> Result<Result<tt::Subtree<Span>, PanicMessage>, ServerError> {
160161
let version = self.process.lock().unwrap_or_else(|e| e.into_inner()).version();
161-
let current_dir = env
162-
.iter()
163-
.find(|(name, _)| name == "CARGO_MANIFEST_DIR")
164-
.map(|(_, value)| value.clone());
162+
let current_dir = env.get("CARGO_MANIFEST_DIR");
165163

166164
let mut span_data_table = IndexSet::default();
167165
let def_site = span_data_table.insert_full(def_site).0;
@@ -172,7 +170,7 @@ impl ProcMacro {
172170
macro_name: self.name.to_string(),
173171
attributes: attr.map(|subtree| FlatTree::new(subtree, version, &mut span_data_table)),
174172
lib: self.dylib_path.to_path_buf().into(),
175-
env,
173+
env: env.into(),
176174
current_dir,
177175
has_global_spans: ExpnGlobals {
178176
serialize: version >= HAS_GLOBAL_SPANS,

crates/project-model/src/tests.rs

+7
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ fn get_test_json_file<T: DeserializeOwned>(file: &str) -> T {
9898
}
9999
}
100100

101+
fn replace_cargo(s: &mut String) {
102+
let path = toolchain::Tool::Cargo.path().to_string().escape_debug().collect::<String>();
103+
*s = s.replace(&path, "$CARGO$");
104+
}
105+
101106
fn replace_root(s: &mut String, direction: bool) {
102107
if direction {
103108
let root = if cfg!(windows) { r#"C:\\ROOT\"# } else { "/ROOT/" };
@@ -156,7 +161,9 @@ fn to_crate_graph(project_workspace: ProjectWorkspace) -> (CrateGraph, ProcMacro
156161

157162
fn check_crate_graph(crate_graph: CrateGraph, expect: ExpectFile) {
158163
let mut crate_graph = format!("{crate_graph:#?}");
164+
159165
replace_root(&mut crate_graph, false);
166+
replace_cargo(&mut crate_graph);
160167
replace_fake_sys_root(&mut crate_graph);
161168
expect.assert_eq(&crate_graph);
162169
}

crates/project-model/test_data/output/cargo_hello_world_project_model.txt

+52-48
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,29 @@
1717
),
1818
cfg_options: CfgOptions(
1919
[
20-
"debug_assertions",
2120
"rust_analyzer",
2221
"test",
2322
],
2423
),
2524
potential_cfg_options: None,
2625
env: Env {
2726
entries: {
28-
"CARGO_PKG_LICENSE": "",
29-
"CARGO_PKG_VERSION_MAJOR": "0",
27+
"CARGO": "$CARGO$",
28+
"CARGO_CRATE_NAME": "hello_world",
3029
"CARGO_MANIFEST_DIR": "$ROOT$hello-world",
31-
"CARGO_PKG_VERSION": "0.1.0",
3230
"CARGO_PKG_AUTHORS": "",
33-
"CARGO_CRATE_NAME": "hello_world",
34-
"CARGO_PKG_LICENSE_FILE": "",
35-
"CARGO_PKG_HOMEPAGE": "",
3631
"CARGO_PKG_DESCRIPTION": "",
32+
"CARGO_PKG_HOMEPAGE": "",
33+
"CARGO_PKG_LICENSE": "",
34+
"CARGO_PKG_LICENSE_FILE": "",
3735
"CARGO_PKG_NAME": "hello-world",
38-
"CARGO_PKG_VERSION_PATCH": "0",
39-
"CARGO": "cargo",
36+
"CARGO_PKG_README": "",
4037
"CARGO_PKG_REPOSITORY": "",
38+
"CARGO_PKG_RUST_VERSION": "",
39+
"CARGO_PKG_VERSION": "0.1.0",
40+
"CARGO_PKG_VERSION_MAJOR": "0",
4141
"CARGO_PKG_VERSION_MINOR": "1",
42+
"CARGO_PKG_VERSION_PATCH": "0",
4243
"CARGO_PKG_VERSION_PRE": "",
4344
},
4445
},
@@ -77,28 +78,29 @@
7778
),
7879
cfg_options: CfgOptions(
7980
[
80-
"debug_assertions",
8181
"rust_analyzer",
8282
"test",
8383
],
8484
),
8585
potential_cfg_options: None,
8686
env: Env {
8787
entries: {
88-
"CARGO_PKG_LICENSE": "",
89-
"CARGO_PKG_VERSION_MAJOR": "0",
88+
"CARGO": "$CARGO$",
89+
"CARGO_CRATE_NAME": "hello_world",
9090
"CARGO_MANIFEST_DIR": "$ROOT$hello-world",
91-
"CARGO_PKG_VERSION": "0.1.0",
9291
"CARGO_PKG_AUTHORS": "",
93-
"CARGO_CRATE_NAME": "hello_world",
94-
"CARGO_PKG_LICENSE_FILE": "",
95-
"CARGO_PKG_HOMEPAGE": "",
9692
"CARGO_PKG_DESCRIPTION": "",
93+
"CARGO_PKG_HOMEPAGE": "",
94+
"CARGO_PKG_LICENSE": "",
95+
"CARGO_PKG_LICENSE_FILE": "",
9796
"CARGO_PKG_NAME": "hello-world",
98-
"CARGO_PKG_VERSION_PATCH": "0",
99-
"CARGO": "cargo",
97+
"CARGO_PKG_README": "",
10098
"CARGO_PKG_REPOSITORY": "",
99+
"CARGO_PKG_RUST_VERSION": "",
100+
"CARGO_PKG_VERSION": "0.1.0",
101+
"CARGO_PKG_VERSION_MAJOR": "0",
101102
"CARGO_PKG_VERSION_MINOR": "1",
103+
"CARGO_PKG_VERSION_PATCH": "0",
102104
"CARGO_PKG_VERSION_PRE": "",
103105
},
104106
},
@@ -144,28 +146,29 @@
144146
),
145147
cfg_options: CfgOptions(
146148
[
147-
"debug_assertions",
148149
"rust_analyzer",
149150
"test",
150151
],
151152
),
152153
potential_cfg_options: None,
153154
env: Env {
154155
entries: {
155-
"CARGO_PKG_LICENSE": "",
156-
"CARGO_PKG_VERSION_MAJOR": "0",
156+
"CARGO": "$CARGO$",
157+
"CARGO_CRATE_NAME": "an_example",
157158
"CARGO_MANIFEST_DIR": "$ROOT$hello-world",
158-
"CARGO_PKG_VERSION": "0.1.0",
159159
"CARGO_PKG_AUTHORS": "",
160-
"CARGO_CRATE_NAME": "an_example",
161-
"CARGO_PKG_LICENSE_FILE": "",
162-
"CARGO_PKG_HOMEPAGE": "",
163160
"CARGO_PKG_DESCRIPTION": "",
161+
"CARGO_PKG_HOMEPAGE": "",
162+
"CARGO_PKG_LICENSE": "",
163+
"CARGO_PKG_LICENSE_FILE": "",
164164
"CARGO_PKG_NAME": "hello-world",
165-
"CARGO_PKG_VERSION_PATCH": "0",
166-
"CARGO": "cargo",
165+
"CARGO_PKG_README": "",
167166
"CARGO_PKG_REPOSITORY": "",
167+
"CARGO_PKG_RUST_VERSION": "",
168+
"CARGO_PKG_VERSION": "0.1.0",
169+
"CARGO_PKG_VERSION_MAJOR": "0",
168170
"CARGO_PKG_VERSION_MINOR": "1",
171+
"CARGO_PKG_VERSION_PATCH": "0",
169172
"CARGO_PKG_VERSION_PRE": "",
170173
},
171174
},
@@ -211,28 +214,29 @@
211214
),
212215
cfg_options: CfgOptions(
213216
[
214-
"debug_assertions",
215217
"rust_analyzer",
216218
"test",
217219
],
218220
),
219221
potential_cfg_options: None,
220222
env: Env {
221223
entries: {
222-
"CARGO_PKG_LICENSE": "",
223-
"CARGO_PKG_VERSION_MAJOR": "0",
224+
"CARGO": "$CARGO$",
225+
"CARGO_CRATE_NAME": "it",
224226
"CARGO_MANIFEST_DIR": "$ROOT$hello-world",
225-
"CARGO_PKG_VERSION": "0.1.0",
226227
"CARGO_PKG_AUTHORS": "",
227-
"CARGO_CRATE_NAME": "it",
228-
"CARGO_PKG_LICENSE_FILE": "",
229-
"CARGO_PKG_HOMEPAGE": "",
230228
"CARGO_PKG_DESCRIPTION": "",
229+
"CARGO_PKG_HOMEPAGE": "",
230+
"CARGO_PKG_LICENSE": "",
231+
"CARGO_PKG_LICENSE_FILE": "",
231232
"CARGO_PKG_NAME": "hello-world",
232-
"CARGO_PKG_VERSION_PATCH": "0",
233-
"CARGO": "cargo",
233+
"CARGO_PKG_README": "",
234234
"CARGO_PKG_REPOSITORY": "",
235+
"CARGO_PKG_RUST_VERSION": "",
236+
"CARGO_PKG_VERSION": "0.1.0",
237+
"CARGO_PKG_VERSION_MAJOR": "0",
235238
"CARGO_PKG_VERSION_MINOR": "1",
239+
"CARGO_PKG_VERSION_PATCH": "0",
236240
"CARGO_PKG_VERSION_PRE": "",
237241
},
238242
},
@@ -278,15 +282,13 @@
278282
),
279283
cfg_options: CfgOptions(
280284
[
281-
"debug_assertions",
282285
"feature=default",
283286
"feature=std",
284287
],
285288
),
286289
potential_cfg_options: Some(
287290
CfgOptions(
288291
[
289-
"debug_assertions",
290292
"feature=align",
291293
"feature=const-extern-fn",
292294
"feature=default",
@@ -299,20 +301,22 @@
299301
),
300302
env: Env {
301303
entries: {
302-
"CARGO_PKG_LICENSE": "",
303-
"CARGO_PKG_VERSION_MAJOR": "0",
304-
"CARGO_MANIFEST_DIR": "$ROOT$.cargo/registry/src/jiasu.xzqcsaa.nyc.mn-1ecc6299db9ec823/libc-0.2.98",
305-
"CARGO_PKG_VERSION": "0.2.98",
306-
"CARGO_PKG_AUTHORS": "",
304+
"CARGO": "$CARGO$",
307305
"CARGO_CRATE_NAME": "libc",
306+
"CARGO_MANIFEST_DIR": "$ROOT$.cargo/registry/src/jiasu.xzqcsaa.nyc.mn-1ecc6299db9ec823/libc-0.2.98",
307+
"CARGO_PKG_AUTHORS": "The Rust Project Developers",
308+
"CARGO_PKG_DESCRIPTION": "Raw FFI bindings to platform libraries like libc.\n",
309+
"CARGO_PKG_HOMEPAGE": "https://github.com/rust-lang/libc",
310+
"CARGO_PKG_LICENSE": "MIT OR Apache-2.0",
308311
"CARGO_PKG_LICENSE_FILE": "",
309-
"CARGO_PKG_HOMEPAGE": "",
310-
"CARGO_PKG_DESCRIPTION": "",
311312
"CARGO_PKG_NAME": "libc",
312-
"CARGO_PKG_VERSION_PATCH": "98",
313-
"CARGO": "cargo",
314-
"CARGO_PKG_REPOSITORY": "",
313+
"CARGO_PKG_README": "README.md",
314+
"CARGO_PKG_REPOSITORY": "https://github.com/rust-lang/libc",
315+
"CARGO_PKG_RUST_VERSION": "",
316+
"CARGO_PKG_VERSION": "0.2.98",
317+
"CARGO_PKG_VERSION_MAJOR": "0",
315318
"CARGO_PKG_VERSION_MINOR": "2",
319+
"CARGO_PKG_VERSION_PATCH": "98",
316320
"CARGO_PKG_VERSION_PRE": "",
317321
},
318322
},

0 commit comments

Comments
 (0)