Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.

Commit 659eb94

Browse files
committed
Generate scratchpad test environment from test fixtures
1 parent 9747636 commit 659eb94

File tree

3 files changed

+74
-5
lines changed

3 files changed

+74
-5
lines changed

src/test/harness.rs

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,57 @@ use rls_analysis::{AnalysisHost, Target};
2727
use rls_vfs::Vfs;
2828
use serde_json;
2929

30+
#[path = "../../tests/support/mod.rs"]
31+
mod support;
32+
33+
lazy_static! {
34+
static ref COUNTER: AtomicUsize = AtomicUsize::new(0);
35+
}
36+
3037
crate struct Environment {
3138
crate config: Option<Config>,
3239
crate cache: Cache,
3340
crate target_path: PathBuf,
3441
}
3542

3643
impl Environment {
37-
crate fn new(project_dir: &str) -> Self {
38-
use std::sync::atomic::{AtomicUsize, Ordering};
44+
crate fn generate_from_fixture(fixture_dir: impl AsRef<Path>) -> Self {
45+
let fixture_dir = fixture_dir.as_ref();
46+
47+
let _ = env_logger::try_init();
48+
if env::var("RUSTC").is_err() {
49+
env::set_var("RUSTC", "rustc");
50+
}
51+
52+
let manifest_dir = &Path::new(env!("CARGO_MANIFEST_DIR"));
53+
let fixture_dir = manifest_dir.join("test_data").join(fixture_dir);
54+
55+
let project = support::ProjectBuilder::try_from_fixture(fixture_dir)
56+
.unwrap()
57+
.build();
58+
59+
let target_dir = env::var("CARGO_TARGET_DIR")
60+
.map(|s| Path::new(&s).to_owned())
61+
.unwrap_or_else(|_| manifest_dir.join("target"));
3962

40-
lazy_static! {
41-
static ref COUNTER: AtomicUsize = AtomicUsize::new(0);
63+
let working_dir = target_dir
64+
.join("tests")
65+
.join(format!("{}", COUNTER.fetch_add(1, Ordering::Relaxed)));
66+
67+
let mut config = Config::default();
68+
config.target_dir = Inferrable::Specified(Some(working_dir.clone()));
69+
config.unstable_features = true;
70+
71+
let cache = Cache::new(project.root().to_owned());
72+
73+
Self {
74+
config: Some(config),
75+
cache,
76+
target_path: working_dir,
4277
}
78+
}
4379

80+
crate fn new(project_dir: &str) -> Self {
4481
let _ = env_logger::try_init();
4582
if env::var("RUSTC").is_err() {
4683
env::set_var("RUSTC", "rustc");

src/test/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ use std::sync::{Arc, Mutex};
3333
use std::time::Instant;
3434
use url::Url;
3535

36+
#[path = "../../tests/support/mod.rs"]
37+
mod support;
38+
3639
fn initialize(id: usize, root_path: Option<String>) -> Request<ls_server::InitializeRequest> {
3740
initialize_with_opts(id, root_path, None)
3841
}

tests/support/mod.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use serde_json;
11+
use serde_json::{self, json};
12+
use walkdir::WalkDir;
13+
1214
use std::env;
1315
use std::fs;
1416
use std::io::{self, Read, Write};
@@ -335,6 +337,33 @@ impl ProjectBuilder {
335337
}
336338
}
337339

340+
pub fn try_from_fixture(fixture_dir: impl AsRef<Path>) -> std::io::Result<Self> {
341+
let fixture_dir = fixture_dir.as_ref();
342+
343+
let dirname = fixture_dir.file_name()
344+
.ok_or_else(|| std::io::Error::new(std::io::ErrorKind::NotFound, "No filename"))?;
345+
346+
// Generate a new, unique directory for working dir under target/
347+
let genroot = paths::root();
348+
let mut builder = ProjectBuilder::new(genroot.join(dirname));
349+
350+
// Read existing fixture data to be later copied into scratch genroot
351+
for entry in WalkDir::new(fixture_dir).into_iter() {
352+
let entry = entry?;
353+
let path = entry.path();
354+
let body = if !std::fs::metadata(path)?.is_dir() {
355+
std::fs::read_to_string(path)?
356+
} else {
357+
continue;
358+
};
359+
360+
let relative = entry.path().strip_prefix(fixture_dir).unwrap();
361+
builder._file(relative, &body);
362+
}
363+
364+
Ok(builder)
365+
}
366+
338367
pub fn file<B: AsRef<Path>>(mut self, path: B, body: &str) -> Self {
339368
self._file(path.as_ref(), body);
340369
self

0 commit comments

Comments
 (0)