Skip to content

Commit 6e0b214

Browse files
committed
test: [#117] suppress stderr in error exit code test for clean output
- Added exec_with_exit_code_silent method to RunningBinaryContainer - Method redirects stderr to /dev/null within the container - Updated it_should_return_error_exit_code_when_installation_fails to use silent execution - Eliminates error message noise in test output while still validating exit codes
1 parent a0d9276 commit 6e0b214

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

packages/dependency-installer/tests/containers/running_binary_container.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,50 @@ impl RunningBinaryContainer {
9393
status.code().unwrap_or(1)
9494
}
9595

96+
/// Execute a command and return the exit code, suppressing stderr output
97+
///
98+
/// This is useful for tests that intentionally trigger errors and want to
99+
/// keep test output clean. The command's stderr is redirected to /dev/null
100+
/// within the container.
101+
///
102+
/// # Arguments
103+
///
104+
/// * `command` - Command and arguments to execute
105+
///
106+
/// # Returns
107+
///
108+
/// The exit code of the command, or 1 if the process was terminated by a signal
109+
///
110+
/// # Example
111+
///
112+
/// ```rust
113+
/// // Test that expects an error but doesn't want stderr noise in test output
114+
/// let exit_code = container.exec_with_exit_code_silent(&[
115+
/// "dependency-installer",
116+
/// "install",
117+
/// "--dependency",
118+
/// "invalid-name",
119+
/// ]);
120+
/// assert_ne!(exit_code, 0);
121+
/// ```
122+
#[allow(dead_code)] // Used by install_command tests but not check_command tests
123+
pub fn exec_with_exit_code_silent(&self, command: &[&str]) -> ExitCode {
124+
// Build the command with stderr redirected to /dev/null
125+
let command_str = format!("{} 2>/dev/null", command.join(" "));
126+
127+
let status = Command::new("docker")
128+
.arg("exec")
129+
.arg(&self.container_id)
130+
.arg("sh")
131+
.arg("-c")
132+
.arg(&command_str)
133+
.status()
134+
.expect("Failed to execute docker exec command");
135+
136+
// Return 1 (failure) if terminated by signal, otherwise use actual exit code
137+
status.code().unwrap_or(1)
138+
}
139+
96140
/// Copy a file from the host into this running container
97141
///
98142
/// This method uses Docker CLI to copy files into the running container.

packages/dependency-installer/tests/install_command_docker_integration.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,18 @@ async fn it_should_install_ansible_successfully() {
191191
}
192192

193193
/// Test that install command returns proper exit code on failure
194+
///
195+
/// Note: This test uses `exec_with_exit_code_silent` to suppress the expected
196+
/// error output, keeping test output clean while still validating the exit code.
194197
#[tokio::test]
195198
async fn it_should_return_error_exit_code_when_installation_fails() {
196199
let binary_path = get_binary_path();
197200

198201
let container = UbuntuContainerBuilder::new(&binary_path).start().await;
199202

200203
// Try to install an invalid/unknown dependency (should fail)
201-
let exit_code = container.exec_with_exit_code(&[
204+
// Use silent execution to avoid error noise in test output
205+
let exit_code = container.exec_with_exit_code_silent(&[
202206
"dependency-installer",
203207
"install",
204208
"--dependency",

0 commit comments

Comments
 (0)