-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathexit_code_on_error.rs
More file actions
273 lines (228 loc) · 8.34 KB
/
exit_code_on_error.rs
File metadata and controls
273 lines (228 loc) · 8.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#[cfg(test)]
mod exit_code_tests {
use assert_cmd::prelude::*;
use std::fs;
use std::io::Write;
use std::process::Command;
use tempfile::tempdir;
/// This test verifies that even when there are multiple SQL queries with mixed
/// success/failure, the tool still exits with code 1
#[test]
fn should_exit_with_code_1_when_sql_errors_detected() -> Result<(), Box<dyn std::error::Error>> {
// SETUP
let dir = tempdir()?;
let parent_path = dir.path();
let file_path = parent_path.join("index.ts");
let index_content = r#"
import { sql } from "sqlx-ts";
// This should succeed
const validQuery = sql`SELECT id, name FROM characters WHERE id = $1;`;
// This should fail - unknown table
const invalidQuery = sql`SELECT * FROM unknown_table;`;
// Another valid query after the failure
const anotherValidQuery = sql`SELECT * FROM inventory WHERE character_id = $1;`;
"#;
let mut temp_file = fs::File::create(&file_path)?;
writeln!(temp_file, "{}", index_content)?;
// EXECUTE
let mut cmd = Command::cargo_bin("sqlx-ts").unwrap();
cmd
.arg(parent_path.to_str().unwrap())
.arg("--ext=ts")
.arg("--db-type=postgres")
.arg("--db-host=127.0.0.1")
.arg("--db-port=54321")
.arg("--db-user=postgres")
.arg("--db-pass=postgres")
.arg("--db-name=postgres");
// ASSERT - should exit with non-zero code due to the error
cmd
.assert()
.failure()
.stderr(predicates::str::contains("relation \"unknown_table\" does not exist"))
.stderr(predicates::str::contains("SQLs failed to compile!"));
Ok(())
}
/// Test that when all queries succeed, exit code is 0
#[test]
fn should_exit_with_code_0_when_no_errors() -> Result<(), Box<dyn std::error::Error>> {
// SETUP
let dir = tempdir()?;
let parent_path = dir.path();
let file_path = parent_path.join("valid.ts");
let index_content = r#"
import { sql } from "sqlx-ts";
const query1 = sql`SELECT id, name FROM characters WHERE id = $1;`;
const query2 = sql`SELECT * FROM inventory WHERE character_id = $1;`;
const query3 = sql`SELECT * FROM items WHERE id = $1;`;
"#;
let mut temp_file = fs::File::create(&file_path)?;
writeln!(temp_file, "{}", index_content)?;
// EXECUTE
let mut cmd = Command::cargo_bin("sqlx-ts").unwrap();
cmd
.arg(parent_path.to_str().unwrap())
.arg("--ext=ts")
.arg("--db-type=postgres")
.arg("--db-host=127.0.0.1")
.arg("--db-port=54321")
.arg("--db-user=postgres")
.arg("--db-pass=postgres")
.arg("--db-name=postgres");
// ASSERT - should succeed
cmd
.assert()
.success()
.stdout(predicates::str::contains("No SQL errors detected!"));
Ok(())
}
/// Test with many successes and one failure in the middle
/// Pattern: 5 successes -> 1 failure -> 5 successes
/// Expected: exit code 1
#[test]
fn should_fail_with_many_successes_and_one_failure_in_middle() -> Result<(), Box<dyn std::error::Error>> {
// SETUP
let dir = tempdir()?;
let parent_path = dir.path();
let file_path = parent_path.join("one_failure.ts");
let index_content = r#"
import { sql } from "sqlx-ts";
const query1 = sql`SELECT id FROM characters WHERE id = $1;`;
const query2 = sql`SELECT name FROM characters WHERE id = $1;`;
const query3 = sql`SELECT * FROM inventory WHERE id = $1;`;
const query4 = sql`SELECT * FROM items WHERE id = $1;`;
const query5 = sql`SELECT quantity FROM inventory WHERE id = $1;`;
// Single failure in the middle
const failedQuery = sql`SELECT * FROM this_table_does_not_exist;`;
const query6 = sql`SELECT rarity FROM items WHERE id = $1;`;
const query7 = sql`SELECT character_id FROM inventory WHERE id = $1;`;
const query8 = sql`SELECT flavor_text FROM items WHERE id = $1;`;
const query9 = sql`SELECT id, quantity FROM inventory WHERE character_id = $1;`;
const query10 = sql`SELECT id, name FROM characters LIMIT 10;`;
"#;
let mut temp_file = fs::File::create(&file_path)?;
writeln!(temp_file, "{}", index_content)?;
// EXECUTE
let mut cmd = Command::cargo_bin("sqlx-ts").unwrap();
cmd
.arg(parent_path.to_str().unwrap())
.arg("--ext=ts")
.arg("--db-type=postgres")
.arg("--db-host=127.0.0.1")
.arg("--db-port=54321")
.arg("--db-user=postgres")
.arg("--db-pass=postgres")
.arg("--db-name=postgres");
// ASSERT - should fail despite 10 successes and only 1 failure
cmd
.assert()
.failure()
.stderr(predicates::str::contains(
"relation \"this_table_does_not_exist\" does not exist",
))
.stderr(predicates::str::contains("SQLs failed to compile!"));
Ok(())
}
/// Test with multiple files: one success file and one failure file
/// Expected: exit code 1
#[test]
fn should_fail_with_multiple_files_one_success_one_failure() -> Result<(), Box<dyn std::error::Error>> {
// SETUP
let dir = tempdir()?;
let parent_path = dir.path();
// File 1: All successful queries
let file1_path = parent_path.join("success.ts");
let file1_content = r#"
import { sql } from "sqlx-ts";
const query1 = sql`SELECT id, name FROM characters WHERE id = $1;`;
const query2 = sql`SELECT * FROM inventory WHERE character_id = $1;`;
const query3 = sql`SELECT * FROM items WHERE id = $1;`;
"#;
let mut file1 = fs::File::create(&file1_path)?;
writeln!(file1, "{}", file1_content)?;
// File 2: Contains failures
let file2_path = parent_path.join("failure.ts");
let file2_content = r#"
import { sql } from "sqlx-ts";
const failQuery1 = sql`SELECT * FROM nonexistent_table;`;
const failQuery2 = sql`SELECT * FROM another_missing_table;`;
"#;
let mut file2 = fs::File::create(&file2_path)?;
writeln!(file2, "{}", file2_content)?;
// EXECUTE
let mut cmd = Command::cargo_bin("sqlx-ts").unwrap();
cmd
.arg(parent_path.to_str().unwrap())
.arg("--ext=ts")
.arg("--db-type=postgres")
.arg("--db-host=127.0.0.1")
.arg("--db-port=54321")
.arg("--db-user=postgres")
.arg("--db-pass=postgres")
.arg("--db-name=postgres");
// ASSERT - should fail because file2 has errors
cmd
.assert()
.failure()
.stderr(predicates::str::contains(
"relation \"nonexistent_table\" does not exist",
))
.stderr(predicates::str::contains(
"relation \"another_missing_table\" does not exist",
))
.stderr(predicates::str::contains("SQLs failed to compile!"));
Ok(())
}
/// Test with multiple files: all files contain successful queries
/// Expected: exit code 0
#[test]
fn should_succeed_with_multiple_files_all_successful() -> Result<(), Box<dyn std::error::Error>> {
// SETUP
let dir = tempdir()?;
let parent_path = dir.path();
// File 1: Successful queries
let file1_path = parent_path.join("queries1.ts");
let file1_content = r#"
import { sql } from "sqlx-ts";
const query1 = sql`SELECT id FROM characters WHERE id = $1;`;
const query2 = sql`SELECT name FROM characters WHERE id = $1;`;
"#;
let mut file1 = fs::File::create(&file1_path)?;
writeln!(file1, "{}", file1_content)?;
// File 2: More successful queries
let file2_path = parent_path.join("queries2.ts");
let file2_content = r#"
import { sql } from "sqlx-ts";
const query3 = sql`SELECT * FROM inventory WHERE id = $1;`;
const query4 = sql`SELECT * FROM items WHERE id = $1;`;
"#;
let mut file2 = fs::File::create(&file2_path)?;
writeln!(file2, "{}", file2_content)?;
// File 3: Even more successful queries
let file3_path = parent_path.join("queries3.ts");
let file3_content = r#"
import { sql } from "sqlx-ts";
const query5 = sql`SELECT quantity FROM inventory WHERE character_id = $1;`;
const query6 = sql`SELECT rarity FROM items WHERE id = $1;`;
"#;
let mut file3 = fs::File::create(&file3_path)?;
writeln!(file3, "{}", file3_content)?;
// EXECUTE
let mut cmd = Command::cargo_bin("sqlx-ts").unwrap();
cmd
.arg(parent_path.to_str().unwrap())
.arg("--ext=ts")
.arg("--db-type=postgres")
.arg("--db-host=127.0.0.1")
.arg("--db-port=54321")
.arg("--db-user=postgres")
.arg("--db-pass=postgres")
.arg("--db-name=postgres");
// ASSERT - should succeed
cmd
.assert()
.success()
.stdout(predicates::str::contains("No SQL errors detected!"));
Ok(())
}
}