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

No elmi #15

Merged
merged 33 commits into from
Dec 8, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
d3aec28
add parser to detect exposed values
harrysarson Oct 30, 2020
ba6b277
drop dep on elmi on path
harrysarson Oct 31, 2020
fef3845
remove dbg
harrysarson Oct 31, 2020
158922e
update cargo deps
harrysarson Nov 22, 2020
3e4f061
use tree-sitter crate
harrysarson Nov 22, 2020
4777137
tidy parser up slightly
harrysarson Nov 22, 2020
7d91a36
fix parsing comments
harrysarson Nov 22, 2020
29d1ed2
apply review feedback
harrysarson Nov 22, 2020
2b942f1
remove todo
harrysarson Nov 22, 2020
d763fee
fmt
harrysarson Nov 22, 2020
7b0340b
make regexes wrap for readability
harrysarson Nov 22, 2020
9a00b4d
remove all reference of elmi
harrysarson Nov 22, 2020
e52f54b
merge with updated master
mpizenberg Nov 29, 2020
008b2e8
update cargo lock
mpizenberg Nov 29, 2020
abbee2d
change imports in run.rs
mpizenberg Nov 29, 2020
06ad318
merge with updated master
mpizenberg Dec 2, 2020
13fad6f
Remove unused compilation step
mpizenberg Dec 2, 2020
9852287
Clarify that module paths are absolute
mpizenberg Dec 2, 2020
66a3d01
Remove unneeded clone
mpizenberg Dec 2, 2020
737130e
Aesthetics changes in get_module_name
mpizenberg Dec 2, 2020
12d18be
Replace regex to check valid module name
mpizenberg Dec 3, 2020
36c3a10
Minor code tweaks
mpizenberg Dec 3, 2020
1bad6ac
Parsing using tree-sitter queries
mpizenberg Dec 4, 2020
ff6cc30
Add query for all top level declarations
mpizenberg Dec 4, 2020
3e45cca
Move the query approach to the bottom of the file
mpizenberg Dec 4, 2020
77b762e
Small refactor of query approach
mpizenberg Dec 4, 2020
794b4c7
Remove tree-walking approach
mpizenberg Dec 4, 2020
d7d3278
Remove build dependencies
mpizenberg Dec 7, 2020
addca30
Simplify code detecting tests
mpizenberg Dec 7, 2020
0b2748d
Code reorganization
mpizenberg Dec 7, 2020
d7f90de
Remove unused dependency
mpizenberg Dec 7, 2020
e75734e
Update readme with tests detection explanation
mpizenberg Dec 7, 2020
e529933
Add a section about behavior differences in readme
mpizenberg Dec 7, 2020
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
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "tree-sitter-elm"]
path = tree-sitter-elm
url = https://github.com/Razzeee/tree-sitter-elm
89 changes: 89 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,13 @@ miniserde = "0.1"
pathdiff = "0.1"
rand = { version = "0.7.3", default-features = false, features = ["std"] }
num_cpus = "1"
thiserror = "1.0.21"
tree-sitter = "0.17.0"
regex = "1.4.1"
lazy_static = "1.4.0"

[dev-dependencies]

[build-dependencies]
cc="1"

17 changes: 17 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use std::path::PathBuf;

fn main() {
let dir: PathBuf = ["tree-sitter-elm", "src"].iter().collect();

cc::Build::new()
.include(&dir)
.file(dir.join("parser.c"))
.flag_if_supported("-Wno-unused-but-set-variable")
.compile("tree-sitter-parser");

cc::Build::new()
.cpp(true)
.include(&dir)
.file(dir.join("scanner.cc"))
.compile("tree-sitter-scanner");
}
73 changes: 24 additions & 49 deletions src/elmi.rs
Original file line number Diff line number Diff line change
@@ -1,62 +1,37 @@
//! Basically a wrapper module for elmi-to-json for the time being.
//! It reads the compiled .elmi files and extracts exposed tests.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of date comment?


use miniserde::{json, Deserialize};
use std::collections::HashSet;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::fs;

/// Use elmi-to-json as a binary to extract all exposed tests
/// from compiled .elmi files.
pub fn all_tests<P: AsRef<Path>>(
work_dir: P,
src_files: &HashSet<PathBuf>,
use std::path::Path;

/// Find all possible tests (all values) in test_files.
pub fn all_tests(
test_files: impl IntoIterator<Item = impl AsRef<Path>>,
) -> Result<Vec<TestModule>, String> {
let output = Command::new("elmi-to-json")
.arg("--for-elm-test")
.arg("--elm-version")
.arg("0.19.1")
// stdio config
.current_dir(&work_dir)
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::inherit())
.output()
.expect("command failed to start");
let str_output = std::str::from_utf8(&output.stdout)
.map_err(|_| "Output of elmi-to-json is not valid UTF-8".to_string())?;
let output: ElmiToJsonOutput =
json::from_str(str_output).map_err(|_| "Deserialization error".to_string())?;
Ok(output
.test_modules
test_files
.into_iter()
// Filter out modules with no test
.filter(|m| !m.tests.is_empty())
// Filter out modules not in the list of src_files (must also be canonical)
.filter(|m| {
let path =
work_dir.as_ref().join(&m.path).canonicalize().expect(
"There was an issue when retrieving module paths from elmi-to-json output",
);
src_files.contains(&path)
})
// No need to verify that module names are valid, elm 0.19.1 already verifies that.
// No need to filter exposed since only exposed values are in elmi files.
.collect())
}
.map(|test_file| {
let source = fs::read_to_string(&test_file).unwrap();

#[derive(Deserialize, Debug)]
/// Struct mirroring the json result of elmi-to-json --for-elm-test.
struct ElmiToJsonOutput {
#[serde(rename = "testModules")]
test_modules: Vec<TestModule>,
let tree = {
let mut parser = tree_sitter::Parser::new();
let language = super::parser::tree_sitter_elm();
parser.set_language(language).unwrap();
parser.parse(&source, None).unwrap()
};

crate::parser::get_all_exposed_values(&tree, &source)
.map(|tests| TestModule {
path: test_file.as_ref().to_str().unwrap().to_string(),
tests: tests.iter().map(|s| s.to_string()).collect(),
})
.map_err(|s| s.to_string())
})
.collect()
}

#[derive(Deserialize, Debug)]
/// Test modules as listed in the json result of elmi-to-json.
pub struct TestModule {
#[serde(rename = "moduleName")]
pub module_name: String,
pub path: String,
pub tests: Vec<String>,
}
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod elmi;
mod help;
mod init;
mod install;
mod parser;
mod run;
mod utils;

Expand Down Expand Up @@ -46,9 +47,8 @@ fn main_args() -> Result<Args, Box<dyn std::error::Error>> {
/// This happens for example with the command: `elm-test-rs /path/to/some/Module.elm`.
fn no_subcommand_args(
first_arg: Option<String>,
args: pico_args::Arguments,
mut args: pico_args::Arguments,
) -> Result<Args, Box<dyn std::error::Error>> {
let mut args = args;
let mut rng = rand::thread_rng();
Ok(Args::Run(run::Options {
help: args.contains("--help"),
Expand Down
Loading