Rinomina: crate akode-*, cartelle akode-rs/ e akode-cli/, binario akode, ~/.akode, AKODE_* (A)
Riapplica la fase 5 di aKode (86e7c72e) sulla base ripulita:
- crate codex-* -> akode-*, codex-rs -> akode-rs, codex-cli -> akode-cli, binario e ausiliari akode;
- ~/.codex -> ~/.akode, CODEX_HOME -> AKODE_HOME, variabili CODEX_* -> AKODE_*, .codex/ -> .akode/ di progetto;
nessun fallback a ~/.codex o a CODEX_*;
- testi per l'utente "Codex" -> "aKode"; testi verso il modello e istruzioni di sistema invariati;
- snapshot insta rinominati (non ancora rigenerati).
I conflitti con la base ripulita sono stati risolti riapplicando la rinomina ai file senza le
funzioni di imeAI (script di mappa dei token per file).
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
91
akode-rs/apply-patch/tests/suite/cli.rs
Normal file
91
akode-rs/apply-patch/tests/suite/cli.rs
Normal file
@@ -0,0 +1,91 @@
|
||||
use assert_cmd::Command;
|
||||
use std::fs;
|
||||
use tempfile::tempdir;
|
||||
|
||||
fn apply_patch_command() -> anyhow::Result<Command> {
|
||||
Ok(Command::new(akode_utils_cargo_bin::cargo_bin(
|
||||
"apply_patch",
|
||||
)?))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_apply_patch_cli_add_and_update() -> anyhow::Result<()> {
|
||||
let tmp = tempdir()?;
|
||||
let file = "cli_test.txt";
|
||||
let absolute_path = tmp.path().join(file);
|
||||
|
||||
// 1) Add a file
|
||||
let add_patch = format!(
|
||||
r#"*** Begin Patch
|
||||
*** Add File: {file}
|
||||
+hello
|
||||
*** End Patch"#
|
||||
);
|
||||
apply_patch_command()?
|
||||
.arg(add_patch)
|
||||
.current_dir(tmp.path())
|
||||
.assert()
|
||||
.success()
|
||||
.stdout(format!("Success. Updated the following files:\nA {file}\n"));
|
||||
assert_eq!(fs::read_to_string(&absolute_path)?, "hello\n");
|
||||
|
||||
// 2) Update the file
|
||||
let update_patch = format!(
|
||||
r#"*** Begin Patch
|
||||
*** Update File: {file}
|
||||
@@
|
||||
-hello
|
||||
+world
|
||||
*** End Patch"#
|
||||
);
|
||||
apply_patch_command()?
|
||||
.arg(update_patch)
|
||||
.current_dir(tmp.path())
|
||||
.assert()
|
||||
.success()
|
||||
.stdout(format!("Success. Updated the following files:\nM {file}\n"));
|
||||
assert_eq!(fs::read_to_string(&absolute_path)?, "world\n");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_apply_patch_cli_stdin_add_and_update() -> anyhow::Result<()> {
|
||||
let tmp = tempdir()?;
|
||||
let file = "cli_test_stdin.txt";
|
||||
let absolute_path = tmp.path().join(file);
|
||||
|
||||
// 1) Add a file via stdin
|
||||
let add_patch = format!(
|
||||
r#"*** Begin Patch
|
||||
*** Add File: {file}
|
||||
+hello
|
||||
*** End Patch"#
|
||||
);
|
||||
apply_patch_command()?
|
||||
.current_dir(tmp.path())
|
||||
.write_stdin(add_patch)
|
||||
.assert()
|
||||
.success()
|
||||
.stdout(format!("Success. Updated the following files:\nA {file}\n"));
|
||||
assert_eq!(fs::read_to_string(&absolute_path)?, "hello\n");
|
||||
|
||||
// 2) Update the file via stdin
|
||||
let update_patch = format!(
|
||||
r#"*** Begin Patch
|
||||
*** Update File: {file}
|
||||
@@
|
||||
-hello
|
||||
+world
|
||||
*** End Patch"#
|
||||
);
|
||||
apply_patch_command()?
|
||||
.current_dir(tmp.path())
|
||||
.write_stdin(update_patch)
|
||||
.assert()
|
||||
.success()
|
||||
.stdout(format!("Success. Updated the following files:\nM {file}\n"));
|
||||
assert_eq!(fs::read_to_string(&absolute_path)?, "world\n");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
6
akode-rs/apply-patch/tests/suite/mod.rs
Normal file
6
akode-rs/apply-patch/tests/suite/mod.rs
Normal file
@@ -0,0 +1,6 @@
|
||||
mod cli;
|
||||
#[cfg(unix)]
|
||||
mod no_follow;
|
||||
mod scenarios;
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
mod tool;
|
||||
150
akode-rs/apply-patch/tests/suite/no_follow.rs
Normal file
150
akode-rs/apply-patch/tests/suite/no_follow.rs
Normal file
@@ -0,0 +1,150 @@
|
||||
use akode_apply_patch::ApplyPatchOptions;
|
||||
use akode_apply_patch::MaybeApplyPatchVerified;
|
||||
use akode_apply_patch::apply_patch_with_options;
|
||||
use akode_apply_patch::parse_patch;
|
||||
use akode_apply_patch::verify_apply_patch_args;
|
||||
use akode_exec_server::LOCAL_FS;
|
||||
use akode_utils_path_uri::PathUri;
|
||||
use pretty_assertions::assert_eq;
|
||||
use std::fs;
|
||||
use std::os::unix::fs::symlink;
|
||||
|
||||
#[tokio::test]
|
||||
async fn no_follow_rejects_leaf_and_ancestor_links_for_every_patch_operation() -> anyhow::Result<()>
|
||||
{
|
||||
for path in ["link.txt", "linked/victim.txt"] {
|
||||
for body in [
|
||||
format!("*** Add File: {path}\n+changed"),
|
||||
format!("*** Update File: {path}\n@@\n-original\n+changed"),
|
||||
format!("*** Delete File: {path}"),
|
||||
format!("*** Update File: {path}\n*** Move to: moved.txt\n@@\n-original\n+changed"),
|
||||
format!("*** Update File: source.txt\n*** Move to: {path}\n@@\n-original\n+changed"),
|
||||
"*** Add File: linked/new/nested.txt\n+changed".to_string(),
|
||||
"*** Update File: source.txt\n*** Move to: linked/new/nested.txt\n@@\n-original\n+changed".to_string(),
|
||||
] {
|
||||
let temp = tempfile::tempdir()?;
|
||||
// macOS's temporary directory can itself contain a /var symlink.
|
||||
let root = temp.path().canonicalize()?;
|
||||
let work = root.join("work");
|
||||
let outside = root.join("outside");
|
||||
fs::create_dir(&work)?;
|
||||
fs::create_dir(&outside)?;
|
||||
fs::write(outside.join("victim.txt"), "original\n")?;
|
||||
fs::write(work.join("source.txt"), "original\n")?;
|
||||
symlink(outside.join("victim.txt"), work.join("link.txt"))?;
|
||||
symlink(&outside, work.join("linked"))?;
|
||||
|
||||
let patch = format!("*** Begin Patch\n{body}\n*** End Patch");
|
||||
let failure = apply_patch_with_options(
|
||||
&patch,
|
||||
ApplyPatchOptions {
|
||||
follow_symlinks: false,
|
||||
},
|
||||
&PathUri::from_host_native_path(&work)?,
|
||||
&mut Vec::new(),
|
||||
&mut Vec::new(),
|
||||
LOCAL_FS.as_ref(),
|
||||
/*sandbox*/ None,
|
||||
)
|
||||
.await
|
||||
.expect_err(&body);
|
||||
|
||||
assert!(failure.delta().is_empty(), "{body}");
|
||||
assert_eq!(fs::read_to_string(outside.join("victim.txt"))?, "original\n");
|
||||
assert_eq!(fs::read_to_string(work.join("source.txt"))?, "original\n");
|
||||
assert!(!outside.join("new").exists(), "{body}");
|
||||
assert!(!work.join("moved.txt").exists(), "{body}");
|
||||
assert!(fs::symlink_metadata(work.join("link.txt"))?.is_symlink());
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn no_follow_rechecks_paths_after_verification() -> anyhow::Result<()> {
|
||||
let temp = tempfile::tempdir()?;
|
||||
let root = temp.path().canonicalize()?;
|
||||
fs::create_dir(root.join("approved"))?;
|
||||
fs::create_dir(root.join("outside"))?;
|
||||
fs::write(root.join("approved/file.txt"), "original\n")?;
|
||||
fs::write(root.join("outside/file.txt"), "original\n")?;
|
||||
let cwd = PathUri::from_host_native_path(&root)?;
|
||||
let patch = "*** Begin Patch\n*** Update File: approved/file.txt\n@@\n-original\n+changed\n*** End Patch";
|
||||
let MaybeApplyPatchVerified::Body(action) = verify_apply_patch_args(
|
||||
parse_patch(patch)?,
|
||||
&cwd,
|
||||
LOCAL_FS.as_ref(),
|
||||
/*sandbox*/ None,
|
||||
)
|
||||
.await
|
||||
else {
|
||||
panic!("regular-file patch should verify");
|
||||
};
|
||||
fs::rename(root.join("approved"), root.join("original"))?;
|
||||
symlink(root.join("outside"), root.join("approved"))?;
|
||||
|
||||
apply_patch_with_options(
|
||||
&action.patch,
|
||||
ApplyPatchOptions {
|
||||
follow_symlinks: false,
|
||||
},
|
||||
&cwd,
|
||||
&mut Vec::new(),
|
||||
&mut Vec::new(),
|
||||
LOCAL_FS.as_ref(),
|
||||
/*sandbox*/ None,
|
||||
)
|
||||
.await
|
||||
.expect_err("swapped ancestor must not be followed");
|
||||
assert_eq!(
|
||||
fs::read_to_string(root.join("outside/file.txt"))?,
|
||||
"original\n"
|
||||
);
|
||||
assert_eq!(
|
||||
fs::read_to_string(root.join("original/file.txt"))?,
|
||||
"original\n"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn no_follow_applies_regular_files_and_default_still_follows_links() -> anyhow::Result<()> {
|
||||
let temp = tempfile::tempdir()?;
|
||||
let root = temp.path().canonicalize()?;
|
||||
let cwd = PathUri::from_host_native_path(&root)?;
|
||||
let patch = "*** Begin Patch\n*** Add File: new/file.txt\n+original\n*** Update File: new/file.txt\n*** Move to: moved/file.txt\n@@\n-original\n+changed\n*** Add File: remove.txt\n+remove\n*** Delete File: remove.txt\n*** End Patch";
|
||||
apply_patch_with_options(
|
||||
patch,
|
||||
ApplyPatchOptions {
|
||||
follow_symlinks: false,
|
||||
},
|
||||
&cwd,
|
||||
&mut Vec::new(),
|
||||
&mut Vec::new(),
|
||||
LOCAL_FS.as_ref(),
|
||||
/*sandbox*/ None,
|
||||
)
|
||||
.await?;
|
||||
assert_eq!(
|
||||
fs::read_to_string(root.join("moved/file.txt"))?,
|
||||
"changed\n"
|
||||
);
|
||||
assert!(!root.join("new/file.txt").exists());
|
||||
assert!(!root.join("remove.txt").exists());
|
||||
|
||||
symlink(root.join("moved/file.txt"), root.join("link.txt"))?;
|
||||
akode_apply_patch::apply_patch(
|
||||
"*** Begin Patch\n*** Update File: link.txt\n@@\n-changed\n+followed\n*** End Patch",
|
||||
&cwd,
|
||||
&mut Vec::new(),
|
||||
&mut Vec::new(),
|
||||
LOCAL_FS.as_ref(),
|
||||
/*sandbox*/ None,
|
||||
)
|
||||
.await?;
|
||||
assert_eq!(
|
||||
fs::read_to_string(root.join("moved/file.txt"))?,
|
||||
"followed\n"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
131
akode-rs/apply-patch/tests/suite/scenarios.rs
Normal file
131
akode-rs/apply-patch/tests/suite/scenarios.rs
Normal file
@@ -0,0 +1,131 @@
|
||||
use anyhow::Context;
|
||||
use akode_utils_cargo_bin::find_resource;
|
||||
use pretty_assertions::assert_eq;
|
||||
use std::collections::BTreeMap;
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
use std::process::Command;
|
||||
use tempfile::tempdir;
|
||||
|
||||
#[test]
|
||||
fn test_apply_patch_scenarios() -> anyhow::Result<()> {
|
||||
let scenarios_marker = find_resource!("tests/fixtures/scenarios/.gitattributes")?;
|
||||
let scenarios_dir = scenarios_marker
|
||||
.parent()
|
||||
.context("scenario marker should have a parent directory")?;
|
||||
for scenario in fs::read_dir(scenarios_dir)
|
||||
.with_context(|| format!("failed to read {}", scenarios_dir.display()))?
|
||||
{
|
||||
let scenario = scenario?;
|
||||
let path = scenario.path();
|
||||
if path.is_dir() {
|
||||
run_apply_patch_scenario(&path)
|
||||
.with_context(|| format!("failed to run scenario {}", path.display()))?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Reads a scenario directory, copies the input files to a temporary directory, runs apply-patch,
|
||||
/// and asserts that the final state matches the expected state exactly.
|
||||
fn run_apply_patch_scenario(dir: &Path) -> anyhow::Result<()> {
|
||||
let tmp = tempdir()?;
|
||||
|
||||
// Copy the input files to the temporary directory
|
||||
let input_dir = dir.join("input");
|
||||
if input_dir.is_dir() {
|
||||
copy_dir_recursive(&input_dir, tmp.path())?;
|
||||
}
|
||||
|
||||
// Read the patch.txt file
|
||||
let patch_path = dir.join("patch.txt");
|
||||
let patch = fs::read_to_string(&patch_path)
|
||||
.with_context(|| format!("failed to read {}", patch_path.display()))?;
|
||||
|
||||
// Run apply_patch in the temporary directory. We intentionally do not assert
|
||||
// on the exit status here; the scenarios are specified purely in terms of
|
||||
// final filesystem state, which we compare below.
|
||||
Command::new(akode_utils_cargo_bin::cargo_bin("apply_patch")?)
|
||||
.arg(patch)
|
||||
.current_dir(tmp.path())
|
||||
.output()
|
||||
.with_context(|| format!("failed to run scenario {}", dir.display()))?;
|
||||
|
||||
// Assert that the final state matches the expected state exactly
|
||||
let expected_dir = dir.join("expected");
|
||||
let expected_snapshot = snapshot_dir(&expected_dir)?;
|
||||
let actual_snapshot = snapshot_dir(tmp.path())?;
|
||||
|
||||
assert_eq!(
|
||||
actual_snapshot,
|
||||
expected_snapshot,
|
||||
"Scenario {} did not match expected final state",
|
||||
dir.display()
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
enum Entry {
|
||||
File(Vec<u8>),
|
||||
Dir,
|
||||
}
|
||||
|
||||
fn snapshot_dir(root: &Path) -> anyhow::Result<BTreeMap<PathBuf, Entry>> {
|
||||
let mut entries = BTreeMap::new();
|
||||
if root.is_dir() {
|
||||
snapshot_dir_recursive(root, root, &mut entries)?;
|
||||
}
|
||||
Ok(entries)
|
||||
}
|
||||
|
||||
fn snapshot_dir_recursive(
|
||||
base: &Path,
|
||||
dir: &Path,
|
||||
entries: &mut BTreeMap<PathBuf, Entry>,
|
||||
) -> anyhow::Result<()> {
|
||||
for entry in fs::read_dir(dir)? {
|
||||
let entry = entry?;
|
||||
let path = entry.path();
|
||||
let Some(stripped) = path.strip_prefix(base).ok() else {
|
||||
continue;
|
||||
};
|
||||
let rel = stripped.to_path_buf();
|
||||
|
||||
// Under Buck2, files in `__srcs` are often materialized as symlinks.
|
||||
// Use `metadata()` (follows symlinks) so our fixture snapshots work
|
||||
// under both Cargo and Buck2.
|
||||
let metadata = fs::metadata(&path)?;
|
||||
if metadata.is_dir() {
|
||||
entries.insert(rel.clone(), Entry::Dir);
|
||||
snapshot_dir_recursive(base, &path, entries)?;
|
||||
} else if metadata.is_file() {
|
||||
let contents = fs::read(&path)?;
|
||||
entries.insert(rel, Entry::File(contents));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn copy_dir_recursive(src: &Path, dst: &Path) -> anyhow::Result<()> {
|
||||
for entry in fs::read_dir(src)? {
|
||||
let entry = entry?;
|
||||
let path = entry.path();
|
||||
let dest_path = dst.join(entry.file_name());
|
||||
|
||||
// See note in `snapshot_dir_recursive` about Buck2 symlink trees.
|
||||
let metadata = fs::metadata(&path)?;
|
||||
if metadata.is_dir() {
|
||||
fs::create_dir_all(&dest_path)?;
|
||||
copy_dir_recursive(&path, &dest_path)?;
|
||||
} else if metadata.is_file() {
|
||||
if let Some(parent) = dest_path.parent() {
|
||||
fs::create_dir_all(parent)?;
|
||||
}
|
||||
fs::copy(&path, &dest_path)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
396
akode-rs/apply-patch/tests/suite/tool.rs
Normal file
396
akode-rs/apply-patch/tests/suite/tool.rs
Normal file
@@ -0,0 +1,396 @@
|
||||
use assert_cmd::Command;
|
||||
use pretty_assertions::assert_eq;
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
use tempfile::tempdir;
|
||||
|
||||
fn run_apply_patch_in_dir(dir: &Path, patch: &str) -> anyhow::Result<assert_cmd::assert::Assert> {
|
||||
let mut cmd = Command::new(akode_utils_cargo_bin::cargo_bin("apply_patch")?);
|
||||
cmd.current_dir(dir);
|
||||
Ok(cmd.arg(patch).assert())
|
||||
}
|
||||
|
||||
fn assert_apply_patch_updates_file(
|
||||
file_name: &str,
|
||||
original: &[u8],
|
||||
patch: &str,
|
||||
expected: &[u8],
|
||||
) -> anyhow::Result<()> {
|
||||
let tmp = tempdir()?;
|
||||
let target_path = tmp.path().join(file_name);
|
||||
fs::write(&target_path, original)?;
|
||||
|
||||
run_apply_patch_in_dir(tmp.path(), patch)?
|
||||
.success()
|
||||
.stdout(format!(
|
||||
"Success. Updated the following files:\nM {file_name}\n"
|
||||
));
|
||||
|
||||
assert_eq!(fs::read(target_path)?, expected);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn apply_patch_command(dir: &Path) -> anyhow::Result<Command> {
|
||||
let mut cmd = Command::new(akode_utils_cargo_bin::cargo_bin("apply_patch")?);
|
||||
cmd.current_dir(dir);
|
||||
Ok(cmd)
|
||||
}
|
||||
|
||||
fn resolved_under(root: &Path, path: &str) -> anyhow::Result<PathBuf> {
|
||||
Ok(root.canonicalize()?.join(path))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_apply_patch_cli_applies_multiple_operations() -> anyhow::Result<()> {
|
||||
let tmp = tempdir()?;
|
||||
let add_path = tmp.path().join("nested/new.txt");
|
||||
let modify_path = tmp.path().join("modify.txt");
|
||||
let delete_path = tmp.path().join("delete.txt");
|
||||
|
||||
fs::write(&modify_path, "line1\nline2\n")?;
|
||||
fs::write(&delete_path, "obsolete\n")?;
|
||||
|
||||
let patch = "*** Begin Patch\n*** Add File: nested/new.txt\n+created\n*** Delete File: delete.txt\n*** Update File: modify.txt\n@@\n-line2\n+changed\n*** End Patch";
|
||||
|
||||
run_apply_patch_in_dir(tmp.path(), patch)?.success().stdout(
|
||||
"Success. Updated the following files:\nA nested/new.txt\nM modify.txt\nD delete.txt\n",
|
||||
);
|
||||
|
||||
assert_eq!(fs::read_to_string(add_path)?, "created\n");
|
||||
assert_eq!(fs::read_to_string(&modify_path)?, "line1\nchanged\n");
|
||||
assert!(!delete_path.exists());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_apply_patch_cli_applies_multiple_chunks() -> anyhow::Result<()> {
|
||||
let tmp = tempdir()?;
|
||||
let target_path = tmp.path().join("multi.txt");
|
||||
fs::write(&target_path, "line1\nline2\nline3\nline4\n")?;
|
||||
|
||||
let patch = "*** Begin Patch\n*** Update File: multi.txt\n@@\n-line2\n+changed2\n@@\n-line4\n+changed4\n*** End Patch";
|
||||
|
||||
run_apply_patch_in_dir(tmp.path(), patch)?
|
||||
.success()
|
||||
.stdout("Success. Updated the following files:\nM multi.txt\n");
|
||||
|
||||
assert_eq!(
|
||||
fs::read_to_string(&target_path)?,
|
||||
"line1\nchanged2\nline3\nchanged4\n"
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_apply_patch_cli_rejects_overlapping_end_of_file_chunks() -> anyhow::Result<()> {
|
||||
let tmp = tempdir()?;
|
||||
let target_path = tmp.path().join("overlapping.txt");
|
||||
let expected_target_path = resolved_under(tmp.path(), "overlapping.txt")?;
|
||||
fs::write(&target_path, "one\n")?;
|
||||
|
||||
let patch = "*** Begin Patch\n*** Update File: overlapping.txt\n@@\n-one\n+first\n@@\n-one\n+second\n*** End of File\n*** End Patch";
|
||||
|
||||
run_apply_patch_in_dir(tmp.path(), patch)?
|
||||
.failure()
|
||||
.stderr(format!(
|
||||
"Failed to find expected lines in {}:\none\n",
|
||||
expected_target_path.display()
|
||||
));
|
||||
|
||||
assert_eq!(fs::read_to_string(target_path)?, "one\n");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_apply_patch_cli_preserves_crlf_from_target_file() -> anyhow::Result<()> {
|
||||
let patch = "*** Begin Patch\n*** Update File: crlf.txt\n@@\n-one\n+uno\n@@\n two\n+\n+between\n three\n*** End Patch";
|
||||
|
||||
assert_apply_patch_updates_file(
|
||||
"crlf.txt",
|
||||
b"one\r\ntwo\r\nthree\r\n",
|
||||
patch,
|
||||
b"uno\r\ntwo\r\n\r\nbetween\r\nthree\r\n",
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_apply_patch_cli_appends_after_trailing_blank_crlf_line() -> anyhow::Result<()> {
|
||||
let patch = "*** Begin Patch\n*** Update File: trailing_blank.txt\n@@\n+new\n*** End Patch";
|
||||
|
||||
assert_apply_patch_updates_file(
|
||||
"trailing_blank.txt",
|
||||
b"a\r\n\r\n",
|
||||
patch,
|
||||
b"a\r\n\r\nnew\r\n",
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_apply_patch_cli_preserves_cr_from_target_file() -> anyhow::Result<()> {
|
||||
let patch = "*** Begin Patch\n*** Update File: cr.txt\n@@\n-one\n+uno\n@@\n two\n+\n+between\n three\n*** End Patch";
|
||||
|
||||
assert_apply_patch_updates_file(
|
||||
"cr.txt",
|
||||
b"one\rtwo\rthree\r",
|
||||
patch,
|
||||
b"uno\rtwo\r\rbetween\rthree\r",
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_apply_patch_cli_preserves_change_order_with_repeated_lines() -> anyhow::Result<()> {
|
||||
let patch =
|
||||
"*** Begin Patch\n*** Update File: repeated.txt\n@@\n-a\n-b\n+b\n+b\n+a\n*** End Patch";
|
||||
|
||||
assert_apply_patch_updates_file("repeated.txt", b"a\nb\n", patch, b"b\nb\na\n")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_apply_patch_cli_preserves_repeated_context_line_ending() -> anyhow::Result<()> {
|
||||
let patch =
|
||||
"*** Begin Patch\n*** Update File: repeated_context.txt\n@@\n-same\n same\n*** End Patch";
|
||||
|
||||
assert_apply_patch_updates_file("repeated_context.txt", b"same\r\nsame\n", patch, b"same\n")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_apply_patch_cli_preserves_untouched_mixed_line_endings() -> anyhow::Result<()> {
|
||||
let patch = "*** Begin Patch\n*** Update File: mixed.txt\n@@\n one\n two\n-three\n+THREE\n four\n*** End Patch";
|
||||
|
||||
assert_apply_patch_updates_file(
|
||||
"mixed.txt",
|
||||
b"one\r\ntwo\rthree\nfour\r\n",
|
||||
patch,
|
||||
b"one\r\ntwo\rTHREE\r\nfour\r\n",
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_apply_patch_cli_uses_crlf_for_new_trailing_newline() -> anyhow::Result<()> {
|
||||
let patch =
|
||||
"*** Begin Patch\n*** Update File: no_trailing_newline.txt\n@@\n-one\n+ONE\n*** End Patch";
|
||||
|
||||
assert_apply_patch_updates_file(
|
||||
"no_trailing_newline.txt",
|
||||
b"one\r\ntwo",
|
||||
patch,
|
||||
b"ONE\r\ntwo\r\n",
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_apply_patch_cli_moves_file_to_new_directory() -> anyhow::Result<()> {
|
||||
let tmp = tempdir()?;
|
||||
let original_path = tmp.path().join("old/name.txt");
|
||||
let new_path = tmp.path().join("renamed/dir/name.txt");
|
||||
fs::create_dir_all(original_path.parent().expect("parent should exist"))?;
|
||||
fs::write(&original_path, "old content\n")?;
|
||||
|
||||
let patch = "*** Begin Patch\n*** Update File: old/name.txt\n*** Move to: renamed/dir/name.txt\n@@\n-old content\n+new content\n*** End Patch";
|
||||
|
||||
run_apply_patch_in_dir(tmp.path(), patch)?
|
||||
.success()
|
||||
.stdout("Success. Updated the following files:\nM renamed/dir/name.txt\n");
|
||||
|
||||
assert!(!original_path.exists());
|
||||
assert_eq!(fs::read_to_string(&new_path)?, "new content\n");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_apply_patch_cli_rejects_empty_patch() -> anyhow::Result<()> {
|
||||
let tmp = tempdir()?;
|
||||
|
||||
apply_patch_command(tmp.path())?
|
||||
.arg("*** Begin Patch\n*** End Patch")
|
||||
.assert()
|
||||
.failure()
|
||||
.stderr("No files were modified.\n");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_apply_patch_cli_reports_missing_context() -> anyhow::Result<()> {
|
||||
let tmp = tempdir()?;
|
||||
let target_path = tmp.path().join("modify.txt");
|
||||
let expected_target_path = resolved_under(tmp.path(), "modify.txt")?;
|
||||
fs::write(&target_path, "line1\nline2\n")?;
|
||||
|
||||
apply_patch_command(tmp.path())?
|
||||
.arg("*** Begin Patch\n*** Update File: modify.txt\n@@\n-missing\n+changed\n*** End Patch")
|
||||
.assert()
|
||||
.failure()
|
||||
.stderr(format!(
|
||||
"Failed to find expected lines in {}:\nmissing\n",
|
||||
expected_target_path.display()
|
||||
));
|
||||
assert_eq!(fs::read_to_string(&target_path)?, "line1\nline2\n");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_apply_patch_cli_rejects_missing_file_delete() -> anyhow::Result<()> {
|
||||
let tmp = tempdir()?;
|
||||
let missing_path = resolved_under(tmp.path(), "missing.txt")?;
|
||||
|
||||
apply_patch_command(tmp.path())?
|
||||
.arg("*** Begin Patch\n*** Delete File: missing.txt\n*** End Patch")
|
||||
.assert()
|
||||
.failure()
|
||||
.stderr(format!(
|
||||
"Failed to delete file {}\n",
|
||||
missing_path.display()
|
||||
));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_apply_patch_cli_rejects_empty_update_hunk() -> anyhow::Result<()> {
|
||||
let tmp = tempdir()?;
|
||||
|
||||
apply_patch_command(tmp.path())?
|
||||
.arg("*** Begin Patch\n*** Update File: foo.txt\n*** End Patch")
|
||||
.assert()
|
||||
.failure()
|
||||
.stderr("Invalid patch hunk on line 2: Update file hunk for path 'foo.txt' is empty\n");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_apply_patch_cli_requires_existing_file_for_update() -> anyhow::Result<()> {
|
||||
let tmp = tempdir()?;
|
||||
let missing_path = resolved_under(tmp.path(), "missing.txt")?;
|
||||
|
||||
apply_patch_command(tmp.path())?
|
||||
.arg("*** Begin Patch\n*** Update File: missing.txt\n@@\n-old\n+new\n*** End Patch")
|
||||
.assert()
|
||||
.failure()
|
||||
.stderr(format!(
|
||||
"Failed to read file to update {}: No such file or directory (os error 2)\n",
|
||||
missing_path.display()
|
||||
));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_apply_patch_cli_move_overwrites_existing_destination() -> anyhow::Result<()> {
|
||||
let tmp = tempdir()?;
|
||||
let original_path = tmp.path().join("old/name.txt");
|
||||
let destination = tmp.path().join("renamed/dir/name.txt");
|
||||
fs::create_dir_all(original_path.parent().expect("parent should exist"))?;
|
||||
fs::create_dir_all(destination.parent().expect("parent should exist"))?;
|
||||
fs::write(&original_path, "from\n")?;
|
||||
fs::write(&destination, "existing\n")?;
|
||||
|
||||
run_apply_patch_in_dir(
|
||||
tmp.path(),
|
||||
"*** Begin Patch\n*** Update File: old/name.txt\n*** Move to: renamed/dir/name.txt\n@@\n-from\n+new\n*** End Patch",
|
||||
)?
|
||||
.success()
|
||||
.stdout("Success. Updated the following files:\nM renamed/dir/name.txt\n");
|
||||
|
||||
assert!(!original_path.exists());
|
||||
assert_eq!(fs::read_to_string(&destination)?, "new\n");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_apply_patch_cli_add_overwrites_existing_file() -> anyhow::Result<()> {
|
||||
let tmp = tempdir()?;
|
||||
let path = tmp.path().join("duplicate.txt");
|
||||
fs::write(&path, "old content\n")?;
|
||||
|
||||
run_apply_patch_in_dir(
|
||||
tmp.path(),
|
||||
"*** Begin Patch\n*** Add File: duplicate.txt\n+new content\n*** End Patch",
|
||||
)?
|
||||
.success()
|
||||
.stdout("Success. Updated the following files:\nA duplicate.txt\n");
|
||||
|
||||
assert_eq!(fs::read_to_string(&path)?, "new content\n");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_apply_patch_cli_delete_directory_fails() -> anyhow::Result<()> {
|
||||
let tmp = tempdir()?;
|
||||
let dir = tmp.path().join("dir");
|
||||
let expected_dir = resolved_under(tmp.path(), "dir")?;
|
||||
fs::create_dir(&dir)?;
|
||||
|
||||
apply_patch_command(tmp.path())?
|
||||
.arg("*** Begin Patch\n*** Delete File: dir\n*** End Patch")
|
||||
.assert()
|
||||
.failure()
|
||||
.stderr(format!(
|
||||
"Failed to delete file {}\n",
|
||||
expected_dir.display()
|
||||
));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_apply_patch_cli_rejects_invalid_hunk_header() -> anyhow::Result<()> {
|
||||
let tmp = tempdir()?;
|
||||
|
||||
apply_patch_command(tmp.path())?
|
||||
.arg("*** Begin Patch\n*** Frobnicate File: foo\n*** End Patch")
|
||||
.assert()
|
||||
.failure()
|
||||
.stderr("Invalid patch hunk on line 2: '*** Frobnicate File: foo' is not a valid hunk header. Valid hunk headers: '*** Add File: {path}', '*** Delete File: {path}', '*** Update File: {path}'\n");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_apply_patch_cli_updates_file_appends_trailing_newline() -> anyhow::Result<()> {
|
||||
let tmp = tempdir()?;
|
||||
let target_path = tmp.path().join("no_newline.txt");
|
||||
fs::write(&target_path, "no newline at end")?;
|
||||
|
||||
run_apply_patch_in_dir(
|
||||
tmp.path(),
|
||||
"*** Begin Patch\n*** Update File: no_newline.txt\n@@\n-no newline at end\n+first line\n+second line\n*** End Patch",
|
||||
)?
|
||||
.success()
|
||||
.stdout("Success. Updated the following files:\nM no_newline.txt\n");
|
||||
|
||||
let contents = fs::read_to_string(&target_path)?;
|
||||
assert!(contents.ends_with('\n'));
|
||||
assert_eq!(contents, "first line\nsecond line\n");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_apply_patch_cli_failure_after_partial_success_leaves_changes() -> anyhow::Result<()> {
|
||||
let tmp = tempdir()?;
|
||||
let new_file = tmp.path().join("created.txt");
|
||||
let missing_file = resolved_under(tmp.path(), "missing.txt")?;
|
||||
|
||||
apply_patch_command(tmp.path())?
|
||||
.arg("*** Begin Patch\n*** Add File: created.txt\n+hello\n*** Update File: missing.txt\n@@\n-old\n+new\n*** End Patch")
|
||||
.assert()
|
||||
.failure()
|
||||
.stdout("")
|
||||
.stderr(format!(
|
||||
"Failed to read file to update {}: No such file or directory (os error 2)\n",
|
||||
missing_file.display()
|
||||
));
|
||||
|
||||
assert_eq!(fs::read_to_string(&new_file)?, "hello\n");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user