pop_common/
test.rs

1// SPDX-License-Identifier: GPL-3.0
2
3use crate::errors::Error;
4use duct::cmd;
5use std::path::Path;
6
7/// Run tests of a Rust project.
8///
9/// # Arguments
10///
11/// * `path` - location of the project.
12pub fn test_project(path: Option<&Path>) -> Result<(), Error> {
13	// Execute `cargo test` command in the specified directory.
14	cmd("cargo", vec!["test"])
15		.dir(path.unwrap_or_else(|| Path::new("./")))
16		.run()
17		.map_err(|e| Error::TestCommand(format!("Cargo test command failed: {}", e)))?;
18	Ok(())
19}
20
21#[cfg(test)]
22mod tests {
23	use super::*;
24	use tempfile;
25
26	#[test]
27	fn test_project_works() -> Result<(), Error> {
28		let temp_dir = tempfile::tempdir()?;
29		cmd("cargo", ["new", "test_contract", "--bin"]).dir(temp_dir.path()).run()?;
30		test_project(Some(&temp_dir.path().join("test_contract")))?;
31		Ok(())
32	}
33
34	#[test]
35	fn test_project_wrong_directory() -> Result<(), Error> {
36		let temp_dir = tempfile::tempdir()?;
37		assert!(matches!(
38			test_project(Some(&temp_dir.path().join(""))),
39			Err(Error::TestCommand(..))
40		));
41		Ok(())
42	}
43}