Skip to content

Commit 6da4ade

Browse files
egithinjiEric Githinji
andauthored
Add xtask command for running web-tests. (#2743)
Currently, to run the tests that are located in the `tests` directory (the js tests), one has to navigate to the directory and run `npm test` or `npm start`. We now have a way of automating such task execution using the binary in the `xtask` directory. This pr makes use of this by introducing a new command `cargo xtask web-tests` that can be run from anywhere in the repo to run the tests in the `tests` directory. --------- Co-authored-by: Eric Githinji <[email protected]>
1 parent ce9d747 commit 6da4ade

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

tests/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ used mainly in the [CI](../github/workflows/build.yml) to serve the book on port
1414
`localhost:8080` such that the test runner can access it. This mode is used when
1515
`npm start` or `npm test` is executed.
1616

17+
> **Tip:** Use `cargo xtask web-tests` to run the tests in this directory from
18+
> anywhere in the repository.
19+
1720
For local testing and quick iterations it is possible to use `mdbook serve`
1821
which creates a small HTTP server on port 3000 by default. There is a special
1922
config that is invoked with `npm run test-mdbook` that uses

xtask/src/main.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ fn execute_task() -> Result<()> {
4545
let task = Args::parse().task;
4646
match task.as_str() {
4747
"install-tools" => install_tools()?,
48+
"web-tests" => run_web_tests()?,
4849
_ => {
4950
return Err(anyhow!(unrecognized_task_string(task.as_str())));
5051
}
@@ -95,10 +96,34 @@ fn install_tools() -> Result<()> {
9596
Ok(())
9697
}
9798

99+
fn run_web_tests() -> Result<()> {
100+
println!("Running web tests...");
101+
102+
let path_to_tests_dir = Path::new(env!("CARGO_WORKSPACE_DIR")).join("tests");
103+
104+
let status = Command::new("npm")
105+
.current_dir(path_to_tests_dir.to_str().unwrap())
106+
.arg("test")
107+
.status()
108+
.expect("Failed to execute npm test");
109+
110+
if !status.success() {
111+
let error_message = format!(
112+
"Command 'cargo web-tests' exited with status code: {}",
113+
status.code().unwrap()
114+
);
115+
return Err(anyhow!(error_message));
116+
}
117+
118+
Ok(())
119+
}
120+
121+
// TODO - https://github.com/google/comprehensive-rust/issues/2741: Replace this with Clap
98122
fn unrecognized_task_string(task: &str) -> String {
99123
format!(
100124
"Unrecognized task '{task}'. Available tasks:
101125
102-
install-tools Installs the tools the project depends on."
126+
install-tools Installs the tools the project depends on.
127+
web-tests Runs the web driver tests in the tests directory."
103128
)
104129
}

0 commit comments

Comments
 (0)