forge
Explore
Log in

Don't you dare go Hollow

forge

Clone
★ 0

0 watching0 forksRustAGPL-3.07.1 MiB490f099Tahinli7 hours ago322 commits

fixture.rs170 lines, 4.9 KiBHistoryRawDownload
1//! Shared test fixture: a store with one owner, a bare repo built through the git CLI, and a
2//! router with the app context.
3
4use std::path::{Path, PathBuf};
5use std::process::Command;
6
7use forge_core::{Store, Visibility};
8
9use topcoat::router::request::Request;
10use topcoat::router::{Body, Router, to_bytes};
11
12pub const OWNER: &str = "tahinli";
13pub const REPO: &str = "demo";
14
15pub struct Fixture {
16 pub router: Router,
17 pub store: Store,
18 /// The live hub the router's pages share, to bump topics from a test.
19 pub live: std::sync::Arc<crate::live::LiveHub>,
20 /// Root commit: adds `a.txt` and `dir/b.txt`.
21 pub root: String,
22 /// Adds `new.txt` and changes `a.txt`.
23 pub modified: String,
24 /// Working checkout and bare repo the fixture pushed to.
25 pub work: PathBuf,
26 pub bare: PathBuf,
27 _dir: tempfile::TempDir,
28}
29
30pub fn git(dir: &Path, args: &[&str]) -> String {
31 let out = Command::new("git")
32 .args([
33 "-c",
34 "user.name=Tester",
35 "-c",
36 "user.email=tester@example.com",
37 "-c",
38 "commit.gpgsign=false",
39 ])
40 .args(args)
41 .current_dir(dir)
42 .output()
43 .expect("git runs");
44 assert!(
45 out.status.success(),
46 "git {args:?} failed: {}",
47 String::from_utf8_lossy(&out.stderr)
48 );
49 String::from_utf8_lossy(&out.stdout).trim().to_owned()
50}
51
52impl Fixture {
53 pub async fn new(visibility: Visibility) -> Self {
54 let tmp = tempfile::tempdir().expect("tempdir");
55 let dir = tmp.path().to_path_buf();
56 let state = crate::test_support::test_state(&dir).await;
57 let store = state.store.clone();
58 let live = state.live.clone();
59 let router = crate::test_support::app_router(state);
60
61 let owner = store
62 .create_user(OWNER, None, "not-a-real-hash", false)
63 .await
64 .unwrap();
65 let repo = store
66 .create_repo(&owner.id, REPO, "", visibility, "main")
67 .await
68 .unwrap();
69 let bare = repo_dir(&dir, &repo.id);
70 std::fs::create_dir_all(bare.parent().unwrap()).unwrap();
71 git(
72 &dir,
73 &[
74 "init",
75 "-q",
76 "--bare",
77 "-b",
78 "main",
79 &bare.to_string_lossy(),
80 ],
81 );
82
83 let work = dir.join("work");
84 std::fs::create_dir_all(&work).unwrap();
85 git(&work, &["init", "-q", "-b", "main"]);
86 std::fs::write(work.join("a.txt"), "one\ntwo\nthree\n").unwrap();
87 std::fs::create_dir_all(work.join("dir")).unwrap();
88 std::fs::write(work.join("dir/b.txt"), "b\n").unwrap();
89 git(&work, &["add", "-A"]);
90 git(&work, &["commit", "-q", "-m", "root commit"]);
91 let root = git(&work, &["rev-parse", "HEAD"]);
92
93 std::fs::write(work.join("a.txt"), "one\nTWO\nthree\nfour\n").unwrap();
94 std::fs::write(work.join("new.txt"), "new file\n").unwrap();
95 git(&work, &["add", "-A"]);
96 git(
97 &work,
98 &[
99 "commit",
100 "-q",
101 "-m",
102 "Modify a.txt and add new.txt",
103 "-m",
104 "Body line one.\n\nSecond paragraph.",
105 ],
106 );
107 let modified = git(&work, &["rev-parse", "HEAD"]);
108
109 // 53 empty commits on top: 55 in all, so history spans two pages.
110 for n in 1..=53 {
111 git(
112 &work,
113 &[
114 "commit",
115 "-q",
116 "--allow-empty",
117 "-m",
118 &format!("filler {n}"),
119 ],
120 );
121 }
122 git(
123 &work,
124 &[
125 "push",
126 "-q",
127 &bare.to_string_lossy(),
128 "HEAD:refs/heads/main",
129 ],
130 );
131
132 Self {
133 router,
134 store,
135 live,
136 root,
137 modified,
138 work,
139 bare,
140 _dir: tmp,
141 }
142 }
143}
144
145/// Same layout as `forge_git::repo::repo_path`, computed here so the test can create it.
146fn repo_dir(data_dir: &Path, repo_id: &str) -> PathBuf {
147 let id = repo_id.to_ascii_lowercase();
148 data_dir
149 .join("repos")
150 .join(&id[0..2])
151 .join(&id[2..4])
152 .join(format!("{id}.git"))
153}
154
155/// GET through the router: `(status, headers, body)`.
156pub async fn get(router: &Router, uri: &str) -> (u16, http::HeaderMap, Vec<u8>) {
157 let req = Request::builder()
158 .method("GET")
159 .uri(uri)
160 .body(Body::empty())
161 .unwrap();
162 let res = router.handle(req).await;
163 let status = res.status().as_u16();
164 let headers = res.headers().clone();
165 let body = to_bytes(res.into_body(), usize::MAX)
166 .await
167 .unwrap()
168 .to_vec();
169 (status, headers, body)
170}