Add tests to prevent regressions in downloading type definitions

This commit is contained in:
Filip Tibell 2023-01-24 11:53:06 -05:00
parent 751adc04c3
commit 709c307374
No known key found for this signature in database

View file

@ -8,6 +8,9 @@ use lune::Lune;
use crate::utils::{files::find_parse_file_path, github::Client as GithubClient}; use crate::utils::{files::find_parse_file_path, github::Client as GithubClient};
const LUNE_SELENE_FILE_NAME: &str = "lune.yml";
const LUNE_LUAU_FILE_NAME: &str = "luneTypes.d.luau";
/// Lune CLI /// Lune CLI
#[derive(Parser, Debug, Default)] #[derive(Parser, Debug, Default)]
#[command(author, version, about, long_about = None)] #[command(author, version, about, long_about = None)]
@ -29,8 +32,8 @@ pub struct Cli {
download_luau_types: bool, download_luau_types: bool,
} }
#[allow(dead_code)]
impl Cli { impl Cli {
#[allow(dead_code)]
pub fn from_path<S>(path: S) -> Self pub fn from_path<S>(path: S) -> Self
where where
S: Into<String>, S: Into<String>,
@ -41,7 +44,6 @@ impl Cli {
} }
} }
#[allow(dead_code)]
pub fn from_path_with_args<S, A>(path: S, args: A) -> Self pub fn from_path_with_args<S, A>(path: S, args: A) -> Self
where where
S: Into<String>, S: Into<String>,
@ -54,6 +56,20 @@ impl Cli {
} }
} }
pub fn download_selene_types() -> Self {
Self {
download_selene_types: true,
..Default::default()
}
}
pub fn download_luau_types() -> Self {
Self {
download_luau_types: true,
..Default::default()
}
}
pub async fn run(self) -> Result<ExitCode> { pub async fn run(self) -> Result<ExitCode> {
// Download definition files, if wanted // Download definition files, if wanted
let download_types_requested = self.download_selene_types || self.download_luau_types; let download_types_requested = self.download_selene_types || self.download_luau_types;
@ -66,14 +82,14 @@ impl Cli {
if self.download_selene_types { if self.download_selene_types {
println!("Downloading Selene type definitions..."); println!("Downloading Selene type definitions...");
client client
.fetch_release_asset(&release, "lune.yml") .fetch_release_asset(&release, LUNE_SELENE_FILE_NAME)
.await .await
.map_err(LuaError::external)?; .map_err(LuaError::external)?;
} }
if self.download_luau_types { if self.download_luau_types {
println!("Downloading Luau type definitions..."); println!("Downloading Luau type definitions...");
client client
.fetch_release_asset(&release, "luneTypes.d.luau") .fetch_release_asset(&release, LUNE_LUAU_FILE_NAME)
.await .await
.map_err(LuaError::external)?; .map_err(LuaError::external)?;
} }
@ -108,3 +124,47 @@ impl Cli {
}) })
} }
} }
#[cfg(test)]
mod tests {
use super::{Cli, LUNE_LUAU_FILE_NAME, LUNE_SELENE_FILE_NAME};
use anyhow::{bail, Result};
use serde_json::Value;
use smol::fs::{create_dir_all, read_to_string};
use std::env::set_current_dir;
async fn run_cli(cli: Cli) -> Result<()> {
create_dir_all("bin").await?;
set_current_dir("bin")?;
cli.run().await?;
Ok(())
}
#[test]
fn download_selene_types() -> Result<()> {
smol::block_on(async {
run_cli(Cli::download_selene_types()).await?;
match read_to_string(LUNE_SELENE_FILE_NAME).await {
Ok(file_contents) => match serde_json::from_str::<Value>(&file_contents) {
Err(_) => Ok(()),
Ok(_) => bail!("Downloading selene definitions returned json, expected luau"),
},
Err(_) => bail!("Failed to download selene definitions!"),
}
})
}
#[test]
fn download_luau_types() -> Result<()> {
smol::block_on(async {
run_cli(Cli::download_luau_types()).await?;
match read_to_string(LUNE_LUAU_FILE_NAME).await {
Ok(file_contents) => match serde_json::from_str::<Value>(&file_contents) {
Err(_) => Ok(()),
Ok(_) => bail!("Downloading luau definitions returned json, expected luau"),
},
Err(_) => bail!("Failed to download luau definitions!"),
}
})
}
}