I’m stuck on Stage #JV1
I’ve tried enabling debug output, but it made no change in my logs.
Here are my logs:
[compile] Compiling codecrafters-shell v0.1.0 (/app)
[compile] Finished `release` profile [optimized] target(s) in 0.83s
[compile] Moved ./.codecrafters/run.sh → ./your_program.sh
[compile] Compilation successful.
[tester::#JV1] Running tests for Stage #JV1 (Redirection - Redirect stdout)
[tester::#JV1] [setup] export PATH=/tmp/mango/pear/orange:$PATH
[tester::#JV1] Running ./your_program.sh
[tester::#JV1] [setup] echo "banana" > "/tmp/rat/banana"
[tester::#JV1] [setup] echo "grape" > "/tmp/rat/grape"
[tester::#JV1] [setup] echo "pear" > "/tmp/rat/pear"
[your-program] $ ls -1 /tmp/rat > /tmp/pig/bee.md
[your-program] $ cat /tmp/pig/bee.md
[your-program] banana
[your-program] grape
[your-program] pear
[tester::#JV1] ✓ Received redirected file content
[your-program] $ echo 'Hello Maria' 1> /tmp/pig/dog.md
[your-program] $ cat /tmp/pig/dog.md
[your-program] Hello Maria
[tester::#JV1] ✓ Received redirected file content
[your-program] $ cat /tmp/rat/grape nonexistent 1> /tmp/pig/rat.md
[your-program] cat: nonexistent: No such file or directory
[tester::#JV1] ✓ Received error message
[your-program] $ cat /tmp/pig/rat.md
[your-program] cat: /tmp/pig/rat.md: No such file or directory
[tester::#JV1] ^ Line does not match expected value.
[tester::#JV1] Expected: "grape"
[tester::#JV1] Received: "cat: /tmp/pig/rat.md: No such file or directory"
[your-program] $
[tester::#JV1] Assertion failed.
[tester::#JV1] Test failed
I don’t quite understand how this error happens, as the error seems to be from cat, not my code. For some reason cat returns an error that says it can’t find the file or directory, but the test expects there to be a file there.
Preceding tests shows cat working fine and printing the expected contents when there is a file (tmp/pig/bee and tmp/pig/dog), so why the /tmp/pig/rat.md call to cat fails to find a file when the test expects it to find one is a mystery to me.
I’ve no idea how my code can be responsible for cat sometimes, but not always, failing to find a file that exists. If it wasn’t for how unlikely it is, I’d honestly conclude that it’s the test that’s set up wrong, erroneously trying to access and print the contents of a file that doesn’t exist. But I assume that’s not the case, and that there’s some part of my code that’s messing up.
Here’s the code that should be relevant to the output of my logs:
// This is the code in charge of running the program and passing on its output
// The function this code exists in returns a Resul<String, String>
// It returns OK with parsed output when the program runs and outputs parseable text
// It returns Err with appropriate message if program fails to run, outputs to stderr or the parsing of output fails
if let Ok(output) = process::Command::new(cmd).args(args).output() {
if output.status.success() {
match str::from_utf8(&output.stdout) {
Ok(val) => result = Ok(val.trim().to_string()),
Err(_) => result = Err(String::from("failed to parse program output as text"))
}
} else {
match str::from_utf8(&output.stderr) {
Ok(val) => result = Err(val.trim().to_string()),
Err(_) => result = Err(String::from("failed to parse program output as text"))
}
}
} else {
let err_message = String::from("couldn't execute program");
result = Err(err_message)
}
// Later in the main loop this code handles results sent from the various functions associated with the shells different commands
// It prints to stderr if the result returned an error message
// It writes to file if the result returned an OK and the user initially gave arguments to pipe output to a file
// It prints to stdout if the result returned OK with a non-empty string and the user didn't try to pipe to a file
match result {
Ok(message) => {
if !output_file_path.is_empty() {
write_output_to_file(message, output_file_path);
} else if !message.is_empty() {
println!("{message}");
}
}
Err(message) => {
eprintln!("{message}");
}
}