Skip to content

Commit 9c3a78f

Browse files
committed
Add tests
1 parent 53a72ac commit 9c3a78f

File tree

2 files changed

+104
-4
lines changed

2 files changed

+104
-4
lines changed

crates/prek/src/languages/script.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ impl ShellSpec {
7474

7575
#[cfg(not(windows))]
7676
fn resolve_default_shell_spec() -> Result<ShellSpec> {
77-
let tried = "bash, sh";
7877
if let Ok(path) = which::which("bash") {
7978
return Ok(ShellSpec {
8079
program: path.to_string_lossy().to_string(),
@@ -89,12 +88,11 @@ fn resolve_default_shell_spec() -> Result<ShellSpec> {
8988
extension: "sh",
9089
});
9190
}
92-
anyhow::bail!("No suitable default shell found (tried {tried})")
91+
anyhow::bail!("No suitable default shell found (tried bash, sh)")
9392
}
9493

9594
#[cfg(windows)]
9695
fn resolve_default_shell_spec() -> Result<ShellSpec> {
97-
let tried = "pwsh, powershell, cmd";
9896
// Prefer PowerShell 7+ if available.
9997
if let Ok(path) = which::which("pwsh") {
10098
return Ok(ShellSpec {
@@ -131,7 +129,7 @@ fn resolve_default_shell_spec() -> Result<ShellSpec> {
131129
});
132130
}
133131

134-
anyhow::bail!("No suitable default shell found (tried {tried})")
132+
anyhow::bail!("No suitable default shell found (tried pwsh, powershell, cmd)")
135133
}
136134

137135
fn extension_for_interpreter(interpreter: &str) -> &'static str {

crates/prek/tests/languages/script.rs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,108 @@ mod unix {
250250
Ok(())
251251
}
252252

253+
#[test]
254+
fn inline_script_with_sh_shebang() {
255+
let context = TestContext::new();
256+
context.init_project();
257+
context.write_pre_commit_config(indoc::indoc! {r#"
258+
repos:
259+
- repo: local
260+
hooks:
261+
- id: inline
262+
name: inline
263+
language: script
264+
entry: |
265+
#!/usr/bin/env sh
266+
echo "hello from sh"
267+
pass_filenames: false
268+
verbose: true
269+
"#});
270+
context.git_add(".");
271+
272+
cmd_snapshot!(context.filters(), context.run(), @r"
273+
success: true
274+
exit_code: 0
275+
----- stdout -----
276+
inline...................................................................Passed
277+
- hook id: inline
278+
- duration: [TIME]
279+
280+
hello from sh
281+
282+
----- stderr -----
283+
");
284+
}
285+
286+
#[test]
287+
fn inline_script_no_shebang_crlf_line_endings() {
288+
let context = TestContext::new();
289+
context.init_project();
290+
291+
// Ensure CRLF is accepted (common on Windows checkouts) even on unix.
292+
let config = indoc::indoc! {r#"
293+
repos:
294+
- repo: local
295+
hooks:
296+
- id: inline
297+
name: inline
298+
language: script
299+
entry: "echo one\r\necho two\r\n"
300+
pass_filenames: false
301+
verbose: true
302+
"#}
303+
.replace('\n', "\r\n");
304+
305+
context.write_pre_commit_config(&config);
306+
context.git_add(".");
307+
308+
cmd_snapshot!(context.filters(), context.run(), @r"
309+
success: true
310+
exit_code: 0
311+
----- stdout -----
312+
inline...................................................................Passed
313+
- hook id: inline
314+
- duration: [TIME]
315+
316+
one
317+
two
318+
319+
----- stderr -----
320+
");
321+
}
322+
323+
#[test]
324+
fn inline_script_no_shebang_stops_on_error() {
325+
let context = TestContext::new();
326+
context.init_project();
327+
context.write_pre_commit_config(indoc::indoc! {r#"
328+
repos:
329+
- repo: local
330+
hooks:
331+
- id: inline
332+
name: inline
333+
language: script
334+
entry: |
335+
false
336+
echo "should not print"
337+
pass_filenames: false
338+
verbose: true
339+
"#});
340+
context.git_add(".");
341+
342+
cmd_snapshot!(context.filters(), context.run(), @r"
343+
success: false
344+
exit_code: 1
345+
----- stdout -----
346+
inline...................................................................Failed
347+
- hook id: inline
348+
- duration: [TIME]
349+
- exit code: 1
350+
351+
----- stderr -----
352+
");
353+
}
354+
253355
#[test]
254356
fn multiline_entry_still_script_path() -> Result<()> {
255357
let context = TestContext::new();

0 commit comments

Comments
 (0)