From e598bf0835639a1c2fe34af00dad646cf4657f28 Mon Sep 17 00:00:00 2001 From: Jeffrey Finkelstein Date: Mon, 14 Feb 2022 21:16:37 -0500 Subject: [PATCH] tests: add CmdResult::stdout_is_fixture_bytes() Add helper method `CmdResult::stdout_is_fixture_bytes()`, which is like `stdout_is_fixture()` but compares stdout to the raw bytes of a given file instead of decoding the contents of the file to a UTF-8 string. --- tests/common/util.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/common/util.rs b/tests/common/util.rs index 4db4e2561..422d36328 100644 --- a/tests/common/util.rs +++ b/tests/common/util.rs @@ -265,6 +265,28 @@ impl CmdResult { let contents = read_scenario_fixture(&self.tmpd, file_rel_path); self.stdout_is(String::from_utf8(contents).unwrap()) } + + /// Assert that the bytes of stdout exactly match those of the given file. + /// + /// Contrast this with [`CmdResult::stdout_is_fixture`], which + /// decodes the contents of the file as a UTF-8 [`String`] before + /// comparison with stdout. + /// + /// # Examples + /// + /// Use this method in a unit test like this: + /// + /// ```rust,ignore + /// #[test] + /// fn test_something() { + /// new_ucmd!().succeeds().stdout_is_fixture_bytes("expected.bin"); + /// } + /// ``` + pub fn stdout_is_fixture_bytes>(&self, file_rel_path: T) -> &Self { + let contents = read_scenario_fixture(&self.tmpd, file_rel_path); + self.stdout_is_bytes(contents) + } + /// like stdout_is_fixture(...), but replaces the data in fixture file based on values provided in template_vars /// command output pub fn stdout_is_templated_fixture>(