Files
worktrunk/tests/integration_tests/e2e_shell_post_start.rs
ec62580c29 revert(hooks): keep docs on pre-start/post-start; code accepts both (#2857)
Per @max-sixty's [direction in
#2838](https://github.com/max-sixty/worktrunk/issues/2838#issuecomment-4509447593):
revert the docs portion of #2840 and keep the code. Docs continue to
recommend `pre-start`/`post-start`; both names work in code so anyone
who already followed the briefly-changed docs (e.g. @EcksDy) isn't
stranded once a release ships these aliases.

## User-visible — back to `pre-start`/`post-start`

- README, docs site, skill mirrors, `dev/*.example.toml`,
`plugins/worktrunk/README.md`, `flake.nix`, `.config/wt.toml`
- `src/cli/mod.rs` / `src/cli/config.rs` / `src/cli/step.rs` /
`src/help.rs` after_long_help and example snippets — and the auto-synced
`docs/content/` and `skills/worktrunk/reference/` mirrors
- `wt hook --help` canonical subcommand names; completion advertises
`-start` only
- `HookType` Display via strum, serde `rename`, and clap `ValueEnum`
name — all `pre-start`/`post-start`. The Rust variant identifiers stay
`PreCreate`/`PostCreate` (internal; we already paid for that rename in
#2840, and now the eventual flip is a Display-only change)
- `HooksConfig` serde canonical fields

## `*-create` still works (kept code)

- `wt hook pre-create` / `post-create` — CLI alias on the canonical
subcommand
- `pre-create` / `post-create` in config: top-level, `[hooks.*]`, and
per-project, in string, `[table]`, and `[[array-of-tables]]` form.
Mechanism: serde `alias = ...` on the field, plus a silent in-memory
rename in `migrate_content()` so the round-trip in `unknown_tree`
doesn't flag table forms as schema-unknown.
- The pre-0.32.0 `post-create` fatal-load-error machinery stays removed
— the name is reclaimed, and both forms load without error.

## Smaller bits

- `valid_user_config_keys()` / `valid_project_config_keys()` append
`pre-create` / `post-create` so the unknown-field round-trip skips them.
`test_valid_*_keys_all_deserialize` skips both aliases (they can't sit
alongside the canonical without a duplicate-field error).
- `DEPRECATED_SECTION_KEYS` drops the `pre-start`/`post-start` entries
#2840 added — `pre-start`/`post-start` are canonical again.
- `find_pre_start_from_doc` / `find_post_start_from_doc` /
`find_renamed_hook_key` / `is_non_empty_item` /
`migrate_start_hooks_doc` and their tests are removed; the migration
direction flips via a new `migrate_create_hooks_doc` (silent, mirrors
the prior shape).
- Test files `e2e_shell_post_create.rs` and `post_create_commands.rs`
rename back to `_post_start_` (via `git mv`, so the rename shows as a
rename).

## Testing

`cargo run -- hook pre-merge --yes` — 3806 tests pass; the 10 failures
are all `case_4` of `shell_wrapper::unix_tests::*` (nu-shell case; `nu`
isn't installed in this runner; same failures occur on `main`).

Also manually verified that a fresh `wt switch --create` against a
project config with `[post-create]` loads cleanly with no unknown-field
warning and the hook fires as `post-start`.

## Follow-up

Per @max-sixty: in a couple of weeks, once a release with
both-names-work is out and users have had a chance to upgrade, the docs
flip is straightforward (most of it is in `src/cli/mod.rs`'s
`after_long_help` and the doc-sync test propagates).

Re #2838.

Co-authored-by: worktrunk-bot <254187624+worktrunk-bot@users.noreply.github.com>
Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-21 16:03:03 +00:00

280 lines
7.8 KiB
Rust

//! Post-start command shell integration tests.
#![cfg(all(unix, feature = "shell-integration-tests"))]
use crate::common::{
TestRepo, repo, resolve_git_common_dir,
shell::{execute_shell_script, generate_init_code, path_export_syntax, wt_bin_dir},
wait_for_file, wait_for_file_content,
};
use rstest::rstest;
use std::fs;
#[rstest]
// Test with bash and fish
#[case("bash")]
#[case("fish")]
fn test_shell_integration_post_start_background(#[case] shell: &str, repo: TestRepo) {
// Create project config with background command
let config_dir = repo.root_path().join(".config");
fs::create_dir_all(&config_dir).unwrap();
fs::write(
config_dir.join("wt.toml"),
r#"post-start = "sleep 0.05 && echo 'Background task done' > bg_marker.txt""#,
)
.unwrap();
repo.commit("Add post-start config");
// Pre-approve the command
repo.write_test_approvals(
r#"[projects."../origin"]
approved-commands = ["sleep 0.05 && echo 'Background task done' > bg_marker.txt"]
"#,
);
let init_code = generate_init_code(&repo, shell);
let bin_path = wt_bin_dir();
let script = format!(
r#"
{}
{}
wt switch --create bg-feature
echo "Switched to worktree"
pwd
"#,
path_export_syntax(shell, &bin_path),
init_code
);
let output = execute_shell_script(&repo, shell, &script);
// Verify that:
// 1. The switch command completed (shell returned)
// 2. We're in the new worktree
assert!(
output.contains("Switched to worktree") && output.contains("bg-feature"),
"Expected to see switch completion and be in bg-feature worktree, got: {}",
output
);
// Verify background command actually ran
let worktree_path = repo.root_path().parent().unwrap().join("repo.bg-feature");
// First check if log file was created (proves process was spawned)
// Logs are centralized in the common git directory
let git_common_dir = resolve_git_common_dir(&worktree_path);
let log_dir = git_common_dir.join("wt/logs");
assert!(
log_dir.exists(),
"Log directory should exist at {}",
log_dir.display()
);
// Check for log files
let log_files: Vec<_> = fs::read_dir(&log_dir)
.unwrap()
.filter_map(|e| e.ok())
.map(|e| e.file_name().to_string_lossy().to_string())
.collect();
assert!(
!log_files.is_empty(),
"Should have log files in {}, found: {:?}",
log_dir.display(),
log_files
);
// Wait for background command to complete AND flush content (allow plenty of margin on CI)
let marker_file = worktree_path.join("bg_marker.txt");
wait_for_file_content(marker_file.as_path());
let content = fs::read_to_string(&marker_file).unwrap();
assert!(
content.contains("Background task done"),
"Expected background task output, got: {}",
content
);
}
#[rstest]
fn test_bash_shell_integration_post_start_parallel(repo: TestRepo) {
// Create project config with multiple background commands
let config_dir = repo.root_path().join(".config");
fs::create_dir_all(&config_dir).unwrap();
fs::write(
config_dir.join("wt.toml"),
r#"[post-start]
task1 = "sleep 0.05 && echo 'Task 1' > task1.txt"
task2 = "sleep 0.05 && echo 'Task 2' > task2.txt"
"#,
)
.unwrap();
repo.commit("Add multiple post-start commands");
// Pre-approve commands
repo.write_test_approvals(
r#"[projects."../origin"]
approved-commands = [
"sleep 0.05 && echo 'Task 1' > task1.txt",
"sleep 0.05 && echo 'Task 2' > task2.txt",
]
"#,
);
let init_code = generate_init_code(&repo, "bash");
let bin_path = wt_bin_dir();
let script = format!(
r#"
export PATH="{}:$PATH"
{}
wt switch --create parallel-test
echo "Returned from wt"
"#,
bin_path, init_code
);
let output = execute_shell_script(&repo, "bash", &script);
// Verify shell returned immediately (didn't wait for background tasks)
assert!(
output.contains("Returned from wt"),
"Expected immediate return from wt, got: {}",
output
);
// Wait for background commands to complete
let worktree_path = repo
.root_path()
.parent()
.unwrap()
.join("repo.parallel-test");
wait_for_file(worktree_path.join("task1.txt").as_path());
wait_for_file(worktree_path.join("task2.txt").as_path());
}
#[rstest]
fn test_bash_shell_integration_post_start_blocks(repo: TestRepo) {
// Create project config with blocking command
let config_dir = repo.root_path().join(".config");
fs::create_dir_all(&config_dir).unwrap();
fs::write(
config_dir.join("wt.toml"),
r#"pre-start = "echo 'Setup done' > setup.txt""#,
)
.unwrap();
repo.commit("Add pre-start command");
// Pre-approve command
repo.write_test_approvals(
r#"[projects."../origin"]
approved-commands = ["echo 'Setup done' > setup.txt"]
"#,
);
let init_code = generate_init_code(&repo, "bash");
let bin_path = wt_bin_dir();
let worktree_path = repo
.root_path()
.parent()
.unwrap()
.join("repo.blocking-test");
let script = format!(
r#"
export PATH="{}:$PATH"
{}
wt switch --create blocking-test
pwd
"#,
bin_path, init_code
);
let output = execute_shell_script(&repo, "bash", &script);
// Verify we switched to the worktree
assert!(
output.contains("blocking-test"),
"Expected to be in blocking-test worktree, got: {}",
output
);
// Verify that pre-start command completed before wt returned (blocking behavior)
// The file should exist immediately after wt exits
let setup_file = worktree_path.join("setup.txt");
assert!(
setup_file.exists(),
"Setup file should exist immediately after wt returns (pre-start is blocking)"
);
let content = fs::read_to_string(&setup_file).unwrap();
assert!(
content.contains("Setup done"),
"Expected setup output, got: {}",
content
);
}
#[cfg(unix)]
#[rstest]
fn test_fish_shell_integration_post_start_background(repo: TestRepo) {
// Create project config with background command
let config_dir = repo.root_path().join(".config");
fs::create_dir_all(&config_dir).unwrap();
fs::write(
config_dir.join("wt.toml"),
r#"[post-start]
fish_bg = "sleep 0.05 && echo 'Fish background done' > fish_bg.txt"
"#,
)
.unwrap();
repo.commit("Add fish background command");
// Pre-approve command
repo.write_test_approvals(
r#"[projects."../origin"]
approved-commands = ["sleep 0.05 && echo 'Fish background done' > fish_bg.txt"]
"#,
);
let init_code = generate_init_code(&repo, "fish");
let bin_path = wt_bin_dir();
let script = format!(
r#"
set -x PATH {} $PATH
{}
wt switch --create fish-bg-test
echo "Fish shell returned"
pwd
"#,
bin_path, init_code
);
let output = execute_shell_script(&repo, "fish", &script);
// Verify fish shell returned immediately
assert!(
output.contains("Fish shell returned") && output.contains("fish-bg-test"),
"Expected fish shell to return immediately, got: {}",
output
);
// Wait for background command AND flush content (allow plenty of margin on CI)
let worktree_path = repo.root_path().parent().unwrap().join("repo.fish-bg-test");
let marker_file = worktree_path.join("fish_bg.txt");
wait_for_file_content(marker_file.as_path());
let content = fs::read_to_string(&marker_file).unwrap();
assert!(
content.contains("Fish background done"),
"Expected fish background output, got: {}",
content
);
}