Skip to content

Commit

Permalink
test: add integration tests for api/ build (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
Valerioageno authored Dec 7, 2024
1 parent 96502b1 commit db1cee2
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
65 changes: 65 additions & 0 deletions crates/tuono/tests/cli_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ use serial_test::serial;
use std::fs;
use utils::TempTuonoProject;

const POST_API_FILE: &str = r"#[tuono_lib::api(POST)]";
const GET_API_FILE: &str = r"#[tuono_lib::api(GET)]";

#[test]
#[serial]
fn it_successfully_create_the_index_route() {
Expand All @@ -30,3 +33,65 @@ fn it_successfully_create_the_index_route() {
r#".route("/", get(index::route)).route("/__tuono/data/data.json", get(index::api))"#
));
}

#[test]
#[serial]
fn it_successfully_create_an_api_route() {
let temp_tuono_project = TempTuonoProject::new();

temp_tuono_project.add_api("./src/routes/api/health_check.rs", POST_API_FILE);

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.");

dbg!(&temp_main_rs_content);

assert!(temp_main_rs_content.contains(r#"#[path="../src/routes/api/health_check.rs"]"#));
assert!(temp_main_rs_content.contains("mod api_health_check;"));

assert!(temp_main_rs_content.contains(
r#".route("/api/health_check", post(api_health_check::post__tuono_internal_api))"#
));
}

#[test]
#[serial]
fn it_successfully_create_multiple_api_for_the_same_file() {
let temp_tuono_project = TempTuonoProject::new();

temp_tuono_project.add_api(
"./src/routes/api/health_check.rs",
&format!("{POST_API_FILE}{GET_API_FILE}"),
);

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/api/health_check.rs"]"#));
assert!(temp_main_rs_content.contains("mod api_health_check;"));

assert!(temp_main_rs_content.contains(
r#".route("/api/health_check", post(api_health_check::post__tuono_internal_api))"#
));
assert!(temp_main_rs_content.contains(
r#".route("/api/health_check", get(api_health_check::get__tuono_internal_api))"#
));
}
14 changes: 14 additions & 0 deletions crates/tuono/tests/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use fs_extra::dir::create_all;
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::{Path, PathBuf};
use tempfile::{tempdir, TempDir};

Expand Down Expand Up @@ -36,6 +37,19 @@ impl TempTuonoProject {
.expect("Failed to create parent route directory");
File::create(path).expect("Failed to create the route file");
}

pub fn add_api<'a>(&self, path: &'a str, content: &'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");

let mut file = File::create(path).expect("Failed to create the route file");
file.write_all(content.as_bytes())
.expect("Failed to write into API file");
}
}

impl Drop for TempTuonoProject {
Expand Down

0 comments on commit db1cee2

Please sign in to comment.