Skip to content

Commit 7432591

Browse files
Copilotjosecelano
andcommitted
refactor: [#116] address code review feedback
Co-authored-by: josecelano <58816+josecelano@users.noreply.github.com>
1 parent 2040c38 commit 7432591

File tree

2 files changed

+47
-14
lines changed

2 files changed

+47
-14
lines changed

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ pub fn copy_file_to_container(container_id: &str, source_path: &Path, dest_path:
4747
/// # Returns
4848
///
4949
/// The combined stdout and stderr output as a string
50+
///
51+
/// # Note
52+
///
53+
/// The output combines stderr and stdout because the CLI uses tracing which writes
54+
/// logs to stderr, while user-facing messages go to stdout. We need both for
55+
/// comprehensive test assertions. Stderr is placed first to maintain chronological
56+
/// order of log messages relative to output.
5057
pub fn exec_in_container(container_id: &str, command: &[&str]) -> String {
5158
let output = Command::new("docker")
5259
.arg("exec")
@@ -55,7 +62,7 @@ pub fn exec_in_container(container_id: &str, command: &[&str]) -> String {
5562
.output()
5663
.expect("Failed to execute docker exec command");
5764

58-
// Combine stdout and stderr to capture all output (logs go to stderr)
65+
// Combine stderr (logs) and stdout (user messages) to capture all output
5966
let stdout = String::from_utf8_lossy(&output.stdout);
6067
let stderr = String::from_utf8_lossy(&output.stderr);
6168
format!("{}{}", stderr, stdout)
@@ -70,7 +77,12 @@ pub fn exec_in_container(container_id: &str, command: &[&str]) -> String {
7077
///
7178
/// # Returns
7279
///
73-
/// The exit code of the command
80+
/// The exit code of the command, or 1 if the process was terminated by a signal
81+
///
82+
/// # Note
83+
///
84+
/// If the process was terminated by a signal (returns None from code()), we return 1
85+
/// to indicate failure rather than 0, which would incorrectly suggest success.
7486
pub fn exec_in_container_with_exit_code(container_id: &str, command: &[&str]) -> i32 {
7587
let status = Command::new("docker")
7688
.arg("exec")
@@ -79,5 +91,6 @@ pub fn exec_in_container_with_exit_code(container_id: &str, command: &[&str]) ->
7991
.status()
8092
.expect("Failed to execute docker exec command");
8193

82-
status.code().unwrap_or(0)
94+
// Return 1 (failure) if terminated by signal, otherwise use actual exit code
95+
status.code().unwrap_or(1)
8396
}

packages/dependency-installer/tests/docker_check_command.rs

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,11 @@ async fn test_check_specific_tool() {
6767
// Check a specific tool (OpenTofu)
6868
let output = container.exec(&["dependency-installer", "check", "--tool", "opentofu"]);
6969

70-
// The output contains the status line with the checkmark/X symbol
70+
// The output contains "OpenTofu: not installed" in the status line
71+
// We check for the plain text version since the ✗ symbol may not be present
72+
// in all terminal environments or when output is redirected
7173
assert!(
72-
output.contains("✗ OpenTofu: not installed") || output.contains("OpenTofu: not installed"),
74+
output.contains("OpenTofu: not installed"),
7375
"Expected OpenTofu to be reported as not installed, got: {}",
7476
output
7577
);
@@ -149,16 +151,34 @@ async fn test_verbose_output() {
149151
///
150152
/// This function assumes the binary was built before running tests.
151153
/// Run `cargo build --bin dependency-installer` before running these tests.
154+
///
155+
/// # Implementation Note
156+
///
157+
/// We use CARGO_MANIFEST_DIR and navigate up to the workspace root, then into
158+
/// the target directory. This works because:
159+
/// 1. CARGO_MANIFEST_DIR points to packages/dependency-installer
160+
/// 2. The workspace root is two directories up
161+
/// 3. The target directory is in the workspace root
162+
///
163+
/// Alternative approaches considered:
164+
/// - CARGO_TARGET_DIR: Not always set
165+
/// - OUT_DIR: Points to build script output, not target/debug
166+
/// - Searching for target dir: Too expensive
152167
fn get_binary_path() -> PathBuf {
153-
// Get the workspace root directory
154-
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
155-
156-
// Navigate to workspace target directory
157-
path.pop(); // packages
158-
path.pop(); // repository root
159-
path.push("target");
160-
path.push("debug");
161-
path.push("dependency-installer");
168+
// Get the package manifest directory (packages/dependency-installer)
169+
let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
170+
171+
// Navigate to workspace root (two levels up from packages/dependency-installer)
172+
let workspace_root = manifest_dir
173+
.parent() // packages/
174+
.and_then(|p| p.parent()) // workspace root
175+
.expect("Failed to find workspace root");
176+
177+
// Build path to the binary in target/debug
178+
let path = workspace_root
179+
.join("target")
180+
.join("debug")
181+
.join("dependency-installer");
162182

163183
assert!(
164184
path.exists(),

0 commit comments

Comments
 (0)