Skip to content

Commit

Permalink
test: full reproduction of issue 18
Browse files Browse the repository at this point in the history
  • Loading branch information
MilesCranmer committed Apr 15, 2024
1 parent b396929 commit 3de221c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ pub fn move_target(
target.display(),
dest.display()
);
if fs::rename(target, dest).is_ok() {
if util::allow_rename() && fs::rename(target, dest).is_ok() {
return Ok(());
}

Expand Down
2 changes: 1 addition & 1 deletion src/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::println as debug;

use crate::util;

const RECORD: &str = ".record";
pub const RECORD: &str = ".record";

#[derive(Debug)]
pub struct RecordItem<'a> {
Expand Down
8 changes: 8 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ impl TestingMode for TestMode {
}
}

pub fn allow_rename() -> bool {
// Test behavior to skip simple rename
env::var("__RIP_ALLOW_RENAME")
.unwrap_or("true".to_string())
.parse::<bool>()
.unwrap()
}

/// Prompt for user input, returning True if the first character is 'y' or 'Y'
/// Will create an error if given a 'q' or 'Q', equivalent to if the user
/// had passed a SIGINT.
Expand Down
35 changes: 29 additions & 6 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rand::distributions::Alphanumeric;
use rand::Rng;
use rip2::args::Args;
use rip2::util::TestMode;
use rip2::{self, util};
use rip2::{self, record, util};
use rstest::rstest;
use std::fs;
use std::io::{ErrorKind, Write};
Expand Down Expand Up @@ -590,6 +590,7 @@ fn issue_0018() {
],
Some(&test_env.src),
)
.env("__RIP_ALLOW_RENAME", "false")
.write_stdin("\n")
.assert()
.stdout(is_match("About to copy a big file").unwrap())
Expand Down Expand Up @@ -623,6 +624,7 @@ fn issue_0018() {
],
Some(&test_env.src),
)
.env("__RIP_ALLOW_RENAME", "false")
.write_stdin("q\n")
.assert()
.stdout(is_match("gnu_meta.zip: file, ").unwrap());
Expand All @@ -631,12 +633,27 @@ fn issue_0018() {
assert!(test_env.src.join("gnu_meta.zip").exists());
// And not in the graveyard:
assert!(!expected_graveyard_path.exists());

// The graveyard record should *only* reference uu_meta.zip:
let record_contents = fs::read_to_string(test_env.graveyard.join(record::RECORD)).unwrap();
assert!(record_contents.contains("uu_meta.zip"));
assert!(!record_contents.contains("gnu_meta.zip"));

// And give this for the last bury
let record = record::Record::new(&test_env.graveyard);
let last_bury = record.get_last_bury().unwrap();
assert!(last_bury.ends_with("uu_meta.zip"));
}

// rip it again but without -i
{
// Should still be there
assert!(test_env.src.join("gnu_meta.zip").exists());

let expected_graveyard_path = util::join_absolute(
&test_env.graveyard,
test_env.src.join("gnu_meta.zip").canonicalize().unwrap(),
);
cli_runner(
[
"--graveyard",
Expand All @@ -645,14 +662,20 @@ fn issue_0018() {
],
Some(&test_env.src),
)
.env("__RIP_ALLOW_RENAME", "false")
.write_stdin("y\n")
.assert();
// .stdout(is_match("About to copy a big file").unwrap());
// .stdout(is_match("delete this file instead?").unwrap());
// .stdout(is_match("y/N").unwrap());
.assert()
.stdout(is_match("About to copy a big file").unwrap())
.stdout(is_match("delete this file instead?").unwrap())
.stdout(is_match("y/N").unwrap());

// Expect it to be permanently deleted
// assert!(!test_env.src.join("gnu_meta.zip").exists());
assert!(!test_env.src.join("gnu_meta.zip").exists());
assert!(!expected_graveyard_path.exists());

// The record should not reference it anymore either
let record_contents = fs::read_to_string(test_env.graveyard.join(record::RECORD)).unwrap();
assert!(!record_contents.contains("gnu_meta.zip"));
}

return;
Expand Down

0 comments on commit 3de221c

Please sign in to comment.