Skip to content

Commit bd0db4e

Browse files
committed
♻️ Extract to shared utility
1. Created cli/tests/cli/utils/time.rs with generic helper functions: - wait_until_time_newer_than<F> - waits until a file's timestamp is newer than a baseline - confirm_time_older_than<F> - confirms a file's timestamp is older than a baseline - ensure_ctime_order - convenience function for creation time ordering - ensure_mtime_order - convenience function for modification time ordering 2. Added pub mod time; to cli/tests/cli/utils.rs 3. Updated 10 test files to use the shared utility instead of duplicated local functions: - cli/tests/cli/append/option_newer_ctime_than.rs - cli/tests/cli/append/option_newer_mtime_than.rs - cli/tests/cli/append/option_older_ctime_than.rs - cli/tests/cli/append/option_older_mtime_than.rs - cli/tests/cli/create/option_newer_ctime_than.rs - cli/tests/cli/create/option_newer_mtime_than.rs - cli/tests/cli/create/option_older_ctime_than.rs - cli/tests/cli/create/option_older_mtime_than.rs - cli/tests/cli/update/option_older_ctime_than.rs - cli/tests/cli/update/option_older_mtime_than.rs
1 parent 7401784 commit bd0db4e

12 files changed

+105
-347
lines changed

cli/tests/cli/append/option_newer_ctime_than.rs

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
use crate::utils::{archive, setup};
1+
use crate::utils::{archive, setup, time::ensure_ctime_order};
22
use clap::Parser;
33
use portable_network_archive::cli;
4-
use std::{
5-
collections::HashSet,
6-
fs, thread,
7-
time::{Duration, SystemTime},
8-
};
4+
use std::{collections::HashSet, fs, thread, time::Duration};
95

106
/// Precondition: Create an archive with an older file, then prepare reference and newer files.
117
/// Action: Run `pna append` with `--newer-ctime-than reference.txt`, specifying older, reference, and newer files.
@@ -110,35 +106,3 @@ fn append_with_newer_ctime_than() {
110106
seen.len()
111107
);
112108
}
113-
114-
fn ensure_ctime_order(older: &str, newer: &str, reference: SystemTime) -> bool {
115-
if !confirm_ctime_older_than(older, reference) {
116-
return false;
117-
}
118-
wait_until_ctime_newer_than(newer, reference)
119-
}
120-
121-
fn wait_until_ctime_newer_than(path: &str, baseline: SystemTime) -> bool {
122-
const MAX_ATTEMPTS: usize = 500;
123-
const SLEEP_MS: u64 = 10;
124-
for _ in 0..MAX_ATTEMPTS {
125-
if fs::metadata(path)
126-
.ok()
127-
.and_then(|meta| meta.created().ok())
128-
.map(|ctime| ctime > baseline)
129-
.unwrap_or(false)
130-
{
131-
return true;
132-
}
133-
thread::sleep(Duration::from_millis(SLEEP_MS));
134-
}
135-
false
136-
}
137-
138-
fn confirm_ctime_older_than(path: &str, baseline: SystemTime) -> bool {
139-
fs::metadata(path)
140-
.ok()
141-
.and_then(|meta| meta.created().ok())
142-
.map(|ctime| ctime < baseline)
143-
.unwrap_or(false)
144-
}

cli/tests/cli/append/option_newer_mtime_than.rs

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
use crate::utils::{archive, setup};
1+
use crate::utils::{archive, setup, time::ensure_mtime_order};
22
use clap::Parser;
33
use portable_network_archive::cli;
4-
use std::{
5-
collections::HashSet,
6-
fs, thread,
7-
time::{Duration, SystemTime},
8-
};
4+
use std::{collections::HashSet, fs, thread, time::Duration};
95

