Skip to content

Commit

Permalink
tests: make workdir::read_to_string() fallible
Browse files Browse the repository at this point in the history
so we get the file not found errors in the test proper, not inside the read_to_string() helper
  • Loading branch information
jqnatividad committed Dec 10, 2024
1 parent 5932e55 commit 5d5e785
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 25 deletions.
4 changes: 2 additions & 2 deletions tests/test_fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ fn fetch_simple_diskcache() {
// sleep for a bit to make sure the cache is written to disk
std::thread::sleep(std::time::Duration::from_secs(2));

let fetchreport = wrk.read_to_string("data.csv.fetch-report.tsv");
let fetchreport = wrk.read_to_string("data.csv.fetch-report.tsv").unwrap();
wrk.create_from_string("no-elapsed.tsv", &fetchreport);

// remove the elapsed_ms column from the report as this is not deterministic
Expand Down Expand Up @@ -1657,7 +1657,7 @@ fn fetchpost_payload_template_with_report() {

assert_eq!(got, expected);

let report = wrk.read_to_string("data.csv.fetchpost-report.tsv");
let report = wrk.read_to_string("data.csv.fetchpost-report.tsv").unwrap();
assert!(!report.is_empty());
}

Expand Down
2 changes: 1 addition & 1 deletion tests/test_fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ fn fmt_output() {

wrk.assert_success(&mut cmd);

let got = wrk.read_to_string(&output_file);
let got = wrk.read_to_string(&output_file).unwrap();

let expected = r#"h1,h2
abcdef,ghijkl
Expand Down
4 changes: 2 additions & 2 deletions tests/test_luau.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1007,7 +1007,7 @@ END {
let expected_end = "Min/Max: 7/72 Grand total of 3 rows: 275\n".to_string();
assert_eq!(end, expected_end);

let table_txt = wrk.read_to_string("count.txt");
let table_txt = wrk.read_to_string("count.txt").unwrap();
let expected_table_txt = "4\n";
assert_eq!(table_txt, expected_table_txt);

Expand Down Expand Up @@ -1141,7 +1141,7 @@ END {
];
assert_eq!(got, expected);

let echo_text = wrk.read_to_string("echo.txt");
let echo_text = wrk.read_to_string("echo.txt").unwrap();
let expected_echo_text = "the quick brown fox jumped over the lazy dog";
assert_eq!(
dos2unix(&echo_text).trim_end(),
Expand Down
4 changes: 2 additions & 2 deletions tests/test_slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ fn test_slice(

wrk.assert_success(&mut cmd);

let gots = wrk.read_to_string(&output_file);
let gots = wrk.read_to_string(&output_file).unwrap();
let gotj: serde_json::Value = serde_json::from_str(&gots).unwrap();
let got = gotj.to_string();

Expand Down Expand Up @@ -348,7 +348,7 @@ fn test_slice_invert(

wrk.assert_success(&mut cmd);

let gots = wrk.read_to_string(&output_file);
let gots = wrk.read_to_string(&output_file).unwrap();
let gotj: serde_json::Value = serde_json::from_str(&gots).unwrap();
let got = gotj.to_string();

Expand Down
2 changes: 1 addition & 1 deletion tests/test_snappy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ fn snappy_compress() {
wrk.assert_success(&mut cmd);

let expected = wrk.load_test_resource("boston311-100.csv");
let got = wrk.read_to_string("out.csv");
let got = wrk.read_to_string("out.csv").unwrap();

assert_eq!(dos2unix(&got).trim_end(), dos2unix(&expected).trim_end());
}
Expand Down
12 changes: 6 additions & 6 deletions tests/test_sqlp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1556,7 +1556,7 @@ fn sqlp_sql_tsv() {

wrk.assert_success(&mut cmd);

let got = wrk.read_to_string(&output_file);
let got = wrk.read_to_string(&output_file).unwrap();

let expected = "idx\tval\n0\tABC\n1\tabc\n2\t000\n3\tA0C\n4\ta0c\n";

Expand Down Expand Up @@ -1587,7 +1587,7 @@ fn sqlp_sql_ssv() {

wrk.assert_success(&mut cmd);

let got = wrk.read_to_string(&output_file);
let got = wrk.read_to_string(&output_file).unwrap();

let expected = "idx;val\n0;ABC\n1;abc\n2;000\n3;A0C\n4;a0c\n";

Expand Down Expand Up @@ -1619,7 +1619,7 @@ fn sqlp_sql_ssv_output() {

wrk.assert_success(&mut cmd);

let got = wrk.read_to_string(&output_file);
let got = wrk.read_to_string(&output_file).unwrap();

let expected = "idx;val\n0;ABC\n1;abc\n2;000\n3;A0C\n4;a0c\n";

Expand Down Expand Up @@ -2965,7 +2965,7 @@ fn sqlp_autotab_delim() {

wrk.assert_success(&mut cmd);

let got = wrk.read_to_string(&output_file);
let got = wrk.read_to_string(&output_file).unwrap();
let expected = r#"countryid cities
DB Dubai,Abu Dhabi,Sharjah
IN Mumbai,Delhi,Bangalore
Expand Down Expand Up @@ -3009,7 +3009,7 @@ fn sqlp_dollar_quoting_string_literals() {

wrk.assert_success(&mut cmd);

let got = wrk.read_to_string(&output_file);
let got = wrk.read_to_string(&output_file).unwrap();
let expected = r#"country_id complex_literal
DB "This is a literal $,%,',"""
IN "This is a literal $,%,',"""
Expand Down Expand Up @@ -3066,7 +3066,7 @@ IT "This is a literal $,%,',"""

// assert_eq!(got, expected);

// let got_dot = wrk.read_to_string(&output_dotfile);
// let got_dot = wrk.read_to_string(&output_dotfile).unwrap();
// let expected_dot = r#"digraph {
// "Projection" -> "Filter";
// "Filter" -> "CsvScan";
Expand Down
18 changes: 11 additions & 7 deletions tests/test_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ fn template_output_file() {

wrk.assert_success(&mut cmd);

let got = wrk.read_to_string(output_file);
let got = wrk.read_to_string(output_file).unwrap();
let expected = "John,New York\nJane,Boston\n";
assert_eq!(got, expected);
}
Expand All @@ -276,8 +276,8 @@ fn template_output_directory() {
wrk.assert_success(&mut cmd);

// Check that files were created with default ROWNO naming
let file1 = wrk.read_to_string(&format!("{outdir}/0/1.txt"));
let file2 = wrk.read_to_string(&format!("{outdir}/0/2.txt"));
let file1 = wrk.read_to_string(&format!("{outdir}/0/1.txt")).unwrap();
let file2 = wrk.read_to_string(&format!("{outdir}/0/2.txt")).unwrap();

assert_eq!(file1, "Hello John from New York!");
assert_eq!(file2, "Hello Jane from Boston!");
Expand All @@ -301,8 +301,12 @@ fn template_output_custom_filename() {
wrk.assert_success(&mut cmd);

// Check that files were created with custom naming
let file1 = wrk.read_to_string(&format!("{outdir}/0/John_greeting-1.txt"));
let file2 = wrk.read_to_string(&format!("{outdir}/0/Jane_greeting-2.txt"));
let file1 = wrk
.read_to_string(&format!("{outdir}/0/John_greeting-1.txt"))
.unwrap();
let file2 = wrk
.read_to_string(&format!("{outdir}/0/Jane_greeting-2.txt"))
.unwrap();

assert_eq!(file1, "Greetings from New York!");
assert_eq!(file2, "Greetings from Boston!");
Expand All @@ -325,8 +329,8 @@ fn template_output_directory_no_headers() {
wrk.assert_success(&mut cmd);

// Check files with row numbers
let file1 = wrk.read_to_string(&format!("{outdir}/0/1.txt"));
let file2 = wrk.read_to_string(&format!("{outdir}/0/2.txt"));
let file1 = wrk.read_to_string(&format!("{outdir}/0/1.txt")).unwrap();
let file2 = wrk.read_to_string(&format!("{outdir}/0/2.txt")).unwrap();

assert_eq!(file1, "Record: John - New York");
assert_eq!(file2, "Record: Jane - Boston");
Expand Down
8 changes: 4 additions & 4 deletions tests/workdir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ impl Workdir {
}

/// read the contents of a file into a string
pub fn read_to_string(&self, filename: &str) -> String {
let mut file = File::open(self.path(filename)).unwrap();
pub fn read_to_string(&self, filename: &str) -> io::Result<String> {
let mut file = File::open(self.path(filename))?;
let mut contents = String::new();
file.read_to_string(&mut contents).unwrap_or_default();
contents
file.read_to_string(&mut contents)?;
Ok(contents)
}

/// read stdout of a command
Expand Down

0 comments on commit 5d5e785

Please sign in to comment.