Skip to content

Commit

Permalink
Revert "use temp files for auth login test (vercel#6115)"
Browse files Browse the repository at this point in the history
This reverts commit dbc78d7.
  • Loading branch information
Nathan Hammond authored and Nathan Hammond committed Oct 9, 2023
1 parent 4fd0556 commit 999bc52
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions crates/turborepo-auth/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,8 @@ async fn run_sso_one_shot_server(

#[cfg(test)]
mod test {
use std::path::Path;

use async_trait::async_trait;
use reqwest::{Method, RequestBuilder, Response, Url};
use turborepo_api_client::{Client, Error, Result};
Expand Down Expand Up @@ -530,10 +532,22 @@ mod test {
let api_server = tokio::spawn(start_test_server(port));
let ui = UI::new(false);
let url = format!("http://localhost:{port}");

let temp_file =
tempfile::NamedTempFile::new().expect("Failed to create temp file for test_login");
let token_path = temp_file.path();
let token_filename: &str = "token.json";
let token_path = Path::new(token_filename);

// Since we are writing to a file, we need to make sure we clean up after in the
// event of an assertion failure.
std::panic::set_hook(Box::new(|_| {
// Remove test token file after completion. Recreate path due to ownership
// rules.
match std::fs::remove_file(Path::new(token_filename)) {
Ok(_) => {}
Err(e) => {
println!("failed to remove token file: {}", e);
}
};
let _ = std::panic::take_hook();
}));

let api_client = MockApiClient::new();

Expand All @@ -547,7 +561,7 @@ mod test {
Ok(())
};

login(&api_client, &ui, token_path, set_token, &url)
login(&api_client, &ui, Path::new(token_filename), set_token, &url)
.await
.unwrap();

Expand All @@ -570,6 +584,14 @@ mod test {
api_server.abort();
assert_eq!(LOGIN_HITS.load(std::sync::atomic::Ordering::SeqCst), 1);
assert_eq!(got_token, turborepo_vercel_api_mock::EXPECTED_TOKEN);

// Remove test token file after completion.
match std::fs::remove_file(token_path) {
Ok(_) => {}
Err(e) => {
println!("failed to remove token file: {}", e);
}
}
}

#[tokio::test]
Expand Down

0 comments on commit 999bc52

Please sign in to comment.