Skip to content

Commit 8076f57

Browse files
committed
Refactoring of creating files in tests
Use `mkdir_p` and `fs::write`.
1 parent 3c673cb commit 8076f57

File tree

3 files changed

+16
-31
lines changed

3 files changed

+16
-31
lines changed

tests/testsuite/owner.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,27 @@
11
//! Tests for the `cargo owner` command.
22
3-
use std::fs::{self, File};
4-
use std::io::prelude::*;
3+
use std::fs;
54

5+
use cargo_test_support::paths::CargoPathExt;
66
use cargo_test_support::project;
77
use cargo_test_support::registry::{self, api_path, registry_url};
88

99
fn setup(name: &str) {
10-
fs::create_dir_all(&api_path().join(format!("api/v1/crates/{}", name))).unwrap();
11-
12-
let dest = api_path().join(format!("api/v1/crates/{}/owners", name));
13-
14-
let content = r#"{
10+
let dir = api_path().join(format!("api/v1/crates/{}", name));
11+
dir.mkdir_p();
12+
fs::write(
13+
dir.join("owners"),
14+
r#"{
1515
"users": [
1616
{
1717
"id": 70,
1818
"login": "github:rust-lang:core",
1919
"name": "Core"
2020
}
2121
]
22-
}"#;
23-
24-
File::create(&dest)
25-
.unwrap()
26-
.write_all(content.as_bytes())
27-
.unwrap();
22+
}"#,
23+
)
24+
.unwrap();
2825
}
2926

3027
#[cargo_test]

tests/testsuite/publish.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,10 +1267,7 @@ fn credentials_ambiguous_filename() {
12671267
registry::init();
12681268

12691269
let credentials_toml = paths::home().join(".cargo/credentials.toml");
1270-
File::create(&credentials_toml)
1271-
.unwrap()
1272-
.write_all(br#"token = "api-token""#)
1273-
.unwrap();
1270+
fs::write(credentials_toml, r#"token = "api-token""#).unwrap();
12741271

12751272
let p = project()
12761273
.file(

tests/testsuite/yank.rs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,15 @@
11
//! Tests for the `cargo yank` command.
22
3-
use std::fs::{self, File};
4-
use std::io::prelude::*;
3+
use std::fs;
54

5+
use cargo_test_support::paths::CargoPathExt;
66
use cargo_test_support::project;
77
use cargo_test_support::registry::{self, api_path, registry_url};
88

99
fn setup(name: &str, version: &str) {
10-
fs::create_dir_all(&api_path().join(format!("api/v1/crates/{}/{}", name, version))).unwrap();
11-
12-
let dest = api_path().join(format!("api/v1/crates/{}/{}/yank", name, version));
13-
14-
let content = r#"{
15-
"ok": true
16-
}"#;
17-
18-
File::create(&dest)
19-
.unwrap()
20-
.write_all(content.as_bytes())
21-
.unwrap();
10+
let dir = api_path().join(format!("api/v1/crates/{}/{}", name, version));
11+
dir.mkdir_p();
12+
fs::write(dir.join("yank"), r#"{"ok": true}"#).unwrap();
2213
}
2314

2415
#[cargo_test]

0 commit comments

Comments
 (0)