Skip to content

Commit 191358f

Browse files
committed
Simplify lockfile tests
1 parent 3fe50e1 commit 191358f

File tree

3 files changed

+70
-94
lines changed

3 files changed

+70
-94
lines changed

tests/cargotest/support/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,13 @@ impl ProjectBuilder {
181181
self
182182
}
183183

184+
pub fn read_lockfile(&self) -> String {
185+
let mut buffer = String::new();
186+
fs::File::open(self.root().join("Cargo.lock")).unwrap()
187+
.read_to_string(&mut buffer).unwrap();
188+
buffer
189+
}
190+
184191
fn rm_root(&self) {
185192
self.root.rm_rf()
186193
}

tests/generate-lockfile.rs

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,8 @@ fn adding_and_removing_packages() {
2828
assert_that(p.cargo_process("generate-lockfile"),
2929
execs().with_status(0));
3030

31-
let lockfile = p.root().join("Cargo.lock");
3231
let toml = p.root().join("Cargo.toml");
33-
let mut lock1 = String::new();
34-
File::open(&lockfile).unwrap().read_to_string(&mut lock1).unwrap();
32+
let lock1 = p.read_lockfile();
3533

3634
// add a dep
3735
File::create(&toml).unwrap().write_all(br#"
@@ -45,8 +43,7 @@ fn adding_and_removing_packages() {
4543
"#).unwrap();
4644
assert_that(p.cargo("generate-lockfile"),
4745
execs().with_status(0));
48-
let mut lock2 = String::new();
49-
File::open(&lockfile).unwrap().read_to_string(&mut lock2).unwrap();
46+
let lock2 = p.read_lockfile();
5047
assert!(lock1 != lock2);
5148

5249
// change the dep
@@ -58,8 +55,7 @@ fn adding_and_removing_packages() {
5855
"#).unwrap();
5956
assert_that(p.cargo("generate-lockfile"),
6057
execs().with_status(0));
61-
let mut lock3 = String::new();
62-
File::open(&lockfile).unwrap().read_to_string(&mut lock3).unwrap();
58+
let lock3 = p.read_lockfile();
6359
assert!(lock1 != lock3);
6460
assert!(lock2 != lock3);
6561

@@ -73,8 +69,7 @@ fn adding_and_removing_packages() {
7369
"#).unwrap();
7470
assert_that(p.cargo("generate-lockfile"),
7571
execs().with_status(0));
76-
let mut lock4 = String::new();
77-
File::open(&lockfile).unwrap().read_to_string(&mut lock4).unwrap();
72+
let lock4 = p.read_lockfile();
7873
assert_eq!(lock1, lock4);
7974
}
8075

@@ -105,25 +100,20 @@ bar = "baz"
105100
foo = "bar"
106101
"#;
107102
let lockfile = p.root().join("Cargo.lock");
108-
{
109-
let mut lock = String::new();
110-
File::open(&lockfile).unwrap().read_to_string(&mut lock).unwrap();
111-
let data = lock + metadata;
112-
File::create(&lockfile).unwrap().write_all(data.as_bytes()).unwrap();
113-
}
103+
let lock = p.read_lockfile();
104+
let data = lock + metadata;
105+
File::create(&lockfile).unwrap().write_all(data.as_bytes()).unwrap();
114106

115107
// Build and make sure the metadata is still there
116108
assert_that(p.cargo("build"),
117109
execs().with_status(0));
118-
let mut lock = String::new();
119-
File::open(&lockfile).unwrap().read_to_string(&mut lock).unwrap();
110+
let lock = p.read_lockfile();
120111
assert!(lock.contains(metadata.trim()), "{}", lock);
121112

122113
// Update and make sure the metadata is still there
123114
assert_that(p.cargo("update"),
124115
execs().with_status(0));
125-
let mut lock = String::new();
126-
File::open(&lockfile).unwrap().read_to_string(&mut lock).unwrap();
116+
let lock = p.read_lockfile();
127117
assert!(lock.contains(metadata.trim()), "{}", lock);
128118
}
129119

@@ -153,10 +143,7 @@ fn preserve_line_endings_issue_2076() {
153143
assert_that(p.cargo("generate-lockfile"),
154144
execs().with_status(0));
155145

156-
let mut lock0 = String::new();
157-
{
158-
File::open(&lockfile).unwrap().read_to_string(&mut lock0).unwrap();
159-
}
146+
let lock0 = p.read_lockfile();
160147

161148
assert!(lock0.starts_with("[root]\n"));
162149

@@ -168,10 +155,7 @@ fn preserve_line_endings_issue_2076() {
168155
assert_that(p.cargo("generate-lockfile"),
169156
execs().with_status(0));
170157

171-
let mut lock2 = String::new();
172-
{
173-
File::open(&lockfile).unwrap().read_to_string(&mut lock2).unwrap();
174-
}
158+
let lock2 = p.read_lockfile();
175159

176160
assert!(lock2.starts_with("[root]\r\n"));
177161
assert_eq!(lock1, lock2);

tests/lockfile-compat.rs

Lines changed: 52 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
extern crate cargotest;
33
extern crate hamcrest;
44

5-
use std::fs::File;
6-
use std::io::prelude::*;
7-
85
use cargotest::support::git;
96
use cargotest::support::registry::Package;
107
use cargotest::support::{execs, project, lines_match};
@@ -14,19 +11,6 @@ use hamcrest::assert_that;
1411
fn oldest_lockfile_still_works() {
1512
Package::new("foo", "0.1.0").publish();
1613

17-
let p = project("bar")
18-
.file("Cargo.toml", r#"
19-
[project]
20-
name = "bar"
21-
version = "0.0.1"
22-
authors = []
23-
24-
[dependencies]
25-
foo = "0.1.0"
26-
"#)
27-
.file("src/lib.rs", "");
28-
p.build();
29-
3014
let lockfile = r#"
3115
[root]
3216
name = "bar"
@@ -40,15 +24,25 @@ name = "foo"
4024
version = "0.1.0"
4125
source = "registry+https://github.com/rust-lang/crates.io-index"
4226
"#;
43-
File::create(p.root().join("Cargo.lock")).unwrap()
44-
.write_all(lockfile.as_bytes()).unwrap();
27+
28+
let p = project("bar")
29+
.file("Cargo.toml", r#"
30+
[project]
31+
name = "bar"
32+
version = "0.0.1"
33+
authors = []
34+
35+
[dependencies]
36+
foo = "0.1.0"
37+
"#)
38+
.file("src/lib.rs", "")
39+
.file("Cargo.lock", lockfile);
40+
p.build();
4541

4642
assert_that(p.cargo("build"),
4743
execs().with_status(0));
4844

49-
let mut lock = String::new();
50-
File::open(p.root().join("Cargo.lock")).unwrap()
51-
.read_to_string(&mut lock).unwrap();
45+
let lock = p.read_lockfile();
5246
assert!(lock.starts_with(lockfile.trim()));
5347
}
5448

@@ -66,10 +60,8 @@ fn totally_wild_checksums_works() {
6660
[dependencies]
6761
foo = "0.1.0"
6862
"#)
69-
.file("src/lib.rs", "");
70-
p.build();
71-
72-
File::create(p.root().join("Cargo.lock")).unwrap().write_all(br#"
63+
.file("src/lib.rs", "")
64+
.file("Cargo.lock", r#"
7365
[root]
7466
name = "bar"
7567
version = "0.0.1"
@@ -85,14 +77,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
8577
[metadata]
8678
"checksum baz 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "checksum"
8779
"checksum foo 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "checksum"
88-
"#).unwrap();
80+
"#);
81+
82+
p.build();
8983

9084
assert_that(p.cargo("build"),
9185
execs().with_status(0));
9286

93-
let mut lock = String::new();
94-
File::open(p.root().join("Cargo.lock")).unwrap()
95-
.read_to_string(&mut lock).unwrap();
87+
let lock = p.read_lockfile();
9688
assert!(lock.starts_with(r#"
9789
[root]
9890
name = "bar"
@@ -124,10 +116,8 @@ fn wrong_checksum_is_an_error() {
124116
[dependencies]
125117
foo = "0.1.0"
126118
"#)
127-
.file("src/lib.rs", "");
128-
p.build();
129-
130-
t!(t!(File::create(p.root().join("Cargo.lock"))).write_all(br#"
119+
.file("src/lib.rs", "")
120+
.file("Cargo.lock", r#"
131121
[root]
132122
name = "bar"
133123
version = "0.0.1"
@@ -142,7 +132,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
142132
143133
[metadata]
144134
"checksum foo 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "checksum"
145-
"#));
135+
"#);
136+
137+
p.build();
146138

147139
assert_that(p.cargo("build"),
148140
execs().with_status(101).with_stderr("\
@@ -177,10 +169,8 @@ fn unlisted_checksum_is_bad_if_we_calculate() {
177169
[dependencies]
178170
foo = "0.1.0"
179171
"#)
180-
.file("src/lib.rs", "");
181-
p.build();
182-
183-
t!(t!(File::create(p.root().join("Cargo.lock"))).write_all(br#"
172+
.file("src/lib.rs", "")
173+
.file("Cargo.lock", r#"
184174
[root]
185175
name = "bar"
186176
version = "0.0.1"
@@ -195,7 +185,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
195185
196186
[metadata]
197187
"checksum foo 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "<none>"
198-
"#));
188+
"#);
189+
p.build();
199190

200191
assert_that(p.cargo("fetch"),
201192
execs().with_status(101).with_stderr("\
@@ -238,10 +229,8 @@ fn listed_checksum_bad_if_we_cannot_compute() {
238229
[dependencies]
239230
foo = {{ git = '{}' }}
240231
"#, git.url()))
241-
.file("src/lib.rs", "");
242-
p.build();
243-
244-
let lockfile = format!(r#"
232+
.file("src/lib.rs", "")
233+
.file("Cargo.lock", &format!(r#"
245234
[root]
246235
name = "bar"
247236
version = "0.0.1"
@@ -256,9 +245,9 @@ source = "git+{0}"
256245
257246
[metadata]
258247
"checksum foo 0.1.0 (git+{0})" = "checksum"
259-
"#, git.url());
260-
File::create(p.root().join("Cargo.lock")).unwrap()
261-
.write_all(lockfile.as_bytes()).unwrap();
248+
"#, git.url()));
249+
250+
p.build();
262251

263252
assert_that(p.cargo("fetch"),
264253
execs().with_status(101).with_stderr("\
@@ -296,9 +285,7 @@ fn current_lockfile_format() {
296285

297286
assert_that(p.cargo("build"), execs().with_status(0));
298287

299-
let mut actual = String::new();
300-
File::open(p.root().join("Cargo.lock")).unwrap()
301-
.read_to_string(&mut actual).unwrap();
288+
let actual = p.read_lockfile();
302289

303290
let expected = "\
304291
[root]
@@ -327,19 +314,6 @@ source = \"registry+https://github.com/rust-lang/crates.io-index\"
327314
fn lockfile_without_root() {
328315
Package::new("foo", "0.1.0").publish();
329316

330-
let p = project("bar")
331-
.file("Cargo.toml", r#"
332-
[package]
333-
name = "bar"
334-
version = "0.0.1"
335-
authors = []
336-
337-
[dependencies]
338-
foo = "0.1.0"
339-
"#)
340-
.file("src/lib.rs", "");
341-
p.build();
342-
343317
let lockfile = r#"[[package]]
344318
name = "bar"
345319
version = "0.0.1"
@@ -352,13 +326,24 @@ name = "foo"
352326
version = "0.1.0"
353327
source = "registry+https://github.com/rust-lang/crates.io-index"
354328
"#;
355-
File::create(p.root().join("Cargo.lock")).unwrap()
356-
.write_all(lockfile.as_bytes()).unwrap();
329+
330+
let p = project("bar")
331+
.file("Cargo.toml", r#"
332+
[package]
333+
name = "bar"
334+
version = "0.0.1"
335+
authors = []
336+
337+
[dependencies]
338+
foo = "0.1.0"
339+
"#)
340+
.file("src/lib.rs", "")
341+
.file("Cargo.lock", lockfile);
342+
343+
p.build();
357344

358345
assert_that(p.cargo("build"), execs().with_status(0));
359346

360-
let mut lock = String::new();
361-
File::open(p.root().join("Cargo.lock")).unwrap()
362-
.read_to_string(&mut lock).unwrap();
347+
let lock = p.read_lockfile();
363348
assert!(lock.starts_with(lockfile.trim()));
364349
}

0 commit comments

Comments
 (0)