Skip to content

Commit

Permalink
Move tests to new API
Browse files Browse the repository at this point in the history
This includes moving the sample tests to Topiary cli (because topiary no longer knows about languages).

Additionally, bash was reverted back to an older version due to a regression that needs to be investigated.
  • Loading branch information
Erin van der Veen committed Jan 3, 2024
1 parent b2e65ca commit d81b80b
Show file tree
Hide file tree
Showing 25 changed files with 98 additions and 152 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

6 changes: 6 additions & 0 deletions topiary-cli/src/configuration/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ impl Serialisation {
}
}

impl Default for Serialisation {
fn default() -> Self {
Self::new()
}
}

/// Convert deserialised TOML values into `Serialisation` values
// TODO Is this necessary, any more?
impl TryFrom<toml::Value> for Serialisation {
Expand Down
4 changes: 2 additions & 2 deletions topiary-cli/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ impl<'cfg> InputFile<'cfg> {
Ok(LanguageDefinition {
language: Language {
name: self.language.name.clone(),
query: query,
grammar: grammar,
query,
grammar,
indent: self.language().indent.clone(),
},
})
Expand Down
76 changes: 76 additions & 0 deletions topiary-cli/tests/sample-tester.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use assert_cmd::Command;
use std::fs;
use std::path::Path;
use topiary::test_utils::pretty_assert_eq;

use tempfile::TempDir;

#[test]
fn input_output_tester() {
let input_dir = fs::read_dir("tests/samples/input").unwrap();
let expected_dir = Path::new("tests/samples/expected");

for file in input_dir {
let file = file.unwrap();

// Load the known good formated files
let expected_path = expected_dir.join(file.file_name());
let expected = fs::read_to_string(expected_path).unwrap();

let tmp_dir = TempDir::new().unwrap();

// Copy the file to a temp dir
let mut input_file = tmp_dir.path().to_path_buf();
input_file.push(file.path().file_name().unwrap());
fs::copy(file.path(), &input_file).unwrap();

// Run topiary on the input file in the temp dir
let mut topiary = Command::cargo_bin("topiary").unwrap();
topiary
// .env("TOPIARY_LANGUAGE_DIR", "../queries/")
.arg("fmt")
.arg(&input_file)
.assert()
.success();

// Read the file after formatting
let formatted = fs::read_to_string(input_file).unwrap();

// Assert the formatted file is as expected
pretty_assert_eq(&expected, &formatted);
}
}

// Test that our query files are properly formatted
#[test]
fn formatted_query_tester() {
let language_dir = fs::read_dir("../queries").unwrap();

for file in language_dir {
let file = file.unwrap();

// Load the query file (we assume is formatted correctly)
let expected = fs::read_to_string(file.path()).unwrap();

let tmp_dir = TempDir::new().unwrap();

// Copy the file to a temp dir
let mut input_file = tmp_dir.path().to_path_buf();
input_file.push(file.path().file_name().unwrap());
fs::copy(file.path(), &input_file).unwrap();

// Run topiary on the input file in the temp dir
let mut topiary = Command::cargo_bin("topiary").unwrap();
topiary
// .env("TOPIARY_LANGUAGE_DIR", "../queries/")
.arg("fmt")
.arg(&input_file)
.assert()
.success();

// Read the file after formatting
let formatted = fs::read_to_string(input_file).unwrap();

pretty_assert_eq(&expected, &formatted);
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion topiary/benches/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use topiary::{formatter, Language, Operation, TopiaryQuery};
// FIXME Configuration is no longer part of the library

async fn format() {
let input = fs::read_to_string("tests/samples/input/ocaml.ml").unwrap();
let input = fs::read_to_string("../topiary-cli/tests/samples/input/ocaml.ml").unwrap();
let query_content = fs::read_to_string("../queries/ocaml.scm").unwrap();
let ocaml = tree_sitter_ocaml::language_ocaml();

Expand Down
17 changes: 11 additions & 6 deletions topiary/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,20 +159,25 @@ pub enum Operation {
/// # tokio_test::block_on(async {
/// use std::fs::File;
/// use std::io::{BufReader, Read};
/// use topiary::{formatter, FormatterError, TopiaryQuery, Operation};
/// use topiary::{formatter, Language, FormatterError, TopiaryQuery, Operation};
///
/// let input = "[1,2]".to_string();
/// let mut input = input.as_bytes();
/// let mut output = Vec::new();
/// let json = tree_sitter_json::language();
///
/// let mut query_file = BufReader::new(File::open("../queries/json.scm").expect("query file"));
/// let mut query_content = String::new();
/// query_file.read_to_string(&mut query_content).expect("read query file");
///
/// let grammar = tree_sitter_json::language();
/// let query = TopiaryQuery::new(&grammar, &query_content).unwrap();
/// let language: Language = Language {
/// name: "json".to_owned(),
/// query: TopiaryQuery::new(&json.into(), &query_content).unwrap(),
/// grammar: json.into(),
/// indent: None,
/// };
///
/// // FIXME The signature of `formatter` has changed
/// match formatter(&mut input, &mut output, &query, &language, &grammar, Operation::Format{ skip_idempotence: false, tolerate_parsing_errors: false }) {
/// match formatter(&mut input, &mut output, &language, Operation::Format{ skip_idempotence: false, tolerate_parsing_errors: false }) {
/// Ok(()) => {
/// let formatted = String::from_utf8(output).expect("valid utf-8");
/// }
Expand Down Expand Up @@ -359,7 +364,7 @@ mod tests {
let expected = "{ \"one\": {\"bar\" \"baz\"}, \"two\": \"bar\" }\n";

let mut output = Vec::new();
let query_content = fs::read_to_string("../languages/json.scm").unwrap();
let query_content = fs::read_to_string("../topiary-queries/queries/json.scm").unwrap();
let grammar = tree_sitter_json::language().into();
let language = Language {
name: "json".to_owned(),
Expand Down
141 changes: 0 additions & 141 deletions topiary/tests/sample-tester.rs

This file was deleted.

0 comments on commit d81b80b

Please sign in to comment.