Add tuono cli integration test (#177)

Co-authored-by: KingTheSim <KingTheSim@abv.bg>
Co-authored-by: KingTheSim <122887037+KingTheSim@users.noreply.github.com>
Co-authored-by: Valerio Ageno <valerio.ageno@qonto.com>
This commit is contained in:
Valerio Ageno
2024-11-29 16:34:56 +01:00
committed by GitHub
parent 53ab927544
commit 4fb1d6077c
4 changed files with 87 additions and 0 deletions
+5
View File
@@ -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"
+32
View File
@@ -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))"#
));
}
+47
View File
@@ -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.");
}
}