Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The example of testing the ls command via cargo test doesn't work on Windows. Here's how to fix it. #15

Open
WraithGlade opened this issue Sep 10, 2023 · 1 comment

Comments

@WraithGlade
Copy link

WraithGlade commented Sep 10, 2023

On page 31 of the Kindle Edition of the book, the following example code from the book doesn't work on Windows:

use std::process::Command; 

#[test]
fn runs() {
    let mut cmd = Command::new("ls"); 
    let res = cmd.output(); 
    assert!(res.is_ok()); 
}

The reason is because there is no separate EXE file corresponding to ls on Windows.

Windows' ls command is actually an internal component of the powershell executable.

To fix it, what actually must be done is to pass "powershell" as the command and then give it "ls" as an argument.

Thus, the following code (in contrast), works on Windows:

use std::process::Command;

#[test]
fn runs() {
    let mut cmd = Command::new("powershell");
    cmd.arg("ls");
    let res = cmd.output();
    assert!(res.is_ok());
}

Perhaps this mistake was made by emulating Windows on Linux (which is not real Windows) or by not testing it on Windows at all.

Anyway, thanks for your time and for writing the book!

@kyclark
Copy link
Owner

kyclark commented Jan 3, 2024

Thanks for the comment! All my code examples worked using WSL1. I confess I didn't extensively test on Windows as I have used Unix-like operating systems for over 20 years and never have reason to use Windows. In chapter 7 (findr), I show how to use #[cfg(windows)] to conditionally compile code when on Windows, but chapter 1 is way to early to introduce such a concept.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants