Skip to content

Commit da4391f

Browse files
djcrami3l
authored andcommitted
tests: deduplicate distribution installation tests
1 parent b5e5a66 commit da4391f

File tree

1 file changed

+86
-122
lines changed

1 file changed

+86
-122
lines changed

tests/suite/dist_install.rs

Lines changed: 86 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,6 @@ fn package_bad_version() {
9292

9393
#[test]
9494
fn basic_install() {
95-
let pkgdir = tempfile::Builder::new().prefix("rustup").tempdir().unwrap();
96-
9795
let mock = MockInstallerBuilder {
9896
components: vec![MockComponentBuilder {
9997
name: "mycomponent".to_string(),
@@ -105,40 +103,25 @@ fn basic_install() {
105103
}],
106104
};
107105

108-
mock.build(pkgdir.path());
109-
110-
let instdir = tempfile::Builder::new().prefix("rustup").tempdir().unwrap();
111-
let prefix = InstallPrefix::from(instdir.path().to_owned());
112-
113-
let tmpdir = tempfile::Builder::new().prefix("rustup").tempdir().unwrap();
114-
let tmp_cx = temp::Context::new(
115-
tmpdir.path().to_owned(),
116-
DEFAULT_DIST_SERVER,
117-
Box::new(|_| ()),
118-
);
119-
let notify = |_: Notification<'_>| ();
120-
let tp = TestProcess::default();
121-
let tx = Transaction::new(prefix.clone(), &tmp_cx, &notify, &tp.process);
122-
123-
let components = Components::open(prefix).unwrap();
124-
125-
let pkg = DirectoryPackage::new(pkgdir.path().to_owned(), true).unwrap();
126-
106+
let cx = DistContext::new(mock).unwrap();
107+
let (tx, components, pkg) = cx.start().unwrap();
127108
let tx = pkg.install(&components, "mycomponent", None, tx).unwrap();
128109
tx.commit();
129110

130-
assert!(utils::path_exists(instdir.path().join("bin/foo")));
131-
assert!(utils::path_exists(instdir.path().join("lib/bar")));
132-
assert!(utils::path_exists(instdir.path().join("doc/stuff/doc1")));
133-
assert!(utils::path_exists(instdir.path().join("doc/stuff/doc2")));
111+
assert!(utils::path_exists(cx.inst_dir.path().join("bin/foo")));
112+
assert!(utils::path_exists(cx.inst_dir.path().join("lib/bar")));
113+
assert!(utils::path_exists(
114+
cx.inst_dir.path().join("doc/stuff/doc1")
115+
));
116+
assert!(utils::path_exists(
117+
cx.inst_dir.path().join("doc/stuff/doc2")
118+
));
134119

135120
assert!(components.find("mycomponent").unwrap().is_some());
136121
}
137122

138123
#[test]
139124
fn multiple_component_install() {
140-
let pkgdir = tempfile::Builder::new().prefix("rustup").tempdir().unwrap();
141-
142125
let mock = MockInstallerBuilder {
143126
components: vec![
144127
MockComponentBuilder {
@@ -152,40 +135,21 @@ fn multiple_component_install() {
152135
],
153136
};
154137

155-
mock.build(pkgdir.path());
156-
157-
let instdir = tempfile::Builder::new().prefix("rustup").tempdir().unwrap();
158-
let prefix = InstallPrefix::from(instdir.path().to_owned());
159-
160-
let tmpdir = tempfile::Builder::new().prefix("rustup").tempdir().unwrap();
161-
let tmp_cx = temp::Context::new(
162-
tmpdir.path().to_owned(),
163-
DEFAULT_DIST_SERVER,
164-
Box::new(|_| ()),
165-
);
166-
let notify = |_: Notification<'_>| ();
167-
let tp = TestProcess::default();
168-
let tx = Transaction::new(prefix.clone(), &tmp_cx, &notify, &tp.process);
169-
170-
let components = Components::open(prefix).unwrap();
171-
172-
let pkg = DirectoryPackage::new(pkgdir.path().to_owned(), true).unwrap();
173-
138+
let cx = DistContext::new(mock).unwrap();
139+
let (tx, components, pkg) = cx.start().unwrap();
174140
let tx = pkg.install(&components, "mycomponent", None, tx).unwrap();
175141
let tx = pkg.install(&components, "mycomponent2", None, tx).unwrap();
176142
tx.commit();
177143

178-
assert!(utils::path_exists(instdir.path().join("bin/foo")));
179-
assert!(utils::path_exists(instdir.path().join("lib/bar")));
144+
assert!(utils::path_exists(cx.inst_dir.path().join("bin/foo")));
145+
assert!(utils::path_exists(cx.inst_dir.path().join("lib/bar")));
180146

181147
assert!(components.find("mycomponent").unwrap().is_some());
182148
assert!(components.find("mycomponent2").unwrap().is_some());
183149
}
184150

185151
#[test]
186152
fn uninstall() {
187-
let pkgdir = tempfile::Builder::new().prefix("rustup").tempdir().unwrap();
188-
189153
let mock = MockInstallerBuilder {
190154
components: vec![
191155
MockComponentBuilder {
@@ -203,43 +167,29 @@ fn uninstall() {
203167
],
204168
};
205169

206-
mock.build(pkgdir.path());
207-
208-
let instdir = tempfile::Builder::new().prefix("rustup").tempdir().unwrap();
209-
let prefix = InstallPrefix::from(instdir.path().to_owned());
210-
211-
let tmpdir = tempfile::Builder::new().prefix("rustup").tempdir().unwrap();
212-
let tmp_cx = temp::Context::new(
213-
tmpdir.path().to_owned(),
214-
DEFAULT_DIST_SERVER,
215-
Box::new(|_| ()),
216-
);
217-
let notify = |_: Notification<'_>| ();
218-
let tp = TestProcess::default();
219-
let tx = Transaction::new(prefix.clone(), &tmp_cx, &notify, &tp.process);
220-
221-
let components = Components::open(prefix.clone()).unwrap();
222-
223-
let pkg = DirectoryPackage::new(pkgdir.path().to_owned(), true).unwrap();
224-
170+
let cx = DistContext::new(mock).unwrap();
171+
let (tx, components, pkg) = cx.start().unwrap();
225172
let tx = pkg.install(&components, "mycomponent", None, tx).unwrap();
226173
let tx = pkg.install(&components, "mycomponent2", None, tx).unwrap();
227174
tx.commit();
228175

229176
// Now uninstall
230177
let notify = |_: Notification<'_>| ();
231-
let tp = TestProcess::default();
232-
let mut tx = Transaction::new(prefix.clone(), &tmp_cx, &notify, &tp.process);
178+
let mut tx = Transaction::new(cx.prefix.clone(), &cx.cx, &notify, &cx.tp.process);
233179
for component in components.list().unwrap() {
234-
tx = component.uninstall(tx, &tp.process).unwrap();
180+
tx = component.uninstall(tx, &cx.tp.process).unwrap();
235181
}
236182
tx.commit();
237183

238-
assert!(!utils::path_exists(instdir.path().join("bin/foo")));
239-
assert!(!utils::path_exists(instdir.path().join("lib/bar")));
240-
assert!(!utils::path_exists(instdir.path().join("doc/stuff/doc1")));
241-
assert!(!utils::path_exists(instdir.path().join("doc/stuff/doc2")));
242-
assert!(!utils::path_exists(instdir.path().join("doc/stuff")));
184+
assert!(!utils::path_exists(cx.inst_dir.path().join("bin/foo")));
185+
assert!(!utils::path_exists(cx.inst_dir.path().join("lib/bar")));
186+
assert!(!utils::path_exists(
187+
cx.inst_dir.path().join("doc/stuff/doc1")
188+
));
189+
assert!(!utils::path_exists(
190+
cx.inst_dir.path().join("doc/stuff/doc2")
191+
));
192+
assert!(!utils::path_exists(cx.inst_dir.path().join("doc/stuff")));
243193
assert!(components.find("mycomponent").unwrap().is_none());
244194
assert!(components.find("mycomponent2").unwrap().is_none());
245195
}
@@ -253,42 +203,28 @@ fn uninstall_best_effort() {
253203

254204
#[test]
255205
fn component_bad_version() {
256-
let pkgdir = tempfile::Builder::new().prefix("rustup").tempdir().unwrap();
257-
258206
let mock = MockInstallerBuilder {
259207
components: vec![MockComponentBuilder {
260208
name: "mycomponent".to_string(),
261209
files: vec![MockFile::new("bin/foo", b"foo")],
262210
}],
263211
};
264212

265-
mock.build(pkgdir.path());
266-
267-
let instdir = tempfile::Builder::new().prefix("rustup").tempdir().unwrap();
268-
let prefix = InstallPrefix::from(instdir.path().to_owned());
269-
270-
let tmpdir = tempfile::Builder::new().prefix("rustup").tempdir().unwrap();
271-
let tmp_cx = temp::Context::new(
272-
tmpdir.path().to_owned(),
273-
DEFAULT_DIST_SERVER,
274-
Box::new(|_| ()),
275-
);
276-
let notify = |_: Notification<'_>| ();
277-
let tp = TestProcess::default();
278-
let tx = Transaction::new(prefix.clone(), &tmp_cx, &notify, &tp.process);
279-
280-
let components = Components::open(prefix.clone()).unwrap();
281-
282-
let pkg = DirectoryPackage::new(pkgdir.path().to_owned(), true).unwrap();
283-
213+
let cx = DistContext::new(mock).unwrap();
214+
let (tx, components, pkg) = cx.start().unwrap();
284215
let tx = pkg.install(&components, "mycomponent", None, tx).unwrap();
285216
tx.commit();
286217

287218
// Write a bogus version to the component manifest directory
288-
utils::write_file("", &prefix.manifest_file("rust-installer-version"), "100\n").unwrap();
219+
utils::write_file(
220+
"",
221+
&cx.prefix.manifest_file("rust-installer-version"),
222+
"100\n",
223+
)
224+
.unwrap();
289225

290226
// Can't open components now
291-
let e = Components::open(prefix).unwrap_err();
227+
let e = Components::open(cx.prefix).unwrap_err();
292228
assert_eq!(
293229
"unsupported metadata version in existing installation: 100",
294230
format!("{e}")
@@ -298,38 +234,66 @@ fn component_bad_version() {
298234
// Installing to a prefix that doesn't exist creates it automatically
299235
#[test]
300236
fn install_to_prefix_that_does_not_exist() {
301-
let pkgdir = tempfile::Builder::new().prefix("rustup").tempdir().unwrap();
302-
303237
let mock = MockInstallerBuilder {
304238
components: vec![MockComponentBuilder {
305239
name: "mycomponent".to_string(),
306240
files: vec![MockFile::new("bin/foo", b"foo")],
307241
}],
308242
};
309243

310-
mock.build(pkgdir.path());
244+
let mut cx = DistContext::new(mock).unwrap();
245+
let does_not_exist = cx.inst_dir.path().join("does_not_exist");
246+
cx.prefix = InstallPrefix::from(does_not_exist.clone());
247+
let (tx, components, pkg) = cx.start().unwrap();
248+
let tx = pkg.install(&components, "mycomponent", None, tx).unwrap();
249+
tx.commit();
311250

312-
let instdir = tempfile::Builder::new().prefix("rustup").tempdir().unwrap();
313251
// The directory that does not exist
314-
let does_not_exist = instdir.path().join("super_not_real");
315-
let prefix = InstallPrefix::from(does_not_exist.clone());
316-
317-
let tmpdir = tempfile::Builder::new().prefix("rustup").tempdir().unwrap();
318-
let tmp_cx = temp::Context::new(
319-
tmpdir.path().to_owned(),
320-
DEFAULT_DIST_SERVER,
321-
Box::new(|_| ()),
322-
);
323-
let notify = |_: Notification<'_>| ();
324-
let tp = TestProcess::default();
325-
let tx = Transaction::new(prefix.clone(), &tmp_cx, &notify, &tp.process);
326-
327-
let components = Components::open(prefix).unwrap();
252+
assert!(utils::path_exists(does_not_exist.join("bin/foo")));
253+
}
328254

329-
let pkg = DirectoryPackage::new(pkgdir.path().to_owned(), true).unwrap();
255+
struct DistContext {
256+
pkg_dir: tempfile::TempDir,
257+
inst_dir: tempfile::TempDir,
258+
prefix: InstallPrefix,
259+
_tmp_dir: tempfile::TempDir,
260+
cx: temp::Context,
261+
tp: TestProcess,
262+
}
330263

331-
let tx = pkg.install(&components, "mycomponent", None, tx).unwrap();
332-
tx.commit();
264+
impl DistContext {
265+
fn new(mock: MockInstallerBuilder) -> anyhow::Result<Self> {
266+
let pkg_dir = tempfile::Builder::new().prefix("rustup").tempdir()?;
267+
mock.build(pkg_dir.path());
268+
269+
let inst_dir = tempfile::Builder::new().prefix("rustup").tempdir()?;
270+
let prefix = InstallPrefix::from(inst_dir.path().to_owned());
271+
let tmp_dir = tempfile::Builder::new().prefix("rustup").tempdir()?;
272+
273+
Ok(Self {
274+
pkg_dir,
275+
inst_dir,
276+
prefix,
277+
cx: temp::Context::new(
278+
tmp_dir.path().to_owned(),
279+
DEFAULT_DIST_SERVER,
280+
Box::new(|_| ()),
281+
),
282+
tp: TestProcess::default(),
283+
_tmp_dir: tmp_dir,
284+
})
285+
}
333286

334-
assert!(utils::path_exists(does_not_exist.join("bin/foo")));
287+
fn start(&self) -> anyhow::Result<(Transaction<'_>, Components, DirectoryPackage)> {
288+
let tx = Transaction::new(
289+
self.prefix.clone(),
290+
&self.cx,
291+
&|_: Notification<'_>| (),
292+
&self.tp.process,
293+
);
294+
295+
let components = Components::open(self.prefix.clone())?;
296+
let pkg = DirectoryPackage::new(self.pkg_dir.path().to_owned(), true)?;
297+
Ok((tx, components, pkg))
298+
}
335299
}

0 commit comments

Comments
 (0)