Skip to content

Commit

Permalink
test: Adjust test to avoid writing to the repo
Browse files Browse the repository at this point in the history
The existing code was inconvenient when running a program which watched files and ran tests when they changed, since it would run in an infinite loop.

This code fixes this to instead write to a temp file, at the cost of verbosity in the test
  • Loading branch information
max-sixty committed Jul 1, 2024
1 parent f9ca9fa commit ea5f911
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 19 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions prqlc/prqlc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ insta-cmd = {workspace = true}
rstest = "0.21.0"
similar = {version = "2.5.0"}
similar-asserts = "1.5.0"
tempfile = {version = "3.2.0"}
test_each_file = "0.3.2"

# We use `benches/bench.rs` for the benchmark harness so disable searching for
Expand Down
65 changes: 46 additions & 19 deletions prqlc/prqlc/tests/integration/cli.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
#![cfg(all(not(target_family = "wasm"), feature = "cli"))]

use std::env::current_dir;
use std::fs;
use std::path::Path;
use std::path::PathBuf;
use std::process::Command;
use std::str::FromStr;

use insta_cmd::assert_cmd_snapshot;
use insta_cmd::get_cargo_bin;
use tempfile::TempDir;
use walkdir::WalkDir;

#[cfg(not(windows))] // Windows has slightly different output (e.g. `prqlc.exe`), so we exclude.
#[test]
Expand Down Expand Up @@ -352,7 +356,7 @@ fn compile_project() {

#[test]
fn format() {
// stdin
// Test stdin formatting (unchanged)
assert_cmd_snapshot!(prqlc_command().args(["fmt"]).pass_stdin("from tracks | take 20"), @r###"
success: true
exit_code: 0
Expand All @@ -363,28 +367,51 @@ fn format() {
----- stderr -----
"###);

// TODO: not good tests, since they don't actually test that the code was
// formatted (though we would see the files changed after running the tests
// if they weren't formatted). Ideally we would have a simulated
// environment, like a fixture.
// Create a temporary directory
let temp_dir = TempDir::new().expect("Failed to create temp directory");

// Single file
assert_cmd_snapshot!(prqlc_command().args(["fmt", project_path().join("artists.prql").to_str().unwrap()]), @r###"
success: true
exit_code: 0
----- stdout -----
// Copy files from project_path() to temp_dir
copy_dir_contents(&project_path(), temp_dir.path()).expect("Failed to copy directory contents");

----- stderr -----
"###);
// Run fmt command on the temp directory
let _result = prqlc_command()
.args(["fmt", temp_dir.path().to_str().unwrap()])
.status()
.unwrap();

// Project
assert_cmd_snapshot!(prqlc_command().args(["fmt", project_path().to_str().unwrap()]), @r###"
success: true
exit_code: 0
----- stdout -----
// Check if files in temp_dir match the original files
compare_directories(&project_path(), temp_dir.path());
}

----- stderr -----
"###);
fn copy_dir_contents(src: &Path, dst: &Path) -> std::io::Result<()> {
for entry in WalkDir::new(src).into_iter().filter_map(|e| e.ok()) {
let path = entry.path();
if path.is_file() {
let relative_path = path.strip_prefix(src).unwrap();
let dst_path = dst.join(relative_path);
if let Some(parent) = dst_path.parent() {
fs::create_dir_all(parent)?;
}
fs::copy(path, dst_path)?;
}
}
Ok(())
}

fn compare_directories(dir1: &Path, dir2: &Path) {
for entry in WalkDir::new(dir1).into_iter().filter_map(|e| e.ok()) {
let path1 = entry.path();
if path1.is_file() {
let relative_path = path1.strip_prefix(dir1).unwrap();
let path2 = dir2.join(relative_path);

assert!(path2.exists());
similar_asserts::assert_eq!(
fs::read_to_string(path1).unwrap(),
fs::read_to_string(path2).unwrap()
);
}
}
}

#[test]
Expand Down

0 comments on commit ea5f911

Please sign in to comment.