diff --git a/Cargo.toml b/Cargo.toml index 3e94794..0ebc525 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,8 +4,21 @@ version = "0.0.1" authors = ["Jef Roosens "] edition = "2018" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[lib] +name = "fej_lib" +src = "src/lib.rs" +test = true +bench = true +doc = true +doctest = true +[[bin]] +name = "fej" +src = "src/main.rs" +test = false +bench = false +doc = false + [dependencies] rocket = "0.4.7" serde = "1.0.124" diff --git a/Dockerfile b/Dockerfile index 4a3ed8b..457e805 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,11 +13,13 @@ RUN apk update && apk add --no-cache openssl-dev build-base curl && \ COPY Cargo.toml Cargo.lock ./ COPY src/ ./src/ -# Finally, build the project +# Run the tests, don't want no broken docker image +# And then finally, build the project # Thank the lords that this article exists # https://users.rust-lang.org/t/sigsegv-with-program-linked-against-openssl-in-an-alpine-container/52172 # TODO add what these flags do & why they work -RUN RUSTFLAGS="-C target-feature=-crt-static" cargo build --release +RUN RUSTFLAGS="-C target-feature=-crt-static" cargo test && \ + RUSTFLAGS="-C target-feature=-crt-static" cargo build --release --bin fej # Now, we create the actual image diff --git a/Makefile b/Makefile index a1baf59..ab50282 100644 --- a/Makefile +++ b/Makefile @@ -23,13 +23,13 @@ push: # Run run: - @ RUST_BACKTRACE=1 cargo run + @ RUST_BACKTRACE=1 cargo run --bin fej .PHONY: run # Testing test: - @ cargo test + @ cargo test --no-fail-fast .PHONY: test format: diff --git a/benches/ivago.rs b/benches/ivago.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/benches/ivago.rs @@ -0,0 +1 @@ + diff --git a/src/errors.rs b/src/errors.rs index a34dba6..dfb137f 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -1,6 +1,7 @@ // I can probably do this way easier using an external crate, I should do that use rocket::http::Status; +#[derive(Debug, PartialEq)] pub enum FejError { InvalidArgument, FailedRequest, diff --git a/src/ivago/controller/structs/basic_date.rs b/src/ivago/controller/structs/basic_date.rs index e7518fa..97ef3e4 100644 --- a/src/ivago/controller/structs/basic_date.rs +++ b/src/ivago/controller/structs/basic_date.rs @@ -10,6 +10,7 @@ use std::convert::TryFrom; /// This class is a simple wrapper around chrono's DateTime. Its sole purpose /// is to avoid error E0117. +#[derive(Debug, PartialEq)] pub struct BasicDate(pub DateTime); /// This allows us to use BasicDate as a query parameter in our routes. @@ -54,3 +55,15 @@ impl Serialize for BasicDate { serializer.serialize_str(&String::from(self)) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_invalid_date() { + let val = "2012-13-12"; + let date = BasicDate::try_from(val); + assert_eq!(date, Err(FejError::InvalidArgument)); + } +} diff --git a/src/ivago/mod.rs b/src/ivago/mod.rs index 4a03e48..28ac98b 100644 --- a/src/ivago/mod.rs +++ b/src/ivago/mod.rs @@ -1,6 +1,4 @@ mod controller; -#[cfg(test)] -mod tests; use controller::structs::{BasicDate, PickupTime, Street}; use controller::{get_pickup_times, search_streets}; diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..b1e2733 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,11 @@ +#![feature(proc_macro_hygiene, decl_macro)] + +#[macro_use] +extern crate rocket; + +// Route modules +pub mod ivago; + +// Helper modules +pub mod catchers; +pub mod errors; diff --git a/src/main.rs b/src/main.rs index 485f370..ad86d98 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,14 +1,7 @@ -#![feature(proc_macro_hygiene, decl_macro)] - #[macro_use] extern crate rocket; -extern crate chrono_tz; - -// Route modules -mod catchers; -mod errors; -mod ivago; +use fej_lib::{catchers, ivago}; fn rocket() -> rocket::Rocket { rocket::ignite() diff --git a/src/ivago/tests.rs b/tests/ivago.rs similarity index 91% rename from src/ivago/tests.rs rename to tests/ivago.rs index 2b91e48..619b9c7 100644 --- a/src/ivago/tests.rs +++ b/tests/ivago.rs @@ -2,7 +2,7 @@ use rocket::http::Status; use rocket::local::Client; fn rocket() -> rocket::Rocket { - rocket::ignite().mount("/", super::routes()) + rocket::ignite().mount("/", fej_lib::ivago::routes()) } /// Test 404 response