Skip to content

Commit

Permalink
unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chrislearn committed Jan 21, 2025
1 parent facafb6 commit b0aa09f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 40 deletions.
15 changes: 12 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,24 @@ pub struct NewCmd {
#[clap(short, long)]
lang: Option<String>,
}
#[derive(Debug, Clone)]
pub struct Project {
pub name: String,
pub lang: String,
}
#[tokio::main]
async fn main() -> Result<()> {
printer::print_logo();
let opts: Opts = Opts::parse();
match opts.subcmd {
SubCommand::New(new_cmd) => {
set_locale(&new_cmd.lang);
SubCommand::New(NewCmd {project_name, lang}) => {
set_locale(&lang);
let proj = Project {
name: project_name,
lang: lang.unwrap_or("en".to_string()),
};
// updater::check_for_updates().await;
match project::create(&new_cmd) {
match project::create(&proj) {
Ok(_) => (),
Err(e) => printer::error(e.to_string()),
};
Expand Down
14 changes: 6 additions & 8 deletions src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ use std::{env, slice};
use anyhow::{Context, Result};
use rust_i18n::t;

use crate::namer;
use crate::printer::{self, success, warning};
use crate::NewCmd;
use crate::{namer, Project};

pub fn create(new_cmd: &NewCmd) -> Result<()> {
check_name(&new_cmd.project_name)?;
let project_name = &new_cmd.project_name;
let project_path = Path::new(project_name);
pub fn create(proj: &Project) -> Result<()> {
check_name(&proj.name)?;
let project_path = Path::new(&proj.name);
if project_path.exists() {
anyhow::bail!(t!(
"error_project_path_exist",
Expand All @@ -21,8 +19,8 @@ pub fn create(new_cmd: &NewCmd) -> Result<()> {
}

check_path(project_path)?;
crate::templates::classic::generate(new_cmd)?;
after_print_info(project_name);
crate::templates::classic::generate(proj)?;
after_print_info(&proj.name);
Ok(())
}

Expand Down
14 changes: 7 additions & 7 deletions src/templates/classic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,36 @@ use liquid::model::Object;
use rust_i18n::t;

use crate::printer::warning;
use crate::{git, NewCmd};
use crate::{git, Project};

mod selection;
pub(crate) mod selection;
use selection::Selected;

#[derive(rust_embed::RustEmbed)]
#[folder = "./templates/classic"]
struct Template;

pub fn generate(new_cmd: &NewCmd) -> Result<()> {
pub fn generate(proj: &Project) -> Result<()> {
let Some(config) = selection::get_selected()? else {
anyhow::bail!("cli quit!")
};
let project_path = Path::new(&new_cmd.project_name);
let project_path = Path::new(&proj.name);
match git::init_repository(project_path) {
Ok(_) => {}
Err(e) => {
warning(t!("warning_init_git", error = e).replace(r"\n", "\n"));
}
}

create_files(project_path, config, new_cmd)?;
create_files(project_path, config, proj)?;
Ok(())
}

fn create_files(project_path: &Path, user_selected: Selected, new_cmd: &NewCmd) -> Result<()> {
pub(crate) fn create_files(project_path: &Path, user_selected: Selected, proj: &Project) -> Result<()> {
let db_lib = user_selected.db_lib.to_string();
let db_type = user_selected.db_type.to_string();
let data = liquid::object!({
"project_name": new_cmd.project_name,
"project_name": proj.name,
"db_type":db_type,
"db_lib":db_lib,
"main_log_message":t!("main_log_message"),
Expand Down
40 changes: 18 additions & 22 deletions src/tests/test_write_project.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#[cfg(test)]
mod tests {
use crate::{
utils::{
create_project::write_project_file,
get_selection::{DbLib, DbType, Selected},
},
Project,
};
use itertools::Itertools;
use std::path::Path;

use itertools::Itertools;

use crate::templates::classic;
use crate::templates::classic::selection::{DbLib, DbType, Selected};
use crate::Project;

#[test]
fn test_write_project_all_combinations() {
//let db_types = [DbType::Sqlite, DbType::Mysql, DbType::Postgres, DbType::Mssql];
Expand All @@ -19,7 +18,6 @@ mod tests {
DbLib::Diesel,
DbLib::Rbatis,
DbLib::Mongodb,
DbLib::Nothing,
];

// Generate all combinations
Expand All @@ -30,22 +28,20 @@ mod tests {

// Test each combination
for (db_type, db_lib) in combinations {
// Generate a unique project name for each combination
let project_name = format!("test_{:?}_{:?}", db_type, db_lib);
println!("Testing combination: {:?}", project_name);
let path_str = format!("target/{}", project_name);
let proj = Project {
name: format!("test_{:?}_{:?}", db_type, db_lib),
lang: "zh".to_string(),
};
println!("Testing combination: {:?}", proj.name);
let path_str = format!("target/{}", proj.name);
std::fs::remove_dir_all(&path_str).unwrap_or(());
let path = Path::new(&path_str);

let user_selected = Selected {
db_type: *db_type,
db_lib: *db_lib,
};
let project = Project {
project_name: project_name.clone(),
lang: Some("zh".to_string()),
};
match write_project_file(path, user_selected, project) {
match classic::create_files(path, user_selected, &proj) {
Ok(()) => {
let output = std::process::Command::new("cargo")
.arg("check")
Expand All @@ -54,17 +50,17 @@ mod tests {
.expect("failed to execute process");
if !output.status.success() {
eprintln!(
"Failed on combination: code_style={:?}, db_type={:?}, db_lib={:?}",
code_style, db_type, db_lib
"Failed on combination: db_type={:?}, db_lib={:?}",
db_type, db_lib
);
eprintln!("Output: {:?}", output);
panic!();
}
}
Err(e) => {
eprintln!(
"Failed to write project file on combination: code_style={:?}, db_type={:?}, db_lib={:?}",
code_style, db_type, db_lib
"Failed to write project file on combination: db_type={:?}, db_lib={:?}",
db_type, db_lib
);
eprintln!("Error: {:?}", e);
panic!();
Expand Down

0 comments on commit b0aa09f

Please sign in to comment.