From 834349a9713cb375faa233431a44fb96be840d25 Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Wed, 2 Mar 2022 16:23:30 -0800 Subject: [PATCH] tests: add helper for matching regex and capturing groups --- Cargo.toml | 2 ++ src/testutils.rs | 18 ++++++++++++++++++ tests/smoke_test.rs | 32 ++++++++------------------------ 3 files changed, 28 insertions(+), 24 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 578d11782..4e9b1eb04 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,7 +40,9 @@ jujutsu-lib = { version = "=0.2.0", path = "lib"} maplit = "1.0.2" pest = "2.1.3" pest_derive = "2.1.0" +predicates = "2.1.1" rand = "0.8.4" +regex = "1.5.4" tempfile = "3.3.0" textwrap = "0.14.2" thiserror = "1.0.30" diff --git a/src/testutils.rs b/src/testutils.rs index 048312c51..ac2bd472e 100644 --- a/src/testutils.rs +++ b/src/testutils.rs @@ -14,6 +14,8 @@ use std::path::{Path, PathBuf}; +use itertools::Itertools; +use regex::Regex; use tempfile::TempDir; pub struct TestEnvironment { @@ -52,3 +54,19 @@ impl TestEnvironment { &self.home_dir } } + +pub fn capture_matches( + assert: assert_cmd::assert::Assert, + pattern: &str, +) -> (assert_cmd::assert::Assert, Vec) { + let stdout_string = String::from_utf8(assert.get_output().stdout.clone()).unwrap(); + let assert = assert.stdout(predicates::str::is_match(pattern).unwrap()); + let matches = Regex::new(pattern) + .unwrap() + .captures(&stdout_string) + .unwrap() + .iter() + .map(|m| m.unwrap().as_str().to_owned()) + .collect_vec(); + (assert, matches) +} diff --git a/tests/smoke_test.rs b/tests/smoke_test.rs index 0d1c23010..0e315e949 100644 --- a/tests/smoke_test.rs +++ b/tests/smoke_test.rs @@ -12,8 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -use jujutsu::testutils::TestEnvironment; -use regex::Regex; +use jujutsu::testutils::{capture_matches, TestEnvironment}; #[test] fn smoke_test() { @@ -26,20 +25,12 @@ fn smoke_test() { let repo_path = test_env.env_root().join("repo"); // Check the output of `jj status` right after initializing repo let assert = test_env.jj_cmd(&repo_path, &["status"]).assert().success(); - let stdout_string_empty = String::from_utf8(assert.get_output().stdout.clone()).unwrap(); let output_regex = "^Parent commit: 000000000000[ ] Working copy : ([[:xdigit:]]+)[ ] The working copy is clean $"; - assert.stdout(predicates::str::is_match(output_regex).unwrap()); - let wc_hex_id_empty = Regex::new(output_regex) - .unwrap() - .captures(&stdout_string_empty) - .unwrap() - .get(1) - .unwrap() - .as_str() - .to_owned(); + let (_, matches) = capture_matches(assert, output_regex); + let wc_hex_id_empty = matches[0].clone(); // Write some files and check the output of `jj status` std::fs::write(repo_path.join("file1"), "file1").unwrap(); @@ -47,7 +38,6 @@ $"; std::fs::write(repo_path.join("file3"), "file3").unwrap(); let assert = test_env.jj_cmd(&repo_path, &["status"]).assert().success(); - let stdout_string_non_empty = String::from_utf8(assert.get_output().stdout.clone()).unwrap(); let output_regex = "^Parent commit: 000000000000[ ] Working copy : ([[:xdigit:]]+)[ ] Working copy changes: @@ -55,23 +45,17 @@ A file1 A file2 A file3 $"; - assert.stdout(predicates::str::is_match(output_regex).unwrap()); - let wc_hex_id_non_empty = Regex::new(output_regex) - .unwrap() - .captures(&stdout_string_non_empty) - .unwrap() - .get(1) - .unwrap() - .as_str() - .to_owned(); + let (_, matches) = capture_matches(assert, output_regex); + let wc_hex_id_non_empty = matches[0].clone(); // The working copy's id should have changed assert_ne!(wc_hex_id_non_empty, wc_hex_id_empty); // Running `jj status` again gives the same output let assert = test_env.jj_cmd(&repo_path, &["status"]).assert().success(); - let stdout_string_again = String::from_utf8(assert.get_output().stdout.clone()).unwrap(); - assert_eq!(stdout_string_again, stdout_string_non_empty); + let (_, matches) = capture_matches(assert, output_regex); + let wc_hex_id_again = matches[0].clone(); + assert_eq!(wc_hex_id_again, wc_hex_id_non_empty); // Add a commit description let assert = test_env