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

Commit c7d507f

Browse files
committed
Clean up imports and modules
1 parent 433c175 commit c7d507f

File tree

5 files changed

+150
-164
lines changed

5 files changed

+150
-164
lines changed

src/test/harness.rs

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

30-
#[path = "../../tests/support/mod.rs"]
31-
mod support;
30+
#[path = "../../tests/support/project_builder.rs"]
31+
mod project_builder;
3232

3333
lazy_static! {
3434
static ref COUNTER: AtomicUsize = AtomicUsize::new(0);
@@ -50,7 +50,7 @@ impl Environment {
5050
}
5151

5252
let fixture_dir = FIXTURES_DIR.join(fixture_dir.as_ref());
53-
let project = support::ProjectBuilder::try_from_fixture(fixture_dir)
53+
let project = project_builder::ProjectBuilder::try_from_fixture(fixture_dir)
5454
.unwrap()
5555
.build();
5656

@@ -74,38 +74,6 @@ impl Environment {
7474
target_path: working_dir,
7575
}
7676
}
77-
78-
crate fn new(project_dir: &str) -> Self {
79-
let _ = env_logger::try_init();
80-
if env::var("RUSTC").is_err() {
81-
env::set_var("RUSTC", "rustc");
82-
}
83-
84-
let cur_dir = env::var_os("RLS_TEST_WORKSPACE_DIR")
85-
.map(PathBuf::from)
86-
.unwrap_or_else(|| MANIFEST_DIR.to_owned());
87-
let project_path = cur_dir.join("test_data").join(project_dir);
88-
89-
let target_dir = env::var("CARGO_TARGET_DIR")
90-
.map(|s| Path::new(&s).to_owned())
91-
.unwrap_or_else(|_| cur_dir.join("target"));
92-
93-
let working_dir = target_dir
94-
.join("tests")
95-
.join(format!("{}", COUNTER.fetch_add(1, Ordering::Relaxed)));
96-
97-
let mut config = Config::default();
98-
config.target_dir = Inferrable::Specified(Some(working_dir.clone()));
99-
config.unstable_features = true;
100-
101-
let cache = Cache::new(project_path);
102-
103-
Self {
104-
config: Some(config),
105-
cache,
106-
target_path: working_dir,
107-
}
108-
}
10977
}
11078

