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
55 changes: 46 additions & 9 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 Expand Up @@ -291,14 +328,14 @@ crate fn compare_json(actual: &serde_json::Value, expected: &str) {
}

#[derive(Clone, Copy, Debug)]
crate struct Src<'a, 'b> {
crate struct Src<'a> {
crate file_name: &'a Path,
// 1 indexed
crate line: usize,
crate name: &'b str,
crate name: &'a str,
}

crate fn src<'a, 'b>(file_name: &'a Path, line: usize, name: &'b str) -> Src<'a, 'b> {
crate fn src<'a>(file_name: &'a Path, line: usize, name: &'a str) -> Src<'a> {
Src {
file_name,
line,
Expand All @@ -319,7 +356,7 @@ impl Cache {
}
}

crate fn mk_ls_position(&mut self, src: Src<'_, '_>) -> ls_types::Position {
crate fn mk_ls_position(&mut self, src: Src<'_>) -> ls_types::Position {
let line = self.get_line(src);
let col = line
.find(src.name)
Expand Down Expand Up @@ -352,7 +389,7 @@ impl Cache {
}
}

fn get_line(&mut self, src: Src<'_, '_>) -> String {
fn get_line(&mut self, src: Src<'_>) -> String {
let base_path = &self.base_path;
let lines = self
.files
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
35 changes: 32 additions & 3 deletions 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 Expand Up @@ -365,8 +394,8 @@ impl ProjectBuilder {
}

impl Project {
pub fn root(&self) -> PathBuf {
self.root.clone()
pub fn root(&self) -> &Path {
&self.root
}

pub fn spawn_rls(&self) -> RlsHandle {
Expand Down
22 changes: 11 additions & 11 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ fn cmd_changing_workspace_lib_retains_bin_diagnostics() {
}
],
"textDocument": {
"uri": format!("file://{}/library/src/lib.rs", root_path.as_path().display()),
"uri": format!("file://{}/library/src/lib.rs", root_path.display()),
"version": 0
}
})),
Expand Down Expand Up @@ -385,7 +385,7 @@ fn cmd_changing_workspace_lib_retains_bin_diagnostics() {
}
],
"textDocument": {
"uri": format!("file://{}/library/src/lib.rs", root_path.as_path().display()),
"uri": format!("file://{}/library/src/lib.rs", root_path.display()),
"version": 1
}
})),
Expand Down Expand Up @@ -501,7 +501,7 @@ fn cmd_implicit_workspace_pick_up_lib_changes() {
}
],
"textDocument": {
"uri": format!("file://{}/inner/src/lib.rs", root_path.as_path().display()),
"uri": format!("file://{}/inner/src/lib.rs", root_path.display()),
"version": 0
}
})),
Expand Down Expand Up @@ -543,7 +543,7 @@ fn cmd_implicit_workspace_pick_up_lib_changes() {
}
],
"textDocument": {
"uri": format!("file://{}/inner/src/lib.rs", root_path.as_path().display()),
"uri": format!("file://{}/inner/src/lib.rs", root_path.display()),
"version": 1
}
})),
Expand Down Expand Up @@ -640,7 +640,7 @@ fn cmd_test_complete_self_crate_name() {
"line": 2
},
"textDocument": {
"uri": format!("file://{}/library/tests/test.rs", root_path.as_path().display()),
"uri": format!("file://{}/library/tests/test.rs", root_path.display()),
"version": 1
}
})),
Expand Down Expand Up @@ -734,7 +734,7 @@ fn test_completion_suggests_arguments_in_statements() {
"line": 3
},
"textDocument": {
"uri": format!("file://{}/library/tests/test.rs", root_path.as_path().display()),
"uri": format!("file://{}/library/tests/test.rs", root_path.display()),
"version": 1
}
})),
Expand Down Expand Up @@ -819,7 +819,7 @@ fn test_use_statement_completion_doesnt_suggest_arguments() {
"line": 2
},
"textDocument": {
"uri": format!("file://{}/library/tests/test.rs", root_path.as_path().display()),
"uri": format!("file://{}/library/tests/test.rs", root_path.display()),
"version": 1
}
})),
Expand Down Expand Up @@ -924,7 +924,7 @@ fn cmd_dependency_typo_and_fix() {
"workspace/didChangeWatchedFiles",
Some(json!({
"changes": [{
"uri": format!("file://{}/Cargo.toml", root_path.as_path().display()),
"uri": format!("file://{}/Cargo.toml", root_path.display()),
"type": 2
}],
})),
Expand All @@ -951,7 +951,7 @@ fn cmd_dependency_typo_and_fix() {
"workspace/didChangeWatchedFiles",
Some(json!({
"changes": [{
"uri": format!("file://{}/Cargo.toml", root_path.as_path().display()),
"uri": format!("file://{}/Cargo.toml", root_path.display()),
"type": 2
}],
})),
Expand Down Expand Up @@ -1220,7 +1220,7 @@ fn cmd_handle_utf16_unit_text_edits() {
"textDocument/didChange",
Some(json!(
{"textDocument": {
"uri": format!("file://{}/src/unrelated.rs", root_path.as_path().display()),
"uri": format!("file://{}/src/unrelated.rs", root_path.display()),
"version": 1
},
// "😢" -> ""
Expand Down Expand Up @@ -1283,7 +1283,7 @@ fn cmd_format_utf16_range() {
Some(json!(
{
"textDocument": {
"uri": format!("file://{}/src/main.rs", root_path.as_path().display()),
"uri": format!("file://{}/src/main.rs", root_path.display()),
"version": 1
},
"options": {
Expand Down