|
| 1 | +use assert_cmd::Command; |
| 2 | +use std::process::ExitStatus; |
| 3 | + |
| 4 | +const BIN: &str = "postgres-language-server"; |
| 5 | + |
| 6 | +/// Get database URL from environment or use default docker-compose URL |
| 7 | +fn get_database_url() -> Option<String> { |
| 8 | + std::env::var("DATABASE_URL") |
| 9 | + .ok() |
| 10 | + .or_else(|| Some("postgres://postgres:postgres@127.0.0.1:5432/postgres".to_string())) |
| 11 | +} |
| 12 | + |
| 13 | +/// Execute SQL against the database |
| 14 | +fn execute_sql(sql: &str) -> bool { |
| 15 | + let Some(url) = get_database_url() else { |
| 16 | + return false; |
| 17 | + }; |
| 18 | + |
| 19 | + std::process::Command::new("psql") |
| 20 | + .arg(&url) |
| 21 | + .arg("-c") |
| 22 | + .arg(sql) |
| 23 | + .output() |
| 24 | + .map(|o| o.status.success()) |
| 25 | + .unwrap_or(false) |
| 26 | +} |
| 27 | + |
| 28 | +/// Setup test schema with known issues for splinter to detect |
| 29 | +fn setup_test_schema() { |
| 30 | + // Create a table without a primary key (triggers no_primary_key rule) |
| 31 | + execute_sql("DROP TABLE IF EXISTS dblint_test_no_pk CASCADE"); |
| 32 | + execute_sql("CREATE TABLE dblint_test_no_pk (id int, name text)"); |
| 33 | +} |
| 34 | + |
| 35 | +/// Cleanup test schema |
| 36 | +fn cleanup_test_schema() { |
| 37 | + execute_sql("DROP TABLE IF EXISTS dblint_test_no_pk CASCADE"); |
| 38 | +} |
| 39 | + |
| 40 | +#[test] |
| 41 | +#[cfg_attr( |
| 42 | + target_os = "windows", |
| 43 | + ignore = "snapshot expectations only validated on unix-like platforms" |
| 44 | +)] |
| 45 | +fn dblint_runs_without_errors() { |
| 46 | + let output = run_dblint(&[]); |
| 47 | + assert!( |
| 48 | + output.contains("Command completed"), |
| 49 | + "Expected successful completion, got: {output}", |
| 50 | + ); |
| 51 | +} |
| 52 | + |
| 53 | +#[test] |
| 54 | +#[cfg_attr( |
| 55 | + target_os = "windows", |
| 56 | + ignore = "snapshot expectations only validated on unix-like platforms" |
| 57 | +)] |
| 58 | +fn dblint_detects_no_primary_key() { |
| 59 | + // Setup: create table without primary key |
| 60 | + setup_test_schema(); |
| 61 | + |
| 62 | + // Run dblint |
| 63 | + let output = run_dblint(&[]); |
| 64 | + |
| 65 | + // Cleanup |
| 66 | + cleanup_test_schema(); |
| 67 | + |
| 68 | + // Should detect the no_primary_key issue |
| 69 | + assert!( |
| 70 | + output.contains("noPrimaryKey") || output.contains("primary key"), |
| 71 | + "Expected to detect missing primary key issue, got: {output}", |
| 72 | + ); |
| 73 | +} |
| 74 | + |
| 75 | +#[test] |
| 76 | +#[cfg_attr( |
| 77 | + target_os = "windows", |
| 78 | + ignore = "snapshot expectations only validated on unix-like platforms" |
| 79 | +)] |
| 80 | +fn dblint_fails_without_database() { |
| 81 | + // Test that dblint fails gracefully when no database is configured |
| 82 | + let mut cmd = Command::cargo_bin(BIN).expect("binary not built"); |
| 83 | + let output = cmd |
| 84 | + .args(["dblint", "--disable-db", "--log-level", "none"]) |
| 85 | + .output() |
| 86 | + .expect("failed to run CLI"); |
| 87 | + |
| 88 | + let stdout = String::from_utf8_lossy(&output.stdout); |
| 89 | + let stderr = String::from_utf8_lossy(&output.stderr); |
| 90 | + |
| 91 | + // Should complete (possibly with warning about no database) |
| 92 | + assert!( |
| 93 | + output.status.success() |
| 94 | + || stderr.contains("database") |
| 95 | + || stdout.contains("Command completed"), |
| 96 | + "Expected graceful handling without database, got stdout: {stdout}, stderr: {stderr}", |
| 97 | + ); |
| 98 | +} |
| 99 | + |
| 100 | +fn run_dblint(args: &[&str]) -> String { |
| 101 | + let url = get_database_url().expect("database URL required"); |
| 102 | + |
| 103 | + let mut cmd = Command::cargo_bin(BIN).expect("binary not built"); |
| 104 | + let mut full_args = vec!["dblint", "--connection-string", &url, "--log-level", "none"]; |
| 105 | + full_args.extend_from_slice(args); |
| 106 | + |
| 107 | + let output = cmd.args(full_args).output().expect("failed to run CLI"); |
| 108 | + |
| 109 | + normalize_output( |
| 110 | + output.status, |
| 111 | + &String::from_utf8_lossy(&output.stdout), |
| 112 | + &String::from_utf8_lossy(&output.stderr), |
| 113 | + ) |
| 114 | +} |
| 115 | + |
| 116 | +fn normalize_output(status: ExitStatus, stdout: &str, stderr: &str) -> String { |
| 117 | + let normalized_stdout = normalize_durations(stdout); |
| 118 | + let status_label = if status.success() { |
| 119 | + "success" |
| 120 | + } else { |
| 121 | + "failure" |
| 122 | + }; |
| 123 | + format!( |
| 124 | + "status: {status_label}\nstdout:\n{}\nstderr:\n{}\n", |
| 125 | + normalized_stdout.trim_end(), |
| 126 | + stderr.trim_end() |
| 127 | + ) |
| 128 | +} |
| 129 | + |
| 130 | +fn normalize_durations(input: &str) -> String { |
| 131 | + let mut content = input.to_owned(); |
| 132 | + |
| 133 | + let mut search_start = 0; |
| 134 | + while let Some(relative) = content[search_start..].find(" in ") { |
| 135 | + let start = search_start + relative + 4; |
| 136 | + if let Some(end_rel) = content[start..].find('.') { |
| 137 | + let end = start + end_rel; |
| 138 | + if content[start..end].chars().any(|c| c.is_ascii_digit()) { |
| 139 | + content.replace_range(start..end, "<duration>"); |
| 140 | + search_start = start + "<duration>".len() + 1; |
| 141 | + continue; |
| 142 | + } |
| 143 | + search_start = end + 1; |
| 144 | + } else { |
| 145 | + break; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + content |
| 150 | +} |
0 commit comments