Skip to content

Commit

Permalink
Add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
ImprintNav committed Jul 13, 2023
1 parent 5d186db commit 31a701a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
27 changes: 25 additions & 2 deletions package/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use tempfile::TempDir;
use termcolor::{Color, WriteColor};

use crate::utils::build::fingerprint;
use crate::utils::exe::{execute, execute_with_input, Platform, CURRENT_PLATFORM};
use crate::utils::exe::{execute, execute_with_input, execute_no_error, Platform, CURRENT_PLATFORM};
use crate::utils::fs::{
copy, create_tempdir, ensure_directory, remove_dir, rename, softlink, touch, write_file,
};
Expand All @@ -38,7 +38,7 @@ fn decode_output(output: Vec<u8>) -> Result<String> {
}

fn assert_stderr_output(command: &mut Command, expected_messages: Vec<&str>) -> Output {
let output = execute(command.stderr(Stdio::piped())).unwrap();
let output = execute_no_error(command.stderr(Stdio::piped()));
let stderr = decode_output(output.stderr.clone()).unwrap();
for expected_message in expected_messages {
assert!(
Expand Down Expand Up @@ -103,6 +103,7 @@ pub(crate) fn run_integration_tests(
test_initialize_new_pants_project(scie_pants_scie);
test_set_pants_version(scie_pants_scie);
test_ignore_empty_pants_version_pants_sha(scie_pants_scie);
test_bad_pants_version(scie_pants_scie);

let clone_root = create_tempdir()?;
test_use_in_repo_with_pants_script(scie_pants_scie, &clone_root);
Expand Down Expand Up @@ -392,6 +393,28 @@ fn test_ignore_empty_pants_version_pants_sha(scie_pants_scie: &Path) {
);
}

fn test_bad_pants_version(scie_pants_scie: &Path) {
integration_test!("Verifying Pants gives good error messages for nonexistent version numbers");
let non_existent_version = "1.2.3.4.5";
assert_stderr_output(
Command::new(scie_pants_scie)
.arg("-V")
.env("PANTS_VERSION", non_existent_version),
vec![
"Could not find Pants tag 1.2.3.4.5 in https://github.com/pantsbuild/pants/releases"
],
);
let prefix_version = "1";
assert_stderr_output(
Command::new(scie_pants_scie)
.arg("-V")
.env("PANTS_VERSION", prefix_version),
vec![
"Could not find Pants tag 1 in https://github.com/pantsbuild/pants/releases"
],
);
}

fn test_use_in_repo_with_pants_script(scie_pants_scie: &Path, clone_root: &TempDir) {
integration_test!("Verify scie-pants can be used as `pants` in a repo with the `pants` script");
// This verifies a fix for https://github.com/pantsbuild/scie-pants/issues/28.
Expand Down
16 changes: 12 additions & 4 deletions package/src/utils/exe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,22 @@ pub(crate) fn prepare_exe(path: &Path) -> Result<()> {
}

pub(crate) fn execute_with_input(command: &mut Command, stdin_data: &[u8]) -> Result<Output> {
_execute_with_input(command, Some(stdin_data))
_execute_with_input(command, Some(stdin_data), true)
}

pub(crate) fn execute(command: &mut Command) -> Result<Output> {
_execute_with_input(command, None)
_execute_with_input(command, None, true)
}

fn _execute_with_input(command: &mut Command, stdin_data: Option<&[u8]>) -> Result<Output> {
pub(crate) fn execute_no_error(command: &mut Command) -> Output {
_execute_with_input(command, None, false).unwrap()
}

fn _execute_with_input(
command: &mut Command,
stdin_data: Option<&[u8]>,
err_on_command_fail: bool,
) -> Result<Output> {
info!("Executing {command:#?}");
if stdin_data.is_some() {
command.stdin(std::process::Stdio::piped());
Expand All @@ -107,7 +115,7 @@ fn _execute_with_input(command: &mut Command, stdin_data: Option<&[u8]>) -> Resu
let output = child
.wait_with_output()
.with_context(|| format!("Failed to gather exit status of command: {command:?}"))?;
if !output.status.success() {
if err_on_command_fail && !output.status.success() {
let mut message_lines = vec![format!(
"Command {command:?} failed with exit code: {code:?}",
code = output.status.code()
Expand Down

0 comments on commit 31a701a

Please sign in to comment.