forked from mpizenberg/elm-test-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Instead we use the approach of <rtfeldman/node-test-runner#442> from which I have taken a lot of inspiration for this commit.
- Loading branch information
1 parent
d3aec28
commit ba6b277
Showing
8 changed files
with
185 additions
and
80 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
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>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.