Skip to content

Commit

Permalink
generate types CLI option priority test
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonShin committed Nov 4, 2023
1 parent a0cca14 commit 974a2ad
Showing 1 changed file with 55 additions and 12 deletions.
67 changes: 55 additions & 12 deletions tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ mod cli_test {
let parent_path = dir.path();
let config_file_path = parent_path.join(".sqlxrc.json");

if sample_query_path.exists() {
fs::remove_file(&sample_query_path)?;
}
if sample_query_path.exists() { fs::remove_file(&sample_query_path)?; }
let mut temp_file = fs::File::create(&config_file_path)?;
let config_content = r#"{}"#;
writeln!(temp_file, "{}", config_content)?;
Expand All @@ -35,9 +33,9 @@ mod cli_test {
.arg("--db-name=postgres")
.arg("-g");

cmd.assert().failure().stderr(predicates::str::contains(
"Empty or invalid JSON provided for file based configuration - config file:",
));
cmd.assert()
.failure()
.stderr(predicates::str::contains("Empty or invalid JSON provided for file based configuration - config file:"));

assert_eq!(sample_query_path.exists(), false);
Ok(())
Expand All @@ -52,9 +50,7 @@ mod cli_test {
let parent_path = dir.path();
let config_file_path = parent_path.join(".sqlxrc.json");

if sample_query_path.exists() {
fs::remove_file(&sample_query_path)?;
}
if sample_query_path.exists() { fs::remove_file(&sample_query_path)?; }
let mut temp_file = fs::File::create(&config_file_path)?;
let config_content = r#""#;
writeln!(temp_file, "{}", config_content)?;
Expand All @@ -72,9 +68,9 @@ mod cli_test {
.arg("--db-name=sqlx-ts")
.arg("-g");

cmd.assert().failure().stderr(predicates::str::contains(
"Empty or invalid JSON provided for file based configuration - config file:",
));
cmd.assert()
.failure()
.stderr(predicates::str::contains("Empty or invalid JSON provided for file based configuration - config file:"));

assert_eq!(sample_query_path.exists(), false);
Ok(())
Expand Down Expand Up @@ -170,4 +166,51 @@ mod cli_test {
assert_eq!(sample_query_path.exists(), true);
Ok(())
}

#[test]
fn generate_types_enabled_uses_the_cli_option() -> Result<(), Box<dyn std::error::Error>> {
// SETUP
let demo_path = current_dir().unwrap().join("tests/sample");
let sample_query_path = demo_path.join("sample.queries.ts");
let dir = tempdir()?;
let parent_path = dir.path();
let config_file_path = parent_path.join(".sqlxrc.json");

if sample_query_path.exists() {
fs::remove_file(&sample_query_path)?;
}
let mut temp_file = fs::File::create(&config_file_path)?;
let config_content = r#"
{
"generateTypes": {
"enabled": false
},
"connections": {
"default": {
"DB_TYPE": "postgres",
"DB_HOST": "127.0.0.1",
"DB_PORT": 54321,
"DB_USER": "postgres",
"DB_PASS": "postgres",
"DB_NAME": "postgres"
}
}
}"#;
writeln!(temp_file, "{}", config_content)?;

// EXECUTE
let mut cmd = Command::cargo_bin("sqlx-ts").unwrap();
cmd.arg(demo_path.to_str().unwrap())
.arg("--ext=ts")
.arg(format!("--config={}", config_file_path.to_str().unwrap()))
.arg("-g");

// ASSERT
cmd.assert()
.success()
.stdout(predicates::str::contains("No SQL errors detected!"));

assert_eq!(sample_query_path.exists(), true);
Ok(())
}
}

0 comments on commit 974a2ad

Please sign in to comment.