Skip to content

Commit

Permalink
Avoid using into_path so tempdir is properly cleaned up
Browse files Browse the repository at this point in the history
  • Loading branch information
r-ash committed Mar 20, 2024
1 parent 769ea43 commit b311bcd
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ mod tests {
let initial_branches = git_remote_branches(&test_git.local);
assert_eq!(initial_branches.count(), 2); // HEAD and main

git_fetch(&test_git.dir.join("local")).unwrap();
git_fetch(&test_git.dir.path().join("local")).unwrap();

let post_fetch_ref = git_get_latest_commit(&test_git.local, "refs/remotes/origin/HEAD");
assert_eq!(
Expand Down
13 changes: 5 additions & 8 deletions test-utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use std::fs::File;
use std::io::prelude::*;
use std::path::{Path, PathBuf};

use git2::{Branches, BranchType, Commit, Repository, Signature};
use git2::build::RepoBuilder;
use tempdir::TempDir;

pub struct TestGit {
pub dir: PathBuf,
pub dir: TempDir,
pub remote: Repository,
pub local: Repository,
}
Expand All @@ -17,9 +15,9 @@ pub struct TestGit {
// local - 2 commits, initial, first commit
// So that if we fetch on local then it should know about the second file
pub fn initialise_git_repo(path: Option<&PathBuf>) -> TestGit {
let tmp_dir = TempDir::new("repo").expect("Temp dir created").into_path();
let remote_path = tmp_dir.join("remote");
let local_path = tmp_dir.join("local");
let tmp_dir = TempDir::new("repo").expect("Temp dir created");
let remote_path = tmp_dir.path().join("remote");
let local_path = tmp_dir.path().join("local");
match path {
Some(p) => copy_recursively(p, &remote_path),
None => std::fs::create_dir(&remote_path),
Expand Down Expand Up @@ -72,8 +70,7 @@ pub fn copy_recursively(
}

fn create_file(repo_path: &Path, file_name: &str) {
let mut file = File::create(repo_path.join(file_name)).unwrap();
file.write_all(b"File contents").unwrap();
std::fs::write(repo_path.join(file_name), b"File contents").unwrap();
}

fn create_initial_commit(repo: &Repository) {
Expand Down
2 changes: 1 addition & 1 deletion tests/test_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ async fn request_id_is_logged() {
async fn can_fetch_git() {
let test_dir = get_test_dir();
let test_git = initialise_git_repo(Some(&test_dir));
let mut client = TestClient::new(test_git.dir.join("local"));
let mut client = TestClient::new(test_git.dir.path().join("local"));

let remote_ref = git_get_latest_commit(&test_git.remote, "HEAD");
let initial_ref = git_get_latest_commit(&test_git.local, "refs/remotes/origin/HEAD");
Expand Down

0 comments on commit b311bcd

Please sign in to comment.