diff --git a/src/common/config.rs b/src/common/config.rs index a9a91148..133c9cd8 100644 --- a/src/common/config.rs +++ b/src/common/config.rs @@ -327,10 +327,7 @@ impl Config { pub fn get_log_level(file_config_path: &PathBuf) -> LogLevel { let file_based_config = fs::read_to_string(file_config_path); let file_based_config = &file_based_config.map(|f| serde_json::from_str::(f.as_str()).unwrap()); - let log_level_from_file = file_based_config - .as_ref() - .ok() - .and_then(|config| config.log_level); + let log_level_from_file = file_based_config.as_ref().ok().and_then(|config| config.log_level); CLI_ARGS.log_level.or(log_level_from_file).unwrap_or(LogLevel::Info) } diff --git a/src/common/logger.rs b/src/common/logger.rs index ceb30f38..fad7f8e8 100644 --- a/src/common/logger.rs +++ b/src/common/logger.rs @@ -74,4 +74,3 @@ macro_rules! error { } }); } - diff --git a/src/main.rs b/src/main.rs index f6fc7b0f..9bd7fafc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -44,8 +44,8 @@ async fn main() -> Result<()> { let ext = &CLI_ARGS.ext; info!("Scanning {:?} for SQLs with extension {:?}", source_folder, ext); - - // If CLI_ARGS.generate_types is true, it will clear the single TS file so `execute` will generate a new one from scratch + + // If CLI_ARGS.generate_types is true, it will clear the single TS file so `execute` will generate a new one from scratch clear_single_ts_file_if_exists()?; let files = scan_folder(source_folder, ext); diff --git a/src/parser/tag.rs b/src/parser/tag.rs index 8fe80b66..3f49edb1 100644 --- a/src/parser/tag.rs +++ b/src/parser/tag.rs @@ -1,8 +1,6 @@ use crate::common::SQL; use swc_common::MultiSpan; -use swc_ecma_ast::{ - BlockStmt, ClassMember, Expr, OptChainBase, Pat, Prop, PropOrSpread, SuperProp, VarDeclarator, -}; +use swc_ecma_ast::{BlockStmt, ClassMember, Expr, OptChainBase, Pat, Prop, PropOrSpread, SuperProp, VarDeclarator}; use super::{get_var_decl_name_from_key, recurse_and_find_sql}; diff --git a/src/ts_generator/generator.rs b/src/ts_generator/generator.rs index 074bd550..fe13c312 100644 --- a/src/ts_generator/generator.rs +++ b/src/ts_generator/generator.rs @@ -83,7 +83,6 @@ pub fn write_single_ts_file(sqls_to_write: String) -> Result<()> { let mut file_to_write = OpenOptions::new() .create(true) .read(true) - .append(true) .open(&output)?; diff --git a/test-utils/src/sandbox.rs b/test-utils/src/sandbox.rs index d537b911..cd55860e 100644 --- a/test-utils/src/sandbox.rs +++ b/test-utils/src/sandbox.rs @@ -1,3 +1,5 @@ +use std::path::PathBuf; + use serde; use serde::{Deserialize, Serialize}; @@ -21,12 +23,14 @@ pub struct TestConfig { pub db_user: String, pub db_pass: Option, pub db_name: String, + pub generate_path: Option, pub generate_types: bool, pub config_file_name: Option, } impl TestConfig { - pub fn new(db_type: &str, generate_types: bool, config_file_name: Option) -> Self { + pub fn new(db_type: &str, generate_types: bool, generate_path: Option, config_file_name: Option) -> Self { + let generate_path = generate_path.clone(); if db_type == "mysql" { return TestConfig { db_type: "mysql".into(), @@ -36,6 +40,7 @@ impl TestConfig { db_user: "root".to_string(), db_pass: None, db_name: "sqlx-ts".to_string(), + generate_path, generate_types, config_file_name, } @@ -48,6 +53,7 @@ impl TestConfig { db_user: "postgres".to_string(), db_pass: Some("postgres".to_string()), db_name: "postgres".to_string(), + generate_path, generate_types, config_file_name, } @@ -107,6 +113,7 @@ $( let db_pass = test_config.db_pass; let db_name = test_config.db_name; let config_file_name = test_config.config_file_name; + let generate_path = test_config.generate_path; // SETUP let dir = tempdir()?; @@ -128,7 +135,15 @@ $( .arg(format!("--db-user={db_user}")) .arg(format!("--db-name={db_name}")); - println!("checking generate types {:?}", test_config.generate_types); + if &generate_path.is_some() == &true { + let generate_path = generate_path.clone(); + let generate_path = generate_path.unwrap(); + let generate_path = generate_path.as_path(); + let generate_path = parent_path.join(generate_path); + let generate_path = generate_path.display(); + cmd.arg(format!("--generate-path={generate_path}")); + } + if (test_config.generate_types) { cmd.arg("-g"); } @@ -152,6 +167,18 @@ $( let generated_types: &str = $generated_types.clone(); + if generate_path.is_some() { + let generate_path = parent_path.join(generate_path.unwrap().as_path()); + let type_file = fs::read_to_string(generate_path); + let type_file = type_file.unwrap(); + + assert_eq!( + generated_types.trim().to_string().flatten(), + type_file.trim().to_string().flatten() + ); + return Ok(()); + } + let type_file = fs::read_to_string(parent_path.join("index.queries.ts")); if type_file.is_ok() { let type_file = type_file.unwrap().clone(); diff --git a/tests/covert_to_camelcase.rs b/tests/covert_to_camelcase.rs index 92a8562c..29b07798 100644 --- a/tests/covert_to_camelcase.rs +++ b/tests/covert_to_camelcase.rs @@ -1,7 +1,7 @@ /// Test suites for converting any case to camelCase if generateTypes.convertToCamelCase is true /// #[cfg(test)] -mod string_functions_tests { +mod convert_camelcase_tests { use assert_cmd::prelude::*; use predicates::prelude::*; use std::env; @@ -15,7 +15,7 @@ mod string_functions_tests { use test_utils::{run_test, sandbox::TestConfig}; #[rustfmt::skip] -run_test!(retain_original, TestConfig::new("postgres", true, Some(".sqlxrc.camelcase1.json".to_string())), +run_test!(retain_original, TestConfig::new("postgres", true, None, Some(".sqlxrc.camelcase1.json".to_string())), //// TS query //// r#" const someQuery = sql` @@ -45,7 +45,7 @@ export interface ISomeQueryQuery { ); #[rustfmt::skip] -run_test!(convert_camelcase, TestConfig::new("postgres", true, Some(".sqlxrc.camelcase2.json".to_string())), +run_test!(convert_camelcase, TestConfig::new("postgres", true, None, Some(".sqlxrc.camelcase2.json".to_string())), //// TS query //// r#" @@ -76,7 +76,7 @@ export interface ISomeQueryQuery { ); #[rustfmt::skip] -run_test!(retain_original_on_missing_config, TestConfig::new("postgres", true, Some(".sqlxrc.camelcase3.json".to_string())), +run_test!(retain_original_on_missing_config, TestConfig::new("postgres", true, None, Some(".sqlxrc.camelcase3.json".to_string())), //// TS query //// r#" diff --git a/tests/generate_path.rs b/tests/generate_path.rs new file mode 100644 index 00000000..862c921c --- /dev/null +++ b/tests/generate_path.rs @@ -0,0 +1,48 @@ +/// Test suites for converting any case to camelCase if generateTypes.convertToCamelCase is true +/// +#[cfg(test)] +mod generate_path_tests { + use assert_cmd::prelude::*; + use predicates::prelude::*; + use std::env; + use std::fs; + use std::io::Write; + use std::process::Command; + use std::path::PathBuf; + use tempfile::tempdir; + + use pretty_assertions::assert_eq; + use test_utils::test_utils::TSString; + use test_utils::{run_test, sandbox::TestConfig}; + + #[rustfmt::skip] +run_test!(should_generate_path, TestConfig::new("postgres", true, Some(PathBuf::from("types/types.ts")), Some(".sqlxrc.camelcase1.json".to_string())), +//// TS query //// +r#" +const someQuery = sql` +SELECT + food_type, + id AS HelloWorld, + id AS hello_world +FROM items; +` +"#, + +//// Generated TS interfaces //// +r#" +export type SomeQueryParams = []; + +export interface ISomeQueryResult { + HelloWorld: number; + food_type: string; + hello_world: number; +}; + +export interface ISomeQueryQuery { + params: SomeQueryParams; + result: ISomeQueryResult; +}; +"# +); + +} diff --git a/tests/mysql_delete_query_parameters.rs b/tests/mysql_delete_query_parameters.rs index ee38b6f3..cc70609e 100644 --- a/tests/mysql_delete_query_parameters.rs +++ b/tests/mysql_delete_query_parameters.rs @@ -13,7 +13,7 @@ mod mysql_delete_query_parameters { use test_utils::{run_test, sandbox::TestConfig}; #[rustfmt::skip] -run_test!(should_pick_query_params_from_binary_ops, TestConfig::new("mysql", true, None), +run_test!(should_pick_query_params_from_binary_ops, TestConfig::new("mysql", true, None, None), //// TS query //// r#" @@ -40,7 +40,7 @@ export interface ISomeDeleteQueryQuery { "#); #[rustfmt::skip] -run_test!(should_pick_query_params_from_subquery, TestConfig::new("mysql", true, None), +run_test!(should_pick_query_params_from_subquery, TestConfig::new("mysql", true, None, None), //// TS query //// r#" diff --git a/tests/mysql_insert_query_parameters.rs b/tests/mysql_insert_query_parameters.rs index 96994ddc..2c800680 100644 --- a/tests/mysql_insert_query_parameters.rs +++ b/tests/mysql_insert_query_parameters.rs @@ -13,7 +13,7 @@ mod mysql_insert_query_parameters { use test_utils::{run_test, sandbox::TestConfig}; #[rustfmt::skip] -run_test!(should_pick_query_params_from_single_row_of_values, TestConfig::new("mysql", true, None), +run_test!(should_pick_query_params_from_single_row_of_values, TestConfig::new("mysql", true, None, None), //// TS query //// r#" @@ -39,7 +39,7 @@ export interface ISomeInputQueryQuery { "#); #[rustfmt::skip] -run_test!(should_pick_query_params_from_multiple_rows_of_values, TestConfig::new("mysql", true, None), +run_test!(should_pick_query_params_from_multiple_rows_of_values, TestConfig::new("mysql", true, None, None), //// TS query //// r#" diff --git a/tests/mysql_query_parameters.rs b/tests/mysql_query_parameters.rs index 28a60c15..c0632d61 100644 --- a/tests/mysql_query_parameters.rs +++ b/tests/mysql_query_parameters.rs @@ -13,7 +13,7 @@ mod mysql_query_parameters_tests { use test_utils::{run_test, sandbox::TestConfig}; #[rustfmt::skip] -run_test!(should_pick_query_params_from_flat_list_of_binary_ops, TestConfig::new("mysql", true, None), +run_test!(should_pick_query_params_from_flat_list_of_binary_ops, TestConfig::new("mysql", true, None, None), //// TS query //// r#" @@ -45,7 +45,7 @@ export interface ISomeQueryQuery { "#); #[rustfmt::skip] -run_test!(should_pick_query_params_from_in_list, TestConfig::new("mysql", true, None), +run_test!(should_pick_query_params_from_in_list, TestConfig::new("mysql", true, None, None), //// TS query //// r#" @@ -75,7 +75,7 @@ export interface ISomeQueryQuery { "#); #[rustfmt::skip] -run_test!(should_pick_query_params_from_in_subqueries, TestConfig::new("mysql", true, None), +run_test!(should_pick_query_params_from_in_subqueries, TestConfig::new("mysql", true, None, None), //// TS query //// r#" @@ -114,7 +114,7 @@ export interface ISomeQueryQuery { "#); #[rustfmt::skip] -run_test!(should_pick_query_params_from_subqueries, TestConfig::new("mysql", true, None), +run_test!(should_pick_query_params_from_subqueries, TestConfig::new("mysql", true, None, None), //// TS query //// r#" diff --git a/tests/mysql_update_query_parameters.rs b/tests/mysql_update_query_parameters.rs index 20d3c6eb..14ea5fea 100644 --- a/tests/mysql_update_query_parameters.rs +++ b/tests/mysql_update_query_parameters.rs @@ -13,7 +13,7 @@ mod mysql_update_query_parameters { use test_utils::{run_test, sandbox::TestConfig}; #[rustfmt::skip] -run_test!(should_pick_query_params_from_single_row_of_values, TestConfig::new("mysql", true, None), +run_test!(should_pick_query_params_from_single_row_of_values, TestConfig::new("mysql", true, None, None), //// TS query //// r#" @@ -40,7 +40,7 @@ export interface ISomeUpdateQueryQuery { "#); #[rustfmt::skip] -run_test!(should_pick_query_params_from_multiple_rows_of_values, TestConfig::new("mysql", true, None), +run_test!(should_pick_query_params_from_multiple_rows_of_values, TestConfig::new("mysql", true, None, None), //// TS query //// r#" diff --git a/tests/postgres_delete_query_parameters.rs b/tests/postgres_delete_query_parameters.rs index f95add6b..62068004 100644 --- a/tests/postgres_delete_query_parameters.rs +++ b/tests/postgres_delete_query_parameters.rs @@ -13,7 +13,7 @@ mod mysql_delete_query_parameters { use test_utils::{run_test, sandbox::TestConfig}; #[rustfmt::skip] -run_test!(should_pick_query_params_from_binary_ops, TestConfig::new("postgres", true, None), +run_test!(should_pick_query_params_from_binary_ops, TestConfig::new("postgres", true, None, None), //// TS query //// r#" @@ -40,7 +40,7 @@ export interface ISomeDeleteQueryQuery { "#); #[rustfmt::skip] -run_test!(should_pick_query_params_from_subquery, TestConfig::new("postgres", true, None), +run_test!(should_pick_query_params_from_subquery, TestConfig::new("postgres", true, None, None), //// TS query //// r#" diff --git a/tests/postgres_insert_query_parameters.rs b/tests/postgres_insert_query_parameters.rs index 9c8545d3..052b8406 100644 --- a/tests/postgres_insert_query_parameters.rs +++ b/tests/postgres_insert_query_parameters.rs @@ -13,7 +13,7 @@ mod postgres_insert_query_parameters { use test_utils::{run_test, sandbox::TestConfig}; #[rustfmt::skip] -run_test!(should_pick_query_params_from_single_row_of_values, TestConfig::new("postgres", true, None), +run_test!(should_pick_query_params_from_single_row_of_values, TestConfig::new("postgres", true, None, None), //// TS query //// r#" @@ -39,7 +39,7 @@ export interface ISomeInputQueryQuery { "#); #[rustfmt::skip] -run_test!(should_pick_query_params_from_multiple_rows_of_values, TestConfig::new("postgres", true, None), +run_test!(should_pick_query_params_from_multiple_rows_of_values, TestConfig::new("postgres", true, None, None), //// TS query //// r#" diff --git a/tests/postgres_query_parameters.rs b/tests/postgres_query_parameters.rs index f0898717..094e2280 100644 --- a/tests/postgres_query_parameters.rs +++ b/tests/postgres_query_parameters.rs @@ -13,7 +13,7 @@ mod postgres_query_parameters_tests { use test_utils::{run_test, sandbox::TestConfig}; #[rustfmt::skip] -run_test!(should_pick_query_params_from_flat_list_of_binary_ops, TestConfig::new("postgres", true, None), +run_test!(should_pick_query_params_from_flat_list_of_binary_ops, TestConfig::new("postgres", true, None, None), //// TS query //// r#" diff --git a/tests/postgres_update_query_parameters.rs b/tests/postgres_update_query_parameters.rs index 02e183b7..a84c8ac8 100644 --- a/tests/postgres_update_query_parameters.rs +++ b/tests/postgres_update_query_parameters.rs @@ -13,7 +13,7 @@ mod postgres_update_query_parameters { use test_utils::{run_test, sandbox::TestConfig}; #[rustfmt::skip] -run_test!(should_pick_query_params_from_single_row_of_values, TestConfig::new("postgres", true, None), +run_test!(should_pick_query_params_from_single_row_of_values, TestConfig::new("postgres", true, None, None), //// TS query //// r#" diff --git a/tests/sqlxrc_file.rs b/tests/sqlxrc_file.rs index 7e816998..636ec05f 100644 --- a/tests/sqlxrc_file.rs +++ b/tests/sqlxrc_file.rs @@ -1,7 +1,7 @@ /// Test suites for converting any case to camelCase if generateTypes.convertToCamelCase is true /// #[cfg(test)] -mod string_functions_tests { +mod sqlxrc_file { use assert_cmd::prelude::*; use predicates::prelude::*; use std::env; @@ -15,7 +15,7 @@ mod string_functions_tests { use test_utils::{run_test, sandbox::TestConfig}; #[rustfmt::skip] -run_test!(not_enabled, TestConfig::new("postgres", false, Some(".sqlxrc.not_enabled.json".to_string())), +run_test!(not_enabled, TestConfig::new("postgres", false, None, Some(".sqlxrc.not_enabled.json".to_string())), //// TS query //// r#" const someQuery = sql` @@ -32,7 +32,7 @@ FROM items; ); #[rustfmt::skip] -run_test!(not_enabled_but_enabled_cli, TestConfig::new("postgres", true, Some(".sqlxrc.not_enabled.json".to_string())), +run_test!(not_enabled_but_enabled_cli, TestConfig::new("postgres", true, None, Some(".sqlxrc.not_enabled.json".to_string())), //// TS query //// r#" const someQuery = sql` @@ -62,7 +62,7 @@ export interface ISomeQueryQuery { ); #[rustfmt::skip] -run_test!(enabled_and_enabled_cli, TestConfig::new("postgres", true, Some(".sqlxrc.enabled.json".to_string())), +run_test!(enabled_and_enabled_cli, TestConfig::new("postgres", true, None, Some(".sqlxrc.enabled.json".to_string())), //// TS query //// r#" const someQuery = sql` @@ -92,7 +92,7 @@ export interface ISomeQueryQuery { ); #[rustfmt::skip] -run_test!(enabled_but_not_enabled_cli, TestConfig::new("postgres", false, Some(".sqlxrc.enabled.json".to_string())), +run_test!(enabled_but_not_enabled_cli, TestConfig::new("postgres", false, None, Some(".sqlxrc.enabled.json".to_string())), //// TS query //// r#" const someQuery = sql` diff --git a/tests/string_functions.rs b/tests/string_functions.rs index 7237091f..2fc60bff 100644 --- a/tests/string_functions.rs +++ b/tests/string_functions.rs @@ -13,7 +13,7 @@ mod string_functions_tests { use test_utils::{run_test, sandbox::TestConfig}; #[rustfmt::skip] -run_test!(overlay, TestConfig::new("postgres", true, None), +run_test!(overlay, TestConfig::new("postgres", true, None, None), //// TS query //// r#" @@ -40,7 +40,7 @@ export interface ISomeQueryQuery { ); #[rustfmt::skip] -run_test!(trim, TestConfig::new("postgres", true, None), +run_test!(trim, TestConfig::new("postgres", true, None, None), //// TS query //// "const someQuery = sql`SELECT TRIM($1) AS test FROM items;`", @@ -61,7 +61,7 @@ export interface ISomeQueryQuery { ); #[rustfmt::skip] -run_test!(substring, TestConfig::new("postgres", true, None), +run_test!(substring, TestConfig::new("postgres", true, None, None), //// TS query ////" "const someQuery = sql`SELECT SUBSTRING($1, 5, 6) AS ExtractString FROM items;`", @@ -82,7 +82,7 @@ export interface ISomeQueryQuery { ); #[rustfmt::skip] -run_test!(like, TestConfig::new("postgres", true, None), +run_test!(like, TestConfig::new("postgres", true, None, None), //// TS query ////" "const someQuery = sql`SELECT id FROM items WHERE food_type LIKE $1;`",