From 4be5aaa0010c870ad70e4bbb84e800b8f8932a87 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sat, 26 Mar 2022 18:27:21 +0200 Subject: refactor(atward): Split tests into separate file Change-Id: I447bc95b60a7dca912ca75e118c075e52f8c8ebe Reviewed-on: https://cl.tvl.fyi/c/depot/+/5402 Tested-by: BuildkiteCI Reviewed-by: sterni Reviewed-by: wpcarro --- web/atward/src/main.rs | 194 +----------------------------------------------- web/atward/src/tests.rs | 187 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 190 insertions(+), 191 deletions(-) create mode 100644 web/atward/src/tests.rs (limited to 'web/atward') diff --git a/web/atward/src/main.rs b/web/atward/src/main.rs index 26d79cde1a1b..eb2603a226c6 100644 --- a/web/atward/src/main.rs +++ b/web/atward/src/main.rs @@ -8,6 +8,9 @@ use regex::Regex; use rouille::input::cookies; use rouille::{Request, Response}; +#[cfg(test)] +mod tests; + /// A query handler supported by atward. It consists of a pattern on /// which to match and trigger the query, and a function to execute /// that returns the target URL. @@ -195,194 +198,3 @@ fn main() { }) }); } - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn bug_query() { - assert_eq!( - dispatch(&handlers(), &"b/42".into()), - Some("https://b.tvl.fyi/42".to_string()) - ); - - assert_eq!( - dispatch(&handlers(), &"something only mentioning b/42".into()), - None, - ); - assert_eq!(dispatch(&handlers(), &"b/invalid".into()), None,); - } - - #[test] - fn cl_query() { - assert_eq!( - dispatch(&handlers(), &"cl/42".into()), - Some("https://cl.tvl.fyi/42".to_string()) - ); - - assert_eq!( - dispatch(&handlers(), &"something only mentioning cl/42".into()), - None, - ); - assert_eq!(dispatch(&handlers(), &"cl/invalid".into()), None,); - } - - #[test] - fn depot_path_cgit_query() { - assert_eq!( - dispatch(&handlers(), &"//web/atward/default.nix".into()), - Some("https://code.tvl.fyi/tree/web/atward/default.nix".to_string()), - ); - - assert_eq!( - dispatch(&handlers(), &"//nix/readTree/README.md".into()), - Some("https://code.tvl.fyi/about/nix/readTree/README.md".to_string()), - ); - - assert_eq!(dispatch(&handlers(), &"/not/a/depot/path".into()), None); - } - - #[test] - fn depot_path_sourcegraph_query() { - assert_eq!( - dispatch( - &handlers(), - &Query { - query: "//web/atward/default.nix".to_string(), - cs: true, - } - ), - Some("https://cs.tvl.fyi/depot/-/tree/web/atward/default.nix".to_string()), - ); - - assert_eq!( - dispatch( - &handlers(), - &Query { - query: "/not/a/depot/path".to_string(), - cs: true, - } - ), - None - ); - } - - #[test] - fn depot_root_cgit_query() { - assert_eq!( - dispatch( - &handlers(), - &Query { - query: "//".to_string(), - cs: false, - } - ), - Some("https://code.tvl.fyi/tree/".to_string()), - ); - } - - #[test] - fn plain_host_queries() { - assert_eq!( - dispatch(&handlers(), &"cs".into()), - Some("https://cs.tvl.fyi/".to_string()), - ); - - assert_eq!( - dispatch(&handlers(), &"cl".into()), - Some("https://cl.tvl.fyi/".to_string()), - ); - - assert_eq!( - dispatch(&handlers(), &"b".into()), - Some("https://b.tvl.fyi/".to_string()), - ); - - assert_eq!( - dispatch(&handlers(), &"todo".into()), - Some("https://todo.tvl.fyi/".to_string()), - ); - } - - #[test] - fn request_to_query() { - assert_eq!( - Query::from_request(&Request::fake_http("GET", "/?q=b%2F42", vec![], vec![])) - .expect("request should parse to a query"), - Query { - query: "b/42".to_string(), - cs: false, - }, - ); - - assert_eq!( - Query::from_request(&Request::fake_http("GET", "/", vec![], vec![])), - None - ); - } - - #[test] - fn settings_from_cookie() { - assert_eq!( - Query::from_request(&Request::fake_http( - "GET", - "/?q=b%2F42", - vec![("Cookie".to_string(), "cs=true;".to_string())], - vec![] - )) - .expect("request should parse to a query"), - Query { - query: "b/42".to_string(), - cs: true, - }, - ); - } - - #[test] - fn settings_from_query_parameter() { - assert_eq!( - Query::from_request(&Request::fake_http( - "GET", - "/?q=b%2F42&cs=true", - vec![], - vec![] - )) - .expect("request should parse to a query"), - Query { - query: "b/42".to_string(), - cs: true, - }, - ); - - // Query parameter should override cookie - assert_eq!( - Query::from_request(&Request::fake_http( - "GET", - "/?q=b%2F42&cs=false", - vec![("Cookie".to_string(), "cs=true;".to_string())], - vec![] - )) - .expect("request should parse to a query"), - Query { - query: "b/42".to_string(), - cs: false, - }, - ); - } - - #[test] - fn depot_revision_query() { - assert_eq!( - dispatch(&handlers(), &"r/3002".into()), - Some("https://code.tvl.fyi/commit/?id=refs/r/3002".to_string()) - ); - - assert_eq!( - dispatch(&handlers(), &"something only mentioning r/3002".into()), - None, - ); - - assert_eq!(dispatch(&handlers(), &"r/invalid".into()), None,); - } -} diff --git a/web/atward/src/tests.rs b/web/atward/src/tests.rs new file mode 100644 index 000000000000..a23f96ee9a74 --- /dev/null +++ b/web/atward/src/tests.rs @@ -0,0 +1,187 @@ +use super::*; + +#[test] +fn bug_query() { + assert_eq!( + dispatch(&handlers(), &"b/42".into()), + Some("https://b.tvl.fyi/42".to_string()) + ); + + assert_eq!( + dispatch(&handlers(), &"something only mentioning b/42".into()), + None, + ); + assert_eq!(dispatch(&handlers(), &"b/invalid".into()), None,); +} + +#[test] +fn cl_query() { + assert_eq!( + dispatch(&handlers(), &"cl/42".into()), + Some("https://cl.tvl.fyi/42".to_string()) + ); + + assert_eq!( + dispatch(&handlers(), &"something only mentioning cl/42".into()), + None, + ); + assert_eq!(dispatch(&handlers(), &"cl/invalid".into()), None,); +} + +#[test] +fn depot_path_cgit_query() { + assert_eq!( + dispatch(&handlers(), &"//web/atward/default.nix".into()), + Some("https://code.tvl.fyi/tree/web/atward/default.nix".to_string()), + ); + + assert_eq!( + dispatch(&handlers(), &"//nix/readTree/README.md".into()), + Some("https://code.tvl.fyi/about/nix/readTree/README.md".to_string()), + ); + + assert_eq!(dispatch(&handlers(), &"/not/a/depot/path".into()), None); +} + +#[test] +fn depot_path_sourcegraph_query() { + assert_eq!( + dispatch( + &handlers(), + &Query { + query: "//web/atward/default.nix".to_string(), + cs: true, + } + ), + Some("https://cs.tvl.fyi/depot/-/tree/web/atward/default.nix".to_string()), + ); + + assert_eq!( + dispatch( + &handlers(), + &Query { + query: "/not/a/depot/path".to_string(), + cs: true, + } + ), + None + ); +} + +#[test] +fn depot_root_cgit_query() { + assert_eq!( + dispatch( + &handlers(), + &Query { + query: "//".to_string(), + cs: false, + } + ), + Some("https://code.tvl.fyi/tree/".to_string()), + ); +} + +#[test] +fn plain_host_queries() { + assert_eq!( + dispatch(&handlers(), &"cs".into()), + Some("https://cs.tvl.fyi/".to_string()), + ); + + assert_eq!( + dispatch(&handlers(), &"cl".into()), + Some("https://cl.tvl.fyi/".to_string()), + ); + + assert_eq!( + dispatch(&handlers(), &"b".into()), + Some("https://b.tvl.fyi/".to_string()), + ); + + assert_eq!( + dispatch(&handlers(), &"todo".into()), + Some("https://todo.tvl.fyi/".to_string()), + ); +} + +#[test] +fn request_to_query() { + assert_eq!( + Query::from_request(&Request::fake_http("GET", "/?q=b%2F42", vec![], vec![])) + .expect("request should parse to a query"), + Query { + query: "b/42".to_string(), + cs: false, + }, + ); + + assert_eq!( + Query::from_request(&Request::fake_http("GET", "/", vec![], vec![])), + None + ); +} + +#[test] +fn settings_from_cookie() { + assert_eq!( + Query::from_request(&Request::fake_http( + "GET", + "/?q=b%2F42", + vec![("Cookie".to_string(), "cs=true;".to_string())], + vec![] + )) + .expect("request should parse to a query"), + Query { + query: "b/42".to_string(), + cs: true, + }, + ); +} + +#[test] +fn settings_from_query_parameter() { + assert_eq!( + Query::from_request(&Request::fake_http( + "GET", + "/?q=b%2F42&cs=true", + vec![], + vec![] + )) + .expect("request should parse to a query"), + Query { + query: "b/42".to_string(), + cs: true, + }, + ); + + // Query parameter should override cookie + assert_eq!( + Query::from_request(&Request::fake_http( + "GET", + "/?q=b%2F42&cs=false", + vec![("Cookie".to_string(), "cs=true;".to_string())], + vec![] + )) + .expect("request should parse to a query"), + Query { + query: "b/42".to_string(), + cs: false, + }, + ); +} + +#[test] +fn depot_revision_query() { + assert_eq!( + dispatch(&handlers(), &"r/3002".into()), + Some("https://code.tvl.fyi/commit/?id=refs/r/3002".to_string()) + ); + + assert_eq!( + dispatch(&handlers(), &"something only mentioning r/3002".into()), + None, + ); + + assert_eq!(dispatch(&handlers(), &"r/invalid".into()), None,); +} -- cgit 1.4.1