forge
Explore
Log in

Don't you dare go Hollow

forge

Clone
★ 0

0 watching0 forksRustAGPL-3.07.1 MiB490f099Tahinli7 hours ago322 commits

tests.rs175 lines, 6.1 KiBHistoryRawDownload
1use std::io::Read;
2
3use forge_core::Visibility;
4use forge_core::store::exports::ExportState;
5
6use super::*;
7use crate::test_support::test_state;
8
9/// Reads a `.tar.gz` into `(path, bytes)` pairs.
10fn entries(path: &Path) -> Vec<(String, Vec<u8>)> {
11 let file = std::fs::File::open(path).expect("archive opens");
12 let mut archive = tar::Archive::new(flate2::read::GzDecoder::new(file));
13 let mut out = Vec::new();
14 for entry in archive.entries().expect("entries") {
15 let mut entry = entry.expect("entry");
16 let name = entry.path().expect("path").display().to_string();
17 let mut bytes = Vec::new();
18 entry.read_to_end(&mut bytes).expect("read");
19 out.push((name, bytes));
20 }
21 out
22}
23
24async fn setup() -> (AppState, User, Repo, tempfile::TempDir) {
25 let dir = tempfile::tempdir().expect("tempdir");
26 let state = test_state(dir.path()).await;
27 let user = state
28 .store
29 .create_user("tahinli", Some("tahinli@example.com"), "unused", false)
30 .await
31 .expect("user");
32 let repo = state
33 .store
34 .create_repo(&user.id, "project", "A project", Visibility::Public, "main")
35 .await
36 .expect("repo");
37 let bare = forge_git::repo::repo_path(&state.git.data_dir, &repo.id).expect("path");
38 forge_git::repo::init_bare(&state.git, &bare, "main")
39 .await
40 .expect("init");
41 // One commit so the bundle is not empty.
42 let run = |args: &[&str]| {
43 let out = std::process::Command::new("git")
44 .arg(format!("--git-dir={}", bare.display()))
45 .args(args)
46 .env("GIT_AUTHOR_NAME", "t")
47 .env("GIT_AUTHOR_EMAIL", "t@example.com")
48 .env("GIT_COMMITTER_NAME", "t")
49 .env("GIT_COMMITTER_EMAIL", "t@example.com")
50 .output()
51 .expect("git");
52 assert!(out.status.success(), "{out:?}");
53 String::from_utf8_lossy(&out.stdout).trim().to_string()
54 };
55 let tree = run(&["hash-object", "-t", "tree", "-w", "--stdin"]);
56 let commit = run(&["commit-tree", &tree, "-m", "first"]);
57 run(&["update-ref", "refs/heads/main", &commit]);
58 let issue = state
59 .store
60 .create_issue(&repo.id, &user.id, "It breaks", "steps")
61 .await
62 .expect("issue");
63 state
64 .store
65 .add_comment(&issue.id, &user.id, "same here")
66 .await
67 .expect("comment");
68 (state, user, repo, dir)
69}
70
71#[tokio::test]
72async fn the_archive_holds_the_profile_bundles_issues_and_releases() {
73 let (state, user, _repo, _dir) = setup().await;
74 let export = start(&state, &user).await.expect("start");
75 // Wait for the background build by following its own signal, not by polling.
76 let mut wake = subscribe(&user.id);
77 loop {
78 let row = state.store.data_export(&export.id).await.unwrap().unwrap();
79 if row.state != ExportState::Building {
80 assert_eq!(row.state, ExportState::Ready, "{:?}", row.error);
81 assert!(row.size > 0);
82 break;
83 }
84 tokio::time::timeout(Duration::from_secs(60), wake.changed())
85 .await
86 .expect("the export finishes")
87 .expect("signal open");
88 }
89
90 let files = entries(&archive_path(&state, &export.id));
91 let names: Vec<&str> = files.iter().map(|(n, _)| n.as_str()).collect();
92 let has = |suffix: &str| names.iter().any(|n| n.ends_with(suffix));
93 assert!(has("profile.json"), "{names:?}");
94 assert!(has("repos/project/repo.bundle"), "{names:?}");
95 assert!(has("repos/project/repo.json"));
96 assert!(has("repos/project/issues.json"));
97 assert!(has("repos/project/releases.json"));
98
99 let read = |suffix: &str| {
100 let (_, bytes) = files.iter().find(|(n, _)| n.ends_with(suffix)).unwrap();
101 String::from_utf8_lossy(bytes).into_owned()
102 };
103 let profile = read("profile.json");
104 assert!(profile.contains("tahinli@example.com"));
105 assert!(
106 !profile.contains("password"),
107 "no credential in the profile"
108 );
109 let issues: serde_json::Value = serde_json::from_str(&read("issues.json")).unwrap();
110 assert_eq!(issues[0]["title"], "It breaks");
111 assert_eq!(issues[0]["comments"][0]["body"], "same here");
112 assert_eq!(issues[0]["comments"][0]["author"], "tahinli");
113 // The bundle is a real bundle.
114 let bundle = files
115 .iter()
116 .find(|(n, _)| n.ends_with("repo.bundle"))
117 .unwrap();
118 assert!(bundle.1.starts_with(b"# v2 git bundle") || bundle.1.starts_with(b"# v3 git bundle"));
119}
120
121#[tokio::test]
122async fn an_expired_export_is_removed_with_its_file() {
123 let (state, user, _repo, _dir) = setup().await;
124 let export = state
125 .store
126 .create_export(&user.id, Duration::from_secs(0))
127 .await
128 .expect("export");
129 let path = archive_path(&state, &export.id);
130 tokio::fs::create_dir_all(path.parent().unwrap())
131 .await
132 .unwrap();
133 tokio::fs::write(&path, b"x").await.unwrap();
134 sweep_expired(&state).await;
135 assert!(state.store.data_export(&export.id).await.unwrap().is_none());
136 assert!(!path.exists());
137}
138
139#[tokio::test]
140async fn a_fresh_export_survives_the_sweep() {
141 let (state, user, _repo, _dir) = setup().await;
142 let export = state
143 .store
144 .create_export(&user.id, EXPORT_TTL)
145 .await
146 .unwrap();
147 sweep_expired(&state).await;
148 assert!(state.store.data_export(&export.id).await.unwrap().is_some());
149}
150
151#[tokio::test]
152async fn a_second_request_while_building_returns_the_running_export() {
153 let (state, user, _repo, _dir) = setup().await;
154 let running = state
155 .store
156 .create_export(&user.id, EXPORT_TTL)
157 .await
158 .unwrap();
159 let again = start(&state, &user).await.unwrap();
160 assert_eq!(again.id, running.id);
161}
162
163#[tokio::test]
164async fn ready_mail_is_queued_for_an_account_with_an_address() {
165 let (state, user, _repo, _dir) = setup().await;
166 notify_ready(&state, &user, "abc").await;
167 notify_ready(&state, &user, "abc").await;
168 let queued = state
169 .store
170 .list_mail(None, "9999-12-31T00:00:00Z", 10)
171 .await
172 .expect("mail list")
173 .len();
174 assert_eq!(queued, 1, "one mail, however often the ready step runs");
175}