diff --git a/.gitignore b/.gitignore index 0d4bd596..31b0626e 100644 --- a/.gitignore +++ b/.gitignore @@ -24,3 +24,6 @@ examples/*/pnpm-lock.yaml # MSVC Windows builds of rustc generate these, which store debugging information *.pdb + +# Files, generated by the code editors +.vscode/ \ No newline at end of file diff --git a/crates/tuono/Cargo.toml b/crates/tuono/Cargo.toml index e5cd2555..258806d3 100644 --- a/crates/tuono/Cargo.toml +++ b/crates/tuono/Cargo.toml @@ -33,3 +33,8 @@ reqwest = {version = "0.12.4", features =["blocking", "json"]} serde_json = "1.0" fs_extra = "1.3.0" http = "1.1.0" + +[dev-dependencies] +tempfile = "3.14.0" +assert_cmd = "2.0.16" +serial_test = "0.4.0" diff --git a/crates/tuono/tests/cli_tests.rs b/crates/tuono/tests/cli_tests.rs new file mode 100644 index 00000000..fb2fe228 --- /dev/null +++ b/crates/tuono/tests/cli_tests.rs @@ -0,0 +1,32 @@ +mod utils; +use assert_cmd::Command; +use serial_test::serial; +use std::fs; +use utils::TempTuonoProject; + +#[test] +#[serial] +fn it_successfully_create_the_index_route() { + let temp_tuono_project = TempTuonoProject::new(); + + temp_tuono_project.add_route("./src/routes/index.rs"); + + let mut test_tuono_build = Command::cargo_bin("tuono").unwrap(); + test_tuono_build + .arg("build") + .arg("--no-js-emit") + .assert() + .success(); + + let temp_main_rs_path = temp_tuono_project.path().join(".tuono/main.rs"); + + let temp_main_rs_content = + fs::read_to_string(&temp_main_rs_path).expect("Failed to read '.tuono/main.rs' content."); + + assert!(temp_main_rs_content.contains(r#"#[path="../src/routes/index.rs"]"#)); + assert!(temp_main_rs_content.contains("mod index;")); + + assert!(temp_main_rs_content.contains( + r#".route("/", get(index::route)).route("/__tuono/data/data.json", get(index::api))"# + )); +} diff --git a/crates/tuono/tests/utils.rs b/crates/tuono/tests/utils.rs new file mode 100644 index 00000000..fa4df874 --- /dev/null +++ b/crates/tuono/tests/utils.rs @@ -0,0 +1,47 @@ +use fs_extra::dir::create_all; +use std::env; +use std::fs::File; +use std::path::{Path, PathBuf}; +use tempfile::{tempdir, TempDir}; + +#[derive(Debug)] +pub struct TempTuonoProject { + original_dir: PathBuf, + temp_dir: TempDir, +} + +impl TempTuonoProject { + pub fn new() -> Self { + let original_dir = env::current_dir().expect("Failed to read current_dir"); + let temp_dir = tempdir().expect("Failed to create temp_dir"); + + env::set_current_dir(temp_dir.path()).expect("Failed to change current dir into temp_dir"); + + TempTuonoProject { + original_dir, + temp_dir, + } + } + + pub fn path(&self) -> &Path { + self.temp_dir.path() + } + + pub fn add_route<'a>(&self, path: &'a str) { + let path = PathBuf::from(path); + create_all( + path.parent().expect("Route path does not have any parent"), + false, + ) + .expect("Failed to create parent route directory"); + File::create(path).expect("Failed to create the route file"); + } +} + +impl Drop for TempTuonoProject { + fn drop(&mut self) { + // Set back the current dir in the previous state + env::set_current_dir(self.original_dir.to_owned()) + .expect("Failed to restore the original directory."); + } +}