Skip to content

Commit

Permalink
test: Adjust test to avoid writing to the repo (#4681)
Browse files Browse the repository at this point in the history
  • Loading branch information
max-sixty committed Jul 1, 2024
1 parent d3ed63e commit 5a2a857
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 20 deletions.
3 changes: 2 additions & 1 deletion 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.10.0"}
test_each_file = "0.3.2"

# We use `benches/bench.rs` for the benchmark harness so disable searching for
Expand Down
70 changes: 51 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
assert_cmd_snapshot!(prqlc_command().args(["fmt"]).pass_stdin("from tracks | take 20"), @r###"
success: true
exit_code: 0
Expand All @@ -363,28 +367,56 @@ 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.
// Test formatting a path:

// Single file
assert_cmd_snapshot!(prqlc_command().args(["fmt", project_path().join("artists.prql").to_str().unwrap()]), @r###"
success: true
exit_code: 0
----- stdout -----
// Create a temporary directory
let temp_dir = TempDir::new().expect("Failed to create temp directory");

----- stderr -----
"###);
// Copy files from project_path() to temp_dir
copy_dir(&project_path(), temp_dir.path());

// Project
assert_cmd_snapshot!(prqlc_command().args(["fmt", project_path().to_str().unwrap()]), @r###"
success: true
exit_code: 0
----- stdout -----
// Run fmt command on the temp directory
let _result = prqlc_command()
.args(["fmt", temp_dir.path().to_str().unwrap()])
.status()
.unwrap();

----- stderr -----
"###);
// Check if files in temp_dir match the original files
compare_directories(&project_path(), temp_dir.path());
}

fn copy_dir(src: &Path, dst: &Path) {
for entry in WalkDir::new(src) {
let entry = entry.unwrap();
let path = entry.path();
if path.is_file() {
let relative_path = path.strip_prefix(src).unwrap();
let target_path = dst.join(relative_path);
fs::create_dir_all(target_path.parent().unwrap()).unwrap();
fs::copy(path, target_path).unwrap();
}
}
}

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(),
"File {:?} doesn't exist in the formatted directory",
relative_path
);

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

#[test]
Expand Down

0 comments on commit 5a2a857

Please sign in to comment.