Author:
Hash:
Timestamp:
+175 -176 +/-8 browse
Kevin Schoon [me@kevinschoon.com]
cab2840cfbd527ef55d831a80bd113b7f26e65ea
Wed, 27 Aug 2025 13:27:48 +0000 (2 months ago)
| 1 | diff --git a/Cargo.lock b/Cargo.lock |
| 2 | index 59ea932..9272d64 100644 |
| 3 | --- a/Cargo.lock |
| 4 | +++ b/Cargo.lock |
| 5 | @@ -303,6 +303,7 @@ dependencies = [ |
| 6 | "rustc_version", |
| 7 | "serde", |
| 8 | "serde_json", |
| 9 | + "tempfile", |
| 10 | "thiserror 2.0.12", |
| 11 | "time", |
| 12 | "timeutil", |
| 13 | @@ -384,6 +385,7 @@ dependencies = [ |
| 14 | "git2", |
| 15 | "rand", |
| 16 | "serde", |
| 17 | + "tempfile", |
| 18 | "tokio", |
| 19 | "tracing", |
| 20 | ] |
| 21 | @@ -2500,15 +2502,15 @@ dependencies = [ |
| 22 | |
| 23 | [[package]] |
| 24 | name = "tempfile" |
| 25 | - version = "3.20.0" |
| 26 | + version = "3.21.0" |
| 27 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 28 | - checksum = "e8a64e3985349f2441a1a9ef0b853f869006c3855f2cda6862a94d26ebb9d6a1" |
| 29 | + checksum = "15b61f8f20e3a6f7e0649d825294eaf317edce30f82cf6026e7e4cb9222a7d1e" |
| 30 | dependencies = [ |
| 31 | "fastrand", |
| 32 | "getrandom 0.3.3", |
| 33 | "once_cell", |
| 34 | "rustix", |
| 35 | - "windows-sys 0.59.0", |
| 36 | + "windows-sys 0.60.2", |
| 37 | ] |
| 38 | |
| 39 | [[package]] |
| 40 | diff --git a/Cargo.toml b/Cargo.toml |
| 41 | index 8f2214d..5cf4932 100644 |
| 42 | --- a/Cargo.toml |
| 43 | +++ b/Cargo.toml |
| 44 | @@ -38,3 +38,4 @@ url = { version = "2.5.4", features = ["serde"]} |
| 45 | tokio = { version = "1.46.1", features = ["full"] } |
| 46 | tokio-util = { version = "0.7.15", features = ["io", "compat"] } |
| 47 | tokio-stream = "0.1.17" |
| 48 | + tempfile = "3.21.0" |
| 49 | diff --git a/ayllu/Cargo.toml b/ayllu/Cargo.toml |
| 50 | index 56139eb..033fa3b 100644 |
| 51 | --- a/ayllu/Cargo.toml |
| 52 | +++ b/ayllu/Cargo.toml |
| 53 | @@ -53,6 +53,9 @@ askama = { version = "0.14.0" } |
| 54 | openssh-keys = "0.6.4" |
| 55 | nix = { version = "0.30.1", default-features = false, features = ["user"] } |
| 56 | |
| 57 | + [dev-dependencies] |
| 58 | + tempfile = {workspace = true} |
| 59 | + |
| 60 | [build-dependencies] |
| 61 | rustc_version = "0.4.1" |
| 62 | git2 = { workspace = true } |
| 63 | diff --git a/ayllu/src/web2/routes/rss.rs b/ayllu/src/web2/routes/rss.rs |
| 64 | index d494588..81fc8ae 100644 |
| 65 | --- a/ayllu/src/web2/routes/rss.rs |
| 66 | +++ b/ayllu/src/web2/routes/rss.rs |
| 67 | @@ -465,25 +465,30 @@ mod tests { |
| 68 | |
| 69 | #[test] |
| 70 | fn test_firehose_commits() { |
| 71 | - let mut test_repo = testing::Builder::default().with_commands(vec![ |
| 72 | - // an old commit to be filtered out |
| 73 | - format!( |
| 74 | + let test_dir = tempfile::tempdir().unwrap(); |
| 75 | + let test_repo_path = testing::init( |
| 76 | + test_dir.path(), |
| 77 | + false, |
| 78 | + &[ |
| 79 | + // an old commit to be filtered out |
| 80 | + format!( |
| 81 | "echo 'content' > file_1.txt && git add file_1.txt && {} git commit -m 'commit 1'", |
| 82 | testing::timestamp_envs("Tue, 14 Dec 2023 20:55:10 +0000") |
| 83 | ) |
| 84 | - .as_str(), |
| 85 | - format!( |
| 86 | + .as_str(), |
| 87 | + format!( |
| 88 | "echo 'content' > file_2.txt && git add file_2.txt && {} git commit -m 'commit 2'", |
| 89 | testing::timestamp_envs("Tue, 19 Dec 2023 20:00:00 +0000") |
| 90 | ) |
| 91 | - .as_str(), |
| 92 | - format!( |
| 93 | + .as_str(), |
| 94 | + format!( |
| 95 | "echo 'content' > file_3.txt && git add file_3.txt && {} git commit -m 'commit 3'", |
| 96 | testing::timestamp_envs("Tue, 19 Dec 2023 20:01:00 +0000") |
| 97 | ) |
| 98 | - .as_str(), |
| 99 | - ]); |
| 100 | - let (name, path) = test_repo.build().expect("failed to init repo"); |
| 101 | + .as_str(), |
| 102 | + ], |
| 103 | + ) |
| 104 | + .unwrap(); |
| 105 | let builder = Builder { |
| 106 | origin: String::from("localhost:8080"), |
| 107 | title: String::from("test"), |
| 108 | @@ -491,7 +496,10 @@ mod tests { |
| 109 | current_time: OffsetDateTime::parse(CURRENT_TIME, &Rfc2822).unwrap(), |
| 110 | }; |
| 111 | let channel = builder |
| 112 | - .firehose(vec![(path, name)], Duration::days(1)) |
| 113 | + .firehose( |
| 114 | + vec![(test_repo_path, "test".to_string())], |
| 115 | + Duration::days(1), |
| 116 | + ) |
| 117 | .expect("failed to build items"); |
| 118 | assert!(channel.items.len() == 2); |
| 119 | assert!(channel.items[0] |
| 120 | @@ -502,30 +510,34 @@ mod tests { |
| 121 | .title |
| 122 | .as_ref() |
| 123 | .is_some_and(|title| title == "Commit: commit 2")); |
| 124 | - test_repo.cleanup().expect("failed to cleanup repo"); |
| 125 | } |
| 126 | |
| 127 | #[test] |
| 128 | fn test_firehose_releases() { |
| 129 | - let mut test_repo = testing::Builder::default().with_commands(vec![ |
| 130 | - format!( |
| 131 | + let test_dir = tempfile::tempdir().unwrap(); |
| 132 | + let test_repo_path = testing::init( |
| 133 | + test_dir.path(), |
| 134 | + false, |
| 135 | + &[ |
| 136 | + format!( |
| 137 | "echo 'content' > file_1.txt && git add file_1.txt && {} git commit -m 'commit 1'", |
| 138 | testing::timestamp_envs("Tue Dec 19 20:00:00 2023 +0000") |
| 139 | ) |
| 140 | - .as_str(), |
| 141 | - format!( |
| 142 | + .as_str(), |
| 143 | + format!( |
| 144 | "echo 'content' > file_2.txt && git add file_2.txt && {} git commit -m 'commit 2'", |
| 145 | testing::timestamp_envs("Tue Dec 19 20:01:00 2023 +0000") |
| 146 | ) |
| 147 | - .as_str(), |
| 148 | - // release a new version |
| 149 | - format!( |
| 150 | - "{} git tag -m 'release version 0.0.1!' v0.0.1", |
| 151 | - testing::timestamp_envs("Tue Dec 19 20:02:00 2023 +0000") |
| 152 | - ) |
| 153 | - .as_str(), |
| 154 | - ]); |
| 155 | - let (name, path) = test_repo.build().expect("failed to init repo"); |
| 156 | + .as_str(), |
| 157 | + // release a new version |
| 158 | + format!( |
| 159 | + "{} git tag -m 'release version 0.0.1!' v0.0.1", |
| 160 | + testing::timestamp_envs("Tue Dec 19 20:02:00 2023 +0000") |
| 161 | + ) |
| 162 | + .as_str(), |
| 163 | + ], |
| 164 | + ) |
| 165 | + .unwrap(); |
| 166 | let builder = Builder { |
| 167 | origin: String::from("localhost:8080"), |
| 168 | title: String::from("test"), |
| 169 | @@ -533,7 +545,10 @@ mod tests { |
| 170 | current_time: OffsetDateTime::parse(CURRENT_TIME, &Rfc2822).unwrap(), |
| 171 | }; |
| 172 | let channel = builder |
| 173 | - .firehose(vec![(path, name)], Duration::days(1)) |
| 174 | + .firehose( |
| 175 | + vec![(test_repo_path.to_path_buf(), "test".to_string())], |
| 176 | + Duration::days(1), |
| 177 | + ) |
| 178 | .expect("failed to build items"); |
| 179 | assert!(channel.items.len() == 3); |
| 180 | assert!(channel.items[0] |
| 181 | @@ -555,30 +570,34 @@ mod tests { |
| 182 | assert!(channel.items[0].guid.is_some()); |
| 183 | // FIXME: assert!(channel.items[1].guid.is_some()); |
| 184 | assert!(channel.items[2].guid.is_some()); |
| 185 | - test_repo.cleanup().expect("failed to cleanup repo"); |
| 186 | } |
| 187 | |
| 188 | #[test] |
| 189 | fn test_feed_1d() { |
| 190 | - let mut test_repo = testing::Builder::default().with_commands(vec![ |
| 191 | - // older commit which is filtered |
| 192 | - format!( |
| 193 | + let test_dir = tempfile::tempdir().unwrap(); |
| 194 | + let test_repo_path = testing::init( |
| 195 | + test_dir.path(), |
| 196 | + false, |
| 197 | + &[ |
| 198 | + // older commit which is filtered |
| 199 | + format!( |
| 200 | "echo 'content' > file_1.txt && git add file_1.txt && {} git commit -m 'commit 1'", |
| 201 | testing::timestamp_envs("Tue Dec 16 20:00:00 2023 +0000") |
| 202 | ) |
| 203 | - .as_str(), |
| 204 | - format!( |
| 205 | + .as_str(), |
| 206 | + format!( |
| 207 | "echo 'content' > file_2.txt && git add file_2.txt && {} git commit -m 'commit 2'", |
| 208 | testing::timestamp_envs("Tue Dec 18 20:01:00 2023 +0000") |
| 209 | ) |
| 210 | - .as_str(), |
| 211 | - format!( |
| 212 | + .as_str(), |
| 213 | + format!( |
| 214 | "echo 'content' > file_3.txt && git add file_3.txt && {} git commit -m 'commit 3'", |
| 215 | testing::timestamp_envs("Tue Dec 18 20:02:00 2023 +0000") |
| 216 | ) |
| 217 | - .as_str(), |
| 218 | - ]); |
| 219 | - let (name, path) = test_repo.build().expect("failed to init repo"); |
| 220 | + .as_str(), |
| 221 | + ], |
| 222 | + ) |
| 223 | + .unwrap(); |
| 224 | let builder = Builder { |
| 225 | origin: String::from("localhost:8080"), |
| 226 | title: String::from("test"), |
| 227 | @@ -586,7 +605,7 @@ mod tests { |
| 228 | current_time: OffsetDateTime::parse(CURRENT_TIME, &Rfc2822).unwrap(), |
| 229 | }; |
| 230 | let channel = builder |
| 231 | - .summary(vec![(path, name)], Timeframe::Daily) |
| 232 | + .summary(vec![(test_repo_path, "test".to_string())], Timeframe::Daily) |
| 233 | .expect("failed to build items"); |
| 234 | assert!(channel.items.len() == 1); |
| 235 | assert!(channel.items[0] |
| 236 | @@ -599,19 +618,24 @@ mod tests { |
| 237 | |
| 238 | #[test] |
| 239 | fn test_feed_1w() { |
| 240 | - let mut test_repo = testing::Builder::default().with_commands(vec![ |
| 241 | - format!( |
| 242 | + let test_dir = tempfile::tempdir().unwrap(); |
| 243 | + let test_repo_path = testing::init( |
| 244 | + test_dir.path(), |
| 245 | + false, |
| 246 | + &[ |
| 247 | + format!( |
| 248 | "echo 'content' > file_1.txt && git add file_1.txt && {} git commit -m 'commit 1'", |
| 249 | testing::timestamp_envs("Tue Dec 15 20:00:00 2023 +0000") |
| 250 | ) |
| 251 | - .as_str(), |
| 252 | - format!( |
| 253 | + .as_str(), |
| 254 | + format!( |
| 255 | "echo 'content' > file_2.txt && git add file_2.txt && {} git commit -m 'commit 2'", |
| 256 | testing::timestamp_envs("Tue Dec 16 20:01:00 2023 +0000") |
| 257 | ) |
| 258 | - .as_str(), |
| 259 | - ]); |
| 260 | - let (name, path) = test_repo.build().expect("failed to init repo"); |
| 261 | + .as_str(), |
| 262 | + ], |
| 263 | + ) |
| 264 | + .unwrap(); |
| 265 | let builder = Builder { |
| 266 | origin: String::from("localhost:8080"), |
| 267 | title: String::from("test"), |
| 268 | @@ -619,7 +643,10 @@ mod tests { |
| 269 | current_time: OffsetDateTime::parse(CURRENT_TIME, &Rfc2822).unwrap(), |
| 270 | }; |
| 271 | let channel = builder |
| 272 | - .summary(vec![(path, name)], Timeframe::Weekly) |
| 273 | + .summary( |
| 274 | + vec![(test_repo_path, "test".to_string())], |
| 275 | + Timeframe::Weekly, |
| 276 | + ) |
| 277 | .expect("failed to build items"); |
| 278 | assert!(channel.items.len() == 1); |
| 279 | assert!(channel.items[0] |
| 280 | @@ -632,25 +659,30 @@ mod tests { |
| 281 | |
| 282 | #[test] |
| 283 | fn test_feed_1m() { |
| 284 | - let mut test_repo = testing::Builder::default().with_commands(vec![ |
| 285 | - format!( |
| 286 | + let test_dir = tempfile::tempdir().unwrap(); |
| 287 | + let test_repo_path = testing::init( |
| 288 | + test_dir.path(), |
| 289 | + false, |
| 290 | + &[ |
| 291 | + format!( |
| 292 | "echo 'content' > file_1.txt && git add file_1.txt && {} git commit -m 'commit 1'", |
| 293 | testing::timestamp_envs("Tue Nov 23 00:01:00 2023 +0000") |
| 294 | ) |
| 295 | - .as_str(), |
| 296 | - format!( |
| 297 | + .as_str(), |
| 298 | + format!( |
| 299 | "echo 'content' > file_2.txt && git add file_2.txt && {} git commit -m 'commit 2'", |
| 300 | testing::timestamp_envs("Tue Nov 23 00:01:00 2023 +0000") |
| 301 | ) |
| 302 | - .as_str(), |
| 303 | - // a recent commit to be filtered out |
| 304 | - format!( |
| 305 | + .as_str(), |
| 306 | + // a recent commit to be filtered out |
| 307 | + format!( |
| 308 | "echo 'content' > file_3.txt && git add file_3.txt && {} git commit -m 'commit 3'", |
| 309 | testing::timestamp_envs("Tue Dec 18 00:01:00 2023 +0000") |
| 310 | ) |
| 311 | - .as_str(), |
| 312 | - ]); |
| 313 | - let (name, path) = test_repo.build().expect("failed to init repo"); |
| 314 | + .as_str(), |
| 315 | + ], |
| 316 | + ) |
| 317 | + .unwrap(); |
| 318 | let builder = Builder { |
| 319 | origin: String::from("localhost:8080"), |
| 320 | title: String::from("test"), |
| 321 | @@ -658,7 +690,10 @@ mod tests { |
| 322 | current_time: OffsetDateTime::parse(CURRENT_TIME, &Rfc2822).unwrap(), |
| 323 | }; |
| 324 | let channel = builder |
| 325 | - .summary(vec![(path, name)], Timeframe::Monthly) |
| 326 | + .summary( |
| 327 | + vec![(test_repo_path, "test".to_string())], |
| 328 | + Timeframe::Monthly, |
| 329 | + ) |
| 330 | .expect("failed to build items"); |
| 331 | assert!(channel.items.len() == 1); |
| 332 | assert!(channel.items[0] |
| 333 | @@ -671,15 +706,20 @@ mod tests { |
| 334 | |
| 335 | #[test] |
| 336 | fn test_feed_1d_no_commits() { |
| 337 | - let mut test_repo = testing::Builder::default().with_commands(vec![ |
| 338 | - // older commit which is filtered |
| 339 | - format!( |
| 340 | + let test_dir = tempfile::tempdir().unwrap(); |
| 341 | + let test_repo_path = testing::init( |
| 342 | + test_dir.path(), |
| 343 | + false, |
| 344 | + &[ |
| 345 | + // older commit which is filtered |
| 346 | + format!( |
| 347 | "echo 'content' > file_1.txt && git add file_1.txt && {} git commit -m 'commit 1'", |
| 348 | testing::timestamp_envs("Tue Dec 16 20:00:00 2023 +0000") |
| 349 | ) |
| 350 | - .as_str(), |
| 351 | - ]); |
| 352 | - let (name, path) = test_repo.build().expect("failed to init repo"); |
| 353 | + .as_str(), |
| 354 | + ], |
| 355 | + ) |
| 356 | + .unwrap(); |
| 357 | let builder = Builder { |
| 358 | origin: String::from("localhost:8080"), |
| 359 | title: String::from("test"), |
| 360 | @@ -687,7 +727,7 @@ mod tests { |
| 361 | current_time: OffsetDateTime::parse(CURRENT_TIME, &Rfc2822).unwrap(), |
| 362 | }; |
| 363 | let channel = builder |
| 364 | - .summary(vec![(path, name)], Timeframe::Daily) |
| 365 | + .summary(vec![(test_repo_path, "test".to_string())], Timeframe::Daily) |
| 366 | .expect("failed to build items"); |
| 367 | assert!(channel.items.is_empty()); |
| 368 | assert!(channel.ttl.as_ref().is_some_and(|ttl| ttl == "60")) |
| 369 | diff --git a/crates/git/Cargo.toml b/crates/git/Cargo.toml |
| 370 | index c5d3aa2..963fb1e 100644 |
| 371 | --- a/crates/git/Cargo.toml |
| 372 | +++ b/crates/git/Cargo.toml |
| 373 | @@ -10,3 +10,6 @@ rand = { workspace = true } |
| 374 | serde = { workspace = true } |
| 375 | tokio = { workspace = true } |
| 376 | tracing = { workspace = true } |
| 377 | + |
| 378 | + [dev-dependencies] |
| 379 | + tempfile = { workspace = true } |
| 380 | diff --git a/crates/git/src/scanner.rs b/crates/git/src/scanner.rs |
| 381 | index af6193e..1ae9b21 100644 |
| 382 | --- a/crates/git/src/scanner.rs |
| 383 | +++ b/crates/git/src/scanner.rs |
| 384 | @@ -106,17 +106,15 @@ mod tests { |
| 385 | |
| 386 | #[test] |
| 387 | fn test_detect_git_repo() { |
| 388 | - let mut test_repo = testing::Builder::default(); |
| 389 | - let repo_path = test_repo.build().expect("failed to init test repo").1; |
| 390 | - assert!(git_dir(&repo_path).expect("failed to run")); |
| 391 | - test_repo.cleanup().expect("failed to clean up test repo") |
| 392 | + let test_dir = tempfile::tempdir().unwrap(); |
| 393 | + let test_repo_path = testing::init(test_dir.path(), false, &[]).unwrap(); |
| 394 | + assert!(git_dir(&test_repo_path).expect("failed to run")); |
| 395 | } |
| 396 | |
| 397 | #[test] |
| 398 | fn test_detect_bare_git_repo() { |
| 399 | - let mut test_repo = testing::Builder::default().bare(true); |
| 400 | - let repo_path = test_repo.build().expect("failed to init test repo").1; |
| 401 | - assert!(git_dir(&repo_path).expect("failed to run")); |
| 402 | - test_repo.cleanup().expect("failed to clean up test repo") |
| 403 | + let test_dir = tempfile::tempdir().unwrap(); |
| 404 | + let test_repo_path = testing::init(test_dir.path(), true, &[]).unwrap(); |
| 405 | + assert!(git_dir(&test_repo_path).expect("failed to run")); |
| 406 | } |
| 407 | } |
| 408 | diff --git a/crates/git/src/testing.rs b/crates/git/src/testing.rs |
| 409 | index a3b5110..fe2678e 100644 |
| 410 | --- a/crates/git/src/testing.rs |
| 411 | +++ b/crates/git/src/testing.rs |
| 412 | @@ -1,9 +1,8 @@ |
| 413 | - use std::fs; |
| 414 | - use std::io::Error; |
| 415 | use std::path::PathBuf; |
| 416 | use std::process::Command; |
| 417 | + use std::{fs, path::Path}; |
| 418 | |
| 419 | - use rand::{distr::Alphanumeric, Rng}; |
| 420 | + use crate::Wrapper; |
| 421 | |
| 422 | const DEFAULT_GIT_CONFIG: &str = r#" |
| 423 | [user] |
| 424 | @@ -19,75 +18,21 @@ pub fn timestamp_envs(timestamp: &str) -> String { |
| 425 | format!("GIT_COMMITTER_DATE='{timestamp}' GIT_AUTHOR_DATE='{timestamp}'",) |
| 426 | } |
| 427 | |
| 428 | - /// Helper struct that will setup a Git repository and then run a series of |
| 429 | - /// commands in that repository. This is only useful for testing. |
| 430 | - #[derive(Default, Debug)] |
| 431 | - pub struct Builder { |
| 432 | - bare: bool, |
| 433 | - commands: Vec<String>, |
| 434 | - repo_path: Option<PathBuf>, |
| 435 | - base_path: Option<PathBuf>, |
| 436 | - } |
| 437 | - |
| 438 | - impl Builder { |
| 439 | - pub fn bare(mut self, bare: bool) -> Self { |
| 440 | - self.bare = bare; |
| 441 | - self |
| 442 | - } |
| 443 | - |
| 444 | - pub fn with_commands(mut self, commands: Vec<&str>) -> Self { |
| 445 | - self.commands = commands.iter().map(|cmd| cmd.to_string()).collect(); |
| 446 | - self |
| 447 | - } |
| 448 | - |
| 449 | - pub fn with_basepath(mut self, base_path: PathBuf) -> Self { |
| 450 | - self.base_path = Some(base_path.clone()); |
| 451 | - self |
| 452 | - } |
| 453 | - |
| 454 | - pub fn cleanup(self) -> Result<(), Error> { |
| 455 | - match self.repo_path { |
| 456 | - Some(path) => fs::remove_dir_all(path), |
| 457 | - None => Ok(()), |
| 458 | - } |
| 459 | - } |
| 460 | - |
| 461 | - pub fn build(&mut self) -> Result<(String, PathBuf), Error> { |
| 462 | - let rand_id: String = rand::rng() |
| 463 | - .sample_iter(&Alphanumeric) |
| 464 | - .take(12) |
| 465 | - .map(char::from) |
| 466 | - .collect(); |
| 467 | - let unique_name = format!("test-repo-{rand_id}"); |
| 468 | - let base_path = if let Some(base_path) = self.base_path.as_ref() { |
| 469 | - base_path.join(&unique_name) |
| 470 | - } else { |
| 471 | - let base_path = PathBuf::from(".").canonicalize()?; |
| 472 | - base_path.join("test").join(&unique_name) |
| 473 | - }; |
| 474 | - let source_path = base_path.join("src"); |
| 475 | - fs::create_dir_all(&source_path)?; |
| 476 | - let git_config_path = base_path.join("gitconfig"); |
| 477 | - fs::write(&git_config_path, DEFAULT_GIT_CONFIG)?; |
| 478 | - let git_config_path = git_config_path.to_str().unwrap(); |
| 479 | - let args = if self.bare { |
| 480 | - ["init", "--bare"].to_vec() |
| 481 | - } else { |
| 482 | - ["init"].to_vec() |
| 483 | - }; |
| 484 | - self.repo_path = Some(source_path.clone()); |
| 485 | - Command::new("git") |
| 486 | - .args(args) |
| 487 | + /// Initialize a test repository running all of the commands provided within |
| 488 | + pub fn init(base_dir: &Path, bare: bool, commands: &[&str]) -> Result<PathBuf, std::io::Error> { |
| 489 | + let source_path = base_dir.join("src"); |
| 490 | + let git_config_path = base_dir.join("gitconfig"); |
| 491 | + let git_config_path = git_config_path.as_path(); |
| 492 | + fs::write(git_config_path, DEFAULT_GIT_CONFIG)?; |
| 493 | + let repository = |
| 494 | + Wrapper::create(source_path.as_path(), bare).expect("Could not initialize repository"); |
| 495 | + commands.iter().try_for_each(|command| { |
| 496 | + Command::new("/bin/sh") |
| 497 | + .args(vec!["-c", command]) |
| 498 | .env("GIT_CONFIG_GLOBAL", git_config_path) |
| 499 | - .current_dir(&source_path) |
| 500 | + .current_dir(repository.path()) |
| 501 | .output()?; |
| 502 | - for command in self.commands.iter() { |
| 503 | - Command::new("/bin/sh") |
| 504 | - .args(vec!["-c", command.as_str()]) |
| 505 | - .env("GIT_CONFIG_GLOBAL", git_config_path) |
| 506 | - .current_dir(&source_path) |
| 507 | - .output()?; |
| 508 | - } |
| 509 | - Ok((unique_name, source_path)) |
| 510 | - } |
| 511 | + Ok::<(), std::io::Error>(()) |
| 512 | + })?; |
| 513 | + Ok(source_path) |
| 514 | } |
| 515 | diff --git a/crates/git/src/wrapper.rs b/crates/git/src/wrapper.rs |
| 516 | index d3d29e2..eef0b7b 100644 |
| 517 | --- a/crates/git/src/wrapper.rs |
| 518 | +++ b/crates/git/src/wrapper.rs |
| 519 | @@ -1038,31 +1038,34 @@ impl Wrapper { |
| 520 | mod tests { |
| 521 | |
| 522 | use crate::testing; |
| 523 | - use std::fs; |
| 524 | use std::time::{Duration, SystemTime, UNIX_EPOCH}; |
| 525 | |
| 526 | use super::*; |
| 527 | |
| 528 | #[test] |
| 529 | fn test_git_init() { |
| 530 | - let mut test_repo = testing::Builder::default(); |
| 531 | - let repo_path = test_repo.build().expect("failed to init repo").1; |
| 532 | - let repository = Wrapper::new(&repo_path).expect("failed to load repository"); |
| 533 | + let test_dir = tempfile::tempdir().unwrap(); |
| 534 | + let test_repo_path = testing::init(test_dir.path(), false, &[]).unwrap(); |
| 535 | + let repository = Wrapper::new(&test_repo_path).expect("failed to load repository"); |
| 536 | repository.config().expect("failed to load config"); |
| 537 | let is_empty = repository.is_empty().expect("failed to check if empty"); |
| 538 | assert!(is_empty); |
| 539 | - test_repo.cleanup().expect("failed to clean up repository"); |
| 540 | } |
| 541 | |
| 542 | #[test] |
| 543 | fn test_git_tags_range() { |
| 544 | - let mut test_repo = testing::Builder::default().with_commands(vec![ |
| 545 | - "echo 'content' > file_1.txt && git add file_1.txt && git commit -m 'commit 1'", |
| 546 | - "echo 'content' > file_2.txt && git add file_2.txt && git commit -m 'commit 2'", |
| 547 | - "git tag -m 'release version 0.0.1!' v0.0.1", |
| 548 | - ]); |
| 549 | - let repo_path = test_repo.build().expect("failed to init repo").1; |
| 550 | - let repository = Wrapper::new(&repo_path).expect("failed to load repository"); |
| 551 | + let test_dir = tempfile::tempdir().unwrap(); |
| 552 | + let test_repo_path = testing::init( |
| 553 | + test_dir.path(), |
| 554 | + false, |
| 555 | + &[ |
| 556 | + "echo 'content' > file_1.txt && git add file_1.txt && git commit -m 'commit 1'", |
| 557 | + "echo 'content' > file_2.txt && git add file_2.txt && git commit -m 'commit 2'", |
| 558 | + "git tag -m 'release version 0.0.1!' v0.0.1", |
| 559 | + ], |
| 560 | + ) |
| 561 | + .unwrap(); |
| 562 | + let repository = Wrapper::new(&test_repo_path).expect("failed to load repository"); |
| 563 | let end = SystemTime::now() |
| 564 | .duration_since(UNIX_EPOCH) |
| 565 | .unwrap() |
| 566 | @@ -1081,14 +1084,19 @@ mod tests { |
| 567 | |
| 568 | #[test] |
| 569 | fn test_git_file_history() { |
| 570 | - let mut test_repo = testing::Builder::default().with_commands(vec![ |
| 571 | - "echo 'content' > file_1.txt && git add file_1.txt && git commit -m 'commit 1'", |
| 572 | - "echo 'content' > file_2.txt && git add file_2.txt && git commit -m 'commit 2'", |
| 573 | - "echo 'content' > file_3.txt && git add file_3.txt && git commit -m 'commit 3'", |
| 574 | - "echo 'content' > file_4.txt && git add file_4.txt && git commit -m 'commit 4'", |
| 575 | - ]); |
| 576 | - let repo_path = test_repo.build().expect("failed to init repo").1; |
| 577 | - let repository = Wrapper::new(&repo_path).expect("failed to load repository"); |
| 578 | + let test_dir = tempfile::tempdir().unwrap(); |
| 579 | + let test_repo_path = testing::init( |
| 580 | + test_dir.path(), |
| 581 | + false, |
| 582 | + &[ |
| 583 | + "echo 'content' > file_1.txt && git add file_1.txt && git commit -m 'commit 1'", |
| 584 | + "echo 'content' > file_2.txt && git add file_2.txt && git commit -m 'commit 2'", |
| 585 | + "echo 'content' > file_3.txt && git add file_3.txt && git commit -m 'commit 3'", |
| 586 | + "echo 'content' > file_4.txt && git add file_4.txt && git commit -m 'commit 4'", |
| 587 | + ], |
| 588 | + ) |
| 589 | + .unwrap(); |
| 590 | + let repository = Wrapper::new(&test_repo_path).expect("failed to load repository"); |
| 591 | let tree = repository.tree(None, None).expect("failed to load tree"); |
| 592 | let commits = repository |
| 593 | .resolve_tree(None, &tree, None) |
| 594 | @@ -1105,7 +1113,6 @@ mod tests { |
| 595 | let c4 = commits.get(3).unwrap(); |
| 596 | println!("c4: {}", c4.message); |
| 597 | assert!(c4.message.trim_end() == "commit 4"); |
| 598 | - fs::remove_dir_all(repo_path).expect("failed to clean up repository") |
| 599 | } |
| 600 | |
| 601 | #[test] |
| 602 | @@ -1113,13 +1120,13 @@ mod tests { |
| 603 | let timestamp_1 = "Fri Jul 14 02:40:00 AM UTC 2017"; // @1500000000 |
| 604 | let timestamp_2 = "Fri Jul 14 02:40:01 AM UTC 2017"; // @1500000001 |
| 605 | let timestamp_3 = "Sun Sep 13 12:26:40 PM UTC 2020"; // @1600000000 |
| 606 | - let mut test_repo = testing::Builder::default().with_commands(vec![ |
| 607 | + let test_dir = tempfile::tempdir().unwrap(); |
| 608 | + let test_repo_path = testing::init(test_dir.path(), false, &[ |
| 609 | format!("echo 'content' > file_1.txt && git add file_1.txt && GIT_COMMITTER_DATE='{timestamp_1}' GIT_AUTHOR_DATE='{timestamp_1}' git commit -m 'commit 1'").as_str(), |
| 610 | format!("echo 'content' > file_2.txt && git add file_2.txt && GIT_COMMITTER_DATE='{timestamp_2}' GIT_AUTHOR_DATE='{timestamp_2}' git commit -m 'commit 2'").as_str(), |
| 611 | format!("echo 'content' > file_3.txt && git add file_3.txt && GIT_COMMITTER_DATE='{timestamp_3}' GIT_AUTHOR_DATE='{timestamp_3}' git commit -m 'commit 3'").as_str(), |
| 612 | - ]); |
| 613 | - let repo_path = test_repo.build().expect("failed to init repo").1; |
| 614 | - let repository = Wrapper::new(&repo_path).expect("failed to load repository"); |
| 615 | + ]).unwrap(); |
| 616 | + let repository = Wrapper::new(&test_repo_path).expect("failed to load repository"); |
| 617 | let commits = repository |
| 618 | .commits_range(None, Some((1499999999, 1550000000))) |
| 619 | .expect("failed to get range"); |
| 620 | @@ -1133,7 +1140,8 @@ mod tests { |
| 621 | let timestamp_1 = "Fri Jul 14 02:40:00 AM UTC 2017"; // @1500000000 |
| 622 | let timestamp_2 = "Fri Jul 14 02:40:01 AM UTC 2017"; // @1500000001 |
| 623 | let timestamp_3 = "Sun Sep 13 12:26:40 PM UTC 2020"; // @1600000000 |
| 624 | - let mut test_repo = testing::Builder::default().with_commands(vec![ |
| 625 | + let test_dir = tempfile::tempdir().unwrap(); |
| 626 | + let test_repo_path = testing::init(test_dir.path(), false, &[ |
| 627 | format!("echo 'content' > file_1.txt && git add file_1.txt && GIT_COMMITTER_DATE='{timestamp_1}' GIT_AUTHOR_DATE='{timestamp_1}' git commit -m 'commit 1'").as_str(), |
| 628 | // branch within the requested timeframe |
| 629 | "git checkout -b hello-world", |
| 630 | @@ -1148,9 +1156,8 @@ mod tests { |
| 631 | "echo 'content' > file_6.txt && git add file_6.txt && git commit -m 'commit 7'", |
| 632 | // one more branch with no commits on it |
| 633 | "git checkout -b one-more-branch" |
| 634 | - ]); |
| 635 | - let repo_path = test_repo.build().expect("failed to init repo").1; |
| 636 | - let repository = Wrapper::new(&repo_path).expect("failed to load repository"); |
| 637 | + ]).unwrap(); |
| 638 | + let repository = Wrapper::new(&test_repo_path).expect("failed to load repository"); |
| 639 | let branches = repository |
| 640 | .branches_range(Some((1499999999, 1550000000))) |
| 641 | .expect("failed to get range"); |