Skip to content

Commit 30b52b7

Browse files
committed
tests: move DistContext into library
1 parent da4391f commit 30b52b7

File tree

3 files changed

+56
-55
lines changed

3 files changed

+56
-55
lines changed

src/test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub use clitools::{
3232
output_release_file, print_command, print_indented,
3333
};
3434
pub(crate) mod dist;
35+
pub use dist::DistContext;
3536
pub(crate) mod mock;
3637
pub use mock::{MockComponentBuilder, MockFile, MockInstallerBuilder};
3738

src/test/dist.rs

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,67 @@ use std::sync::{LazyLock, Mutex};
99

1010
use url::Url;
1111

12+
use super::clitools::hard_link;
13+
use super::mock::MockInstallerBuilder;
14+
use super::{CROSS_ARCH1, CROSS_ARCH2, MULTI_ARCH1, create_hash, this_host_triple};
1215
use crate::dist::{
13-
Profile, TargetTriple,
16+
DEFAULT_DIST_SERVER, Profile, TargetTriple,
17+
component::{Components, DirectoryPackage, Transaction},
1418
manifest::{
1519
Component, CompressionKind, HashedBinary, Manifest, ManifestVersion, Package,
1620
PackageTargets, Renamed, TargetedPackage,
1721
},
22+
prefix::InstallPrefix,
23+
temp,
1824
};
25+
use crate::notifications::Notification;
26+
use crate::process::TestProcess;
27+
28+
pub struct DistContext {
29+
pkg_dir: tempfile::TempDir,
30+
pub inst_dir: tempfile::TempDir,
31+
pub prefix: InstallPrefix,
32+
_tmp_dir: tempfile::TempDir,
33+
pub cx: temp::Context,
34+
pub tp: TestProcess,
35+
}
1936

20-
use super::clitools::hard_link;
21-
use super::mock::MockInstallerBuilder;
22-
use super::{CROSS_ARCH1, CROSS_ARCH2, MULTI_ARCH1, create_hash, this_host_triple};
37+
impl DistContext {
38+
pub fn new(mock: MockInstallerBuilder) -> anyhow::Result<Self> {
39+
let pkg_dir = tempfile::Builder::new().prefix("rustup").tempdir()?;
40+
mock.build(pkg_dir.path());
41+
42+
let inst_dir = tempfile::Builder::new().prefix("rustup").tempdir()?;
43+
let prefix = InstallPrefix::from(inst_dir.path().to_owned());
44+
let tmp_dir = tempfile::Builder::new().prefix("rustup").tempdir()?;
45+
46+
Ok(Self {
47+
pkg_dir,
48+
inst_dir,
49+
prefix,
50+
cx: temp::Context::new(
51+
tmp_dir.path().to_owned(),
52+
DEFAULT_DIST_SERVER,
53+
Box::new(|_| ()),
54+
),
55+
tp: TestProcess::default(),
56+
_tmp_dir: tmp_dir,
57+
})
58+
}
59+
60+
pub fn start(&self) -> anyhow::Result<(Transaction<'_>, Components, DirectoryPackage)> {
61+
let tx = Transaction::new(
62+
self.prefix.clone(),
63+
&self.cx,
64+
&|_: Notification<'_>| (),
65+
&self.tp.process,
66+
);
67+
68+
let components = Components::open(self.prefix.clone())?;
69+
let pkg = DirectoryPackage::new(self.pkg_dir.path().to_owned(), true)?;
70+
Ok((tx, components, pkg))
71+
}
72+
}
2373

2474
pub(super) struct Release {
2575
// Either "nightly", "stable", "beta", or an explicit version number

tests/suite/dist_install.rs

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
use std::fs::File;
22
use std::io::Write;
33

4-
use rustup::dist::DEFAULT_DIST_SERVER;
54
use rustup::dist::component::Components;
65
use rustup::dist::component::Transaction;
76
use rustup::dist::component::{DirectoryPackage, Package};
87
use rustup::dist::prefix::InstallPrefix;
9-
use rustup::dist::temp;
108
use rustup::notifications::Notification;
11-
use rustup::process::TestProcess;
9+
use rustup::test::{DistContext, MockComponentBuilder, MockFile, MockInstallerBuilder};
1210
use rustup::utils;
1311

14-
use rustup::test::{MockComponentBuilder, MockFile, MockInstallerBuilder};
15-
1612
// Just testing that the mocks work
1713
#[test]
1814
fn mock_smoke_test() {
@@ -251,49 +247,3 @@ fn install_to_prefix_that_does_not_exist() {
251247
// The directory that does not exist
252248
assert!(utils::path_exists(does_not_exist.join("bin/foo")));
253249
}
254-
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-
}
263-
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-
}
286-
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-
}
299-
}

0 commit comments

Comments
 (0)