11179
impl Environment {

src/test/mod.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ 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-
3936
fn initialize(id: usize, root_path: Option<String>) -> Request<ls_server::InitializeRequest> {
4037
initialize_with_opts(id, root_path, None)
4138
}

tests/support/mod.rs

Lines changed: 7 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,23 @@
99
// except according to those terms.
1010

1111
use serde_json::{self, json};
12-
use walkdir::WalkDir;
1312

1413
use std::env;
15-
use std::fs;
1614
use std::io::{self, Read, Write};
1715
use std::mem;
1816
use std::panic;
19-
use std::path::{Path, PathBuf};
17+
use std::path::PathBuf;
2018
use std::process::{Child, ChildStdin, Command, Stdio};
2119
use std::str;
2220
use std::sync::{Arc, Mutex};
2321
use std::thread;
2422
use std::time::{Duration, Instant};
2523

26-
use self::paths::TestPathExt;
27-
28-
pub mod paths;
24+
// FIXME(move unit tests here): Right now the modules are organized as follows:
25+
// support/mod.rs -> project_builder.rs -> paths.rs.
26+
// This is done so that the main binary can directly pull in project_builder.rs
27+
// to not duplicate logic and use it for generating data for project unit tests.
28+
pub mod project_builder;
2929

3030
/// Parse valid LSP stdout into a list of json messages
3131
pub fn parse_messages(stdout: &str) -> Vec<String> {
@@ -289,115 +289,7 @@ impl RlsStdout {
289289
}
290290
}
291291

292-
#[derive(PartialEq, Clone)]
293-
struct FileBuilder {
294-
path: PathBuf,
295-
body: String,
296-
}
297-
298-
impl FileBuilder {
299-
pub fn new(path: PathBuf, body: &str) -> FileBuilder {
300-
FileBuilder {
301-
path,
302-
body: body.to_string(),
303-
}
304-
}
305-
306-
fn mk(&self) {
307-
self.dirname().mkdir_p();
308-
309-
let mut file = fs::File::create(&self.path)
310-
.unwrap_or_else(|e| panic!("could not create file {}: {}", self.path.display(), e));
311-
312-
file.write_all(self.body.as_bytes()).unwrap();
313-
}
314-
315-
fn dirname(&self) -> &Path {
316-
self.path.parent().unwrap()
317-
}
318-
}
319-
320-
#[derive(PartialEq, Clone)]
321-
pub struct Project {
322-
root: PathBuf,
323-
}
324-
325-
#[must_use]
326-
#[derive(PartialEq, Clone)]
327-
pub struct ProjectBuilder {
328-
root: Project,
329-
files: Vec<FileBuilder>,
330-
}
331-
332-
impl ProjectBuilder {
333-
pub fn new(root: PathBuf) -> ProjectBuilder {
334-
ProjectBuilder {
335-
root: Project { root },
336-
files: vec![],
337-
}
338-
}
339-
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-
367-
pub fn file<B: AsRef<Path>>(mut self, path: B, body: &str) -> Self {
368-
self._file(path.as_ref(), body);
369-
self
370-
}
371-
372-
fn _file(&mut self, path: &Path, body: &str) {
373-
self.files
374-
.push(FileBuilder::new(self.root.root.join(path), body));
375-
}
376-
377-
pub fn build(self) -> Project {
378-
// First, clean the directory if it already exists
379-
self.rm_root();
380-
381-
// Create the empty directory
382-
self.root.root.mkdir_p();
383-
384-
for file in &self.files {
385-
file.mk();
386-
}
387-
388-
self.root
389-
}
390-
391-
fn rm_root(&self) {
392-
self.root.root.rm_rf()
393-
}
394-
}
395-
396-
impl Project {
397-
pub fn root(&self) -> &Path {
398-
&self.root
399-
}
400-
292+
impl project_builder::Project {
401293
pub fn spawn_rls(&self) -> RlsHandle {
402294
RlsHandle::new(
403295
Command::new(rls_exe())
@@ -410,11 +302,6 @@ impl Project {
410302
}
411303
}
412304

413-
// Generates a project layout
414-
pub fn project(name: &str) -> ProjectBuilder {
415-
ProjectBuilder::new(paths::root().join(name))
416-
}
417-
418305
// Path to cargo executables
419306
pub fn target_conf_dir() -> PathBuf {
420307
let mut path = env::current_exe().unwrap();

tests/support/project_builder.rs

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
//! Contains helper types that are capable of dynamically creating project
2+
//! layouts under target/ for testing purposes.
3+
//! This module is currently pulled by main binary and Cargo integration tests.
4+
#![allow(dead_code)]
5+
6+
use walkdir::WalkDir;
7+
8+
use std::path::{Path, PathBuf};
9+
use std::fs;
10+
use std::io::{Write};
11+
12+
use self::paths::TestPathExt;
13+
14+
// Modules are organized: support/mod.rs -> project_builder.rs -> paths.rs,
15+
// see note in support.mod.rs
16+
#[path = "paths.rs"]
17+
mod paths;
18+
19+
#[derive(PartialEq, Clone)]
20+
struct FileBuilder {
21+
path: PathBuf,
22+
body: String,
23+
}
24+
25+
impl FileBuilder {
26+
pub fn new(path: PathBuf, body: &str) -> FileBuilder {
27+
FileBuilder {
28+
path,
29+
body: body.to_string(),
30+
}
31+
}
32+
33+
fn mk(&self) {
34+
self.dirname().mkdir_p();
35+
36+
let mut file = fs::File::create(&self.path)
37+
.unwrap_or_else(|e| panic!("could not create file {}: {}", self.path.display(), e));
38+
39+
file.write_all(self.body.as_bytes()).unwrap();
40+
}
41+
42+
fn dirname(&self) -> &Path {
43+
self.path.parent().unwrap()
44+
}
45+
}
46+
47+
#[derive(PartialEq, Clone)]
48+
pub struct Project {
49+
root: PathBuf,
50+
}
51+
52+
#[must_use]
53+
#[derive(PartialEq, Clone)]
54+
pub struct ProjectBuilder {
55+
root: Project,
56+
files: Vec<FileBuilder>,
57+
}
58+
59+
impl ProjectBuilder {
60+
pub fn new(root: PathBuf) -> ProjectBuilder {
61+
ProjectBuilder {
62+
root: Project { root },
63+
files: vec![],
64+
}
65+
}
66+
67+
pub fn try_from_fixture(fixture_dir: impl AsRef<Path>) -> std::io::Result<Self> {
68+
let fixture_dir = fixture_dir.as_ref();
69+
70+
let dirname = fixture_dir.file_name()
71+
.ok_or_else(|| std::io::Error::new(std::io::ErrorKind::NotFound, "No filename"))?;
72+
73+
// Generate a new, unique directory for working dir under target/
74+
let genroot = paths::root();
75+
let mut builder = ProjectBuilder::new(genroot.join(dirname));
76+
77+
// Read existing fixture data to be later copied into scratch genroot
78+
for entry in WalkDir::new(fixture_dir).into_iter() {
79+
let entry = entry?;
80+
let path = entry.path();
81+
let body = if !std::fs::metadata(path)?.is_dir() {
82+
std::fs::read_to_string(path)?
83+
} else {
84+
continue;
85+
};
86+
87+
let relative = entry.path().strip_prefix(fixture_dir).unwrap();
88+
builder._file(relative, &body);
89+
}
90+
91+
Ok(builder)
92+
}
93+
94+
pub fn file<B: AsRef<Path>>(mut self, path: B, body: &str) -> Self {
95+
self._file(path.as_ref(), body);
96+
self
97+
}
98+
99+
fn _file(&mut self, path: &Path, body: &str) {
100+
self.files
101+
.push(FileBuilder::new(self.root.root.join(path), body));
102+
}
103+
104+
pub fn build(self) -> Project {
105+
// First, clean the directory if it already exists
106+
self.rm_root();
107+
108+
// Create the empty directory
109+
self.root.root.mkdir_p();
110+
111+
for file in &self.files {
112+
file.mk();
113+
}
114+
115+
self.root
116+
}
117+
118+
fn rm_root(&self) {
119+
self.root.root.rm_rf()
120+
}
121+
}
122+
123+
impl Project {
124+
pub fn root(&self) -> &Path {
125+
&self.root
126+
}
127+
}
128+
129+
// Generates a project layout
130+
pub fn project(name: &str) -> ProjectBuilder {
131+
ProjectBuilder::new(paths::root().join(name))
132+
}
133+
134+

tests/tests.rs

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

11-
#[macro_use]
12-
extern crate serde_json;
11+
use serde_json::{self, json};
1312

14-
mod support;
15-
16-
use self::support::{basic_bin_manifest, project};
17-
use crate::support::RlsStdout;
1813
use std::io::Write;
1914
use std::time::Duration;
2015

16+
use self::support::{basic_bin_manifest, RlsStdout};
17+
use self::support::project_builder::project;
18+
19+
mod support;
20+
2121
/// Returns a timeout for waiting for rls stdout messages
2222
///
2323
/// Env var `RLS_TEST_WAIT_FOR_AGES` allows super long waiting for CI

0 commit comments

Comments
 (0)