mirror of
https://github.com/max-sixty/worktrunk.git
synced 2026-11-26 05:25:35 +08:00
Three simplifications to the config write paths, removing guards that second-guess an explicit argument or duplicate a stronger check. ## `--output` writes the path it names `wt config update --output <path>` refused three things: the config being migrated, an existing file without `--yes`, and an existing file with no terminal to prompt on. A named destination is the caller's choice — `cp`, `curl -o`, and a shell redirect all replace what is there — so all three go, along with the clobber/no-clobber branch at the write. This is the maintainer's stated policy for this PR, not an inference: a path the caller names is written rather than refused. That also fixes `--output` for automation, which is what it exists for. The old code didn't skip the prompt without a TTY, it failed: `… already exists; to overwrite it with the … migration, add --yes`. So `wt config update --output migrated.toml` in CI worked the first time and errored every time after. `--yes` keeps its meaning. It answers the in-place `Apply updates?` prompt, which renders the migration diff before rewriting the file. ## Overwriting the config being migrated moves its approvals first Writing the migration over the config it came from drops that config's `approved-commands`. An earlier revision of this PR left them dropped and printed the existing warning, whose remedy — "to migrate them to approvals.toml, run `wt config update`" — is false precisely in that case: the write has already removed them, so the follow-up run reports `No deprecated settings found` and the entries are gone. `copy_approved_commands_to_approvals_file` had exactly one production caller, the in-place loop. That block is now `move_approvals_out_of`, and both writes that land a migration on top of the config it came from call it. Any other destination leaves the config alone, so its warning still names a `wt config update` with something to migrate. ## One check on both config-mutation writes `ConfigFile::edited` returns one of two candidate documents: the file with the edit applied, or the file with the load-path migrations applied and then the edit. It validated only the first, and `with_locked_mutation` then ran a weaker TOML-parse over whichever came back. Both candidates now face the same test — the content loads back as the config the mutation asked for — and the parse check goes. It immediately caught a mutator in the suite that returned an edit it never made to the config beside it, which previously wrote a file loading as something other than what the mutation said. ## One write primitive per situation Both primitives stay, but no call site picks between them any more: - a path the caller named → `write_atomically` - a path wt observed absent → `write_new_atomically` `wt config create` was on the wrong one, so a dangling `~/.config/worktrunk/config.toml` symlink from a dotfile manager would have been replaced by a regular file rather than refused. Its error for that case said `File exists (os error 17)`, naming neither the path nor the link, immediately after the existence check reported the path free. A dangling symlink now gets a message naming the link and what to do, as the rc-file write already did; any other write failure names the path it was writing. The rule is in CLAUDE.md so it doesn't drift back. ## What stays `wt config update` still re-reads each config after the prompt and fails if it changed. It is the only check here guarding content the user authored rather than a path they named: edit config.toml while the prompt is open, and without it that edit is silently reverted. ## Changelog #4153's 0.79.0 entry described behavior this removes, so it is replaced with one covering the net change from 0.78.0. 0.79.0 is merged to `main` but untagged, so this can still land inside it. > _This was written by Claude Code on behalf of max-sixty_ --------- Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
207 lines
7.2 KiB
Rust
207 lines
7.2 KiB
Rust
use crate::common::{
|
|
TestRepo, make_snapshot_cmd, repo, set_temp_home_env, set_xdg_config_path,
|
|
setup_home_snapshot_settings, setup_snapshot_settings, temp_home, wt_command,
|
|
};
|
|
use insta_cmd::assert_cmd_snapshot;
|
|
use rstest::rstest;
|
|
use std::fs;
|
|
use tempfile::TempDir;
|
|
|
|
#[rstest]
|
|
fn test_config_init_already_exists(temp_home: TempDir) {
|
|
// Create fake global config at XDG path
|
|
let global_config_dir = temp_home.path().join(".config").join("worktrunk");
|
|
fs::create_dir_all(&global_config_dir).unwrap();
|
|
fs::write(
|
|
global_config_dir.join("config.toml"),
|
|
r#"worktree-path = "../{{ repo }}.{{ branch }}"
|
|
"#,
|
|
)
|
|
.unwrap();
|
|
|
|
let settings = setup_home_snapshot_settings(&temp_home);
|
|
settings.bind(|| {
|
|
let mut cmd = wt_command();
|
|
cmd.arg("config").arg("create");
|
|
set_temp_home_env(&mut cmd, temp_home.path());
|
|
set_xdg_config_path(&mut cmd, temp_home.path());
|
|
|
|
assert_cmd_snapshot!(cmd, @"
|
|
success: true
|
|
exit_code: 0
|
|
----- stdout -----
|
|
|
|
----- stderr -----
|
|
[2m○[22m User config already exists: [1m~/.config/worktrunk/config.toml[22m
|
|
[2m↳[22m [2mTo view, run [4mwt config show[24m. To create a project config, run [4mwt config create --project[24m[22m
|
|
");
|
|
});
|
|
}
|
|
|
|
#[rstest]
|
|
fn test_config_init_creates_file(temp_home: TempDir) {
|
|
// Don't create config file - let create create it
|
|
let global_config_dir = temp_home.path().join(".config").join("worktrunk");
|
|
fs::create_dir_all(&global_config_dir).unwrap();
|
|
|
|
let settings = setup_home_snapshot_settings(&temp_home);
|
|
settings.bind(|| {
|
|
let mut cmd = wt_command();
|
|
cmd.arg("config").arg("create");
|
|
set_temp_home_env(&mut cmd, temp_home.path());
|
|
set_xdg_config_path(&mut cmd, temp_home.path());
|
|
|
|
assert_cmd_snapshot!(cmd, @"
|
|
success: true
|
|
exit_code: 0
|
|
----- stdout -----
|
|
|
|
----- stderr -----
|
|
[32m✓[39m [32mCreated user config: [1m~/.config/worktrunk/config.toml[22m[39m
|
|
[2m↳[22m [2mEdit this file to customize worktree paths and LLM settings[22m
|
|
");
|
|
});
|
|
|
|
// Verify file was actually created
|
|
let config_path = global_config_dir.join("config.toml");
|
|
assert!(config_path.exists());
|
|
}
|
|
|
|
#[rstest]
|
|
fn test_config_create_project_creates_file(repo: TestRepo) {
|
|
let settings = setup_snapshot_settings(&repo);
|
|
settings.bind(|| {
|
|
let mut cmd = make_snapshot_cmd(&repo, "config", &["create", "--project"], None);
|
|
assert_cmd_snapshot!(cmd, @"
|
|
success: true
|
|
exit_code: 0
|
|
----- stdout -----
|
|
|
|
----- stderr -----
|
|
[32m✓[39m [32mCreated project config: [1m_REPO_/.config/wt.toml[22m[39m
|
|
[2m↳[22m [2mEdit this file to configure hooks for this repository[22m
|
|
[2m↳[22m [2mSee https://worktrunk.dev/hook/ for hook documentation[22m
|
|
");
|
|
});
|
|
|
|
// Verify file was actually created
|
|
let config_path = repo.root_path().join(".config/wt.toml");
|
|
assert!(
|
|
config_path.exists(),
|
|
"Project config file should be created"
|
|
);
|
|
}
|
|
|
|
#[rstest]
|
|
fn test_config_create_project_already_exists(repo: TestRepo) {
|
|
// Create project config file
|
|
let config_dir = repo.root_path().join(".config");
|
|
fs::create_dir_all(&config_dir).unwrap();
|
|
fs::write(
|
|
config_dir.join("wt.toml"),
|
|
r#"[[project.pre-start]]
|
|
run = "echo hello"
|
|
"#,
|
|
)
|
|
.unwrap();
|
|
|
|
let settings = setup_snapshot_settings(&repo);
|
|
settings.bind(|| {
|
|
let mut cmd = make_snapshot_cmd(&repo, "config", &["create", "--project"], None);
|
|
assert_cmd_snapshot!(cmd, @"
|
|
success: true
|
|
exit_code: 0
|
|
----- stdout -----
|
|
|
|
----- stderr -----
|
|
[2m○[22m Project config already exists: [1m_REPO_/.config/wt.toml[22m
|
|
[2m↳[22m [2mTo view, run [4mwt config show[24m. To create a user config, run [4mwt config create[24m[22m
|
|
");
|
|
});
|
|
}
|
|
|
|
/// A dangling symlink occupies the path while `path.exists()` reads false, so
|
|
/// the create reaches its write with the path looking absent. It must refuse
|
|
/// rather than replace a dotfile manager's link with a regular file, and the
|
|
/// error has to name what it found — "File exists" right after the existence
|
|
/// check said otherwise explains nothing.
|
|
#[cfg(unix)]
|
|
#[rstest]
|
|
fn test_config_create_project_refuses_a_dangling_symlink(repo: TestRepo) {
|
|
let config_dir = repo.root_path().join(".config");
|
|
fs::create_dir_all(&config_dir).unwrap();
|
|
let link = config_dir.join("wt.toml");
|
|
std::os::unix::fs::symlink(config_dir.join("synced/wt.toml"), &link).unwrap();
|
|
|
|
let output = repo
|
|
.wt_command()
|
|
.args(["config", "create", "--project"])
|
|
.output()
|
|
.unwrap();
|
|
|
|
assert_eq!(output.status.code(), Some(1));
|
|
let stderr = String::from_utf8_lossy(&output.stderr);
|
|
assert!(
|
|
stderr.contains("dangling symlink"),
|
|
"the error should name what occupies the path:\n{stderr}"
|
|
);
|
|
assert!(
|
|
link.is_symlink() && !link.exists(),
|
|
"the link should survive unreplaced"
|
|
);
|
|
}
|
|
|
|
/// A write that fails for any other reason names the path it was writing,
|
|
/// rather than the bare I/O error.
|
|
#[cfg(unix)]
|
|
#[rstest]
|
|
fn test_config_create_project_write_failure_names_the_path(repo: TestRepo) {
|
|
use std::fs::Permissions;
|
|
use std::os::unix::fs::PermissionsExt;
|
|
|
|
let config_dir = repo.root_path().join(".config");
|
|
fs::create_dir_all(&config_dir).unwrap();
|
|
// Read-only directory: the temp file the write stages beside the target
|
|
// can't be created.
|
|
fs::set_permissions(&config_dir, Permissions::from_mode(0o555)).unwrap();
|
|
|
|
// Skip when running as root — permissions don't restrict.
|
|
let probe = config_dir.join("__probe");
|
|
if fs::write(&probe, "").is_ok() {
|
|
let _ = fs::remove_file(&probe);
|
|
fs::set_permissions(&config_dir, Permissions::from_mode(0o755)).unwrap();
|
|
eprintln!("Skipping - running with elevated privileges");
|
|
return;
|
|
}
|
|
|
|
let output = repo
|
|
.wt_command()
|
|
.args(["config", "create", "--project"])
|
|
.output()
|
|
.unwrap();
|
|
|
|
// Restore permissions so TempDir cleanup succeeds even if assertions fail.
|
|
fs::set_permissions(&config_dir, Permissions::from_mode(0o755)).unwrap();
|
|
|
|
assert_eq!(output.status.code(), Some(1));
|
|
let stderr = String::from_utf8_lossy(&output.stderr);
|
|
assert!(
|
|
stderr.contains("Failed to write") && stderr.contains(".config/wt.toml"),
|
|
"the error should name the path being written:\n{stderr}"
|
|
);
|
|
assert!(!config_dir.join("wt.toml").exists());
|
|
}
|
|
|
|
/// Running `wt config create --project` from inside a repo's `.git` directory
|
|
/// (not inside a worktree, not a bare repo) must fail with the generic
|
|
/// "no worktree found" error rather than the bare-repo-specific message.
|
|
#[rstest]
|
|
fn test_config_create_project_from_git_dir_errors(repo: TestRepo) {
|
|
let git_dir = repo.path().join(".git");
|
|
let settings = setup_snapshot_settings(&repo);
|
|
settings.bind(|| {
|
|
let mut cmd = make_snapshot_cmd(&repo, "config", &["create", "--project"], Some(&git_dir));
|
|
assert_cmd_snapshot!(cmd);
|
|
});
|
|
}
|