Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/languages/python/pep723.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ use memchr::memmem::Finder;
use serde::Deserialize;
use tracing::trace;

use crate::fs::CWD;
use crate::hook::Hook;
use crate::languages::version::LanguageRequest;

Expand Down Expand Up @@ -260,8 +259,7 @@ pub(crate) async fn extract_pep723_metadata(hook: &mut Hook) -> Result<()> {
return Ok(());
}

// TODO: In workspace mode, local hooks should be relative to the project root.
let repo_path = hook.repo_path().unwrap_or_else(|| CWD.as_path());
let repo_path = hook.repo_path().unwrap_or(hook.work_dir());

let split = hook.entry.split()?;
let file = repo_path.join(&split[0]);
Expand Down
5 changes: 2 additions & 3 deletions src/languages/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::sync::Arc;
use anyhow::Result;

use crate::cli::reporter::HookInstallReporter;
use crate::fs::CWD;
use crate::hook::Hook;
use crate::hook::InstalledHook;
use crate::languages::{LanguageImpl, resolve_command};
Expand Down Expand Up @@ -38,8 +37,8 @@ impl LanguageImpl for Script {
// For `language: script`, the `entry[0]` is a script path.
// For remote hooks, the path is relative to the repo root.
// For local hooks, the path is relative to the current working directory.
// TODO: In workspace mode, local hooks should be relative to the project root.
let repo_path = hook.repo_path().unwrap_or_else(|| CWD.as_path());

let repo_path = hook.repo_path().unwrap_or(hook.work_dir());
let mut split = hook.entry.split()?;

let cmd = repo_path.join(&split[0]);
Expand Down
122 changes: 99 additions & 23 deletions tests/languages/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::common::{TestContext, cmd_snapshot};
mod unix {
use super::*;

use assert_fs::fixture::{FileWriteStr, PathChild};
use assert_fs::fixture::{FileWriteStr, PathChild, PathCreateDir};
use std::os::unix::fs::PermissionsExt;

#[test]
Expand Down Expand Up @@ -42,6 +42,82 @@ mod unix {
"##);
}

#[test]
fn workspace_script_run() -> Result<()> {
let context = TestContext::new();
context.init_project();

let config = indoc::indoc! {r"
repos:
- repo: local
hooks:
- id: script
name: script
language: script
entry: ./script.sh
verbose: true
"};
context.write_pre_commit_config(config);
context
.work_dir()
.child("script.sh")
.write_str(indoc::indoc! {r#"
#!/usr/bin/env bash
echo "Hello, World!"
"#})?;

let child = context.work_dir().child("child");
child.create_dir_all()?;
child.child(".pre-commit-config.yaml").write_str(config)?;
child.child("script.sh").write_str(indoc::indoc! {r#"
#!/usr/bin/env bash
echo "Hello, World from child!"
"#})?;

fs_err::set_permissions(
context.work_dir().child("script.sh"),
std::fs::Permissions::from_mode(0o755),
)?;
fs_err::set_permissions(
child.child("script.sh"),
std::fs::Permissions::from_mode(0o755),
)?;
context.git_add(".");

cmd_snapshot!(context.filters(), context.run(), @r"
success: true
exit_code: 0
----- stdout -----
Running hooks for `child`:
script...................................................................Passed
- hook id: script
- duration: [TIME]
Hello, World from child!

Running hooks for `.`:
script...................................................................Passed
- hook id: script
- duration: [TIME]
Hello, World!

----- stderr -----
");

cmd_snapshot!(context.filters(), context.run().current_dir(&child), @r"
success: true
exit_code: 0
----- stdout -----
script...................................................................Passed
- hook id: script
- duration: [TIME]
Hello, World from child!

----- stderr -----
");

Ok(())
}

#[test]
fn local_repo_bash_shebang() -> Result<()> {
let context = TestContext::new();
Expand Down Expand Up @@ -89,35 +165,35 @@ fn windows_script_run() -> Result<()> {
let context = TestContext::new();
context.init_project();
context.write_pre_commit_config(indoc::indoc! {r"
repos:
- repo: local
hooks:
- id: echo
name: echo
language: script
entry: ./echo.sh
verbose: true
"});
repos:
- repo: local
hooks:
- id: echo
name: echo
language: script
entry: ./echo.sh
verbose: true
"});

let script = context.work_dir().child("echo.sh");
script.write_str(indoc::indoc! {r#"
#!/usr/bin/env python3
print("Hello, World!")
"#})?;
#!/usr/bin/env python3
print("Hello, World!")
"#})?;

context.git_add(".");

cmd_snapshot!(context.filters(), context.run(), @r#"
success: true
exit_code: 0
----- stdout -----
echo.....................................................................Passed
- hook id: echo
- duration: [TIME]
Hello, World!

----- stderr -----
"#);
success: true
exit_code: 0
----- stdout -----
echo.....................................................................Passed
- hook id: echo
- duration: [TIME]
Hello, World!

----- stderr -----
"#);

Ok(())
}
Loading