106
/// Precondition: Create an archive with an older file, then prepare reference and newer files.
117
/// Action: Run `pna append` with `--newer-mtime-than reference.txt`, specifying older, reference, and newer files.
@@ -103,35 +99,3 @@ fn append_with_newer_mtime_than() {
10399
seen.len()
104100
);
105101
}
106-
107-
fn ensure_mtime_order(older: &str, newer: &str, reference: SystemTime) -> bool {
108-
if !confirm_mtime_older_than(older, reference) {
109-
return false;
110-
}
111-
wait_until_mtime_newer_than(newer, reference)
112-
}
113-
114-
fn wait_until_mtime_newer_than(path: &str, baseline: SystemTime) -> bool {
115-
const MAX_ATTEMPTS: usize = 500;
116-
const SLEEP_MS: u64 = 10;
117-
for _ in 0..MAX_ATTEMPTS {
118-
if fs::metadata(path)
119-
.ok()
120-
.and_then(|meta| meta.modified().ok())
121-
.map(|mtime| mtime > baseline)
122-
.unwrap_or(false)
123-
{
124-
return true;
125-
}
126-
thread::sleep(Duration::from_millis(SLEEP_MS));
127-
}
128-
false
129-
}
130-
131-
fn confirm_mtime_older_than(path: &str, baseline: SystemTime) -> bool {
132-
fs::metadata(path)
133-
.ok()
134-
.and_then(|meta| meta.modified().ok())
135-
.map(|mtime| mtime < baseline)
136-
.unwrap_or(false)
137-
}

cli/tests/cli/append/option_older_ctime_than.rs

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
use crate::utils::{archive, setup};
1+
use crate::utils::{
2+
archive, setup,
3+
time::{confirm_time_older_than, wait_until_time_newer_than},
4+
};
25
use clap::Parser;
36
use portable_network_archive::cli;
4-
use std::{
5-
collections::HashSet,
6-
fs, thread,
7-
time::{Duration, SystemTime},
8-
};
7+
use std::{collections::HashSet, fs, thread, time::Duration};
98

109
/// Precondition: Archive already contains `older.txt`. Prepare `reference.txt` and `newer.txt` with
1110
/// strictly ordered creation times (older < reference < newer).
@@ -49,8 +48,8 @@ fn append_with_older_ctime_than() {
4948
thread::sleep(Duration::from_millis(10));
5049
fs::write(&newer_file, "newer content").unwrap();
5150

52-
if !confirm_ctime_older_than(&older_file, reference_ctime)
53-
|| !wait_until_ctime_newer_than(&newer_file, reference_ctime)
51+
if !confirm_time_older_than(&older_file, reference_ctime, |m| m.created().ok())
52+
|| !wait_until_time_newer_than(&newer_file, reference_ctime, |m| m.created().ok())
5453
{
5554
eprintln!(
5655
"Skipping test: unable to establish required creation time ordering on this filesystem"
@@ -98,28 +97,3 @@ fn append_with_older_ctime_than() {
9897
seen.len()
9998
);
10099
}
101-
102-
fn wait_until_ctime_newer_than(path: &str, baseline: SystemTime) -> bool {
103-
const MAX_ATTEMPTS: usize = 500;
104-
const SLEEP_MS: u64 = 10;
105-
for _ in 0..MAX_ATTEMPTS {
106-
if fs::metadata(path)
107-
.ok()
108-
.and_then(|meta| meta.created().ok())
109-
.map(|ctime| ctime > baseline)
110-
.unwrap_or(false)
111-
{
112-
return true;
113-
}
114-
thread::sleep(Duration::from_millis(SLEEP_MS));
115-
}
116-
false
117-
}
118-
119-
fn confirm_ctime_older_than(path: &str, baseline: SystemTime) -> bool {
120-
fs::metadata(path)
121-
.ok()
122-
.and_then(|meta| meta.created().ok())
123-
.map(|ctime| ctime < baseline)
124-
.unwrap_or(false)
125-
}

cli/tests/cli/append/option_older_mtime_than.rs

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
use crate::utils::{archive, setup};
1+
use crate::utils::{
2+
archive, setup,
3+
time::{confirm_time_older_than, wait_until_time_newer_than},
4+
};
25
use clap::Parser;
36
use portable_network_archive::cli;
4-
use std::{
5-
collections::HashSet,
6-
fs, thread,
7-
time::{Duration, SystemTime},
8-
};
7+
use std::{collections::HashSet, fs, thread, time::Duration};
98

109
/// Precondition: Archive contains `older.txt`. Prepare `reference.txt` and `newer.txt` with strictly
1110
/// increasing modification times.
@@ -42,8 +41,8 @@ fn append_with_older_mtime_than() {
4241
thread::sleep(Duration::from_millis(10));
4342
fs::write(&newer_file, "newer mtime content").unwrap();
4443

45-
if !confirm_mtime_older_than(&older_file, reference_mtime)
46-
|| !wait_until_mtime_newer_than(&newer_file, reference_mtime)
44+
if !confirm_time_older_than(&older_file, reference_mtime, |m| m.modified().ok())
45+
|| !wait_until_time_newer_than(&newer_file, reference_mtime, |m| m.modified().ok())
4746
{
4847
eprintln!(
4948
"Skipping test: unable to establish required modification time ordering on this filesystem"
@@ -91,28 +90,3 @@ fn append_with_older_mtime_than() {
9190
seen.len()
9291
);
9392
}
94-
95-
fn wait_until_mtime_newer_than(path: &str, baseline: SystemTime) -> bool {
96-
const MAX_ATTEMPTS: usize = 500;
97-
const SLEEP_MS: u64 = 10;
98-
for _ in 0..MAX_ATTEMPTS {
99-
if fs::metadata(path)
100-
.ok()
101-
.and_then(|meta| meta.modified().ok())
102-
.map(|mtime| mtime > baseline)
103-
.unwrap_or(false)
104-
{
105-
return true;
106-
}
107-
thread::sleep(Duration::from_millis(SLEEP_MS));
108-
}
109-
false
110-
}
111-
112-
fn confirm_mtime_older_than(path: &str, baseline: SystemTime) -> bool {
113-
fs::metadata(path)
114-
.ok()
115-
.and_then(|meta| meta.modified().ok())
116-
.map(|mtime| mtime < baseline)
117-
.unwrap_or(false)
118-
}

cli/tests/cli/create/option_newer_ctime_than.rs

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
use crate::utils::{archive, setup};
1+
use crate::utils::{
2+
archive, setup,
3+
time::{confirm_time_older_than, wait_until_time_newer_than},
4+
};
25
use clap::Parser;
36
use portable_network_archive::cli;
4-
use std::{
5-
collections::HashSet,
6-
fs, thread,
7-
time::{Duration, SystemTime},
8-
};
7+
use std::{collections::HashSet, fs, thread, time::Duration};
98

109
/// Precondition: Create three files with different creation times (reference, older, newer).
1110
/// Action: Run `pna create` with `--newer-ctime-than reference.txt`, specifying all three files.
@@ -40,8 +39,8 @@ fn create_with_newer_ctime_than() {
4039

4140
// Create the newer file
4241
fs::write(newer_file, "newer file content").unwrap();
43-
if !wait_until_ctime_newer_than(newer_file, reference_ctime)
44-
|| !confirm_ctime_older_than(older_file, reference_ctime)
42+
if !wait_until_time_newer_than(newer_file, reference_ctime, |m| m.created().ok())
43+
|| !confirm_time_older_than(older_file, reference_ctime, |m| m.created().ok())
4544
{
4645
eprintln!("Skipping test: unable to produce distinct creation times on this filesystem");
4746
return;
@@ -98,28 +97,3 @@ fn create_with_newer_ctime_than() {
9897
seen.len()
9998
);
10099
}
101-
102-
fn wait_until_ctime_newer_than(path: &str, baseline: SystemTime) -> bool {
103-
const MAX_ATTEMPTS: usize = 500;
104-
const SLEEP_MS: u64 = 10;
105-
for _ in 0..MAX_ATTEMPTS {
106-
if fs::metadata(path)
107-
.ok()
108-
.and_then(|meta| meta.created().ok())
109-
.map(|ctime| ctime > baseline)
110-
.unwrap_or(false)
111-
{
112-
return true;
113-
}
114-
thread::sleep(Duration::from_millis(SLEEP_MS));
115-
}
116-
false
117-
}
118-
119-
fn confirm_ctime_older_than(path: &str, baseline: SystemTime) -> bool {
120-
fs::metadata(path)
121-
.ok()
122-
.and_then(|meta| meta.created().ok())
123-
.map(|ctime| ctime < baseline)
124-
.unwrap_or(false)
125-
}

cli/tests/cli/create/option_newer_mtime_than.rs

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
use crate::utils::{archive, setup};
1+
use crate::utils::{archive, setup, time::ensure_mtime_order};
22
use clap::Parser;
33
use portable_network_archive::cli;
4-
use std::{
5-
collections::HashSet,
6-
fs, thread,
7-
time::{Duration, SystemTime},
8-
};
4+
use std::{collections::HashSet, fs, thread, time::Duration};
95

106
/// Precondition: Create three files with different modification times (reference, older, newer).
117
/// Action: Run `pna create` with `--newer-mtime-than reference.txt`, specifying all three files.
@@ -89,35 +85,3 @@ fn create_with_newer_mtime_than() {
8985
seen.len()
9086
);
9187
}
92-
93-
fn ensure_mtime_order(older: &str, newer: &str, reference: SystemTime) -> bool {
94-
if !confirm_mtime_older_than(older, reference) {
95-
return false;
96-
}
97-
wait_until_mtime_newer_than(newer, reference)
98-
}
99-
100-
fn wait_until_mtime_newer_than(path: &str, baseline: SystemTime) -> bool {
101-
const MAX_ATTEMPTS: usize = 500;
102-
const SLEEP_MS: u64 = 10;
103-
for _ in 0..MAX_ATTEMPTS {
104-
if fs::metadata(path)
105-
.ok()
106-
.and_then(|meta| meta.modified().ok())
107-
.map(|mtime| mtime > baseline)
108-
.unwrap_or(false)
109-
{
110-
return true;
111-
}
112-
thread::sleep(Duration::from_millis(SLEEP_MS));
113-
}
114-
false
115-
}
116-
117-
fn confirm_mtime_older_than(path: &str, baseline: SystemTime) -> bool {
118-
fs::metadata(path)
119-
.ok()
120-
.and_then(|meta| meta.modified().ok())
121-
.map(|mtime| mtime < baseline)
122-
.unwrap_or(false)
123-
}

cli/tests/cli/create/option_older_ctime_than.rs

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
use crate::utils::{archive, setup};
1+
use crate::utils::{archive, setup, time::ensure_ctime_order};
22
use clap::Parser;
33
use portable_network_archive::cli;
4-
use std::{
5-
collections::HashSet,
6-
fs, thread,
7-
time::{Duration, SystemTime},
8-
};
4+
use std::{collections::HashSet, fs, thread, time::Duration};
95

106
/// Precondition: Create three files (older, reference, newer) with strictly increasing ctime.
117
/// Action: Run `pna create` with `--older-ctime-than reference.txt`, specifying all three files.
@@ -82,35 +78,3 @@ fn create_with_older_ctime_than() {
8278
seen.len()
8379
);
8480
}
85-
86-
fn ensure_ctime_order(older: &str, newer: &str, reference_ctime: SystemTime) -> bool {
87-
if !confirm_ctime_older_than(older, reference_ctime) {
88-
return false;
89-
}
90-
wait_until_ctime_newer_than(newer, reference_ctime)
91-
}
92-
93-
fn wait_until_ctime_newer_than(path: &str, baseline: SystemTime) -> bool {
94-
const MAX_ATTEMPTS: usize = 500;
95-
const SLEEP_MS: u64 = 10;
96-
for _ in 0..MAX_ATTEMPTS {
97-
if fs::metadata(path)
98-
.ok()
99-
.and_then(|meta| meta.created().ok())
100-
.map(|ctime| ctime > baseline)
101-
.unwrap_or(false)
102-
{
103-
return true;
104-
}
105-
thread::sleep(Duration::from_millis(SLEEP_MS));
106-
}
107-
false
108-
}
109-
110-
fn confirm_ctime_older_than(path: &str, baseline: SystemTime) -> bool {
111-
fs::metadata(path)
112-
.ok()
113-
.and_then(|meta| meta.created().ok())
114-
.map(|ctime| ctime < baseline)
115-
.unwrap_or(false)
116-
}

0 commit comments

Comments
 (0)