I’m getting a test failure on Stage #MG5.
According to the logs, PATH is /tmp/baz:/tmp/bar:/tmp/qux:....
My code is returning that the custom executable my_exe is in /tmp/baz, but the expected response is that it’s in /tmp/bar.
From my code logic, I reckon this would only happen if /tmp/baz/my_exe also exists, in which case this test case should not be failing since it’s following the correct order of looking through PATH (baz is in PATH before bar).
Here are my logs:
[compile] Compiling codecrafters-shell v0.1.0 (/app)
[compile] Finished `release` profile [optimized] target(s) in 0.54s
[compile] Moved ./.codecrafters/run.sh → ./your_program.sh
[compile] Compilation successful.
[tester::#MG5] Running tests for Stage #MG5 (The type builtin: executable files)
[tester::#MG5] [setup] export PATH=/tmp/qux:$PATH
[tester::#MG5] [setup] export PATH=/tmp/bar:$PATH
[tester::#MG5] [setup] export PATH=/tmp/baz:$PATH
[tester::#MG5] [setup] PATH is now: /tmp/baz:/tmp/bar:/tmp/qux:...
[tester::#MG5] [setup] Available executables:
[tester::#MG5] [setup] - my_exe
[tester::#MG5] Running ./your_program.sh
[your-program] $ type cat
[your-program] cat is /usr/bin/cat
[tester::#MG5] ✓ Received expected response
[your-program] $ type cp
[your-program] cp is /usr/bin/cp
[tester::#MG5] ✓ Received expected response
[your-program] $ type mkdir
[your-program] mkdir is /usr/bin/mkdir
[tester::#MG5] ✓ Received expected response
[your-program] $ type my_exe
[your-program] my_exe is /tmp/baz/my_exe
[tester::#MG5] ^ Line does not match expected value.
[tester::#MG5] Expected: "my_exe is /tmp/bar/my_exe"
[tester::#MG5] Received: "my_exe is /tmp/baz/my_exe"
[your-program] $
[tester::#MG5] Assertion failed.
[tester::#MG5] Test failed (try setting 'debug: true' in your codecrafters.yml to see more details)
Code is written in Rust.
Here’s the snippet for reading PATH from env:
let env_path = env::var("PATH").unwrap_or("".to_string());
let path: Vec<&str> = env_path.split(":").collect();
And here’s the snippet that handles traversing the folders in PATH:
for &dir in path {
let paths = fs::read_dir(dir);
match paths {
Ok(dir_contents) => {
for bin_path in dir_contents {
let bin = bin_path.as_ref().unwrap().file_name();
let bin_str = bin.to_str().unwrap();
if bin_str == args[0] {
let path_str = bin_path.unwrap().path();
println!("{} is {}", bin_str, path_str.display());
return Ok(());
}
}
}
Err(_e) => (), // TODO: improve error handling
}
}