Skip to content

Commit 4514881

Browse files
authored
Merge pull request #1906 from lzutao/fs-read-to-string
Switch to std::fs::read_to_string
2 parents 1d14725 + 0724f2d commit 4514881

File tree

11 files changed

+31
-52
lines changed

11 files changed

+31
-52
lines changed

download/tests/download-curl-resume.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use url::Url;
88
use download::*;
99

1010
mod support;
11-
use crate::support::{file_contents, serve_file, tmp_dir, write_file};
11+
use crate::support::{serve_file, tmp_dir, write_file};
1212

1313
#[test]
1414
fn partially_downloaded_file_gets_resumed_from_byte_offset() {
@@ -23,7 +23,7 @@ fn partially_downloaded_file_gets_resumed_from_byte_offset() {
2323
download_to_path_with_backend(Backend::Curl, &from_url, &target_path, true, None)
2424
.expect("Test download failed");
2525

26-
assert_eq!(file_contents(&target_path), "12345");
26+
assert_eq!(std::fs::read_to_string(&target_path).unwrap(), "12345");
2727
}
2828

2929
#[test]
@@ -72,5 +72,5 @@ fn callback_gets_all_data_as_if_the_download_happened_all_at_once() {
7272
assert_eq!(*callback_len.lock().unwrap(), Some(5));
7373
let observed_bytes = received_in_callback.into_inner().unwrap();
7474
assert_eq!(observed_bytes, vec![b'1', b'2', b'3', b'4', b'5']);
75-
assert_eq!(file_contents(&target_path), "12345");
75+
assert_eq!(std::fs::read_to_string(&target_path).unwrap(), "12345");
7676
}

download/tests/download-reqwest-resume.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use url::Url;
88
use download::*;
99

1010
mod support;
11-
use crate::support::{file_contents, serve_file, tmp_dir, write_file};
11+
use crate::support::{serve_file, tmp_dir, write_file};
1212

1313
#[test]
1414
fn resume_partial_from_file_url() {
@@ -23,7 +23,7 @@ fn resume_partial_from_file_url() {
2323
download_to_path_with_backend(Backend::Reqwest, &from_url, &target_path, true, None)
2424
.expect("Test download failed");
2525

26-
assert_eq!(file_contents(&target_path), "12345");
26+
assert_eq!(std::fs::read_to_string(&target_path).unwrap(), "12345");
2727
}
2828

2929
#[test]
@@ -72,5 +72,5 @@ fn callback_gets_all_data_as_if_the_download_happened_all_at_once() {
7272
assert_eq!(*callback_len.lock().unwrap(), Some(5));
7373
let observed_bytes = received_in_callback.into_inner().unwrap();
7474
assert_eq!(observed_bytes, vec![b'1', b'2', b'3', b'4', b'5']);
75-
assert_eq!(file_contents(&target_path), "12345");
75+
assert_eq!(std::fs::read_to_string(&target_path).unwrap(), "12345");
7676
}

download/tests/support/mod.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use std::fs::{self, File};
2-
use std::io::{self, Read};
1+
use std::fs;
2+
use std::io;
33
use std::net::SocketAddr;
44
use std::path::Path;
55

@@ -10,15 +10,6 @@ pub fn tmp_dir() -> TempDir {
1010
TempDir::new("rustup-download-test-").expect("creating tempdir for test")
1111
}
1212

13-
pub fn file_contents(path: &Path) -> String {
14-
let mut result = String::new();
15-
File::open(&path)
16-
.unwrap()
17-
.read_to_string(&mut result)
18-
.expect("reading test result file");
19-
result
20-
}
21-
2213
pub fn write_file(path: &Path, contents: &str) {
2314
let mut file = fs::OpenOptions::new()
2415
.write(true)

src/utils/raw.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,6 @@ pub fn write_file(path: &Path, contents: &str) -> io::Result<()> {
6464
Ok(())
6565
}
6666

67-
pub fn read_file(path: &Path) -> io::Result<String> {
68-
let mut file = fs::OpenOptions::new().read(true).open(path)?;
69-
70-
let mut contents = String::new();
71-
72-
io::Read::read_to_string(&mut file, &mut contents)?;
73-
74-
Ok(contents)
75-
}
76-
7767
pub fn filter_file<F: FnMut(&str) -> bool>(
7868
src: &Path,
7969
dest: &Path,

src/utils/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ where
4141
}
4242

4343
pub fn read_file(name: &'static str, path: &Path) -> Result<String> {
44-
raw::read_file(path).chain_err(|| ErrorKind::ReadingFile {
44+
fs::read_to_string(path).chain_err(|| ErrorKind::ReadingFile {
4545
name,
4646
path: PathBuf::from(path),
4747
})

tests/cli-self-upd.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ fn install_adds_path_to_rc(rcfile: &str) {
313313
raw::write_file(&rc, my_rc).unwrap();
314314
expect_ok(config, &["rustup-init", "-y"]);
315315

316-
let new_rc = raw::read_file(&rc).unwrap();
316+
let new_rc = fs::read_to_string(&rc).unwrap();
317317
let addition = format!(r#"export PATH="{}/bin:$PATH""#, config.cargodir.display());
318318
let expected = format!("{}\n{}\n", my_rc, addition);
319319
assert_eq!(new_rc, expected);
@@ -355,7 +355,7 @@ fn install_with_zsh_adds_path_to_zprofile() {
355355
cmd.env("SHELL", "zsh");
356356
assert!(cmd.output().unwrap().status.success());
357357

358-
let new_rc = raw::read_file(&rc).unwrap();
358+
let new_rc = fs::read_to_string(&rc).unwrap();
359359
let addition = format!(r#"export PATH="{}/bin:$PATH""#, config.cargodir.display());
360360
let expected = format!("{}\n{}\n", my_rc, addition);
361361
assert_eq!(new_rc, expected);
@@ -376,7 +376,7 @@ fn install_with_zsh_adds_path_to_zdotdir_zprofile() {
376376
cmd.env("ZDOTDIR", zdotdir.path());
377377
assert!(cmd.output().unwrap().status.success());
378378

379-
let new_rc = raw::read_file(&rc).unwrap();
379+
let new_rc = fs::read_to_string(&rc).unwrap();
380380
let addition = format!(r#"export PATH="{}/bin:$PATH""#, config.cargodir.display());
381381
let expected = format!("{}\n{}\n", my_rc, addition);
382382
assert_eq!(new_rc, expected);
@@ -393,7 +393,7 @@ fn install_adds_path_to_rcfile_just_once() {
393393
expect_ok(config, &["rustup-init", "-y"]);
394394
expect_ok(config, &["rustup-init", "-y"]);
395395

396-
let new_profile = raw::read_file(&profile).unwrap();
396+
let new_profile = fs::read_to_string(&profile).unwrap();
397397
let addition = format!(r#"export PATH="{}/bin:$PATH""#, config.cargodir.display());
398398
let expected = format!("{}\n{}\n", my_profile, addition);
399399
assert_eq!(new_profile, expected);
@@ -409,7 +409,7 @@ fn uninstall_removes_path_from_rc(rcfile: &str) {
409409
expect_ok(config, &["rustup-init", "-y"]);
410410
expect_ok(config, &["rustup", "self", "uninstall", "-y"]);
411411

412-
let new_rc = raw::read_file(&rc).unwrap();
412+
let new_rc = fs::read_to_string(&rc).unwrap();
413413
assert_eq!(new_rc, my_rc);
414414
});
415415
}
@@ -437,7 +437,7 @@ fn uninstall_doesnt_touch_rc_files_that_dont_contain_cargo_home() {
437437
let profile = config.homedir.join(".profile");
438438
raw::write_file(&profile, my_rc).unwrap();
439439

440-
let profile = raw::read_file(&profile).unwrap();
440+
let profile = fs::read_to_string(&profile).unwrap();
441441

442442
assert_eq!(profile, my_rc);
443443
});
@@ -460,15 +460,15 @@ fn when_cargo_home_is_the_default_write_path_specially() {
460460
cmd.env_remove("CARGO_HOME");
461461
assert!(cmd.output().unwrap().status.success());
462462

463-
let new_profile = raw::read_file(&profile).unwrap();
463+
let new_profile = fs::read_to_string(&profile).unwrap();
464464
let expected = format!("{}\nexport PATH=\"$HOME/.cargo/bin:$PATH\"\n", my_profile);
465465
assert_eq!(new_profile, expected);
466466

467467
let mut cmd = clitools::cmd(config, "rustup", &["self", "uninstall", "-y"]);
468468
cmd.env_remove("CARGO_HOME");
469469
assert!(cmd.output().unwrap().status.success());
470470

471-
let new_profile = raw::read_file(&profile).unwrap();
471+
let new_profile = fs::read_to_string(&profile).unwrap();
472472
assert_eq!(new_profile, my_profile);
473473
});
474474
}
@@ -891,7 +891,7 @@ fn produces_env_file_on_unix() {
891891
cmd.env_remove("CARGO_HOME");
892892
assert!(cmd.output().unwrap().status.success());
893893
let envfile = config.homedir.join(".cargo/env");
894-
let envfile = raw::read_file(&envfile).unwrap();
894+
let envfile = fs::read_to_string(&envfile).unwrap();
895895
assert!(envfile.contains(r#"export PATH="$HOME/.cargo/bin:$PATH""#));
896896
});
897897
}

tests/cli-v1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ fn remove_override_toolchain_err_handling() {
163163
fn bad_sha_on_manifest() {
164164
setup(&|config| {
165165
let sha_file = config.distdir.join("dist/channel-rust-nightly.sha256");
166-
let sha_str = rustup::utils::raw::read_file(&sha_file).unwrap();
166+
let sha_str = fs::read_to_string(&sha_file).unwrap();
167167
let mut sha_bytes = sha_str.into_bytes();
168168
sha_bytes[..10].clone_from_slice(b"aaaaaaaaaa");
169169
let sha_str = String::from_utf8(sha_bytes).unwrap();

tests/cli-v2.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ fn bad_sha_on_manifest() {
235235
setup(&|config| {
236236
// Corrupt the sha
237237
let sha_file = config.distdir.join("dist/channel-rust-nightly.toml.sha256");
238-
let sha_str = rustup::utils::raw::read_file(&sha_file).unwrap();
238+
let sha_str = fs::read_to_string(&sha_file).unwrap();
239239
let mut sha_bytes = sha_str.into_bytes();
240240
sha_bytes[..10].clone_from_slice(b"aaaaaaaaaa");
241241
let sha_str = String::from_utf8(sha_bytes).unwrap();
@@ -914,7 +914,7 @@ fn make_component_unavailable(config: &Config, name: &str, target: &TargetTriple
914914
use rustup::dist::manifest::Manifest;
915915

916916
let manifest_path = config.distdir.join("dist/channel-rust-nightly.toml");
917-
let manifest_str = rustup::utils::raw::read_file(&manifest_path).unwrap();
917+
let manifest_str = fs::read_to_string(&manifest_path).unwrap();
918918
let mut manifest = Manifest::parse(&manifest_str).unwrap();
919919
{
920920
let std_pkg = manifest.packages.get_mut(name).unwrap();

tests/dist.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -543,13 +543,13 @@ fn upgrade() {
543543
update_from_dist(url, toolchain, prefix, &[], &[], download_cfg, temp_cfg).unwrap();
544544
assert_eq!(
545545
"2016-02-01",
546-
utils_raw::read_file(&prefix.path().join("bin/rustc")).unwrap()
546+
fs::read_to_string(&prefix.path().join("bin/rustc")).unwrap()
547547
);
548548
change_channel_date(url, "nightly", "2016-02-02");
549549
update_from_dist(url, toolchain, prefix, &[], &[], download_cfg, temp_cfg).unwrap();
550550
assert_eq!(
551551
"2016-02-02",
552-
utils_raw::read_file(&prefix.path().join("bin/rustc")).unwrap()
552+
fs::read_to_string(&prefix.path().join("bin/rustc")).unwrap()
553553
);
554554
});
555555
}

tests/dist_transactions.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ fn add_file() {
3434
drop(file);
3535

3636
assert_eq!(
37-
utils_raw::read_file(&prefix.path().join("foo/bar")).unwrap(),
37+
fs::read_to_string(&prefix.path().join("foo/bar")).unwrap(),
3838
"test"
3939
);
4040
}
@@ -460,7 +460,7 @@ fn write_file() {
460460

461461
let path = prefix.path().join("foo/bar");
462462
assert!(utils::is_file(&path));
463-
let file_content = utils_raw::read_file(&path).unwrap();
463+
let file_content = fs::read_to_string(&path).unwrap();
464464
assert_eq!(content, file_content);
465465
}
466466

@@ -566,7 +566,7 @@ fn modify_file_that_exists() {
566566
tx.modify_file(PathBuf::from("foo")).unwrap();
567567
tx.commit();
568568

569-
assert_eq!(utils_raw::read_file(&path).unwrap(), "wow");
569+
assert_eq!(fs::read_to_string(&path).unwrap(), "wow");
570570
}
571571

572572
#[test]
@@ -613,7 +613,7 @@ fn modify_file_that_exists_then_rollback() {
613613
utils_raw::write_file(&path, "eww").unwrap();
614614
drop(tx);
615615

616-
assert_eq!(utils_raw::read_file(&path).unwrap(), "wow");
616+
assert_eq!(fs::read_to_string(&path).unwrap(), "wow");
617617
}
618618

619619
// This is testing that the backup scheme is smart enough not
@@ -642,7 +642,7 @@ fn modify_twice_then_rollback() {
642642
utils_raw::write_file(&path, "ewww").unwrap();
643643
drop(tx);
644644

645-
assert_eq!(utils_raw::read_file(&path).unwrap(), "wow");
645+
assert_eq!(fs::read_to_string(&path).unwrap(), "wow");
646646
}
647647

648648
fn do_multiple_op_transaction(rollback: bool) {

0 commit comments

Comments
 (0)