Skip to content

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

Description

@WraithGlade

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!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions