Skip to content

Commit 8648be2

Browse files
committed
fix test_fork_operations unit test to not have race condition and fail sometimes
1 parent df934a0 commit 8648be2

File tree

1 file changed

+24
-23
lines changed

1 file changed

+24
-23
lines changed

src/lib/src/repositories/fork.rs

+24-23
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,10 @@ pub fn start_fork(
5454
new_path: PathBuf,
5555
) -> Result<ForkStartResponse, OxenError> {
5656
if new_path.exists() {
57-
return Err(OxenError::basic_str(
58-
"A file already exists at the destination path.",
59-
));
57+
return Err(OxenError::basic_str(format!(
58+
"A file already exists at the destination path: {}",
59+
new_path.to_string_lossy()
60+
)));
6061
}
6162

6263
oxen_fs::create_dir_all(&new_path)?;
@@ -184,6 +185,8 @@ fn count_items(path: &Path, status_repo: &Path, current_count: &mut u32) -> Resu
184185
mod tests {
185186
use std::time::Duration;
186187

188+
use uuid::Uuid;
189+
187190
use super::*;
188191
use crate::error::OxenError;
189192
use crate::test;
@@ -193,11 +196,15 @@ mod tests {
193196
test::run_empty_local_repo_test_async(|original_repo| {
194197
async move {
195198
let original_repo_path = original_repo.path;
199+
let forked_repo_path = original_repo_path
200+
.parent()
201+
.unwrap()
202+
.join("forked")
203+
.join(Uuid::new_v4().to_string());
196204

197205
// Fork creates new repo
198-
let new_repo_path = original_repo_path.parent().unwrap().join("forked/new_repo");
199-
if new_repo_path.exists() {
200-
std::fs::remove_dir_all(&new_repo_path)?;
206+
if forked_repo_path.exists() {
207+
std::fs::remove_dir_all(&forked_repo_path)?;
201208
}
202209

203210
let dir_path = original_repo_path.join("dir");
@@ -206,13 +213,11 @@ mod tests {
206213
let file_path = dir_path.join("test_file.txt");
207214
std::fs::write(file_path, "test file content")?;
208215

209-
start_fork(original_repo_path.clone(), new_repo_path.clone())?;
210-
tokio::time::sleep(Duration::from_millis(100)).await; // Wait for 100 milliseconds
211-
let status = get_fork_status(&new_repo_path); // Await the initial call
212-
let mut current_status = status?.status;
216+
start_fork(original_repo_path.clone(), forked_repo_path.clone())?;
217+
let mut current_status = "in_progress".to_string();
213218
while current_status == "in_progress" {
214219
tokio::time::sleep(Duration::from_millis(100)).await; // Wait for 100 milliseconds
215-
current_status = match get_fork_status(&new_repo_path) {
220+
current_status = match get_fork_status(&forked_repo_path) {
216221
Ok(status) => status.status,
217222
Err(e) => {
218223
if let OxenError::ForkStatusNotFound(_) = e {
@@ -225,9 +230,9 @@ mod tests {
225230
}
226231
let file_path = original_repo_path.clone().join("dir/test_file.txt");
227232

228-
assert!(new_repo_path.exists());
233+
assert!(forked_repo_path.exists());
229234
// Verify that the content of .oxen/config.toml is the same in both repos
230-
let new_file_path = new_repo_path.join("dir/test_file.txt");
235+
let new_file_path = forked_repo_path.join("dir/test_file.txt");
231236
let original_content = fs::read_to_string(&file_path)?;
232237
let mut retries = 20;
233238
let mut sleep_time = 100;
@@ -250,15 +255,12 @@ mod tests {
250255
"The content of test_file.txt should be the same in both repositories"
251256
);
252257

253-
if new_repo_path.exists() {
254-
std::fs::remove_dir_all(&new_repo_path)?;
255-
}
256-
257258
// Fork fails if repo exists
258259
let new_repo_path_1 = original_repo_path
259260
.parent()
260261
.unwrap()
261-
.join("forked/new_repo_1");
262+
.join("forked")
263+
.join(Uuid::new_v4().to_string());
262264
if new_repo_path_1.exists() {
263265
std::fs::remove_dir_all(&new_repo_path_1)?;
264266
}
@@ -272,10 +274,10 @@ mod tests {
272274

273275
// Fork excludes workspaces
274276
let new_repo_path_2 = original_repo_path
275-
.clone()
276277
.parent()
277278
.unwrap()
278-
.join("forked/new_repo_2");
279+
.join("forked")
280+
.join(Uuid::new_v4().to_string());
279281

280282
let workspaces_path = original_repo_path.join(".oxen/workspaces");
281283
// Create a workspace directory and add a file to it
@@ -284,11 +286,10 @@ mod tests {
284286
std::fs::write(workspace_file, "test workspace content")?;
285287

286288
start_fork(original_repo_path.clone(), new_repo_path_2.clone())?;
287-
let status = get_fork_status(&new_repo_path_2);
288-
let mut current_status = status?.status;
289+
let mut current_status = "in_progress".to_string();
289290
while current_status == "in_progress" {
290291
tokio::time::sleep(Duration::from_millis(100)).await;
291-
current_status = match get_fork_status(&new_repo_path) {
292+
current_status = match get_fork_status(&new_repo_path_2) {
292293
Ok(status) => status.status,
293294
Err(e) => {
294295
if let OxenError::ForkStatusNotFound(_) = e {

0 commit comments

Comments
 (0)