Skip to content

Commit 42d84c6

Browse files
committed
Refactor tests with less tuples and more objects
In prepration for the next commit...
1 parent b39a87c commit 42d84c6

File tree

4 files changed

+300
-207
lines changed

4 files changed

+300
-207
lines changed

src/rustup-dist/tests/dist.rs

Lines changed: 31 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ extern crate itertools;
1313
extern crate url;
1414

1515
use rustup_mock::dist::*;
16-
use rustup_mock::{MockCommand, MockInstallerBuilder};
16+
use rustup_mock::{MockFile, MockInstallerBuilder, MockComponentBuilder};
1717
use rustup_dist::prefix::InstallPrefix;
1818
use rustup_dist::ErrorKind;
1919
use rustup_dist::errors::Result;
@@ -112,16 +112,12 @@ pub fn create_mock_channel(channel: &str, date: &str,
112112
components: vec![],
113113
extensions: vec![],
114114
installer: MockInstallerBuilder {
115-
components: vec![
116-
("rustc".to_string(),
117-
vec![
118-
MockCommand::File("bin/rustc".to_string()),
119-
],
120-
vec![
121-
("bin/rustc".to_string(), contents.clone(), false)
122-
],
123-
),
124-
]
115+
components: vec![MockComponentBuilder {
116+
name: "rustc".to_string(),
117+
files: vec![
118+
MockFile::new_arc("bin/rustc", contents.clone()),
119+
],
120+
}],
125121
}
126122
},
127123
MockTargetedPackage {
@@ -146,16 +142,12 @@ pub fn create_mock_channel(channel: &str, date: &str,
146142
components: vec![],
147143
extensions: vec![],
148144
installer: MockInstallerBuilder {
149-
components: vec![
150-
("rust-std-x86_64-apple-darwin".to_string(),
151-
vec![
152-
MockCommand::File("lib/libstd.rlib".to_string()),
153-
],
154-
vec![
155-
("lib/libstd.rlib".to_string(), contents.clone(), false)
156-
],
157-
),
158-
]
145+
components: vec![MockComponentBuilder {
146+
name: "rust-std-x86_64-apple-darwin".to_string(),
147+
files: vec![
148+
MockFile::new_arc("lib/libstd.rlib", contents.clone()),
149+
],
150+
}],
159151
}
160152
},
161153
MockTargetedPackage {
@@ -164,16 +156,12 @@ pub fn create_mock_channel(channel: &str, date: &str,
164156
components: vec![],
165157
extensions: vec![],
166158
installer: MockInstallerBuilder {
167-
components: vec![
168-
("rust-std-i686-apple-darwin".to_string(),
169-
vec![
170-
MockCommand::File("lib/i686-apple-darwin/libstd.rlib".to_string()),
171-
],
172-
vec![
173-
("lib/i686-apple-darwin/libstd.rlib".to_string(), contents.clone(), false)
174-
],
175-
),
176-
]
159+
components: vec![MockComponentBuilder {
160+
name: "rust-std-i686-apple-darwin".to_string(),
161+
files: vec![
162+
MockFile::new_arc("lib/i686-apple-darwin/libstd.rlib", contents.clone()),
163+
],
164+
}],
177165
}
178166
},
179167
MockTargetedPackage {
@@ -182,16 +170,12 @@ pub fn create_mock_channel(channel: &str, date: &str,
182170
components: vec![],
183171
extensions: vec![],
184172
installer: MockInstallerBuilder {
185-
components: vec![
186-
("rust-std-i686-unknown-linux-gnu".to_string(),
187-
vec![
188-
MockCommand::File("lib/i686-unknown-linux-gnu/libstd.rlib".to_string()),
189-
],
190-
vec![
191-
("lib/i686-unknown-linux-gnu/libstd.rlib".to_string(), contents.clone(), false)
192-
],
193-
),
194-
]
173+
components: vec![MockComponentBuilder {
174+
name: "rust-std-i686-unknown-linux-gnu".to_string(),
175+
files: vec![
176+
MockFile::new_arc("lib/i686-unknown-linux-gnu/libstd.rlib", contents.clone()),
177+
],
178+
}],
195179
}
196180
},
197181
]
@@ -209,16 +193,12 @@ pub fn create_mock_channel(channel: &str, date: &str,
209193
components: vec![],
210194
extensions: vec![],
211195
installer: MockInstallerBuilder {
212-
components: vec![
213-
("bonus-x86_64-apple-darwin".to_string(),
214-
vec![
215-
MockCommand::File("bin/bonus".to_string()),
216-
],
217-
vec![
218-
("bin/bonus".to_string(), contents.clone(), false)
219-
],
220-
),
221-
]
196+
components: vec![MockComponentBuilder {
197+
name: "bonus-x86_64-apple-darwin".to_string(),
198+
files: vec![
199+
MockFile::new_arc("bin/bonus", contents.clone()),
200+
],
201+
}],
222202
}
223203
},
224204
]

src/rustup-dist/tests/install.rs

Lines changed: 103 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,34 @@ use rustup_utils::utils;
1414
use rustup_dist::prefix::InstallPrefix;
1515
use std::fs::File;
1616
use std::io::Write;
17-
use std::sync::Arc;
1817
use tempdir::TempDir;
19-
use rustup_mock::{MockInstallerBuilder, MockCommand};
18+
use rustup_mock::{MockInstallerBuilder, MockComponentBuilder, MockFile};
2019

2120
// Just testing that the mocks work
2221
#[test]
2322
fn mock_smoke_test() {
2423
let tempdir = TempDir::new("rustup").unwrap();
2524

2625
let mock = MockInstallerBuilder {
27-
components: vec![("mycomponent".to_string(),
28-
vec![MockCommand::File("bin/foo".to_string()),
29-
MockCommand::File("lib/bar".to_string()),
30-
MockCommand::Dir("doc/stuff".to_string())],
31-
vec![("bin/foo".to_string(), data("foo"), false),
32-
("lib/bar".to_string(), data("bar"), false),
33-
("doc/stuff/doc1".to_string(), data(""), false),
34-
("doc/stuff/doc2".to_string(), data(""), false)]),
35-
("mycomponent2".to_string(),
36-
vec![MockCommand::File("bin/quux".to_string())],
37-
vec![("bin/quux".to_string(), data("quux"), false)]
38-
)]
26+
components: vec![
27+
MockComponentBuilder {
28+
name: "mycomponent".to_string(),
29+
files: vec![
30+
MockFile::new("bin/foo", b"foo"),
31+
MockFile::new("lib/bar", b"bar"),
32+
MockFile::new_dir("doc/stuff", &[
33+
("doc1", b"", false),
34+
("doc2", b"", false),
35+
]),
36+
],
37+
},
38+
MockComponentBuilder {
39+
name: "mycomponent2".to_string(),
40+
files: vec![
41+
MockFile::new("bin/quux", b"quux"),
42+
],
43+
},
44+
],
3945
};
4046

4147
mock.build(tempdir.path());
@@ -55,14 +61,16 @@ fn package_contains() {
5561
let tempdir = TempDir::new("rustup").unwrap();
5662

5763
let mock = MockInstallerBuilder {
58-
components: vec![("mycomponent".to_string(),
59-
vec![MockCommand::File("bin/foo".to_string())],
60-
vec![("bin/foo".to_string(), data("foo"), false)],
61-
),
62-
("mycomponent2".to_string(),
63-
vec![MockCommand::File("bin/bar".to_string())],
64-
vec![("bin/bar".to_string(), data("bar"), false)]
65-
)]
64+
components: vec![
65+
MockComponentBuilder {
66+
name: "mycomponent".to_string(),
67+
files: vec![MockFile::new("bin/foo", b"foo")],
68+
},
69+
MockComponentBuilder {
70+
name: "mycomponent2".to_string(),
71+
files: vec![MockFile::new("bin/bar", b"bar")],
72+
},
73+
],
6674
};
6775

6876
mock.build(tempdir.path());
@@ -77,9 +85,12 @@ fn package_bad_version() {
7785
let tempdir = TempDir::new("rustup").unwrap();
7886

7987
let mock = MockInstallerBuilder {
80-
components: vec![("mycomponent".to_string(),
81-
vec![MockCommand::File("bin/foo".to_string())],
82-
vec![("bin/foo".to_string(), data("foo"), false)])]
88+
components: vec![
89+
MockComponentBuilder {
90+
name: "mycomponent".to_string(),
91+
files: vec![MockFile::new("bin/foo", b"foo")],
92+
},
93+
],
8394
};
8495

8596
mock.build(tempdir.path());
@@ -95,14 +106,19 @@ fn basic_install() {
95106
let pkgdir = TempDir::new("rustup").unwrap();
96107

97108
let mock = MockInstallerBuilder {
98-
components: vec![("mycomponent".to_string(),
99-
vec![MockCommand::File("bin/foo".to_string()),
100-
MockCommand::File("lib/bar".to_string()),
101-
MockCommand::Dir("doc/stuff".to_string())],
102-
vec![("bin/foo".to_string(), data("foo"), false),
103-
("lib/bar".to_string(), data("bar"), false),
104-
("doc/stuff/doc1".to_string(), data(""), false),
105-
("doc/stuff/doc2".to_string(), data(""), false)])]
109+
components: vec![
110+
MockComponentBuilder {
111+
name: "mycomponent".to_string(),
112+
files: vec![
113+
MockFile::new("bin/foo", b"foo"),
114+
MockFile::new("lib/bar", b"bar"),
115+
MockFile::new_dir("doc/stuff", &[
116+
("doc1", b"", false),
117+
("doc2", b"", false),
118+
]),
119+
],
120+
},
121+
],
106122
};
107123

108124
mock.build(pkgdir.path());
@@ -135,12 +151,16 @@ fn multiple_component_install() {
135151
let pkgdir = TempDir::new("rustup").unwrap();
136152

137153
let mock = MockInstallerBuilder {
138-
components: vec![("mycomponent".to_string(),
139-
vec![MockCommand::File("bin/foo".to_string())],
140-
vec![("bin/foo".to_string(), data("foo"), false)]),
141-
("mycomponent2".to_string(),
142-
vec![MockCommand::File("lib/bar".to_string())],
143-
vec![("lib/bar".to_string(), data("bar"), false)])]
154+
components: vec![
155+
MockComponentBuilder {
156+
name: "mycomponent".to_string(),
157+
files: vec![MockFile::new("bin/foo", b"foo")],
158+
},
159+
MockComponentBuilder {
160+
name: "mycomponent2".to_string(),
161+
files: vec![MockFile::new("lib/bar", b"bar")],
162+
},
163+
],
144164
};
145165

146166
mock.build(pkgdir.path());
@@ -173,17 +193,23 @@ fn uninstall() {
173193
let pkgdir = TempDir::new("rustup").unwrap();
174194

175195
let mock = MockInstallerBuilder {
176-
components: vec![("mycomponent".to_string(),
177-
vec![MockCommand::File("bin/foo".to_string()),
178-
MockCommand::File("lib/bar".to_string()),
179-
MockCommand::Dir("doc/stuff".to_string())],
180-
vec![("bin/foo".to_string(), data("foo"), false),
181-
("lib/bar".to_string(), data("bar"), false),
182-
("doc/stuff/doc1".to_string(), data(""), false),
183-
("doc/stuff/doc2".to_string(), data(""), false)]),
184-
("mycomponent2".to_string(),
185-
vec![MockCommand::File("lib/quux".to_string())],
186-
vec![("lib/quux".to_string(), data("quux"), false)])]
196+
components: vec![
197+
MockComponentBuilder {
198+
name: "mycomponent".to_string(),
199+
files: vec![
200+
MockFile::new("bin/foo", b"foo"),
201+
MockFile::new("lib/bar", b"bar"),
202+
MockFile::new_dir("doc/stuff", &[
203+
("doc1", b"", false),
204+
("doc2", b"", false),
205+
]),
206+
],
207+
},
208+
MockComponentBuilder {
209+
name: "mycomponent2".to_string(),
210+
files: vec![MockFile::new("lib/quux", b"quux")],
211+
},
212+
],
187213
};
188214

189215
mock.build(pkgdir.path());
@@ -233,9 +259,12 @@ fn component_bad_version() {
233259
let pkgdir = TempDir::new("rustup").unwrap();
234260

235261
let mock = MockInstallerBuilder {
236-
components: vec![("mycomponent".to_string(),
237-
vec![MockCommand::File("bin/foo".to_string())],
238-
vec![("bin/foo".to_string(), data("foo"), false)])]
262+
components: vec![
263+
MockComponentBuilder {
264+
name: "mycomponent".to_string(),
265+
files: vec![MockFile::new("bin/foo", b"foo")],
266+
},
267+
],
239268
};
240269

241270
mock.build(pkgdir.path());
@@ -274,17 +303,21 @@ fn unix_permissions() {
274303
let pkgdir = TempDir::new("rustup").unwrap();
275304

276305
let mock = MockInstallerBuilder {
277-
components: vec![("mycomponent".to_string(),
278-
vec![MockCommand::File("bin/foo".to_string()),
279-
MockCommand::File("lib/bar".to_string()),
280-
MockCommand::File("lib/foobar".to_string()),
281-
MockCommand::Dir("doc/stuff".to_string())],
282-
vec![("bin/foo".to_string(), data("foo"), false),
283-
("lib/bar".to_string(), data("bar"), false),
284-
("lib/foobar".to_string(), data("foobar"), true),
285-
("doc/stuff/doc1".to_string(), data(""), false),
286-
("doc/stuff/morestuff/doc2".to_string(), data(""), false),
287-
("doc/stuff/morestuff/tool".to_string(), data(""), true)])]
306+
components: vec![
307+
MockComponentBuilder {
308+
name: "mycomponent".to_string(),
309+
files: vec![
310+
MockFile::new("bin/foo", b"foo"),
311+
MockFile::new("lib/bar", b"bar"),
312+
MockFile::new("lib/foobar", b"foobar").executable(true),
313+
MockFile::new_dir("doc/stuff", &[
314+
("doc1", b"", false),
315+
("morestuff/doc2", b"", false),
316+
("morestuff/tool", b"", true),
317+
]),
318+
],
319+
},
320+
],
288321
};
289322

290323
mock.build(pkgdir.path());
@@ -328,9 +361,12 @@ fn install_to_prefix_that_does_not_exist() {
328361
let pkgdir = TempDir::new("rustup").unwrap();
329362

330363
let mock = MockInstallerBuilder {
331-
components: vec![("mycomponent".to_string(),
332-
vec![MockCommand::File("bin/foo".to_string())],
333-
vec![("bin/foo".to_string(), data("foo"), false)])]
364+
components: vec![
365+
MockComponentBuilder {
366+
name: "mycomponent".to_string(),
367+
files: vec![MockFile::new("bin/foo", b"foo")],
368+
},
369+
],
334370
};
335371

336372
mock.build(pkgdir.path());
@@ -354,7 +390,3 @@ fn install_to_prefix_that_does_not_exist() {
354390

355391
assert!(utils::path_exists(does_not_exist.join("bin/foo")));
356392
}
357-
358-
fn data(s: &str) -> Arc<Vec<u8>> {
359-
Arc::new(s.as_bytes().to_vec())
360-
}

0 commit comments

Comments
 (0)