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

Overhaul fixture handling in tests #1190

Merged
merged 10 commits into from
Dec 18, 2018
45 changes: 41 additions & 4 deletions src/test/harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,57 @@ use rls_analysis::{AnalysisHost, Target};
use rls_vfs::Vfs;
use serde_json;

#[path = "../../tests/support/mod.rs"]
Xanewok marked this conversation as resolved.
Show resolved Hide resolved
mod support;

lazy_static! {
static ref COUNTER: AtomicUsize = AtomicUsize::new(0);
}

crate struct Environment {
crate config: Option<Config>,
crate cache: Cache,
crate target_path: PathBuf,
}

impl Environment {
crate fn new(project_dir: &str) -> Self {
use std::sync::atomic::{AtomicUsize, Ordering};
crate fn generate_from_fixture(fixture_dir: impl AsRef<Path>) -> Self {
let fixture_dir = fixture_dir.as_ref();

let _ = env_logger::try_init();
if env::var("RUSTC").is_err() {
env::set_var("RUSTC", "rustc");
}

let manifest_dir = &Path::new(env!("CARGO_MANIFEST_DIR"));
let fixture_dir = manifest_dir.join("test_data").join(fixture_dir);

let project = support::ProjectBuilder::try_from_fixture(fixture_dir)
.unwrap()
.build();

let target_dir = env::var("CARGO_TARGET_DIR")
.map(|s| Path::new(&s).to_owned())
.unwrap_or_else(|_| manifest_dir.join("target"));

lazy_static! {
static ref COUNTER: AtomicUsize = AtomicUsize::new(0);
let working_dir = target_dir
.join("tests")
.join(format!("{}", COUNTER.fetch_add(1, Ordering::Relaxed)));

let mut config = Config::default();
config.target_dir = Inferrable::Specified(Some(working_dir.clone()));
config.unstable_features = true;

let cache = Cache::new(project.root().to_owned());

Self {
config: Some(config),
cache,
target_path: working_dir,
}
}

crate fn new(project_dir: &str) -> Self {
let _ = env_logger::try_init();
if env::var("RUSTC").is_err() {
env::set_var("RUSTC", "rustc");
Expand Down
3 changes: 3 additions & 0 deletions src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ use std::sync::{Arc, Mutex};
use std::time::Instant;
use url::Url;

#[path = "../../tests/support/mod.rs"]
mod support;

fn initialize(id: usize, root_path: Option<String>) -> Request<ls_server::InitializeRequest> {
initialize_with_opts(id, root_path, None)
}
Expand Down
31 changes: 30 additions & 1 deletion tests/support/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use serde_json;
use serde_json::{self, json};
use walkdir::WalkDir;

use std::env;
use std::fs;
use std::io::{self, Read, Write};
Expand Down Expand Up @@ -335,6 +337,33 @@ impl ProjectBuilder {
}
}

pub fn try_from_fixture(fixture_dir: impl AsRef<Path>) -> std::io::Result<Self> {
let fixture_dir = fixture_dir.as_ref();

let dirname = fixture_dir.file_name()
.ok_or_else(|| std::io::Error::new(std::io::ErrorKind::NotFound, "No filename"))?;

// Generate a new, unique directory for working dir under target/
let genroot = paths::root();
let mut builder = ProjectBuilder::new(genroot.join(dirname));

// Read existing fixture data to be later copied into scratch genroot
for entry in WalkDir::new(fixture_dir).into_iter() {
let entry = entry?;
let path = entry.path();
let body = if !std::fs::metadata(path)?.is_dir() {
std::fs::read_to_string(path)?
} else {
continue;
};

let relative = entry.path().strip_prefix(fixture_dir).unwrap();
builder._file(relative, &body);
}

Ok(builder)
}

pub fn file<B: AsRef<Path>>(mut self, path: B, body: &str) -> Self {
self._file(path.as_ref(), body);
self
Expand Down