Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds CLI tests for pest2ion #45

Merged
merged 2 commits into from
Jun 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pest-ion/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ anyhow = "~1.0.40"

[dev-dependencies]
rstest = "~0.10.0"
assert_cmd = "~1.0.5"
tempfile = "~3.2.0"

[[bin]]
name = "pest2ion"
test = false
bench = false
name = "pest2ion"
94 changes: 94 additions & 0 deletions pest-ion/tests/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright Amazon.com, Inc. or its affiliates.

use anyhow::Result;
use assert_cmd::Command;
use ion_rs::value::reader::*;
use rstest::*;
use std::fs::File;
use std::io::{Read, Write};
use tempfile::TempDir;

/// The input or output mode of the CLI.
enum FileMode {
/// Use `STDIN` or `STDOUT`
Default,
/// Use a named file.
Named,
}

#[rstest]
#[case::simple(
r#"
a = { "a" ~ "b" }
"#,
r#"
{
a: {
type: normal,
expression: (sequence (string exact "a") (string exact "b")),
}
}
"#
)]
fn run_it(
#[case] pest_src: &str,
#[case] ion_text: &str,
#[values("", "-t", "-p", "-b")] format_flag: &str,
#[values(FileMode::Default, FileMode::Named)] input_mode: FileMode,
#[values(FileMode::Default, FileMode::Named)] output_mode: FileMode,
) -> Result<()> {
// working space for our tests when they need files
let temp_dir = TempDir::new()?;
let input_path = temp_dir.path().join("INPUT.pest");
let output_path = temp_dir.path().join("OUTPUT.ion");

let mut cmd = Command::cargo_bin("pest2ion")?;
if format_flag != "" {
cmd.arg(format_flag);
}
match output_mode {
FileMode::Default => {
// do nothing
}
FileMode::Named => {
// tell our driver to output to a file
cmd.arg("-o");
cmd.arg(&output_path);
}
}
match input_mode {
FileMode::Default => {
// do nothing
}
FileMode::Named => {
// dump our test data to input file
let mut input_file = File::create(&input_path)?;
input_file.write(pest_src.as_bytes())?;
input_file.flush()?;

// make this the input for our driver
cmd.arg(input_path.to_str().unwrap());
}
};
println!("{:?}", cmd);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was to help me visualize the permutations, it is visible if you run cargo with --show-output.

-------------- TEST START --------------
Command { cmd: "/home/ANT.AMAZON.COM/almann/CLionProjects/partiql-lang-rust/target/debug/pest2ion", stdin: None, timeout: None }
-------------- TEST START --------------
Command { cmd: "/home/ANT.AMAZON.COM/almann/CLionProjects/partiql-lang-rust/target/debug/pest2ion" "-o" "/tmp/.tmpCJA5m0/OUTPUT.ion", stdin: None, timeout: None }
-------------- TEST START --------------
Command { cmd: "/home/ANT.AMAZON.COM/almann/CLionProjects/partiql-lang-rust/target/debug/pest2ion" "/tmp/.tmpIZFcxJ/INPUT.pest", stdin: None, timeout: None }
-------------- TEST START --------------
Command { cmd: "/home/ANT.AMAZON.COM/almann/CLionProjects/partiql-lang-rust/target/debug/pest2ion" "-o" "/tmp/.tmpGNdUse/OUTPUT.ion" "/tmp/.tmpGNdUse/INPUT.pest", stdin: None, timeout: None }
-------------- TEST START --------------
Command { cmd: "/home/ANT.AMAZON.COM/almann/CLionProjects/partiql-lang-rust/target/debug/pest2ion" "-t", stdin: None, timeout: None }
-------------- TEST START --------------
Command { cmd: "/home/ANT.AMAZON.COM/almann/CLionProjects/partiql-lang-rust/target/debug/pest2ion" "-t" "-o" "/tmp/.tmpPM4bcq/OUTPUT.ion", stdin: None, timeout: None }
-------------- TEST START --------------
Command { cmd: "/home/ANT.AMAZON.COM/almann/CLionProjects/partiql-lang-rust/target/debug/pest2ion" "-t" "/tmp/.tmp1HNyMC/INPUT.pest", stdin: None, timeout: None }
-------------- TEST START --------------
Command { cmd: "/home/ANT.AMAZON.COM/almann/CLionProjects/partiql-lang-rust/target/debug/pest2ion" "-t" "-o" "/tmp/.tmpM8Nz6P/OUTPUT.ion" "/tmp/.tmpM8Nz6P/INPUT.pest", stdin: None, timeout: None }
-------------- TEST START --------------
Command { cmd: "/home/ANT.AMAZON.COM/almann/CLionProjects/partiql-lang-rust/target/debug/pest2ion" "-p", stdin: None, timeout: None }
-------------- TEST START --------------
Command { cmd: "/home/ANT.AMAZON.COM/almann/CLionProjects/partiql-lang-rust/target/debug/pest2ion" "-p" "-o" "/tmp/.tmpKpsoGP/OUTPUT.ion", stdin: None, timeout: None }
-------------- TEST START --------------
Command { cmd: "/home/ANT.AMAZON.COM/almann/CLionProjects/partiql-lang-rust/target/debug/pest2ion" "-p" "/tmp/.tmphjCwHT/INPUT.pest", stdin: None, timeout: None }
-------------- TEST START --------------
Command { cmd: "/home/ANT.AMAZON.COM/almann/CLionProjects/partiql-lang-rust/target/debug/pest2ion" "-p" "-o" "/tmp/.tmpZDh6Up/OUTPUT.ion" "/tmp/.tmpZDh6Up/INPUT.pest", stdin: None, timeout: None }
-------------- TEST START --------------
Command { cmd: "/home/ANT.AMAZON.COM/almann/CLionProjects/partiql-lang-rust/target/debug/pest2ion" "-b", stdin: None, timeout: None }
-------------- TEST START --------------
Command { cmd: "/home/ANT.AMAZON.COM/almann/CLionProjects/partiql-lang-rust/target/debug/pest2ion" "-b" "-o" "/tmp/.tmpXB9Mqf/OUTPUT.ion", stdin: None, timeout: None }
-------------- TEST START --------------
Command { cmd: "/home/ANT.AMAZON.COM/almann/CLionProjects/partiql-lang-rust/target/debug/pest2ion" "-b" "/tmp/.tmpcYHp3T/INPUT.pest", stdin: None, timeout: None }
-------------- TEST START --------------
Command { cmd: "/home/ANT.AMAZON.COM/almann/CLionProjects/partiql-lang-rust/target/debug/pest2ion" "-b" "-o" "/tmp/.tmpjHh3fL/OUTPUT.ion" "/tmp/.tmpjHh3fL/INPUT.pest", stdin: None, timeout: None }

let assert = cmd.write_stdin(pest_src).assert();

let actual = match output_mode {
FileMode::Default => {
let output = assert.get_output();
element_reader().read_one(&output.stdout)?
}
FileMode::Named => {
let mut output_file = File::open(output_path)?;
let mut output_buffer = vec![];
output_file.read_to_end(&mut output_buffer)?;
element_reader().read_one(&output_buffer)?
}
};
let expected = element_reader().read_one(ion_text.as_bytes())?;
assert_eq!(expected, actual);

assert.success();

Ok(())
}