Improve tests for downloading definition files

This commit is contained in:
Filip Tibell 2023-01-24 14:05:19 -05:00
parent 5c6fdb4a6e
commit 4f26b02de8
No known key found for this signature in database

View file

@ -121,43 +121,50 @@ impl Cli {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::{env::set_current_dir, time::Duration}; use std::env::{current_dir, set_current_dir};
use anyhow::{bail, Result}; use anyhow::{bail, Context, Result};
use serde_json::Value; use serde_json::Value;
use smol::{ use smol::fs::{create_dir_all, read_to_string, remove_file};
fs::{create_dir_all, read_to_string},
Timer,
};
use super::{Cli, LUNE_LUAU_FILE_NAME, LUNE_SELENE_FILE_NAME}; use super::{Cli, LUNE_LUAU_FILE_NAME, LUNE_SELENE_FILE_NAME};
async fn sleep(seconds: f32) -> Result<()> { async fn run_cli(cli: Cli) -> Result<()> {
Timer::after(Duration::from_secs_f32(seconds)).await; let path = current_dir()
.context("Failed to get current dir")?
.join("bin");
create_dir_all(&path)
.await
.context("Failed to create bin dir")?;
set_current_dir(&path).context("Failed to set current dir")?;
cli.run().await?;
Ok(()) Ok(())
} }
async fn run_cli(cli: Cli) -> Result<()> { async fn ensure_file_exists_and_is_not_json(file_name: &str) -> Result<()> {
create_dir_all("bin").await?; match read_to_string(file_name)
sleep(0.125).await?; .await
set_current_dir("bin")?; .context("Failed to read definitions file")
sleep(0.125).await?; {
cli.run().await?; Ok(file_contents) => match serde_json::from_str::<Value>(&file_contents) {
sleep(0.125).await?; Err(_) => {
remove_file(file_name)
.await
.context("Failed to remove definitions file")?;
Ok(()) Ok(())
} }
Ok(_) => bail!("Downloading selene definitions returned json, expected luau"),
},
Err(e) => bail!("Failed to download selene definitions!\n{e}"),
}
}
#[test] #[test]
fn download_selene_types() -> Result<()> { fn download_selene_types() -> Result<()> {
smol::block_on(async { smol::block_on(async {
run_cli(Cli::download_selene_types()).await?; run_cli(Cli::download_selene_types()).await?;
match read_to_string(LUNE_SELENE_FILE_NAME).await { ensure_file_exists_and_is_not_json(LUNE_SELENE_FILE_NAME).await?;
Ok(file_contents) => match serde_json::from_str::<Value>(&file_contents) { Ok(())
Err(_) => Ok(()),
Ok(_) => bail!("Downloading selene definitions returned json, expected luau"),
},
Err(_) => bail!("Failed to download selene definitions!"),
}
}) })
} }
@ -165,13 +172,8 @@ mod tests {
fn download_luau_types() -> Result<()> { fn download_luau_types() -> Result<()> {
smol::block_on(async { smol::block_on(async {
run_cli(Cli::download_luau_types()).await?; run_cli(Cli::download_luau_types()).await?;
match read_to_string(LUNE_LUAU_FILE_NAME).await { ensure_file_exists_and_is_not_json(LUNE_LUAU_FILE_NAME).await?;
Ok(file_contents) => match serde_json::from_str::<Value>(&file_contents) { Ok(())
Err(_) => Ok(()),
Ok(_) => bail!("Downloading luau definitions returned json, expected luau"),
},
Err(_) => bail!("Failed to download luau definitions!"),
}
}) })
} }
} }