diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2024-12-12 15:19:40 +0000 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2024-12-13 12:37:20 +0000 |
| commit | f294a249307accc0f173917989ce27c27f0c6792 (patch) | |
| tree | 04df321a504ff1108f6c06e69a703577051d7dde /test_utils/src/lib.rs | |
| parent | aeb164a6890a433828739bde23d3477b78e532a4 (diff) | |
test: deterministic commit timestamps in test PRs
now that the remote helper uses the commit author timestamp to
determine if commit was applied from a proposal, the timestamps
in the test proposal can no longer be identical.
this makes them deterministically different based on the file name
Diffstat (limited to 'test_utils/src/lib.rs')
| -rw-r--r-- | test_utils/src/lib.rs | 60 |
1 files changed, 52 insertions, 8 deletions
diff --git a/test_utils/src/lib.rs b/test_utils/src/lib.rs index 0c5de73..3ac171b 100644 --- a/test_utils/src/lib.rs +++ b/test_utils/src/lib.rs | |||
| @@ -10,6 +10,7 @@ use anyhow::{bail, ensure, Context, Result}; | |||
| 10 | use dialoguer::theme::{ColorfulTheme, Theme}; | 10 | use dialoguer::theme::{ColorfulTheme, Theme}; |
| 11 | use futures::executor::block_on; | 11 | use futures::executor::block_on; |
| 12 | use git::GitTestRepo; | 12 | use git::GitTestRepo; |
| 13 | use git2::{Signature, Time}; | ||
| 13 | use nostr::{self, nips::nip65::RelayMetadata, Kind, Tag}; | 14 | use nostr::{self, nips::nip65::RelayMetadata, Kind, Tag}; |
| 14 | use nostr_database::NostrEventsDatabase; | 15 | use nostr_database::NostrEventsDatabase; |
| 15 | use nostr_lmdb::NostrLMDB; | 16 | use nostr_lmdb::NostrLMDB; |
| @@ -1226,21 +1227,64 @@ pub fn create_and_populate_branch( | |||
| 1226 | test_repo.checkout("main")?; | 1227 | test_repo.checkout("main")?; |
| 1227 | test_repo.create_branch(branch_name)?; | 1228 | test_repo.create_branch(branch_name)?; |
| 1228 | test_repo.checkout(branch_name)?; | 1229 | test_repo.checkout(branch_name)?; |
| 1229 | std::fs::write( | 1230 | let file_name = format!("{}3.md", prefix); |
| 1230 | test_repo.dir.join(format!("{}3.md", prefix)), | 1231 | std::fs::write(test_repo.dir.join(&file_name), "some content")?; |
| 1231 | "some content", | 1232 | test_repo.stage_and_commit_custom_signature( |
| 1233 | &format!("add {}3.md", prefix), | ||
| 1234 | Some( | ||
| 1235 | &Signature::new( | ||
| 1236 | "Joe Bloggs", | ||
| 1237 | "joe.bloggs@pm.me", | ||
| 1238 | &Time::new(deterministic_timestamp(&file_name), 0), | ||
| 1239 | ) | ||
| 1240 | .unwrap(), | ||
| 1241 | ), | ||
| 1242 | None, | ||
| 1232 | )?; | 1243 | )?; |
| 1233 | test_repo.stage_and_commit(format!("add {}3.md", prefix).as_str())?; | ||
| 1234 | if !only_one_commit { | 1244 | if !only_one_commit { |
| 1235 | std::fs::write( | 1245 | let file_name = format!("{}4.md", prefix); |
| 1236 | test_repo.dir.join(format!("{}4.md", prefix)), | 1246 | std::fs::write(test_repo.dir.join(&file_name), "some content")?; |
| 1237 | "some content", | 1247 | test_repo.stage_and_commit_custom_signature( |
| 1248 | &format!("add {}4.md", prefix), | ||
| 1249 | Some( | ||
| 1250 | &Signature::new( | ||
| 1251 | "Joe Bloggs", | ||
| 1252 | "joe.bloggs@pm.me", | ||
| 1253 | &Time::new(deterministic_timestamp(&file_name), 0), | ||
| 1254 | ) | ||
| 1255 | .unwrap(), | ||
| 1256 | ), | ||
| 1257 | None, | ||
| 1238 | )?; | 1258 | )?; |
| 1239 | test_repo.stage_and_commit(format!("add {}4.md", prefix).as_str())?; | ||
| 1240 | } | 1259 | } |
| 1241 | Ok(()) | 1260 | Ok(()) |
| 1242 | } | 1261 | } |
| 1243 | 1262 | ||
| 1263 | use sha2::{Digest, Sha256}; | ||
| 1264 | |||
| 1265 | fn deterministic_timestamp(input: &str) -> i64 { | ||
| 1266 | // Create a SHA-256 hasher | ||
| 1267 | let mut hasher = Sha256::new(); | ||
| 1268 | |||
| 1269 | // Hash the input string | ||
| 1270 | hasher.update(input); | ||
| 1271 | |||
| 1272 | // Get the hash result | ||
| 1273 | let result = hasher.finalize(); | ||
| 1274 | |||
| 1275 | // Convert the first 8 bytes of the hash to an i64 | ||
| 1276 | let timestamp_bytes = &result[..8]; | ||
| 1277 | let timestamp = i64::from_be_bytes( | ||
| 1278 | timestamp_bytes | ||
| 1279 | .try_into() | ||
| 1280 | .expect("slice with incorrect length"), | ||
| 1281 | ); | ||
| 1282 | |||
| 1283 | // Normalize the timestamp to a valid Unix timestamp | ||
| 1284 | // You can adjust this to fit your needs, e.g., adding a base timestamp | ||
| 1285 | timestamp.abs() % 1_000_000_000 // Keep it within a reasonable range | ||
| 1286 | } | ||
| 1287 | |||
| 1244 | pub fn cli_tester_create_proposal( | 1288 | pub fn cli_tester_create_proposal( |
| 1245 | test_repo: &GitTestRepo, | 1289 | test_repo: &GitTestRepo, |
| 1246 | branch_name: &str, | 1290 | branch_name: &str, |