diff --git a/src/cli/cli.rs b/src/cli/cli.rs index 02711bc..2b5fb55 100644 --- a/src/cli/cli.rs +++ b/src/cli/cli.rs @@ -8,6 +8,9 @@ use lune::Lune; 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 #[derive(Parser, Debug, Default)] #[command(author, version, about, long_about = None)] @@ -29,8 +32,8 @@ pub struct Cli { download_luau_types: bool, } +#[allow(dead_code)] impl Cli { - #[allow(dead_code)] pub fn from_path(path: S) -> Self where S: Into, @@ -41,7 +44,6 @@ impl Cli { } } - #[allow(dead_code)] pub fn from_path_with_args(path: S, args: A) -> Self where S: Into, @@ -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 { // Download definition files, if wanted let download_types_requested = self.download_selene_types || self.download_luau_types; @@ -66,14 +82,14 @@ impl Cli { if self.download_selene_types { println!("Downloading Selene type definitions..."); client - .fetch_release_asset(&release, "lune.yml") + .fetch_release_asset(&release, LUNE_SELENE_FILE_NAME) .await .map_err(LuaError::external)?; } if self.download_luau_types { println!("Downloading Luau type definitions..."); client - .fetch_release_asset(&release, "luneTypes.d.luau") + .fetch_release_asset(&release, LUNE_LUAU_FILE_NAME) .await .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::(&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::(&file_contents) { + Err(_) => Ok(()), + Ok(_) => bail!("Downloading luau definitions returned json, expected luau"), + }, + Err(_) => bail!("Failed to download luau definitions!"), + } + }) + } +}