Skip to content

Commit 951f07c

Browse files
committed
fix tests for client
1 parent 1cce011 commit 951f07c

File tree

3 files changed

+57
-36
lines changed

3 files changed

+57
-36
lines changed

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ aocd-proc = { path = "./aocd-proc", version = "0.2.1" }
1818
[dev-dependencies]
1919
mockall = "0.11.3"
2020
mockito = "0.31.1"
21+
temp-env = "0.3.1"
2122
tempfile = "3.3.0"

src/client.rs

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -239,32 +239,40 @@ mod tests {
239239
self.input = Some(input.to_string());
240240
self
241241
}
242-
fn run<T>(&self, test: impl FnOnce(Aocd) -> Result<T>) -> Result<T> {
243-
let dir = tempdir()?;
244-
let cache_path = dir.path().join("aocd-cache");
245-
std::env::set_var("AOC_SESSION", "testsession");
246-
std::env::set_var("AOC_CACHE_DIR", &cache_path);
247-
dbg!(&cache_path);
248-
let client = Aocd::new(self.year, self.day);
242+
fn run<F, T>(&self, test: F) -> Result<T>
243+
where
244+
T: std::panic::RefUnwindSafe,
245+
F: FnOnce(&Aocd) -> Result<T>
246+
+ std::panic::UnwindSafe
247+
+ std::panic::RefUnwindSafe
248+
+ Copy,
249+
{
250+
let cache_path = std::env::temp_dir().join("aocd-tests");
251+
let _ = std::fs::remove_dir_all(&cache_path);
249252

250-
let result = if let Some(input) = &self.input {
251-
let url = format!("{}/{}/day/{}/input", client.url, client.year, client.day);
252-
let m = mock("GET", url.as_str())
253-
.with_status(200)
254-
.with_body(input)
255-
.expect(0)
256-
.create();
257-
let result = test(client);
258-
m.assert();
259-
result
260-
} else {
261-
test(client)
262-
};
263-
264-
// Explicitly drop the cache so that we notice if it doesn't work.
265-
drop(cache_path);
266-
dir.close()?;
267-
result
253+
temp_env::with_vars(
254+
vec![
255+
("AOC_SESSION", Some("test-session")),
256+
("AOC_CACHE_DIR", Some(cache_path.to_str().unwrap())),
257+
],
258+
move || {
259+
let client = Aocd::new(self.year, self.day);
260+
if let Some(input) = &self.input {
261+
let url = format!("/{}/day/{}/input", client.year, client.day);
262+
let m = mock("GET", url.as_str())
263+
.with_status(200)
264+
.with_header("content-type", "text/plain")
265+
.with_body(input)
266+
.expect(1)
267+
.create();
268+
let result = test(&client);
269+
m.assert();
270+
result
271+
} else {
272+
test(&client)
273+
}
274+
},
275+
)
268276
}
269277
}
270278

@@ -285,19 +293,22 @@ mod tests {
285293
.day(1)
286294
.input("test input")
287295
.run(|client| {
296+
assert_eq!(client.get_input(), "test input");
297+
// A second call will trigger a cache hit. If it doesn't the test will fail because
298+
// the mock endpoint only expects a single call.
288299
assert_eq!(client.get_input(), "test input");
289300
Ok(())
290301
})
291302
}
292303

293304
#[test]
294305
fn test_find_aoc_token_env() {
295-
std::env::set_var("AOC_SESSION", "testsession");
296-
std::env::set_var("AOC_TOKEN", "testtoken");
297-
assert_eq!(find_aoc_token(), "testsession");
298-
std::env::remove_var("AOC_SESSION");
299-
assert_eq!(find_aoc_token(), "testtoken");
300-
std::env::remove_var("AOC_TOKEN");
306+
temp_env::with_var("AOC_SESSION", Some("testsession"), || {
307+
assert_eq!(find_aoc_token(), "testsession");
308+
});
309+
temp_env::with_var("AOC_TOKEN", Some("testtoken"), || {
310+
assert_eq!(find_aoc_token(), "testtoken");
311+
});
301312
}
302313

303314
#[test]
@@ -307,10 +318,9 @@ mod tests {
307318
let mut file = File::create(&file_path)?;
308319
writeln!(file, "testtokenintempfile")?;
309320

310-
std::env::remove_var("AOC_SESSION");
311-
std::env::remove_var("AOC_TOKEN");
312-
std::env::set_var("AOC_TOKEN_PATH", &file_path);
313-
assert_eq!(find_aoc_token(), "testtokenintempfile");
314-
Ok(())
321+
temp_env::with_var("AOC_TOKEN_PATH", Some(&file_path), || {
322+
assert_eq!(find_aoc_token(), "testtokenintempfile");
323+
Ok(())
324+
})
315325
}
316326
}

0 commit comments

Comments
 (0)