Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

General refactoring #4

Merged
merged 3 commits into from
Jun 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 70 additions & 54 deletions cli/doctest_runner.rs
Original file line number Diff line number Diff line change
@@ -1,44 +1,45 @@
use regex::Regex;
use std::path::{Path, PathBuf};
use std::collections::HashSet;
use std::ffi::OsStr;
use std::path::PathBuf;

use crate::fs as deno_fs;
use crate::installer::is_remote_url;
use crate::test_runner::is_supported;

pub struct DocTest {
// This removes repetition of imports in a file
imports: std::collections::HashSet<String>,
imports: HashSet<String>,
// This contains codes in an @example section with their imports removed
bodies: Vec<DocTestBody>,
}

struct DocTestBody {
caption: String,
line_number: usize,
path: PathBuf,
path: String,
value: String,
ignore: bool,
is_async: bool,
}

pub fn prepare_doctest(
include: Vec<String>,
// root_path: &PathBuf,
mut include: Vec<String>,
root_path: &PathBuf,
) -> Vec<DocTest> {
let include_paths: Vec<_> =
include.into_iter().filter(|n| !is_remote_url(n)).collect();
include.retain(|n| !is_remote_url(n));

let mut prepared = vec![];

for path in include_paths {
let p = deno_fs::normalize_path(&Path::new(&path));
for path in include {
let p = deno_fs::normalize_path(&root_path.join(path));
if p.is_dir() {
let test_files = crate::fs::files_in_subtree(p, |p| {
let supported_files = ["ts", "tsx", "js", "jsx"];
match p.extension().and_then(std::ffi::OsStr::to_str) {
Some(x) => supported_files.contains(&x) && !is_supported(p),
_ => false,
}
let test_files = deno_fs::files_in_subtree(p, |p| {
let valid_ext = ["ts", "tsx", "js", "jsx"];
p.extension()
.and_then(OsStr::to_str)
.map(|ext| valid_ext.contains(&ext) && !is_supported(p))
.unwrap_or(false)
});
prepared.extend(test_files);
} else {
Expand All @@ -50,9 +51,8 @@ pub fn prepare_doctest(
.iter()
.filter_map(|dir| {
// TODO(iykekings) use deno error instead
let content = std::fs::read_to_string(&dir).expect(
format!("File doesn't exist {}", dir.to_str().unwrap_or("")).as_str(),
);
let content = std::fs::read_to_string(&dir)
.unwrap_or_else(|_| panic!("File doesn't exist {}", dir.display()));
extract_jsdoc_examples(content, dir.to_owned())
})
.collect::<Vec<_>>()
Expand All @@ -62,17 +62,15 @@ fn extract_jsdoc_examples(input: String, p: PathBuf) -> Option<DocTest> {
lazy_static! {
static ref JS_DOC_PATTERN: Regex =
Regex::new(r"/\*\*\s*\n([^\*]|\*[^/])*\*/").unwrap();
// IMPORT_PATTERN doesn't match dynamic imports
// IMPORT_PATTERN doesn't match dynamic imports by design
static ref IMPORT_PATTERN: Regex =
Regex::new(r"import[^(].*\n").unwrap();
static ref EXAMPLE_PATTERN: Regex = Regex::new(r"@example\s*(?:<\w+>.*</\w+>)*\n(?:\s*\*\s*\n*)*```").unwrap();
static ref TICKS_OR_IMPORT_PATTERN: Regex = Regex::new(r"(?:import[^(].*)|(?:```\w*)").unwrap();
static ref CAPTION_PATTERN: Regex = Regex::new(r"<caption>([\s\w\W]+)</caption>").unwrap();
static ref TEST_TAG_PATTERN: Regex = Regex::new(r"@example\s*(?:<\w+>.*</\w+>)*\n(?:\s*\*\s*\n*)*```(\w+)").unwrap();
static ref AWAIT_PATTERN: Regex = Regex::new(r"\Wawait\s").unwrap();
}

let mut import_set = std::collections::HashSet::new();
let mut import_set = HashSet::new();

let test_bodies = JS_DOC_PATTERN
.captures_iter(&input)
Expand Down Expand Up @@ -103,38 +101,21 @@ fn extract_jsdoc_examples(input: String, p: PathBuf) -> Option<DocTest> {
import_set.insert(import.to_string());
});

let caption = CAPTION_PATTERN
.captures(&example_section)
.and_then(|cap| cap.get(1).map(|m| m.as_str()))
.unwrap_or("");

let caption = get_caption_from_example(&example_section);
let line_number = &input[0..offset].lines().count();

let body = TICKS_OR_IMPORT_PATTERN
.replace_all(&example_section, "\n")
.lines()
.skip(1)
.filter_map(|line| {
let res = match line.trim_start().starts_with("*") {
true => line.replacen("*", "", 1).trim_start().to_string(),
false => line.trim_start().to_string(),
};
match res.len() {
0 => None,
_ => Some(format!(" {}", res)),
}
})
.collect::<Vec<_>>()
.join("\n");
let is_async = match AWAIT_PATTERN.find(&example_section) {
Some(_) => true,
_ => false,
};
let code_block = get_code_from_example(&example_section);
let is_async = AWAIT_PATTERN.find(&example_section).is_some();

let cwd = std::env::current_dir()
.expect("expected: process has a current working directory");
let path = p
.to_str()
.map(|x| x.replace(cwd.to_str().unwrap_or(""), ""));
Some(DocTestBody {
caption: caption.to_owned(),
line_number: line_number.clone(),
path: p.clone(),
value: body,
caption,
line_number: *line_number,
path: path.unwrap_or("".to_string()),
value: code_block,
ignore: test_tag == Some("ignore"),
is_async,
})
Expand Down Expand Up @@ -192,8 +173,8 @@ pub fn render_doctest_file(
.map(|test| {
let async_str = if test.is_async {"async "} else {""};
format!(
"Deno.test({{\n\tname: \"{} -> {} (line {})\",\n\tignore: {},\n\t{}fn() {{\n{}\n}}\n}});\n",
test.path.display(),
"Deno.test({{\n\tname: \"{} - {} (line {})\",\n\tignore: {},\n\t{}fn() {{\n{}\n}}\n}});\n",
&test.path[1..],
test.caption,
test.line_number,
test.ignore,
Expand Down Expand Up @@ -221,3 +202,38 @@ pub fn render_doctest_file(

test_file
}

fn get_caption_from_example(ex: &str) -> String {
lazy_static! {
static ref CAPTION_PATTERN: Regex =
Regex::new(r"<caption>([\s\w\W]+)</caption>").unwrap();
}
CAPTION_PATTERN
.captures(ex)
.and_then(|cap| cap.get(1).map(|m| m.as_str()))
.unwrap_or("")
.to_string()
}

fn get_code_from_example(ex: &str) -> String {
lazy_static! {
static ref TICKS_OR_IMPORT_PATTERN: Regex =
Regex::new(r"(?:import[^(].*)|(?:```\w*)").unwrap();
}
TICKS_OR_IMPORT_PATTERN
.replace_all(ex, "\n")
.lines()
.skip(1)
.filter_map(|line| {
let res = match line.trim_start().starts_with('*') {
true => line.replacen("*", "", 1).trim_start().to_string(),
false => line.trim_start().to_string(),
};
match res.len() {
0 => None,
_ => Some(format!(" {}", res)),
}
})
.collect::<Vec<_>>()
.join("\n")
}
5 changes: 3 additions & 2 deletions cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,9 +528,10 @@ async fn doctest_command(
filter: Option<String>,
) -> Result<(), ErrBox> {
let global_state = GlobalState::new(flags.clone())?;
let cwd = std::env::current_dir().expect("No current directory");
let cwd = std::env::current_dir()
.expect("expected: process has a current working directory");
let include = include.unwrap_or_else(|| vec![".".to_string()]);
let doctests = doctest_runner::prepare_doctest(include);
let doctests = doctest_runner::prepare_doctest(include, &cwd);

if doctests.is_empty() {
println!("No matching doctest modules found");
Expand Down