Skip to content

Commit

Permalink
Upgrade entrait and unimock and improve mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
canac committed Feb 11, 2024
1 parent c5f7c4a commit 0ed9398
Show file tree
Hide file tree
Showing 9 changed files with 386 additions and 489 deletions.
74 changes: 50 additions & 24 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ license = "MIT"
anyhow = "1.0.79"
clap = { version = "4.4.18", features = ["derive"] }
directories = "5.0.1"
entrait = { version = "0.4.6", features = ["unimock"] }
entrait = { version = "0.6.0", features = ["unimock"] }
rand = "0.8.5"
serde = { version = "1.0.196", features = ["derive"] }
toml = "0.8.8"
unimock = "0.3.14"
unimock = "0.5.8"

[build-dependencies]
clap = { version = "4.4.18", features = ["derive"] }
Expand Down
39 changes: 16 additions & 23 deletions src/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,49 +41,42 @@ impl PortAllocator {
#[cfg(test)]
mod tests {
use super::*;
use crate::dependencies;
use unimock::{matching, MockFn, Unimock};

fn get_deps() -> Unimock {
unimock::mock([dependencies::choose_port::Fn
.each_call(matching!(_))
.answers(|available_ports| available_ports.iter().min().copied())
.in_any_order()])
}
use crate::mocks::choose_port_mock;
use unimock::Unimock;

#[test]
fn test_random_chooser() {
let range = 3000..=3999;
let mut allocator = PortAllocator::new(range.clone());
let deps = get_deps();
assert!(range.contains(&allocator.allocate(&deps, None).unwrap()));
let mocked_deps = Unimock::new(choose_port_mock());
assert!(range.contains(&allocator.allocate(&mocked_deps, None).unwrap()));
}

#[test]
fn test_discard() {
let mut allocator = PortAllocator::new(3000..=3001);
let deps = get_deps();
let mocked_deps = Unimock::new(choose_port_mock());
allocator.discard(3000);
assert_eq!(allocator.allocate(&deps, None).unwrap(), 3001);
assert!(allocator.allocate(&deps, None).is_err());
assert_eq!(allocator.allocate(&mocked_deps, None).unwrap(), 3001);
assert!(allocator.allocate(&mocked_deps, None).is_err());
}

#[test]
fn test_allocate() {
let mut allocator = PortAllocator::new(3000..=3001);
let deps = get_deps();
assert!(allocator.allocate(&deps, None).is_ok());
assert!(allocator.allocate(&deps, None).is_ok());
assert!(allocator.allocate(&deps, None).is_err());
let mocked_deps = Unimock::new(choose_port_mock());
assert!(allocator.allocate(&mocked_deps, None).is_ok());
assert!(allocator.allocate(&mocked_deps, None).is_ok());
assert!(allocator.allocate(&mocked_deps, None).is_err());
}

#[test]
fn test_desired_port() {
let mut allocator = PortAllocator::new(3000..=3002);
let deps = get_deps();
assert_eq!(allocator.allocate(&deps, Some(3001)).unwrap(), 3001);
assert_eq!(allocator.allocate(&deps, Some(4000)).unwrap(), 3000);
assert_eq!(allocator.allocate(&deps, None).unwrap(), 3002);
assert!(allocator.allocate(&deps, None).is_err());
let mocked_deps = Unimock::new(choose_port_mock());
assert_eq!(allocator.allocate(&mocked_deps, Some(3001)).unwrap(), 3001);
assert_eq!(allocator.allocate(&mocked_deps, Some(4000)).unwrap(), 3000);
assert_eq!(allocator.allocate(&mocked_deps, None).unwrap(), 3002);
assert!(allocator.allocate(&mocked_deps, None).is_err());
}
}
14 changes: 8 additions & 6 deletions src/caddy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ pub fn reload(
std::process::Command::new("caddy")
.args(["reload", "--adapter", "caddyfile", "--config"])
.arg(caddyfile_path),
&mut (),
)?;
if status.success() {
Ok(())
Expand All @@ -212,9 +213,10 @@ pub fn reload(

#[cfg(test)]
mod tests {
use unimock::Unimock;

use super::*;
use crate::dependencies::mocks::data_dir_mock;
use crate::registry::tests::get_mocked_registry;
use crate::mocks::{data_dir_mock, get_mocked_registry};

const GOLDEN_CADDYFILE: &str = "localhost {
file_server {
Expand Down Expand Up @@ -242,7 +244,7 @@ app3.localhost {
#[test]
fn test_caddyfile() {
let registry = get_mocked_registry().unwrap();
let deps = unimock::mock([data_dir_mock()]);
let deps = Unimock::new(data_dir_mock());
assert_eq!(
generate_caddyfile(&deps, &registry).unwrap(),
GOLDEN_CADDYFILE
Expand All @@ -251,7 +253,7 @@ app3.localhost {

#[test]
fn test_update_import_no_existing() {
let deps = unimock::mock([data_dir_mock()]);
let deps = Unimock::new(data_dir_mock());
assert_eq!(
update_import(&deps, None).unwrap(),
Some(String::from("import \"/data/Caddyfile\"\n"))
Expand All @@ -260,7 +262,7 @@ app3.localhost {

#[test]
fn test_update_import_already_present() {
let deps = unimock::mock([data_dir_mock()]);
let deps = Unimock::new(data_dir_mock());
assert!(update_import(
&deps,
Some(String::from(
Expand All @@ -273,7 +275,7 @@ app3.localhost {

#[test]
fn test_update_import_prepend() {
let deps = unimock::mock([data_dir_mock()]);
let deps = Unimock::new(data_dir_mock());
assert_eq!(
update_import(&deps, Some(String::from("# Suffix\n"))).unwrap(),
Some(String::from("import \"/data/Caddyfile\"\n# Suffix\n"))
Expand Down
28 changes: 17 additions & 11 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,28 +98,34 @@ mod tests {
use super::*;
use crate::dependencies;
use std::path::PathBuf;
use unimock::{matching, MockFn};
use unimock::{matching, MockFn, Unimock};

#[test]
fn test_load_config() {
let deps = unimock::mock([dependencies::read_file::Fn
.each_call(matching!(_))
.answers(|_| Ok(Some(String::from("ranges = [[3000, 3999]]\nreserved = []"))))
.in_any_order()]);
let deps = Unimock::new(
dependencies::ReadFileMock
.each_call(matching!((path) if path == &PathBuf::from("config.toml")))
.answers(|_| Ok(Some(String::from("ranges = [[3000, 3999]]\nreserved = []"))))
.n_times(1),
);

let config = Config::load(&deps, &PathBuf::new()).unwrap().unwrap();
let config = Config::load(&deps, &PathBuf::from("config.toml"))
.unwrap()
.unwrap();
assert_eq!(config.ranges, vec![(3000, 3999)]);
assert_eq!(config.reserved, vec![]);
}

#[test]
fn test_load_missing_config() {
let deps = unimock::mock([dependencies::read_file::Fn
.each_call(matching!(_))
.answers(|_| bail!("Read error"))
.in_any_order()]);
let deps = Unimock::new(
dependencies::ReadFileMock
.each_call(matching!((path) if path == &PathBuf::from("config.toml")))
.answers(|_| bail!("Read error"))
.n_times(1),
);

let config = Config::load(&deps, &PathBuf::new());
let config = Config::load(&deps, &PathBuf::from("config.toml"));
assert!(config.is_err());
}

Expand Down
Loading

0 comments on commit 0ed9398

Please sign in to comment.