Skip to content

Commit

Permalink
Merge pull request #779 from tweag/nb/test_fetch_grammars_before
Browse files Browse the repository at this point in the history
Load JSON and TOML grammars before tests in `cli-testers.rs`
  • Loading branch information
nbacquey authored Oct 24, 2024
2 parents 2836913 + 77ae922 commit f68cfd4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ This name should be decided amongst the team before the release.

[Full list of changes](https://github.com/tweag/topiary/compare/v0.5.1...HEAD)

### Fixed
- [#779](https://github.com/tweag/topiary/pull/779) Load relevant grammars before CLI tests

## v0.5.1 - Fragrant Frangipani - 2024-10-22

[Full list of changes](https://github.com/tweag/topiary/compare/v0.4.0...v0.5.1)
Expand Down
40 changes: 39 additions & 1 deletion topiary-cli/tests/cli-tester.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{fmt, fs, fs::File, io::Write, path::PathBuf};
use std::{fmt, fs, fs::File, io::Write, path::PathBuf, sync::Once};

use assert_cmd::Command;
use predicates::{
Expand All @@ -17,6 +17,35 @@ const TOML_INPUT: &str = r#" test= 123"#;
const TOML_EXPECTED: &str = r#"test = 123
"#;

// We need to prefetch JSON and TOML grammars before running the tests, on pain of race condition:
// If multiple calls to Topiary are made in parallel and the grammar is missing, they will all try
// to fetch and build it, thus creating an empty .so file while g++ is running. If another instance
// of topiary starts at this moment, it will mistake the empty .so file for an already built grammar,
// and try to run with it, resulting in an error. See https://github.com/tweag/topiary/issues/767
static INIT: Once = Once::new();
pub fn initialize() {
INIT.call_once(|| {
#[cfg(feature = "json")]
Command::cargo_bin("topiary")
.expect("Unable to build Topiary")
.arg("fmt")
.arg("--language")
.arg("json")
.write_stdin("")
.assert()
.success();
#[cfg(feature = "toml")]
Command::cargo_bin("topiary")
.expect("Unable to build Topiary")
.arg("fmt")
.arg("--language")
.arg("toml")
.write_stdin("")
.assert()
.success();
});
}

// The TempDir member of the State is not actually used.
// However, removing it means that the directory is dropped at the end of the new() function, which causes it to be deleted.
// This causes the path to the state file to be invalid and breaks the tests.
Expand Down Expand Up @@ -47,6 +76,7 @@ impl State {
#[test]
#[cfg(feature = "json")]
fn test_fmt_stdin() {
initialize();
let mut topiary = Command::cargo_bin("topiary").unwrap();

topiary
Expand All @@ -63,6 +93,7 @@ fn test_fmt_stdin() {
#[test]
#[cfg(feature = "json")]
fn test_fmt_stdin_query() {
initialize();
let mut topiary = Command::cargo_bin("topiary").unwrap();

topiary
Expand All @@ -81,6 +112,7 @@ fn test_fmt_stdin_query() {
#[test]
#[cfg(feature = "json")]
fn test_fmt_stdin_query_fallback() {
initialize();
let mut topiary = Command::cargo_bin("topiary").unwrap();

topiary
Expand All @@ -99,6 +131,7 @@ fn test_fmt_stdin_query_fallback() {
#[test]
#[cfg(all(feature = "json", feature = "toml"))]
fn test_fmt_files() {
initialize();
let json = State::new(JSON_INPUT, "json");
let toml = State::new(TOML_INPUT, "toml");

Expand All @@ -119,6 +152,7 @@ fn test_fmt_files() {
#[test]
#[cfg(all(feature = "json", feature = "toml"))]
fn test_fmt_files_query_fallback() {
initialize();
let json = State::new(JSON_INPUT, "json");
let toml = State::new(TOML_INPUT, "toml");

Expand All @@ -141,6 +175,7 @@ fn test_fmt_files_query_fallback() {
#[test]
#[cfg(feature = "json")]
fn test_fmt_dir() {
initialize();
let json = State::new(JSON_INPUT, "json");

let mut topiary = Command::cargo_bin("topiary").unwrap();
Expand All @@ -158,6 +193,7 @@ fn test_fmt_dir() {
#[test]
#[cfg(feature = "json")]
fn test_fmt_invalid() {
initialize();
let mut topiary = Command::cargo_bin("topiary").unwrap();

// Can't specify --language with input files
Expand All @@ -183,6 +219,7 @@ fn test_fmt_invalid() {
#[test]
#[cfg(feature = "json")]
fn test_vis() {
initialize();
let mut topiary = Command::cargo_bin("topiary").unwrap();

// Sanity check output is a valid DOT graph
Expand All @@ -202,6 +239,7 @@ fn test_vis() {
#[test]
#[cfg(feature = "json")]
fn test_vis_invalid() {
initialize();
let mut topiary = Command::cargo_bin("topiary").unwrap();

// Can't specify --language with input file
Expand Down

0 comments on commit f68cfd4

Please sign in to comment.