From 4367ec4d11dcd58cf1c42fa99935f33610613fbc Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Thu, 16 Apr 2020 18:41:03 -0700 Subject: [PATCH 1/2] Use Path methods instead of fs::metadata. --- crates/cargo-test-support/src/registry.rs | 2 +- src/bin/cargo/main.rs | 4 +--- src/cargo/ops/cargo_new.rs | 22 ++++++++-------------- src/cargo/ops/cargo_read_manifest.rs | 2 +- src/cargo/ops/registry.rs | 4 ++-- src/cargo/sources/path.rs | 4 ++-- src/cargo/util/command_prelude.rs | 3 +-- src/cargo/util/config/mod.rs | 6 +++--- src/cargo/util/important_paths.rs | 3 +-- src/cargo/util/toml/mod.rs | 12 ++++++------ src/cargo/util/toml/targets.rs | 2 +- tests/testsuite/build_script.rs | 3 +-- tests/testsuite/clean.rs | 2 +- 13 files changed, 29 insertions(+), 40 deletions(-) diff --git a/crates/cargo-test-support/src/registry.rs b/crates/cargo-test-support/src/registry.rs index bfa16de03f6..695cea01b9c 100644 --- a/crates/cargo-test-support/src/registry.rs +++ b/crates/cargo-test-support/src/registry.rs @@ -162,7 +162,7 @@ pub struct Dependency { pub fn init() { let config = paths::home().join(".cargo/config"); t!(fs::create_dir_all(config.parent().unwrap())); - if fs::metadata(&config).is_ok() { + if config.exists() { return; } t!(t!(File::create(&config)).write_all( diff --git a/src/bin/cargo/main.rs b/src/bin/cargo/main.rs index 66e4a67bbfe..f9299367668 100644 --- a/src/bin/cargo/main.rs +++ b/src/bin/cargo/main.rs @@ -165,9 +165,7 @@ fn is_executable>(path: P) -> bool { } #[cfg(windows)] fn is_executable>(path: P) -> bool { - fs::metadata(path) - .map(|metadata| metadata.is_file()) - .unwrap_or(false) + path.as_ref().is_file() } fn search_directories(config: &Config) -> Vec { diff --git a/src/cargo/ops/cargo_new.rs b/src/cargo/ops/cargo_new.rs index a78639aa2ed..bb6262c5f2e 100644 --- a/src/cargo/ops/cargo_new.rs +++ b/src/cargo/ops/cargo_new.rs @@ -273,10 +273,7 @@ fn detect_source_paths_and_types( let pp = i.proposed_path; // path/pp does not exist or is not a file - if !fs::metadata(&path.join(&pp)) - .map(|x| x.is_file()) - .unwrap_or(false) - { + if !path.join(&pp).is_file() { continue; } @@ -358,7 +355,7 @@ fn plan_new_source_file(bin: bool, package_name: String) -> SourceFileInformatio pub fn new(opts: &NewOptions, config: &Config) -> CargoResult<()> { let path = &opts.path; - if fs::metadata(path).is_ok() { + if path.exists() { anyhow::bail!( "destination `{}` already exists\n\n\ Use `cargo init` to initialize the directory", @@ -397,7 +394,7 @@ pub fn init(opts: &NewOptions, config: &Config) -> CargoResult<()> { let path = &opts.path; - if fs::metadata(&path.join("Cargo.toml")).is_ok() { + if path.join("Cargo.toml").exists() { anyhow::bail!("`cargo init` cannot be run on existing Cargo packages") } @@ -428,22 +425,22 @@ pub fn init(opts: &NewOptions, config: &Config) -> CargoResult<()> { if version_control == None { let mut num_detected_vsces = 0; - if fs::metadata(&path.join(".git")).is_ok() { + if path.join(".git").exists() { version_control = Some(VersionControl::Git); num_detected_vsces += 1; } - if fs::metadata(&path.join(".hg")).is_ok() { + if path.join(".hg").exists() { version_control = Some(VersionControl::Hg); num_detected_vsces += 1; } - if fs::metadata(&path.join(".pijul")).is_ok() { + if path.join(".pijul").exists() { version_control = Some(VersionControl::Pijul); num_detected_vsces += 1; } - if fs::metadata(&path.join(".fossil")).is_ok() { + if path.join(".fossil").exists() { version_control = Some(VersionControl::Fossil); num_detected_vsces += 1; } @@ -743,10 +740,7 @@ mod tests { " }; - if !fs::metadata(&path_of_source_file) - .map(|x| x.is_file()) - .unwrap_or(false) - { + if !path_of_source_file.is_file() { paths::write(&path_of_source_file, default_file_content)?; // Format the newly created source file diff --git a/src/cargo/ops/cargo_read_manifest.rs b/src/cargo/ops/cargo_read_manifest.rs index 35438bfaabb..df1083fc30e 100644 --- a/src/cargo/ops/cargo_read_manifest.rs +++ b/src/cargo/ops/cargo_read_manifest.rs @@ -60,7 +60,7 @@ pub fn read_packages( } // Don't automatically discover packages across git submodules - if fs::metadata(&dir.join(".git")).is_ok() { + if dir.join(".git").exists() { return Ok(false); } } diff --git a/src/cargo/ops/registry.rs b/src/cargo/ops/registry.rs index 18c24a5df2b..3b287816a83 100644 --- a/src/cargo/ops/registry.rs +++ b/src/cargo/ops/registry.rs @@ -1,5 +1,5 @@ use std::collections::{BTreeMap, HashSet}; -use std::fs::{self, File}; +use std::fs::File; use std::io::{self, BufRead}; use std::iter::repeat; use std::str; @@ -233,7 +233,7 @@ fn transmit( None => None, }; if let Some(ref file) = *license_file { - if fs::metadata(&pkg.root().join(file)).is_err() { + if !pkg.root().join(file).exists() { bail!("the license file `{}` does not exist", file) } } diff --git a/src/cargo/sources/path.rs b/src/cargo/sources/path.rs index b3c92ac43c8..a0380160c0d 100644 --- a/src/cargo/sources/path.rs +++ b/src/cargo/sources/path.rs @@ -373,14 +373,14 @@ impl<'cfg> PathSource<'cfg> { is_root: bool, filter: &mut dyn FnMut(&Path) -> CargoResult, ) -> CargoResult<()> { - if !fs::metadata(&path).map(|m| m.is_dir()).unwrap_or(false) { + if !path.is_dir() { if (*filter)(path)? { ret.push(path.to_path_buf()); } return Ok(()); } // Don't recurse into any sub-packages that we have. - if !is_root && fs::metadata(&path.join("Cargo.toml")).is_ok() { + if !is_root && path.join("Cargo.toml").exists() { return Ok(()); } diff --git a/src/cargo/util/command_prelude.rs b/src/cargo/util/command_prelude.rs index 03fe98abb7b..e703128c073 100644 --- a/src/cargo/util/command_prelude.rs +++ b/src/cargo/util/command_prelude.rs @@ -13,7 +13,6 @@ use crate::CargoResult; use anyhow::bail; use clap::{self, SubCommand}; use std::ffi::{OsStr, OsString}; -use std::fs; use std::path::PathBuf; pub use crate::core::compiler::CompileMode; @@ -285,7 +284,7 @@ pub trait ArgMatchesExt { if !path.ends_with("Cargo.toml") { anyhow::bail!("the manifest-path must be a path to a Cargo.toml file") } - if fs::metadata(&path).is_err() { + if !path.exists() { anyhow::bail!( "manifest path `{}` does not exist", self._value_of("manifest-path").unwrap() diff --git a/src/cargo/util/config/mod.rs b/src/cargo/util/config/mod.rs index 2bc261ff3d8..576ca50969a 100644 --- a/src/cargo/util/config/mod.rs +++ b/src/cargo/util/config/mod.rs @@ -949,8 +949,8 @@ impl Config { let possible = dir.join(filename_without_extension); let possible_with_extension = dir.join(format!("{}.toml", filename_without_extension)); - if fs::metadata(&possible).is_ok() { - if warn && fs::metadata(&possible_with_extension).is_ok() { + if possible.exists() { + if warn && possible_with_extension.exists() { // We don't want to print a warning if the version // without the extension is just a symlink to the version // WITH an extension, which people may want to do to @@ -973,7 +973,7 @@ impl Config { } Ok(Some(possible)) - } else if fs::metadata(&possible_with_extension).is_ok() { + } else if possible_with_extension.exists() { Ok(Some(possible_with_extension)) } else { Ok(None) diff --git a/src/cargo/util/important_paths.rs b/src/cargo/util/important_paths.rs index efcdbc7a302..572194c0a0a 100644 --- a/src/cargo/util/important_paths.rs +++ b/src/cargo/util/important_paths.rs @@ -1,6 +1,5 @@ use crate::util::errors::CargoResult; use crate::util::paths; -use std::fs; use std::path::{Path, PathBuf}; /// Finds the root `Cargo.toml`. @@ -8,7 +7,7 @@ pub fn find_root_manifest_for_wd(cwd: &Path) -> CargoResult { let file = "Cargo.toml"; for current in paths::ancestors(cwd) { let manifest = current.join(file); - if fs::metadata(&manifest).is_ok() { + if manifest.exists() { return Ok(manifest); } } diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index f81af256b9e..b1c311ec9f7 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -1,6 +1,5 @@ use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; use std::fmt; -use std::fs; use std::path::{Path, PathBuf}; use std::rc::Rc; use std::str; @@ -1441,11 +1440,12 @@ impl TomlManifest { Some(StringOrBool::Bool(true)) => Some(build_rs), Some(StringOrBool::String(ref s)) => Some(PathBuf::from(s)), None => { - match fs::metadata(&build_rs) { - // If there is a `build.rs` file next to the `Cargo.toml`, assume it is - // a build script. - Ok(ref e) if e.is_file() => Some(build_rs), - Ok(_) | Err(_) => None, + // If there is a `build.rs` file next to the `Cargo.toml`, assume it is + // a build script. + if build_rs.is_file() { + Some(build_rs) + } else { + None } } } diff --git a/src/cargo/util/toml/targets.rs b/src/cargo/util/toml/targets.rs index f51c9fe1cb3..fee2b6691c0 100644 --- a/src/cargo/util/toml/targets.rs +++ b/src/cargo/util/toml/targets.rs @@ -557,7 +557,7 @@ fn clean_targets_with_legacy_path( fn inferred_lib(package_root: &Path) -> Option { let lib = package_root.join("src").join("lib.rs"); - if fs::metadata(&lib).is_ok() { + if lib.exists() { Some(lib) } else { None diff --git a/tests/testsuite/build_script.rs b/tests/testsuite/build_script.rs index 889d8b4ea42..cca16b7567b 100644 --- a/tests/testsuite/build_script.rs +++ b/tests/testsuite/build_script.rs @@ -85,7 +85,6 @@ fn custom_build_env_vars() { use std::env; use std::io::prelude::*; use std::path::Path; - use std::fs; fn main() {{ let _target = env::var("TARGET").unwrap(); @@ -103,7 +102,7 @@ fn custom_build_env_vars() { let out = env::var("OUT_DIR").unwrap(); assert!(out.starts_with(r"{0}")); - assert!(fs::metadata(&out).map(|m| m.is_dir()).unwrap_or(false)); + assert!(Path::new(&out).is_dir()); let _host = env::var("HOST").unwrap(); diff --git a/tests/testsuite/clean.rs b/tests/testsuite/clean.rs index 179ca48995b..32f3b29b539 100644 --- a/tests/testsuite/clean.rs +++ b/tests/testsuite/clean.rs @@ -185,7 +185,7 @@ fn build_script() { if env::var("FIRST").is_ok() { std::fs::File::create(out.join("out")).unwrap(); } else { - assert!(!std::fs::metadata(out.join("out")).is_ok()); + assert!(!out.join("out").exists()); } } "#, From 4ae79d2ffdfb1511c1d4a3e659ca1422a8be57e6 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Thu, 16 Apr 2020 21:10:11 -0700 Subject: [PATCH 2/2] Use fs helpers instead of File functions. --- crates/cargo-test-support/src/git.rs | 5 +- crates/cargo-test-support/src/lib.rs | 26 +-- crates/cargo-test-support/src/registry.rs | 45 ++--- src/cargo/ops/vendor.rs | 5 +- src/cargo/sources/registry/remote.rs | 8 +- src/cargo/util/cpu.rs | 6 +- src/cargo/util/paths.rs | 27 +-- tests/testsuite/alt_registry.rs | 59 +++--- tests/testsuite/build.rs | 77 +++---- tests/testsuite/build_script.rs | 154 +++++++------- tests/testsuite/build_script_env.rs | 4 +- tests/testsuite/cargo_command.rs | 45 ++--- tests/testsuite/concurrent.rs | 13 +- tests/testsuite/directory.rs | 55 ++--- tests/testsuite/doc.rs | 42 +--- tests/testsuite/features.rs | 9 +- tests/testsuite/fix.rs | 13 +- tests/testsuite/freshness.rs | 45 ++--- tests/testsuite/generate_lockfile.rs | 69 +++---- tests/testsuite/git.rs | 235 +++++++++------------- tests/testsuite/init.rs | 173 ++++------------ tests/testsuite/install.rs | 24 +-- tests/testsuite/local_registry.rs | 40 ++-- tests/testsuite/login.rs | 19 +- tests/testsuite/new.rs | 132 ++++-------- tests/testsuite/out_dir.rs | 9 +- tests/testsuite/package.rs | 34 +--- tests/testsuite/patch.rs | 89 ++++---- tests/testsuite/path.rs | 68 +++---- tests/testsuite/publish.rs | 9 +- tests/testsuite/registry.rs | 53 ++--- tests/testsuite/rustflags.rs | 27 +-- tests/testsuite/search.rs | 40 ++-- tests/testsuite/update.rs | 31 ++- tests/testsuite/workspaces.rs | 18 +- 35 files changed, 639 insertions(+), 1069 deletions(-) diff --git a/crates/cargo-test-support/src/git.rs b/crates/cargo-test-support/src/git.rs index 6f5f13d4596..c698fded541 100644 --- a/crates/cargo-test-support/src/git.rs +++ b/crates/cargo-test-support/src/git.rs @@ -39,8 +39,7 @@ use some of the helper functions in this file to interact with the repository. */ use crate::{path2url, project, Project, ProjectBuilder}; -use std::fs::{self, File}; -use std::io::prelude::*; +use std::fs; use std::path::{Path, PathBuf}; use url::Url; @@ -81,7 +80,7 @@ impl RepoBuilder { pub fn nocommit_file(self, path: &str, contents: &str) -> RepoBuilder { let dst = self.repo.workdir().unwrap().join(path); t!(fs::create_dir_all(dst.parent().unwrap())); - t!(t!(File::create(&dst)).write_all(contents.as_bytes())); + t!(fs::write(&dst, contents)); self } diff --git a/crates/cargo-test-support/src/lib.rs b/crates/cargo-test-support/src/lib.rs index 12720db9836..118438590fc 100644 --- a/crates/cargo-test-support/src/lib.rs +++ b/crates/cargo-test-support/src/lib.rs @@ -112,7 +112,6 @@ use std::env; use std::ffi::OsStr; use std::fmt; use std::fs; -use std::io::prelude::*; use std::os; use std::path::{Path, PathBuf}; use std::process::{Command, Output}; @@ -166,11 +165,8 @@ impl FileBuilder { fn mk(&self) { self.dirname().mkdir_p(); - - let mut file = fs::File::create(&self.path) + fs::write(&self.path, &self.body) .unwrap_or_else(|e| panic!("could not create file {}: {}", self.path.display(), e)); - - t!(file.write_all(self.body.as_bytes())); } fn dirname(&self) -> &Path { @@ -458,25 +454,15 @@ impl Project { /// Returns the contents of a path in the project root pub fn read_file(&self, path: &str) -> String { - let mut buffer = String::new(); - fs::File::open(self.root().join(path)) - .unwrap() - .read_to_string(&mut buffer) - .unwrap(); - buffer + let full = self.root().join(path); + fs::read_to_string(&full) + .unwrap_or_else(|e| panic!("could not read file {}: {}", full.display(), e)) } /// Modifies `Cargo.toml` to remove all commented lines. pub fn uncomment_root_manifest(&self) { - let mut contents = String::new(); - fs::File::open(self.root().join("Cargo.toml")) - .unwrap() - .read_to_string(&mut contents) - .unwrap(); - fs::File::create(self.root().join("Cargo.toml")) - .unwrap() - .write_all(contents.replace("#", "").as_bytes()) - .unwrap(); + let contents = self.read_file("Cargo.toml").replace("#", ""); + fs::write(self.root().join("Cargo.toml"), contents).unwrap(); } pub fn symlink(&self, src: impl AsRef, dst: impl AsRef) { diff --git a/crates/cargo-test-support/src/registry.rs b/crates/cargo-test-support/src/registry.rs index 695cea01b9c..c65f9cf8a5d 100644 --- a/crates/cargo-test-support/src/registry.rs +++ b/crates/cargo-test-support/src/registry.rs @@ -165,33 +165,34 @@ pub fn init() { if config.exists() { return; } - t!(t!(File::create(&config)).write_all( + t!(fs::write( + &config, format!( r#" - [source.crates-io] - registry = 'https://wut' - replace-with = 'dummy-registry' + [source.crates-io] + registry = 'https://wut' + replace-with = 'dummy-registry' - [source.dummy-registry] - registry = '{reg}' + [source.dummy-registry] + registry = '{reg}' - [registries.alternative] - index = '{alt}' - "#, + [registries.alternative] + index = '{alt}' + "#, reg = registry_url(), alt = alt_registry_url() ) - .as_bytes() )); let credentials = paths::home().join(".cargo/credentials"); - t!(t!(File::create(&credentials)).write_all( - br#" - [registry] - token = "api-token" - - [registries.alternative] - token = "api-token" - "# + t!(fs::write( + &credentials, + r#" + [registry] + token = "api-token" + + [registries.alternative] + token = "api-token" + "# )); // Initialize a new registry. @@ -404,8 +405,7 @@ impl Package { }) .collect::>(); let cksum = { - let mut c = Vec::new(); - t!(t!(File::open(&self.archive_dst())).read_to_end(&mut c)); + let c = t!(fs::read(&self.archive_dst())); cksum(&c) }; let name = if self.invalid_json { @@ -442,10 +442,9 @@ impl Package { } else { registry_path.join(&file) }; - let mut prev = String::new(); - let _ = File::open(&dst).and_then(|mut f| f.read_to_string(&mut prev)); + let prev = fs::read_to_string(&dst).unwrap_or(String::new()); t!(fs::create_dir_all(dst.parent().unwrap())); - t!(t!(File::create(&dst)).write_all((prev + &line[..] + "\n").as_bytes())); + t!(fs::write(&dst, prev + &line[..] + "\n")); // Add the new file to the index. if !self.local { diff --git a/src/cargo/ops/vendor.rs b/src/cargo/ops/vendor.rs index 8a5b8c056f3..b3edd243c29 100644 --- a/src/cargo/ops/vendor.rs +++ b/src/cargo/ops/vendor.rs @@ -8,8 +8,7 @@ use anyhow::bail; use serde::Serialize; use std::collections::HashSet; use std::collections::{BTreeMap, BTreeSet, HashMap}; -use std::fs::{self, File}; -use std::io::Write; +use std::fs; use std::path::{Path, PathBuf}; pub struct VendorOptions<'a> { @@ -223,7 +222,7 @@ fn sync( "files": map, }); - File::create(&cksum)?.write_all(json.to_string().as_bytes())?; + paths::write(&cksum, json.to_string())?; } for path in to_remove { diff --git a/src/cargo/sources/registry/remote.rs b/src/cargo/sources/registry/remote.rs index 552796bcf25..52bb4ca7e4f 100644 --- a/src/cargo/sources/registry/remote.rs +++ b/src/cargo/sources/registry/remote.rs @@ -9,7 +9,7 @@ use lazycell::LazyCell; use log::{debug, trace}; use std::cell::{Cell, Ref, RefCell}; use std::fmt::Write as FmtWrite; -use std::fs::{File, OpenOptions}; +use std::fs::{self, File, OpenOptions}; use std::io::prelude::*; use std::io::SeekFrom; use std::mem; @@ -300,10 +300,8 @@ impl<'cfg> RegistryData for RemoteRegistry<'cfg> { let path = self.cache_path.join(path); let path = self.config.assert_package_cache_locked(&path); - if let Ok(dst) = File::open(path) { - if let Ok(meta) = dst.metadata() { - return meta.len() > 0; - } + if let Ok(meta) = fs::metadata(path) { + return meta.len() > 0; } false } diff --git a/src/cargo/util/cpu.rs b/src/cargo/util/cpu.rs index 94d6d8d9cb5..3fe50d372da 100644 --- a/src/cargo/util/cpu.rs +++ b/src/cargo/util/cpu.rs @@ -26,8 +26,7 @@ impl State { #[cfg(target_os = "linux")] mod imp { - use std::fs::File; - use std::io::{self, Read}; + use std::{fs, io}; pub struct State { user: u64, @@ -43,8 +42,7 @@ mod imp { } pub fn current() -> io::Result { - let mut state = String::new(); - File::open("/proc/stat")?.read_to_string(&mut state)?; + let state = fs::read_to_string("/proc/stat")?; (|| { let mut parts = state.lines().next()?.split_whitespace(); diff --git a/src/cargo/util/paths.rs b/src/cargo/util/paths.rs index 16e5cf19ace..3c6b4c89bf7 100644 --- a/src/cargo/util/paths.rs +++ b/src/cargo/util/paths.rs @@ -1,6 +1,6 @@ use std::env; use std::ffi::{OsStr, OsString}; -use std::fs::{self, File, OpenOptions}; +use std::fs::{self, OpenOptions}; use std::io; use std::io::prelude::*; use std::iter; @@ -118,27 +118,12 @@ pub fn read(path: &Path) -> CargoResult { } pub fn read_bytes(path: &Path) -> CargoResult> { - let res = (|| -> CargoResult<_> { - let mut ret = Vec::new(); - let mut f = File::open(path)?; - if let Ok(m) = f.metadata() { - ret.reserve(m.len() as usize + 1); - } - f.read_to_end(&mut ret)?; - Ok(ret) - })() - .chain_err(|| format!("failed to read `{}`", path.display()))?; - Ok(res) + fs::read(path).chain_err(|| format!("failed to read `{}`", path.display())) } -pub fn write(path: &Path, contents: &[u8]) -> CargoResult<()> { - (|| -> CargoResult<()> { - let mut f = File::create(path)?; - f.write_all(contents)?; - Ok(()) - })() - .chain_err(|| format!("failed to write `{}`", path.display()))?; - Ok(()) +pub fn write, C: AsRef<[u8]>>(path: P, contents: C) -> CargoResult<()> { + let path = path.as_ref(); + fs::write(path, contents.as_ref()).chain_err(|| format!("failed to write `{}`", path.display())) } pub fn write_if_changed, C: AsRef<[u8]>>(path: P, contents: C) -> CargoResult<()> { @@ -190,7 +175,7 @@ pub fn set_invocation_time(path: &Path) -> CargoResult { let timestamp = path.join("invoked.timestamp"); write( ×tamp, - b"This file has an mtime of when this was started.", + "This file has an mtime of when this was started.", )?; let ft = mtime(×tamp)?; log::debug!("invocation time for {:?} is {}", path, ft); diff --git a/tests/testsuite/alt_registry.rs b/tests/testsuite/alt_registry.rs index cf45400b21a..6561bdfbd06 100644 --- a/tests/testsuite/alt_registry.rs +++ b/tests/testsuite/alt_registry.rs @@ -4,8 +4,7 @@ use cargo::util::IntoUrl; use cargo_test_support::publish::validate_alt_upload; use cargo_test_support::registry::{self, Package}; use cargo_test_support::{basic_manifest, git, paths, project}; -use std::fs::{self, File}; -use std::io::Write; +use std::fs; #[cargo_test] fn depend_on_alt_registry() { @@ -534,16 +533,14 @@ fn passwords_in_registry_index_url_forbidden() { registry::init(); let config = paths::home().join(".cargo/config"); - - File::create(config) - .unwrap() - .write_all( - br#" + fs::write( + config, + r#" [registry] index = "ssh://git:secret@foobar.com" "#, - ) - .unwrap(); + ) + .unwrap(); let p = project().file("src/main.rs", "fn main() {}").build(); @@ -559,15 +556,14 @@ fn passwords_in_registries_index_url_forbidden() { let config = paths::home().join(".cargo/config"); - File::create(config) - .unwrap() - .write_all( - br#" + fs::write( + config, + r#" [registries.alternative] index = "ssh://git:secret@foobar.com" "#, - ) - .unwrap(); + ) + .unwrap(); let p = project().file("src/main.rs", "fn main() {}").build(); @@ -1181,15 +1177,14 @@ fn unknown_registry() { fn registries_index_relative_url() { let config = paths::root().join(".cargo/config"); fs::create_dir_all(config.parent().unwrap()).unwrap(); - File::create(&config) - .unwrap() - .write_all( - br#" + fs::write( + &config, + r#" [registries.relative] index = "file:alternative-registry" "#, - ) - .unwrap(); + ) + .unwrap(); registry::init(); @@ -1231,15 +1226,14 @@ fn registries_index_relative_url() { fn registry_index_relative_url() { let config = paths::root().join(".cargo/config"); fs::create_dir_all(config.parent().unwrap()).unwrap(); - File::create(&config) - .unwrap() - .write_all( - br#" + fs::write( + &config, + r#" [registry] index = "file:alternative-registry" "#, - ) - .unwrap(); + ) + .unwrap(); registry::init(); @@ -1283,15 +1277,14 @@ warning: custom registry support via the `registry.index` configuration is being fn registries_index_relative_path_not_allowed() { let config = paths::root().join(".cargo/config"); fs::create_dir_all(config.parent().unwrap()).unwrap(); - File::create(&config) - .unwrap() - .write_all( - br#" + fs::write( + &config, + r#" [registries.relative] index = "alternative-registry" "#, - ) - .unwrap(); + ) + .unwrap(); registry::init(); diff --git a/tests/testsuite/build.rs b/tests/testsuite/build.rs index abe60a13fcd..6fb1bbae430 100644 --- a/tests/testsuite/build.rs +++ b/tests/testsuite/build.rs @@ -8,8 +8,7 @@ use cargo_test_support::{ sleep_ms, symlink_supported, t, Execs, ProjectBuilder, }; use std::env; -use std::fs::{self, File}; -use std::io::prelude::*; +use std::fs; #[cargo_test] fn cargo_compile_simple() { @@ -1108,10 +1107,7 @@ fn compile_path_dep_then_change_version() { p.cargo("build").run(); - File::create(&p.root().join("bar/Cargo.toml")) - .unwrap() - .write_all(basic_manifest("bar", "0.0.2").as_bytes()) - .unwrap(); + p.change_file("bar/Cargo.toml", &basic_manifest("bar", "0.0.2")); p.cargo("build").run(); } @@ -1125,17 +1121,8 @@ fn ignores_carriage_return_in_lockfile() { p.cargo("build").run(); - let lockfile = p.root().join("Cargo.lock"); - let mut lock = String::new(); - File::open(&lockfile) - .unwrap() - .read_to_string(&mut lock) - .unwrap(); - let lock = lock.replace("\n", "\r\n"); - File::create(&lockfile) - .unwrap() - .write_all(lock.as_bytes()) - .unwrap(); + let lock = p.read_lockfile(); + p.change_file("Cargo.lock", &lock.replace("\n", "\r\n")); p.cargo("build").run(); } @@ -2164,7 +2151,7 @@ fn freshness_ignores_excluded() { // Modify an ignored file and make sure we don't rebuild println!("second pass"); - File::create(&foo.root().join("src/bar.rs")).unwrap(); + foo.change_file("src/bar.rs", ""); foo.cargo("build").with_stdout("").run(); } @@ -2212,7 +2199,7 @@ fn rebuild_preserves_out_dir() { ) .run(); - File::create(&foo.root().join("src/bar.rs")).unwrap(); + foo.change_file("src/bar.rs", ""); foo.cargo("build") .with_stderr( "\ @@ -2280,11 +2267,12 @@ fn credentials_is_unreadable() { let credentials = home().join(".cargo/credentials"); t!(fs::create_dir_all(credentials.parent().unwrap())); - t!(t!(File::create(&credentials)).write_all( - br#" - [registry] - token = "api-token" - "# + t!(fs::write( + &credentials, + r#" + [registry] + token = "api-token" + "# )); let stat = fs::metadata(credentials.as_path()).unwrap(); let mut perms = stat.permissions(); @@ -2445,12 +2433,7 @@ fn cargo_platform_specific_dependency_wrong_platform() { assert!(p.bin("foo").is_file()); p.process(&p.bin("foo")).run(); - let loc = p.root().join("Cargo.lock"); - let mut lockfile = String::new(); - File::open(&loc) - .unwrap() - .read_to_string(&mut lockfile) - .unwrap(); + let lockfile = p.read_lockfile(); assert!(lockfile.contains("bar")); } @@ -2868,16 +2851,13 @@ fn custom_target_dir_env() { .run(); assert!(p.root().join("foo2/target/debug").join(&exe_name).is_file()); - fs::create_dir(p.root().join(".cargo")).unwrap(); - File::create(p.root().join(".cargo/config")) - .unwrap() - .write_all( - br#" - [build] - target-dir = "foo/target" - "#, - ) - .unwrap(); + p.change_file( + ".cargo/config", + r#" + [build] + target-dir = "foo/target" + "#, + ); p.cargo("build").env("CARGO_TARGET_DIR", "bar/target").run(); assert!(p.root().join("bar/target/debug").join(&exe_name).is_file()); assert!(p.root().join("foo/target/debug").join(&exe_name).is_file()); @@ -2898,16 +2878,13 @@ fn custom_target_dir_line_parameter() { assert!(p.root().join("foo/target/debug").join(&exe_name).is_file()); assert!(p.root().join("target/debug").join(&exe_name).is_file()); - fs::create_dir(p.root().join(".cargo")).unwrap(); - File::create(p.root().join(".cargo/config")) - .unwrap() - .write_all( - br#" - [build] - target-dir = "foo/target" - "#, - ) - .unwrap(); + p.change_file( + ".cargo/config", + r#" + [build] + target-dir = "foo/target" + "#, + ); p.cargo("build --target-dir bar/target").run(); assert!(p.root().join("bar/target/debug").join(&exe_name).is_file()); assert!(p.root().join("foo/target/debug").join(&exe_name).is_file()); diff --git a/tests/testsuite/build_script.rs b/tests/testsuite/build_script.rs index cca16b7567b..701852c1eb4 100644 --- a/tests/testsuite/build_script.rs +++ b/tests/testsuite/build_script.rs @@ -1,9 +1,8 @@ //! Tests for build.rs scripts. use std::env; -use std::fs::{self, File}; +use std::fs; use std::io; -use std::io::prelude::*; use std::thread; use cargo::util::paths::remove_dir_all; @@ -695,7 +694,7 @@ fn only_rerun_build_script() { p.cargo("build -v").run(); p.root().move_into_the_past(); - File::create(&p.root().join("some-new-file")).unwrap(); + p.change_file("some-new-file", ""); p.root().move_into_the_past(); p.cargo("build -v") @@ -773,7 +772,7 @@ fn rebuild_continues_to_pass_env_vars() { p.cargo("build -v").run(); p.root().move_into_the_past(); - File::create(&p.root().join("some-new-file")).unwrap(); + p.change_file("some-new-file", ""); p.root().move_into_the_past(); p.cargo("build -v").run(); @@ -800,7 +799,7 @@ fn testing_and_such() { p.cargo("build -v").run(); p.root().move_into_the_past(); - File::create(&p.root().join("src/lib.rs")).unwrap(); + p.change_file("src/lib.rs", ""); p.root().move_into_the_past(); println!("test"); @@ -830,10 +829,7 @@ fn testing_and_such() { ) .run(); - File::create(&p.root().join("src/main.rs")) - .unwrap() - .write_all(b"fn main() {}") - .unwrap(); + p.change_file("src/main.rs", "fn main() {}"); println!("run"); p.cargo("run") .with_stderr( @@ -1190,31 +1186,54 @@ fn out_dir_is_preserved() { // Make the file p.cargo("build -v").run(); - p.root().move_into_the_past(); // Change to asserting that it's there - File::create(&p.root().join("build.rs")) - .unwrap() - .write_all( - br#" - use std::env; - use std::old_io::File; - fn main() { - let out = env::var("OUT_DIR").unwrap(); - File::open(&Path::new(&out).join("foo")).unwrap(); - } - "#, + p.change_file( + "build.rs", + r#" + use std::env; + use std::fs::File; + use std::path::Path; + fn main() { + let out = env::var("OUT_DIR").unwrap(); + File::open(&Path::new(&out).join("foo")).unwrap(); + } + "#, + ); + p.cargo("build -v") + .with_stderr( + "\ +[COMPILING] foo [..] +[RUNNING] `rustc --crate-name build_script_build [..] +[RUNNING] `[..]/build-script-build` +[RUNNING] `rustc --crate-name foo [..] +[FINISHED] [..] +", ) - .unwrap(); - p.root().move_into_the_past(); - p.cargo("build -v").run(); + .run(); // Run a fresh build where file should be preserved - p.cargo("build -v").run(); + p.cargo("build -v") + .with_stderr( + "\ +[FRESH] foo [..] +[FINISHED] [..] +", + ) + .run(); // One last time to make sure it's still there. - File::create(&p.root().join("foo")).unwrap(); - p.cargo("build -v").run(); + p.change_file("foo", ""); + p.cargo("build -v") + .with_stderr( + "\ +[COMPILING] foo [..] +[RUNNING] `[..]build-script-build` +[RUNNING] `rustc --crate-name foo [..] +[FINISHED] [..] +", + ) + .run(); } #[cargo_test] @@ -1321,18 +1340,18 @@ fn code_generation() { "build.rs", r#" use std::env; - use std::fs::File; - use std::io::prelude::*; + use std::fs; use std::path::PathBuf; fn main() { let dst = PathBuf::from(env::var("OUT_DIR").unwrap()); - let mut f = File::create(&dst.join("hello.rs")).unwrap(); - f.write_all(b" + fs::write(dst.join("hello.rs"), + " pub fn message() -> &'static str { \"Hello, World!\" } - ").unwrap(); + ") + .unwrap(); } "#, ) @@ -1536,15 +1555,12 @@ fn test_a_lib_with_a_build_command() { "build.rs", r#" use std::env; - use std::io::prelude::*; - use std::fs::File; + use std::fs; use std::path::PathBuf; fn main() { let out = PathBuf::from(env::var("OUT_DIR").unwrap()); - File::create(out.join("foo.rs")).unwrap().write_all(b" - fn foo() -> i32 { 1 } - ").unwrap(); + fs::write(out.join("foo.rs"), "fn foo() -> i32 { 1 }").unwrap(); } "#, ) @@ -2397,19 +2413,16 @@ fn adding_an_override_invalidates() { ) .run(); - File::create(p.root().join(".cargo/config")) - .unwrap() - .write_all( - format!( - " - [target.{}.foo] - rustc-link-search = [\"native=bar\"] - ", - target - ) - .as_bytes(), - ) - .unwrap(); + p.change_file( + ".cargo/config", + &format!( + " + [target.{}.foo] + rustc-link-search = [\"native=bar\"] + ", + target + ), + ); p.cargo("build -v") .with_stderr( @@ -2461,19 +2474,16 @@ fn changing_an_override_invalidates() { ) .run(); - File::create(p.root().join(".cargo/config")) - .unwrap() - .write_all( - format!( - " - [target.{}.foo] - rustc-link-search = [\"native=bar\"] - ", - target - ) - .as_bytes(), - ) - .unwrap(); + p.change_file( + ".cargo/config", + &format!( + " + [target.{}.foo] + rustc-link-search = [\"native=bar\"] + ", + target + ), + ); p.cargo("build -v") .with_stderr( @@ -2634,8 +2644,8 @@ fn rebuild_only_on_explicit_paths() { .run(); sleep_ms(1000); - File::create(p.root().join("foo")).unwrap(); - File::create(p.root().join("bar")).unwrap(); + p.change_file("foo", ""); + p.change_file("bar", ""); sleep_ms(1000); // make sure the to-be-created outfile has a timestamp distinct from the infiles // now the exist, so run once, catch the mtime, then shouldn't run again @@ -2665,7 +2675,7 @@ fn rebuild_only_on_explicit_paths() { // random other files do not affect freshness println!("run baz"); - File::create(p.root().join("baz")).unwrap(); + p.change_file("baz", ""); p.cargo("build -v") .with_stderr( "\ @@ -2677,7 +2687,7 @@ fn rebuild_only_on_explicit_paths() { // but changing dependent files does println!("run foo change"); - File::create(p.root().join("foo")).unwrap(); + p.change_file("foo", ""); p.cargo("build -v") .with_stderr( "\ @@ -3306,19 +3316,17 @@ fn switch_features_rerun() { "build.rs", r#" use std::env; - use std::fs::File; - use std::io::Write; + use std::fs; use std::path::Path; fn main() { let out_dir = env::var_os("OUT_DIR").unwrap(); - let out_dir = Path::new(&out_dir).join("output"); - let mut f = File::create(&out_dir).unwrap(); + let output = Path::new(&out_dir).join("output"); if env::var_os("CARGO_FEATURE_FOO").is_some() { - f.write_all(b"foo").unwrap(); + fs::write(output, "foo").unwrap(); } else { - f.write_all(b"bar").unwrap(); + fs::write(output, "bar").unwrap(); } } "#, diff --git a/tests/testsuite/build_script_env.rs b/tests/testsuite/build_script_env.rs index b11f381059b..49221296dfd 100644 --- a/tests/testsuite/build_script_env.rs +++ b/tests/testsuite/build_script_env.rs @@ -1,7 +1,5 @@ //! Tests for build.rs rerun-if-env-changed. -use std::fs::File; - use cargo_test_support::project; use cargo_test_support::sleep_ms; @@ -97,7 +95,7 @@ fn rerun_if_env_or_file_changes() { .with_stderr("[FINISHED] [..]") .run(); sleep_ms(1000); - File::create(p.root().join("foo")).unwrap(); + p.change_file("foo", ""); p.cargo("build") .env("FOO", "bar") .with_stderr( diff --git a/tests/testsuite/cargo_command.rs b/tests/testsuite/cargo_command.rs index d0aab9e7c44..80cff54754f 100644 --- a/tests/testsuite/cargo_command.rs +++ b/tests/testsuite/cargo_command.rs @@ -2,7 +2,6 @@ use std::env; use std::fs::{self, File}; -use std::io::prelude::*; use std::path::{Path, PathBuf}; use std::str; @@ -174,15 +173,14 @@ fn find_closest_alias() { let root = paths::root(); let my_home = root.join("my_home"); fs::create_dir(&my_home).unwrap(); - File::create(&my_home.join("config")) - .unwrap() - .write_all( - br#" - [alias] - myalias = "build" - "#, - ) - .unwrap(); + fs::write( + &my_home.join("config"), + r#" + [alias] + myalias = "build" + "#, + ) + .unwrap(); cargo_process("myalais") .env("CARGO_HOME", &my_home) @@ -239,17 +237,16 @@ fn override_cargo_home() { let root = paths::root(); let my_home = root.join("my_home"); fs::create_dir(&my_home).unwrap(); - File::create(&my_home.join("config")) - .unwrap() - .write_all( - br#" - [cargo-new] - name = "foo" - email = "bar" - git = false - "#, - ) - .unwrap(); + fs::write( + &my_home.join("config"), + r#" + [cargo-new] + name = "foo" + email = "bar" + git = false + "#, + ) + .unwrap(); cargo_process("new foo") .env("USER", "foo") @@ -257,11 +254,7 @@ fn override_cargo_home() { .run(); let toml = paths::root().join("foo/Cargo.toml"); - let mut contents = String::new(); - File::open(&toml) - .unwrap() - .read_to_string(&mut contents) - .unwrap(); + let contents = fs::read_to_string(&toml).unwrap(); assert!(contents.contains(r#"authors = ["foo "]"#)); } diff --git a/tests/testsuite/concurrent.rs b/tests/testsuite/concurrent.rs index 38b4647010a..6c4e12500c4 100644 --- a/tests/testsuite/concurrent.rs +++ b/tests/testsuite/concurrent.rs @@ -1,7 +1,6 @@ //! Tests for running multiple `cargo` processes at the same time. -use std::fs::{self, File}; -use std::io::Write; +use std::fs; use std::net::TcpListener; use std::process::Stdio; use std::sync::mpsc::channel; @@ -185,10 +184,7 @@ fn git_same_repo_different_tags() { let repo = git2::Repository::open(&a.root()).unwrap(); git::tag(&repo, "tag1"); - File::create(a.root().join("src/lib.rs")) - .unwrap() - .write_all(b"pub fn tag2() {}") - .unwrap(); + a.change_file("src/lib.rs", "pub fn tag2() {}"); git::add(&repo); git::commit(&repo); git::tag(&repo, "tag2"); @@ -308,10 +304,7 @@ fn git_same_branch_different_revs() { // Make a new commit on the master branch let repo = git2::Repository::open(&a.root()).unwrap(); - File::create(a.root().join("src/lib.rs")) - .unwrap() - .write_all(b"pub fn f2() {}") - .unwrap(); + a.change_file("src/lib.rs", "pub fn f2() {}"); git::add(&repo); git::commit(&repo); diff --git a/tests/testsuite/directory.rs b/tests/testsuite/directory.rs index 157a3512e5d..defbffaf030 100644 --- a/tests/testsuite/directory.rs +++ b/tests/testsuite/directory.rs @@ -1,8 +1,7 @@ //! Tests for directory sources. use std::collections::HashMap; -use std::fs::{self, File}; -use std::io::prelude::*; +use std::fs; use std::str; use serde::Serialize; @@ -16,8 +15,9 @@ use cargo_test_support::{basic_manifest, project, t, ProjectBuilder}; fn setup() { let root = paths::root(); t!(fs::create_dir(&root.join(".cargo"))); - t!(t!(File::create(root.join(".cargo/config"))).write_all( - br#" + t!(fs::write( + root.join(".cargo/config"), + r#" [source.crates-io] replace-with = 'my-awesome-local-registry' @@ -442,8 +442,10 @@ fn bad_file_checksum() { .file("src/lib.rs", "") .build(); - let mut f = t!(File::create(paths::root().join("index/bar/src/lib.rs"))); - t!(f.write_all(b"fn bar() -> u32 { 0 }")); + t!(fs::write( + paths::root().join("index/bar/src/lib.rs"), + "fn bar() -> u32 { 0 }" + )); let p = project() .file( @@ -576,24 +578,23 @@ fn git_lock_file_doesnt_change() { p.cargo("build").run(); - let mut lock1 = String::new(); - t!(t!(File::open(p.root().join("Cargo.lock"))).read_to_string(&mut lock1)); + let lock1 = p.read_lockfile(); let root = paths::root(); t!(fs::create_dir(&root.join(".cargo"))); - t!(t!(File::create(root.join(".cargo/config"))).write_all( + t!(fs::write( + root.join(".cargo/config"), format!( r#" - [source.my-git-repo] - git = '{}' - replace-with = 'my-awesome-local-registry' + [source.my-git-repo] + git = '{}' + replace-with = 'my-awesome-local-registry' - [source.my-awesome-local-registry] - directory = 'index' - "#, + [source.my-awesome-local-registry] + directory = 'index' + "#, git.url() ) - .as_bytes() )); p.cargo("build") @@ -606,8 +607,7 @@ fn git_lock_file_doesnt_change() { ) .run(); - let mut lock2 = String::new(); - t!(t!(File::open(p.root().join("Cargo.lock"))).read_to_string(&mut lock2)); + let lock2 = p.read_lockfile(); assert_eq!(lock1, lock2, "lock files changed"); } @@ -637,15 +637,16 @@ fn git_override_requires_lockfile() { let root = paths::root(); t!(fs::create_dir(&root.join(".cargo"))); - t!(t!(File::create(root.join(".cargo/config"))).write_all( - br#" - [source.my-git-repo] - git = 'https://example.com/' - replace-with = 'my-awesome-local-registry' - - [source.my-awesome-local-registry] - directory = 'index' - "# + t!(fs::write( + root.join(".cargo/config"), + r#" + [source.my-git-repo] + git = 'https://example.com/' + replace-with = 'my-awesome-local-registry' + + [source.my-awesome-local-registry] + directory = 'index' + "# )); p.cargo("build") diff --git a/tests/testsuite/doc.rs b/tests/testsuite/doc.rs index 795c5a2e426..0a4a914ef67 100644 --- a/tests/testsuite/doc.rs +++ b/tests/testsuite/doc.rs @@ -1,13 +1,11 @@ //! Tests for the `cargo doc` command. -use std::fs::{self, File}; -use std::io::Read; -use std::str; - use cargo_test_support::paths::CargoPathExt; use cargo_test_support::registry::Package; use cargo_test_support::{basic_lib_manifest, basic_manifest, git, project}; use cargo_test_support::{is_nightly, rustc_host}; +use std::fs; +use std::str; #[cargo_test] fn simple() { @@ -385,14 +383,7 @@ fn doc_lib_bin_same_name_documents_lib() { ", ) .run(); - assert!(p.root().join("target/doc").is_dir()); - let doc_file = p.root().join("target/doc/foo/index.html"); - assert!(doc_file.is_file()); - let mut doc_html = String::new(); - File::open(&doc_file) - .unwrap() - .read_to_string(&mut doc_html) - .unwrap(); + let doc_html = p.read_file("target/doc/foo/index.html"); assert!(doc_html.contains("Library")); assert!(!doc_html.contains("Binary")); } @@ -427,14 +418,7 @@ fn doc_lib_bin_same_name_documents_lib_when_requested() { ", ) .run(); - assert!(p.root().join("target/doc").is_dir()); - let doc_file = p.root().join("target/doc/foo/index.html"); - assert!(doc_file.is_file()); - let mut doc_html = String::new(); - File::open(&doc_file) - .unwrap() - .read_to_string(&mut doc_html) - .unwrap(); + let doc_html = p.read_file("target/doc/foo/index.html"); assert!(doc_html.contains("Library")); assert!(!doc_html.contains("Binary")); } @@ -470,14 +454,7 @@ fn doc_lib_bin_same_name_documents_named_bin_when_requested() { ", ) .run(); - assert!(p.root().join("target/doc").is_dir()); - let doc_file = p.root().join("target/doc/foo/index.html"); - assert!(doc_file.is_file()); - let mut doc_html = String::new(); - File::open(&doc_file) - .unwrap() - .read_to_string(&mut doc_html) - .unwrap(); + let doc_html = p.read_file("target/doc/foo/index.html"); assert!(!doc_html.contains("Library")); assert!(doc_html.contains("Binary")); } @@ -513,14 +490,7 @@ fn doc_lib_bin_same_name_documents_bins_when_requested() { ", ) .run(); - assert!(p.root().join("target/doc").is_dir()); - let doc_file = p.root().join("target/doc/foo/index.html"); - assert!(doc_file.is_file()); - let mut doc_html = String::new(); - File::open(&doc_file) - .unwrap() - .read_to_string(&mut doc_html) - .unwrap(); + let doc_html = p.read_file("target/doc/foo/index.html"); assert!(!doc_html.contains("Library")); assert!(doc_html.contains("Binary")); } diff --git a/tests/testsuite/features.rs b/tests/testsuite/features.rs index ddc289b3b69..3980b9bf85b 100644 --- a/tests/testsuite/features.rs +++ b/tests/testsuite/features.rs @@ -1,11 +1,8 @@ //! Tests for `[features]` table. -use std::fs::File; -use std::io::prelude::*; - use cargo_test_support::paths::CargoPathExt; use cargo_test_support::registry::Package; -use cargo_test_support::{basic_manifest, project, t}; +use cargo_test_support::{basic_manifest, project}; #[cargo_test] fn invalid1() { @@ -920,9 +917,7 @@ fn everything_in_the_lockfile() { .build(); p.cargo("fetch").run(); - let loc = p.root().join("Cargo.lock"); - let mut lockfile = String::new(); - t!(t!(File::open(&loc)).read_to_string(&mut lockfile)); + let lockfile = p.read_lockfile(); assert!( lockfile.contains(r#"name = "d1""#), "d1 not found\n{}", diff --git a/tests/testsuite/fix.rs b/tests/testsuite/fix.rs index d8f28ae22b0..3a293cfc221 100644 --- a/tests/testsuite/fix.rs +++ b/tests/testsuite/fix.rs @@ -1,13 +1,9 @@ //! Tests for the `cargo fix` command. -use std::fs::File; - use cargo_test_support::git; use cargo_test_support::paths; use cargo_test_support::{basic_manifest, project}; -use std::io::Write; - #[cargo_test] fn do_not_fix_broken_builds() { let p = project() @@ -713,7 +709,7 @@ fn warns_if_no_vcs_detected() { fn warns_about_dirty_working_directory() { let p = git::new("foo", |p| p.file("src/lib.rs", "pub fn foo() {}")); - File::create(p.root().join("src/lib.rs")).unwrap(); + p.change_file("src/lib.rs", ""); p.cargo("fix") .with_status(101) @@ -737,10 +733,7 @@ commit the changes to these files: fn warns_about_staged_working_directory() { let (p, repo) = git::new_repo("foo", |p| p.file("src/lib.rs", "pub fn foo() {}")); - File::create(&p.root().join("src/lib.rs")) - .unwrap() - .write_all("pub fn bar() {}".to_string().as_bytes()) - .unwrap(); + p.change_file("src/lib.rs", "pub fn bar() {}"); git::add(&repo); p.cargo("fix") @@ -774,7 +767,7 @@ fn does_not_warn_about_dirty_ignored_files() { .file(".gitignore", "bar\n") }); - File::create(p.root().join("bar")).unwrap(); + p.change_file("bar", ""); p.cargo("fix").run(); } diff --git a/tests/testsuite/freshness.rs b/tests/testsuite/freshness.rs index 1098cd9d1cc..827fe2f756d 100644 --- a/tests/testsuite/freshness.rs +++ b/tests/testsuite/freshness.rs @@ -1,7 +1,7 @@ //! Tests for fingerprinting (rebuild detection). use filetime::FileTime; -use std::fs::{self, File, OpenOptions}; +use std::fs::{self, OpenOptions}; use std::io; use std::io::prelude::*; use std::net::TcpListener; @@ -34,10 +34,7 @@ fn modifying_and_moving() { p.root().move_into_the_past(); p.root().join("target").move_into_the_past(); - File::create(&p.root().join("src/a.rs")) - .unwrap() - .write_all(b"#[allow(unused)]fn main() {}") - .unwrap(); + p.change_file("src/a.rs", "#[allow(unused)]fn main() {}"); p.cargo("build") .with_stderr( "\ @@ -78,16 +75,8 @@ fn modify_only_some_files() { assert!(p.bin("foo").is_file()); let lib = p.root().join("src/lib.rs"); - let bin = p.root().join("src/b.rs"); - - File::create(&lib) - .unwrap() - .write_all(b"invalid rust code") - .unwrap(); - File::create(&bin) - .unwrap() - .write_all(b"#[allow(unused)]fn foo() {}") - .unwrap(); + p.change_file("src/lib.rs", "invalid rust code"); + p.change_file("src/b.rs", "#[allow(unused)]fn foo() {}"); lib.move_into_the_past(); // Make sure the binary is rebuilt, not the lib @@ -538,7 +527,7 @@ fn rebuild_tests_if_lib_changes() { p.cargo("test").run(); sleep_ms(1000); - File::create(&p.root().join("src/lib.rs")).unwrap(); + p.change_file("src/lib.rs", ""); p.cargo("build -v").run(); p.cargo("test -v") @@ -838,18 +827,16 @@ fn rebuild_if_environment_changes() { ) .run(); - File::create(&p.root().join("Cargo.toml")) - .unwrap() - .write_all( - br#" - [package] - name = "foo" - description = "new desc" - version = "0.0.1" - authors = [] - "#, - ) - .unwrap(); + p.change_file( + "Cargo.toml", + r#" + [package] + name = "foo" + description = "new desc" + version = "0.0.1" + authors = [] + "#, + ); p.cargo("run") .with_stdout("new desc") @@ -1471,7 +1458,7 @@ fn bust_patched_dep() { sleep_ms(1000); } - File::create(&p.root().join("reg1new/src/lib.rs")).unwrap(); + p.change_file("reg1new/src/lib.rs", ""); if is_coarse_mtime() { sleep_ms(1000); } diff --git a/tests/testsuite/generate_lockfile.rs b/tests/testsuite/generate_lockfile.rs index 2d19882e063..4b8865a268f 100644 --- a/tests/testsuite/generate_lockfile.rs +++ b/tests/testsuite/generate_lockfile.rs @@ -1,10 +1,8 @@ //! Tests for the `cargo generate-lockfile` command. -use std::fs::{self, File}; -use std::io::prelude::*; - use cargo_test_support::registry::Package; use cargo_test_support::{basic_manifest, paths, project, ProjectBuilder}; +use std::fs; #[cargo_test] fn adding_and_removing_packages() { @@ -16,33 +14,27 @@ fn adding_and_removing_packages() { p.cargo("generate-lockfile").run(); - let toml = p.root().join("Cargo.toml"); let lock1 = p.read_lockfile(); // add a dep - File::create(&toml) - .unwrap() - .write_all( - br#" - [package] - name = "foo" - authors = [] - version = "0.0.1" - - [dependencies.bar] - path = "bar" - "#, - ) - .unwrap(); + p.change_file( + "Cargo.toml", + r#" + [package] + name = "foo" + authors = [] + version = "0.0.1" + + [dependencies.bar] + path = "bar" + "#, + ); p.cargo("generate-lockfile").run(); let lock2 = p.read_lockfile(); assert_ne!(lock1, lock2); // change the dep - File::create(&p.root().join("bar/Cargo.toml")) - .unwrap() - .write_all(basic_manifest("bar", "0.0.2").as_bytes()) - .unwrap(); + p.change_file("bar/Cargo.toml", &basic_manifest("bar", "0.0.2")); p.cargo("generate-lockfile").run(); let lock3 = p.read_lockfile(); assert_ne!(lock1, lock3); @@ -50,17 +42,15 @@ fn adding_and_removing_packages() { // remove the dep println!("lock4"); - File::create(&toml) - .unwrap() - .write_all( - br#" - [package] - name = "foo" - authors = [] - version = "0.0.1" - "#, - ) - .unwrap(); + p.change_file( + "Cargo.toml", + r#" + [package] + name = "foo" + authors = [] + version = "0.0.1" + "#, + ); p.cargo("generate-lockfile").run(); let lock4 = p.read_lockfile(); assert_eq!(lock1, lock4); @@ -112,13 +102,9 @@ fn preserve_metadata() { bar = "baz" foo = "bar" "#; - let lockfile = p.root().join("Cargo.lock"); let lock = p.read_lockfile(); let data = lock + metadata; - File::create(&lockfile) - .unwrap() - .write_all(data.as_bytes()) - .unwrap(); + p.change_file("Cargo.lock", &data); // Build and make sure the metadata is still there p.cargo("build").run(); @@ -149,12 +135,7 @@ fn preserve_line_endings_issue_2076() { assert!(lock0.starts_with("# This file is automatically @generated by Cargo.\n# It is not intended for manual editing.\n")); let lock1 = lock0.replace("\n", "\r\n"); - { - File::create(&lockfile) - .unwrap() - .write_all(lock1.as_bytes()) - .unwrap(); - } + p.change_file("Cargo.lock", &lock1); p.cargo("generate-lockfile").run(); diff --git a/tests/testsuite/git.rs b/tests/testsuite/git.rs index 26c7d1e7923..dd6a6193a0c 100644 --- a/tests/testsuite/git.rs +++ b/tests/testsuite/git.rs @@ -1,7 +1,7 @@ //! Tests for git support. use std::env; -use std::fs::{self, File}; +use std::fs; use std::io::prelude::*; use std::net::{TcpListener, TcpStream}; use std::path::Path; @@ -501,10 +501,7 @@ fn two_revs_same_deps() { let rev1 = repo.revparse_single("HEAD").unwrap().id(); // Commit the changes and make sure we trigger a recompile - File::create(&bar.root().join("src/lib.rs")) - .unwrap() - .write_all(br#"pub fn bar() -> i32 { 2 }"#) - .unwrap(); + bar.change_file("src/lib.rs", "pub fn bar() -> i32 { 2 }"); git::add(&repo); let rev2 = git::commit(&repo); @@ -623,10 +620,7 @@ fn recompilation() { p.cargo("build").with_stdout("").run(); // Modify a file manually, shouldn't trigger a recompile - File::create(&git_project.root().join("src/bar.rs")) - .unwrap() - .write_all(br#"pub fn bar() { println!("hello!"); }"#) - .unwrap(); + git_project.change_file("src/bar.rs", r#"pub fn bar() { println!("hello!"); }"#); p.cargo("build").with_stdout("").run(); @@ -764,10 +758,7 @@ fn update_with_shared_deps() { .run(); // Modify a file manually, and commit it - File::create(&git_project.root().join("src/bar.rs")) - .unwrap() - .write_all(br#"pub fn bar() { println!("hello!"); }"#) - .unwrap(); + git_project.change_file("src/bar.rs", r#"pub fn bar() { println!("hello!"); }"#); let repo = git2::Repository::open(&git_project.root()).unwrap(); let old_head = repo.head().unwrap().target().unwrap(); git::add(&repo); @@ -1026,10 +1017,7 @@ fn two_deps_only_update_one() { ) .run(); - File::create(&git1.root().join("src/lib.rs")) - .unwrap() - .write_all(br#"pub fn foo() {}"#) - .unwrap(); + git1.change_file("src/lib.rs", "pub fn foo() {}"); let repo = git2::Repository::open(&git1.root()).unwrap(); git::add(&repo); let oid = git::commit(&repo); @@ -1086,10 +1074,7 @@ fn stale_cached_version() { // Update the repo, and simulate someone else updating the lock file and then // us pulling it down. - File::create(&bar.root().join("src/lib.rs")) - .unwrap() - .write_all(br#"pub fn bar() -> i32 { 1 + 0 }"#) - .unwrap(); + bar.change_file("src/lib.rs", "pub fn bar() -> i32 { 1 + 0 }"); let repo = git2::Repository::open(&bar.root()).unwrap(); git::add(&repo); git::commit(&repo); @@ -1098,29 +1083,26 @@ fn stale_cached_version() { let rev = repo.revparse_single("HEAD").unwrap().id(); - File::create(&foo.root().join("Cargo.lock")) - .unwrap() - .write_all( - format!( - r#" - [[package]] - name = "foo" - version = "0.0.0" - dependencies = [ - 'bar 0.0.0 (git+{url}#{hash})' - ] - - [[package]] - name = "bar" - version = "0.0.0" - source = 'git+{url}#{hash}' - "#, - url = bar.url(), - hash = rev - ) - .as_bytes(), - ) - .unwrap(); + foo.change_file( + "Cargo.lock", + &format!( + r#" + [[package]] + name = "foo" + version = "0.0.0" + dependencies = [ + 'bar 0.0.0 (git+{url}#{hash})' + ] + + [[package]] + name = "bar" + version = "0.0.0" + source = 'git+{url}#{hash}' + "#, + url = bar.url(), + hash = rev + ), + ); // Now build! foo.cargo("build") @@ -1194,16 +1176,13 @@ fn dep_with_changed_submodule() { .with_stdout("project2\n") .run(); - File::create(&git_project.root().join(".gitmodules")) - .unwrap() - .write_all( - format!( - "[submodule \"src\"]\n\tpath = src\n\turl={}", - git_project3.url() - ) - .as_bytes(), - ) - .unwrap(); + git_project.change_file( + ".gitmodules", + &format!( + "[submodule \"src\"]\n\tpath = src\n\turl={}", + git_project3.url() + ), + ); // Sync the submodule and reset it to the new remote. sub.sync().unwrap(); @@ -1361,7 +1340,7 @@ fn git_build_cmd_freshness() { // Modify an ignored file and make sure we don't rebuild println!("second pass"); - File::create(&foo.root().join("src/bar.rs")).unwrap(); + foo.change_file("src/bar.rs", ""); foo.cargo("build").with_stdout("").run(); } @@ -1459,10 +1438,7 @@ fn git_repo_changing_no_rebuild() { .run(); // Make a commit to lock p2 to a different rev - File::create(&bar.root().join("src/lib.rs")) - .unwrap() - .write_all(br#"pub fn bar() -> i32 { 2 }"#) - .unwrap(); + bar.change_file("src/lib.rs", "pub fn bar() -> i32 { 2 }"); let repo = git2::Repository::open(&bar.root()).unwrap(); git::add(&repo); git::commit(&repo); @@ -1566,10 +1542,7 @@ fn git_dep_build_cmd() { p.process(&p.bin("foo")).with_stdout("0\n").run(); // Touching bar.rs.in should cause the `build` command to run again. - fs::File::create(&p.root().join("bar/src/bar.rs.in")) - .unwrap() - .write_all(b"pub fn gimme() -> i32 { 1 }") - .unwrap(); + p.change_file("bar/src/bar.rs.in", "pub fn gimme() -> i32 { 1 }"); p.cargo("build").run(); @@ -1836,23 +1809,20 @@ fn switch_deps_does_not_update_transitive() { // Update the dependency to point to the second repository, but this // shouldn't update the transitive dependency which is the same. - File::create(&p.root().join("Cargo.toml")) - .unwrap() - .write_all( - format!( - r#" - [project] - name = "foo" - version = "0.5.0" - authors = [] - [dependencies.dep] - git = '{}' - "#, - dep2.url() - ) - .as_bytes(), - ) - .unwrap(); + p.change_file( + "Cargo.toml", + &format!( + r#" + [project] + name = "foo" + version = "0.5.0" + authors = [] + [dependencies.dep] + git = '{}' + "#, + dep2.url() + ), + ); p.cargo("build") .with_stderr(&format!( @@ -1912,19 +1882,12 @@ fn update_one_source_updates_all_packages_in_that_git_source() { let rev1 = repo.revparse_single("HEAD").unwrap().id(); // Just be sure to change a file - File::create(&dep.root().join("src/lib.rs")) - .unwrap() - .write_all(br#"pub fn bar() -> i32 { 2 }"#) - .unwrap(); + dep.change_file("src/lib.rs", "pub fn bar() -> i32 { 2 }"); git::add(&repo); git::commit(&repo); p.cargo("update -p dep").run(); - let mut lockfile = String::new(); - File::open(&p.root().join("Cargo.lock")) - .unwrap() - .read_to_string(&mut lockfile) - .unwrap(); + let lockfile = p.read_lockfile(); assert!( !lockfile.contains(&rev1.to_string()), "{} in {}", @@ -1988,23 +1951,20 @@ fn switch_sources() { ) .run(); - File::create(&p.root().join("b/Cargo.toml")) - .unwrap() - .write_all( - format!( - r#" - [project] - name = "b" - version = "0.5.0" - authors = [] - [dependencies.a] - git = '{}' - "#, - a2.url() - ) - .as_bytes(), - ) - .unwrap(); + p.change_file( + "b/Cargo.toml", + &format!( + r#" + [project] + name = "b" + version = "0.5.0" + authors = [] + [dependencies.a] + git = '{}' + "#, + a2.url() + ), + ); p.cargo("build") .with_stderr( @@ -2221,24 +2181,21 @@ fn add_a_git_dep() { p.cargo("build").run(); - File::create(p.root().join("a/Cargo.toml")) - .unwrap() - .write_all( - format!( - r#" - [package] - name = "a" - version = "0.0.1" - authors = [] - - [dependencies] - git = {{ git = '{}' }} - "#, - git.url() - ) - .as_bytes(), - ) - .unwrap(); + p.change_file( + "a/Cargo.toml", + &format!( + r#" + [package] + name = "a" + version = "0.0.1" + authors = [] + + [dependencies] + git = {{ git = '{}' }} + "#, + git.url() + ), + ); p.cargo("build").run(); } @@ -2584,25 +2541,23 @@ fn templatedir_doesnt_cause_problems() { .file("src/main.rs", "fn main() {}") .build(); - File::create(paths::home().join(".gitconfig")) - .unwrap() - .write_all( - format!( - r#" + fs::write( + paths::home().join(".gitconfig"), + format!( + r#" [init] templatedir = {} "#, - git_project2 - .url() - .to_file_path() - .unwrap() - .to_str() - .unwrap() - .replace("\\", "/") - ) - .as_bytes(), - ) - .unwrap(); + git_project2 + .url() + .to_file_path() + .unwrap() + .to_str() + .unwrap() + .replace("\\", "/") + ), + ) + .unwrap(); p.cargo("build").run(); } diff --git a/tests/testsuite/init.rs b/tests/testsuite/init.rs index bb554983e9c..9acc01f408a 100644 --- a/tests/testsuite/init.rs +++ b/tests/testsuite/init.rs @@ -1,12 +1,10 @@ //! Tests for the `cargo init` command. +use cargo_test_support::{command_is_available, paths, Execs}; use std::env; -use std::fs::{self, File}; -use std::io::prelude::*; +use std::fs; use std::process::Command; -use cargo_test_support::{command_is_available, paths, Execs}; - fn cargo_process(s: &str) -> Execs { let mut execs = cargo_test_support::cargo_process(s); execs.cwd(&paths::root()).env("HOME", &paths::home()); @@ -79,11 +77,7 @@ fn simple_git_ignore_exists() { assert!(paths::root().join("foo/.gitignore").is_file()); let fp = paths::root().join("foo/.gitignore"); - let mut contents = String::new(); - File::open(&fp) - .unwrap() - .read_to_string(&mut contents) - .unwrap(); + let contents = fs::read_to_string(fp).unwrap(); assert_eq!( contents, "/target\n\ @@ -110,11 +104,7 @@ fn git_ignore_exists_no_conflicting_entries() { .run(); let fp = paths::root().join("foo/.gitignore"); - let mut contents = String::new(); - File::open(&fp) - .unwrap() - .read_to_string(&mut contents) - .unwrap(); + let contents = fs::read_to_string(&fp).unwrap(); assert_eq!( contents, "**/some.file\n\n\ @@ -140,16 +130,13 @@ fn bin_already_exists(explicit: bool, rellocation: &str) { let sourcefile_path = path.join(rellocation); - let content = br#" + let content = r#" fn main() { println!("Hello, world 2!"); } "#; - File::create(&sourcefile_path) - .unwrap() - .write_all(content) - .unwrap(); + fs::write(&sourcefile_path, content).unwrap(); if explicit { cargo_process("init --bin --vcs none") @@ -167,12 +154,8 @@ fn bin_already_exists(explicit: bool, rellocation: &str) { assert!(!paths::root().join("foo/src/lib.rs").is_file()); // Check that our file is not overwritten - let mut new_content = Vec::new(); - File::open(&sourcefile_path) - .unwrap() - .read_to_end(&mut new_content) - .unwrap(); - assert_eq!(Vec::from(content as &[u8]), new_content); + let new_content = fs::read_to_string(&sourcefile_path).unwrap(); + assert_eq!(content, new_content); } #[cargo_test] @@ -210,22 +193,19 @@ fn confused_by_multiple_lib_files() { let path = paths::root().join("foo"); fs::create_dir_all(&path.join("src")).unwrap(); - let sourcefile_path1 = path.join("src/lib.rs"); - - File::create(&sourcefile_path1) - .unwrap() - .write_all(br#"fn qqq () { println!("Hello, world 2!"); }"#) - .unwrap(); + let path1 = path.join("src/lib.rs"); + fs::write(path1, r#"fn qqq () { println!("Hello, world 2!"); }"#).unwrap(); - let sourcefile_path2 = path.join("lib.rs"); + let path2 = path.join("lib.rs"); + fs::write(path2, r#" fn qqq () { println!("Hello, world 3!"); }"#).unwrap(); - File::create(&sourcefile_path2) - .unwrap() - .write_all(br#" fn qqq () { println!("Hello, world 3!"); }"#) - .unwrap(); - - cargo_process("init --vcs none").env("USER", "foo").cwd(&path).with_status(101).with_stderr( - "[ERROR] cannot have a package with multiple libraries, found both `src/lib.rs` and `lib.rs`", + cargo_process("init --vcs none") + .env("USER", "foo") + .cwd(&path) + .with_status(101) + .with_stderr( + "[ERROR] cannot have a package with multiple libraries, \ + found both `src/lib.rs` and `lib.rs`", ) .run(); @@ -237,19 +217,11 @@ fn multibin_project_name_clash() { let path = paths::root().join("foo"); fs::create_dir(&path).unwrap(); - let sourcefile_path1 = path.join("foo.rs"); - - File::create(&sourcefile_path1) - .unwrap() - .write_all(br#"fn main () { println!("Hello, world 2!"); }"#) - .unwrap(); + let path1 = path.join("foo.rs"); + fs::write(path1, r#"fn main () { println!("Hello, world 2!"); }"#).unwrap(); - let sourcefile_path2 = path.join("main.rs"); - - File::create(&sourcefile_path2) - .unwrap() - .write_all(br#"fn main () { println!("Hello, world 3!"); }"#) - .unwrap(); + let path2 = path.join("main.rs"); + fs::write(path2, r#"fn main () { println!("Hello, world 3!"); }"#).unwrap(); cargo_process("init --lib --vcs none") .env("USER", "foo") @@ -274,14 +246,8 @@ fn lib_already_exists(rellocation: &str) { let sourcefile_path = path.join(rellocation); - let content = br#" - pub fn qqq() {} - "#; - - File::create(&sourcefile_path) - .unwrap() - .write_all(content) - .unwrap(); + let content = "pub fn qqq() {}"; + fs::write(&sourcefile_path, content).unwrap(); cargo_process("init --vcs none") .env("USER", "foo") @@ -292,12 +258,8 @@ fn lib_already_exists(rellocation: &str) { assert!(!paths::root().join("foo/src/main.rs").is_file()); // Check that our file is not overwritten - let mut new_content = Vec::new(); - File::open(&sourcefile_path) - .unwrap() - .read_to_end(&mut new_content) - .unwrap(); - assert_eq!(Vec::from(content as &[u8]), new_content); + let new_content = fs::read_to_string(&sourcefile_path).unwrap(); + assert_eq!(content, new_content); } #[cargo_test] @@ -397,10 +359,7 @@ fn mercurial_autodetect() { fn gitignore_appended_not_replaced() { fs::create_dir(&paths::root().join(".git")).unwrap(); - File::create(&paths::root().join(".gitignore")) - .unwrap() - .write_all(b"qqqqqq\n") - .unwrap(); + fs::write(&paths::root().join(".gitignore"), "qqqqqq\n").unwrap(); cargo_process("init --lib").env("USER", "foo").run(); @@ -409,32 +368,21 @@ fn gitignore_appended_not_replaced() { assert!(paths::root().join(".git").is_dir()); assert!(paths::root().join(".gitignore").is_file()); - let mut contents = String::new(); - File::open(&paths::root().join(".gitignore")) - .unwrap() - .read_to_string(&mut contents) - .unwrap(); - assert!(contents.contains(r#"qqqqqq"#)); + let contents = fs::read_to_string(&paths::root().join(".gitignore")).unwrap(); + assert!(contents.contains("qqqqqq")); } #[cargo_test] fn gitignore_added_newline_in_existing() { fs::create_dir(&paths::root().join(".git")).unwrap(); - File::create(&paths::root().join(".gitignore")) - .unwrap() - .write_all(b"first") - .unwrap(); + fs::write(&paths::root().join(".gitignore"), "first").unwrap(); cargo_process("init --lib").env("USER", "foo").run(); assert!(paths::root().join(".gitignore").is_file()); - let mut contents = String::new(); - File::open(&paths::root().join(".gitignore")) - .unwrap() - .read_to_string(&mut contents) - .unwrap(); + let contents = fs::read_to_string(&paths::root().join(".gitignore")).unwrap(); assert!(contents.starts_with("first\n")); } @@ -446,11 +394,7 @@ fn gitignore_no_newline_in_new() { assert!(paths::root().join(".gitignore").is_file()); - let mut contents = String::new(); - File::open(&paths::root().join(".gitignore")) - .unwrap() - .read_to_string(&mut contents) - .unwrap(); + let contents = fs::read_to_string(&paths::root().join(".gitignore")).unwrap(); assert!(!contents.starts_with('\n')); } @@ -458,20 +402,13 @@ fn gitignore_no_newline_in_new() { fn mercurial_added_newline_in_existing() { fs::create_dir(&paths::root().join(".hg")).unwrap(); - File::create(&paths::root().join(".hgignore")) - .unwrap() - .write_all(b"first") - .unwrap(); + fs::write(&paths::root().join(".hgignore"), "first").unwrap(); cargo_process("init --lib").env("USER", "foo").run(); assert!(paths::root().join(".hgignore").is_file()); - let mut contents = String::new(); - File::open(&paths::root().join(".hgignore")) - .unwrap() - .read_to_string(&mut contents) - .unwrap(); + let contents = fs::read_to_string(&paths::root().join(".hgignore")).unwrap(); assert!(contents.starts_with("first\n")); } @@ -483,11 +420,7 @@ fn mercurial_no_newline_in_new() { assert!(paths::root().join(".hgignore").is_file()); - let mut contents = String::new(); - File::open(&paths::root().join(".hgignore")) - .unwrap() - .read_to_string(&mut contents) - .unwrap(); + let contents = fs::read_to_string(&paths::root().join(".hgignore")).unwrap(); assert!(!contents.starts_with('\n')); } @@ -558,11 +491,7 @@ fn cargo_lock_gitignored_if_lib1() { assert!(paths::root().join(".gitignore").is_file()); - let mut contents = String::new(); - File::open(&paths::root().join(".gitignore")) - .unwrap() - .read_to_string(&mut contents) - .unwrap(); + let contents = fs::read_to_string(&paths::root().join(".gitignore")).unwrap(); assert!(contents.contains(r#"Cargo.lock"#)); } @@ -570,20 +499,13 @@ fn cargo_lock_gitignored_if_lib1() { fn cargo_lock_gitignored_if_lib2() { fs::create_dir(&paths::root().join(".git")).unwrap(); - File::create(&paths::root().join("lib.rs")) - .unwrap() - .write_all(br#""#) - .unwrap(); + fs::write(&paths::root().join("lib.rs"), "").unwrap(); cargo_process("init --vcs git").env("USER", "foo").run(); assert!(paths::root().join(".gitignore").is_file()); - let mut contents = String::new(); - File::open(&paths::root().join(".gitignore")) - .unwrap() - .read_to_string(&mut contents) - .unwrap(); + let contents = fs::read_to_string(&paths::root().join(".gitignore")).unwrap(); assert!(contents.contains(r#"Cargo.lock"#)); } @@ -597,11 +519,7 @@ fn cargo_lock_not_gitignored_if_bin1() { assert!(paths::root().join(".gitignore").is_file()); - let mut contents = String::new(); - File::open(&paths::root().join(".gitignore")) - .unwrap() - .read_to_string(&mut contents) - .unwrap(); + let contents = fs::read_to_string(&paths::root().join(".gitignore")).unwrap(); assert!(!contents.contains(r#"Cargo.lock"#)); } @@ -609,20 +527,13 @@ fn cargo_lock_not_gitignored_if_bin1() { fn cargo_lock_not_gitignored_if_bin2() { fs::create_dir(&paths::root().join(".git")).unwrap(); - File::create(&paths::root().join("main.rs")) - .unwrap() - .write_all(br#""#) - .unwrap(); + fs::write(&paths::root().join("main.rs"), "").unwrap(); cargo_process("init --vcs git").env("USER", "foo").run(); assert!(paths::root().join(".gitignore").is_file()); - let mut contents = String::new(); - File::open(&paths::root().join(".gitignore")) - .unwrap() - .read_to_string(&mut contents) - .unwrap(); + let contents = fs::read_to_string(&paths::root().join(".gitignore")).unwrap(); assert!(!contents.contains(r#"Cargo.lock"#)); } diff --git a/tests/testsuite/install.rs b/tests/testsuite/install.rs index 6ec9e8c9a94..3ed59c9ddfe 100644 --- a/tests/testsuite/install.rs +++ b/tests/testsuite/install.rs @@ -1,6 +1,6 @@ //! Tests for the `cargo install` command. -use std::fs::{self, File, OpenOptions}; +use std::fs::{self, OpenOptions}; use std::io::prelude::*; use cargo_test_support::cross_compile; @@ -201,18 +201,16 @@ fn install_location_precedence() { let t4 = cargo_home(); fs::create_dir(root.join(".cargo")).unwrap(); - File::create(root.join(".cargo/config")) - .unwrap() - .write_all( - format!( - "[install] - root = '{}' - ", - t3.display() - ) - .as_bytes(), - ) - .unwrap(); + fs::write( + root.join(".cargo/config"), + &format!( + "[install] + root = '{}' + ", + t3.display() + ), + ) + .unwrap(); println!("install --root"); diff --git a/tests/testsuite/local_registry.rs b/tests/testsuite/local_registry.rs index 3b923f32813..38b54372424 100644 --- a/tests/testsuite/local_registry.rs +++ b/tests/testsuite/local_registry.rs @@ -1,24 +1,23 @@ //! Tests for local-registry sources. -use std::fs::{self, File}; -use std::io::prelude::*; - use cargo_test_support::paths::{self, CargoPathExt}; use cargo_test_support::registry::{registry_path, Package}; use cargo_test_support::{basic_manifest, project, t}; +use std::fs; fn setup() { let root = paths::root(); t!(fs::create_dir(&root.join(".cargo"))); - t!(t!(File::create(root.join(".cargo/config"))).write_all( - br#" - [source.crates-io] - registry = 'https://wut' - replace-with = 'my-awesome-local-registry' - - [source.my-awesome-local-registry] - local-registry = 'registry' - "# + t!(fs::write( + root.join(".cargo/config"), + r#" + [source.crates-io] + registry = 'https://wut' + replace-with = 'my-awesome-local-registry' + + [source.my-awesome-local-registry] + local-registry = 'registry' + "# )); } @@ -441,14 +440,15 @@ unable to verify that `bar v0.0.1` is the same as when the lockfile was generate fn crates_io_registry_url_is_optional() { let root = paths::root(); t!(fs::create_dir(&root.join(".cargo"))); - t!(t!(File::create(root.join(".cargo/config"))).write_all( - br#" - [source.crates-io] - replace-with = 'my-awesome-local-registry' - - [source.my-awesome-local-registry] - local-registry = 'registry' - "# + t!(fs::write( + root.join(".cargo/config"), + r#" + [source.crates-io] + replace-with = 'my-awesome-local-registry' + + [source.my-awesome-local-registry] + local-registry = 'registry' + "# )); Package::new("bar", "0.0.1") diff --git a/tests/testsuite/login.rs b/tests/testsuite/login.rs index 33129141a5e..293ecb3acd0 100644 --- a/tests/testsuite/login.rs +++ b/tests/testsuite/login.rs @@ -1,14 +1,13 @@ //! Tests for the `cargo login` command. -use std::fs::{self, File, OpenOptions}; -use std::io::prelude::*; -use std::path::PathBuf; - use cargo::core::Shell; use cargo::util::config::Config; use cargo_test_support::install::cargo_home; use cargo_test_support::registry::{self, registry_url}; use cargo_test_support::{cargo_process, paths, t}; +use std::fs::{self, OpenOptions}; +use std::io::prelude::*; +use std::path::PathBuf; const TOKEN: &str = "test-token"; const TOKEN2: &str = "test-token2"; @@ -26,19 +25,17 @@ fn setup_new_credentials_toml() { fn setup_new_credentials_at(config: PathBuf) { t!(fs::create_dir_all(config.parent().unwrap())); - t!(t!(File::create(&config)) - .write_all(format!(r#"token = "{token}""#, token = ORIGINAL_TOKEN).as_bytes())); + t!(fs::write( + &config, + format!(r#"token = "{token}""#, token = ORIGINAL_TOKEN) + )); } fn check_token(expected_token: &str, registry: Option<&str>) -> bool { let credentials = cargo_home().join("credentials"); assert!(credentials.is_file()); - let mut contents = String::new(); - File::open(&credentials) - .unwrap() - .read_to_string(&mut contents) - .unwrap(); + let contents = fs::read_to_string(&credentials).unwrap(); let toml: toml::Value = contents.parse().unwrap(); let token = match (registry, toml) { diff --git a/tests/testsuite/new.rs b/tests/testsuite/new.rs index 7455094d688..4cc14e1dd14 100644 --- a/tests/testsuite/new.rs +++ b/tests/testsuite/new.rs @@ -1,11 +1,9 @@ //! Tests for the `cargo new` command. -use std::env; -use std::fs::{self, File}; -use std::io::prelude::*; - use cargo_test_support::paths; use cargo_test_support::{cargo_process, git_process}; +use std::env; +use std::fs::{self, File}; fn create_empty_gitconfig() { // This helps on Windows where libgit2 is very aggressive in attempting to @@ -27,11 +25,7 @@ fn simple_lib() { assert!(!paths::root().join("foo/.gitignore").is_file()); let lib = paths::root().join("foo/src/lib.rs"); - let mut contents = String::new(); - File::open(&lib) - .unwrap() - .read_to_string(&mut contents) - .unwrap(); + let contents = fs::read_to_string(&lib).unwrap(); assert_eq!( contents, r#"#[cfg(test)] @@ -86,11 +80,7 @@ fn simple_git() { assert!(paths::root().join("foo/.gitignore").is_file()); let fp = paths::root().join("foo/.gitignore"); - let mut contents = String::new(); - File::open(&fp) - .unwrap() - .read_to_string(&mut contents) - .unwrap(); + let contents = fs::read_to_string(&fp).unwrap(); assert_eq!(contents, "/target\nCargo.lock\n",); cargo_process("build").cwd(&paths::root().join("foo")).run(); @@ -187,11 +177,7 @@ fn finds_author_user() { cargo_process("new foo").env("USER", "foo").run(); let toml = paths::root().join("foo/Cargo.toml"); - let mut contents = String::new(); - File::open(&toml) - .unwrap() - .read_to_string(&mut contents) - .unwrap(); + let contents = fs::read_to_string(&toml).unwrap(); assert!(contents.contains(r#"authors = ["foo"]"#)); } @@ -201,11 +187,7 @@ fn finds_author_user_escaped() { cargo_process("new foo").env("USER", "foo \"bar\"").run(); let toml = paths::root().join("foo/Cargo.toml"); - let mut contents = String::new(); - File::open(&toml) - .unwrap() - .read_to_string(&mut contents) - .unwrap(); + let contents = fs::read_to_string(&toml).unwrap(); assert!(contents.contains(r#"authors = ["foo \"bar\""]"#)); } @@ -218,11 +200,7 @@ fn finds_author_username() { .run(); let toml = paths::root().join("foo/Cargo.toml"); - let mut contents = String::new(); - File::open(&toml) - .unwrap() - .read_to_string(&mut contents) - .unwrap(); + let contents = fs::read_to_string(&toml).unwrap(); assert!(contents.contains(r#"authors = ["foo"]"#)); } @@ -235,11 +213,7 @@ fn finds_author_name() { .run(); let toml = paths::root().join("foo/Cargo.toml"); - let mut contents = String::new(); - File::open(&toml) - .unwrap() - .read_to_string(&mut contents) - .unwrap(); + let contents = fs::read_to_string(&toml).unwrap(); assert!(contents.contains(r#"authors = ["foo"]"#)); } @@ -253,11 +227,7 @@ fn finds_author_priority() { .run(); let toml = paths::root().join("foo/Cargo.toml"); - let mut contents = String::new(); - File::open(&toml) - .unwrap() - .read_to_string(&mut contents) - .unwrap(); + let contents = fs::read_to_string(&toml).unwrap(); assert!(contents.contains(r#"authors = ["bar "]"#)); } @@ -270,11 +240,7 @@ fn finds_author_email() { .run(); let toml = paths::root().join("foo/Cargo.toml"); - let mut contents = String::new(); - File::open(&toml) - .unwrap() - .read_to_string(&mut contents) - .unwrap(); + let contents = fs::read_to_string(&toml).unwrap(); assert!(contents.contains(r#"authors = ["bar "]"#)); } @@ -287,11 +253,7 @@ fn finds_author_git() { cargo_process("new foo").env("USER", "foo").run(); let toml = paths::root().join("foo/Cargo.toml"); - let mut contents = String::new(); - File::open(&toml) - .unwrap() - .read_to_string(&mut contents) - .unwrap(); + let contents = fs::read_to_string(&toml).unwrap(); assert!(contents.contains(r#"authors = ["bar "]"#)); } @@ -309,11 +271,7 @@ fn finds_local_author_git() { cargo_process("init").env("USER", "foo").run(); let toml = paths::root().join("Cargo.toml"); - let mut contents = String::new(); - File::open(&toml) - .unwrap() - .read_to_string(&mut contents) - .unwrap(); + let contents = fs::read_to_string(&toml).unwrap(); assert!(contents.contains(r#"authors = ["bar "]"#)); } @@ -325,11 +283,7 @@ fn finds_git_author() { .run(); let toml = paths::root().join("foo/Cargo.toml"); - let mut contents = String::new(); - File::open(&toml) - .unwrap() - .read_to_string(&mut contents) - .unwrap(); + let contents = fs::read_to_string(&toml).unwrap(); assert!(contents.contains(r#"authors = ["foo "]"#), contents); } @@ -343,11 +297,7 @@ fn finds_git_committer() { .run(); let toml = paths::root().join("foo/Cargo.toml"); - let mut contents = String::new(); - File::open(&toml) - .unwrap() - .read_to_string(&mut contents) - .unwrap(); + let contents = fs::read_to_string(&toml).unwrap(); assert!(contents.contains(r#"authors = ["foo "]"#)); } @@ -359,26 +309,21 @@ fn author_prefers_cargo() { .unwrap(); let root = paths::root(); fs::create_dir(&root.join(".cargo")).unwrap(); - File::create(&root.join(".cargo/config")) - .unwrap() - .write_all( - br#" - [cargo-new] - name = "new-foo" - email = "new-bar" - vcs = "none" - "#, - ) - .unwrap(); + fs::write( + &root.join(".cargo/config"), + r#" + [cargo-new] + name = "new-foo" + email = "new-bar" + vcs = "none" + "#, + ) + .unwrap(); cargo_process("new foo").env("USER", "foo").run(); let toml = paths::root().join("foo/Cargo.toml"); - let mut contents = String::new(); - File::open(&toml) - .unwrap() - .read_to_string(&mut contents) - .unwrap(); + let contents = fs::read_to_string(&toml).unwrap(); assert!(contents.contains(r#"authors = ["new-foo "]"#)); assert!(!root.join("foo/.gitignore").exists()); } @@ -392,11 +337,7 @@ fn strip_angle_bracket_author_email() { .run(); let toml = paths::root().join("foo/Cargo.toml"); - let mut contents = String::new(); - File::open(&toml) - .unwrap() - .read_to_string(&mut contents) - .unwrap(); + let contents = fs::read_to_string(&toml).unwrap(); assert!(contents.contains(r#"authors = ["bar "]"#)); } @@ -404,17 +345,16 @@ fn strip_angle_bracket_author_email() { fn git_prefers_command_line() { let root = paths::root(); fs::create_dir(&root.join(".cargo")).unwrap(); - File::create(&root.join(".cargo/config")) - .unwrap() - .write_all( - br#" - [cargo-new] - vcs = "none" - name = "foo" - email = "bar" - "#, - ) - .unwrap(); + fs::write( + &root.join(".cargo/config"), + r#" + [cargo-new] + vcs = "none" + name = "foo" + email = "bar" + "#, + ) + .unwrap(); cargo_process("new foo --vcs git").env("USER", "foo").run(); assert!(paths::root().join("foo/.gitignore").exists()); diff --git a/tests/testsuite/out_dir.rs b/tests/testsuite/out_dir.rs index 77079cb41f8..f3301a0a088 100644 --- a/tests/testsuite/out_dir.rs +++ b/tests/testsuite/out_dir.rs @@ -1,11 +1,10 @@ //! Tests for --out-dir flag. -use std::env; -use std::fs::{self, File}; -use std::path::Path; - use cargo_test_support::sleep_ms; use cargo_test_support::{basic_manifest, project}; +use std::env; +use std::fs; +use std::path::Path; #[cargo_test] fn binary_with_debug() { @@ -175,8 +174,8 @@ fn include_only_the_binary_from_the_current_package() { fn out_dir_is_a_file() { let p = project() .file("src/main.rs", r#"fn main() { println!("Hello, World!") }"#) + .file("out", "") .build(); - File::create(p.root().join("out")).unwrap(); p.cargo("build -Z unstable-options --out-dir out") .masquerade_as_nightly_cargo() diff --git a/tests/testsuite/package.rs b/tests/testsuite/package.rs index 408b9b94b9d..b12a75eca95 100644 --- a/tests/testsuite/package.rs +++ b/tests/testsuite/package.rs @@ -1,15 +1,13 @@ //! Tests for the `cargo package` command. -use std::fs::{read_to_string, File}; -use std::io::prelude::*; -use std::path::Path; - use cargo_test_support::paths::CargoPathExt; use cargo_test_support::registry::Package; use cargo_test_support::{ basic_manifest, cargo_process, git, path2url, paths, project, publish::validate_crate_contents, registry, symlink_supported, t, }; +use std::fs::{read_to_string, File}; +use std::path::Path; #[cargo_test] fn simple() { @@ -560,18 +558,10 @@ fn package_symlink_to_submodule() { #[cargo_test] fn no_duplicates_from_modified_tracked_files() { - let root = paths::root().join("all"); - let p = git::repo(&root) - .file("Cargo.toml", &basic_manifest("foo", "0.0.1")) - .file("src/main.rs", "fn main() {}") - .build(); - File::create(p.root().join("src/main.rs")) - .unwrap() - .write_all(br#"fn main() { println!("A change!"); }"#) - .unwrap(); - cargo_process("build").cwd(p.root()).run(); - cargo_process("package --list --allow-dirty") - .cwd(p.root()) + let p = git::new("all", |p| p.file("src/main.rs", "fn main() {}")); + p.change_file("src/main.rs", r#"fn main() { println!("A change!"); }"#); + p.cargo("build").run(); + p.cargo("package --list --allow-dirty") .with_stdout( "\ Cargo.lock @@ -669,17 +659,7 @@ fn repackage_on_source_change() { p.cargo("package").run(); // Add another source file - let mut file = File::create(p.root().join("src").join("foo.rs")).unwrap_or_else(|e| { - panic!( - "could not create file {}: {}", - p.root().join("src/foo.rs").display(), - e - ) - }); - - file.write_all(br#"fn main() { println!("foo"); }"#) - .unwrap(); - std::mem::drop(file); + p.change_file("src/foo.rs", r#"fn main() { println!("foo"); }"#); // Check that cargo rebuilds the tarball p.cargo("package") diff --git a/tests/testsuite/patch.rs b/tests/testsuite/patch.rs index 0be730cdea6..1291fd3fafa 100644 --- a/tests/testsuite/patch.rs +++ b/tests/testsuite/patch.rs @@ -1,12 +1,10 @@ //! Tests for `[patch]` table source replacement. -use std::fs::{self, File}; -use std::io::{Read, Write}; - use cargo_test_support::git; use cargo_test_support::paths; use cargo_test_support::registry::Package; -use cargo_test_support::{basic_manifest, project, t}; +use cargo_test_support::{basic_manifest, project}; +use std::fs; #[cargo_test] fn replace() { @@ -260,11 +258,7 @@ fn unused() { .run(); // unused patch should be in the lock file - let mut lock = String::new(); - File::open(p.root().join("Cargo.lock")) - .unwrap() - .read_to_string(&mut lock) - .unwrap(); + let lock = p.read_lockfile(); let toml: toml::Value = toml::from_str(&lock).unwrap(); assert_eq!(toml["patch"]["unused"].as_array().unwrap().len(), 1); assert_eq!(toml["patch"]["unused"][0]["name"].as_str(), Some("bar")); @@ -373,8 +367,9 @@ fn add_patch() { .run(); p.cargo("build").with_stderr("[FINISHED] [..]").run(); - t!(t!(File::create(p.root().join("Cargo.toml"))).write_all( - br#" + p.change_file( + "Cargo.toml", + r#" [package] name = "foo" version = "0.0.1" @@ -385,8 +380,8 @@ fn add_patch() { [patch.crates-io] bar = { path = 'bar' } - "# - )); + "#, + ); p.cargo("build") .with_stderr( @@ -436,8 +431,9 @@ fn add_ignored_patch() { .run(); p.cargo("build").with_stderr("[FINISHED] [..]").run(); - t!(t!(File::create(p.root().join("Cargo.toml"))).write_all( - br#" + p.change_file( + "Cargo.toml", + r#" [package] name = "foo" version = "0.0.1" @@ -448,8 +444,8 @@ fn add_ignored_patch() { [patch.crates-io] bar = { path = 'bar' } - "# - )); + "#, + ); p.cargo("build") .with_stderr( @@ -660,8 +656,9 @@ fn new_major() { .with_stderr("[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]") .run(); - t!(t!(File::create(p.root().join("Cargo.toml"))).write_all( - br#" + p.change_file( + "Cargo.toml", + r#" [package] name = "foo" version = "0.0.1" @@ -669,8 +666,8 @@ fn new_major() { [dependencies] bar = "0.2.0" - "# - )); + "#, + ); p.cargo("build") .with_stderr( "\ @@ -824,45 +821,31 @@ fn remove_patch() { // Generate a lock file where `foo` is unused p.cargo("build").run(); - let mut lock_file1 = String::new(); - File::open(p.root().join("Cargo.lock")) - .unwrap() - .read_to_string(&mut lock_file1) - .unwrap(); + let lock_file1 = p.read_lockfile(); // Remove `foo` and generate a new lock file form the old one - File::create(p.root().join("Cargo.toml")) - .unwrap() - .write_all( - br#" - [package] - name = "foo" - version = "0.0.1" - authors = [] - - [dependencies] - bar = "0.1" - - [patch.crates-io] - bar = { path = 'bar' } - "#, - ) - .unwrap(); + p.change_file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.0.1" + authors = [] + + [dependencies] + bar = "0.1" + + [patch.crates-io] + bar = { path = 'bar' } + "#, + ); p.cargo("build").run(); - let mut lock_file2 = String::new(); - File::open(p.root().join("Cargo.lock")) - .unwrap() - .read_to_string(&mut lock_file2) - .unwrap(); + let lock_file2 = p.read_lockfile(); // Remove the lock file and build from scratch fs::remove_file(p.root().join("Cargo.lock")).unwrap(); p.cargo("build").run(); - let mut lock_file3 = String::new(); - File::open(p.root().join("Cargo.lock")) - .unwrap() - .read_to_string(&mut lock_file3) - .unwrap(); + let lock_file3 = p.read_lockfile(); assert!(lock_file1.contains("foo")); assert_eq!(lock_file2, lock_file3); diff --git a/tests/testsuite/path.rs b/tests/testsuite/path.rs index c13c3f04d53..2c5e598650d 100644 --- a/tests/testsuite/path.rs +++ b/tests/testsuite/path.rs @@ -1,12 +1,10 @@ //! Tests for `path` dependencies. -use std::fs::{self, File}; -use std::io::prelude::*; - use cargo_test_support::paths::{self, CargoPathExt}; use cargo_test_support::registry::Package; use cargo_test_support::{basic_lib_manifest, basic_manifest, main_file, project}; use cargo_test_support::{sleep_ms, t}; +use std::fs; #[cargo_test] // I have no idea why this is failing spuriously on Windows; @@ -359,10 +357,7 @@ fn deep_dependencies_trigger_rebuild() { // We base recompilation off mtime, so sleep for at least a second to ensure // that this write will change the mtime. sleep_ms(1000); - File::create(&p.root().join("baz/src/baz.rs")) - .unwrap() - .write_all(br#"pub fn baz() { println!("hello!"); }"#) - .unwrap(); + p.change_file("baz/src/baz.rs", r#"pub fn baz() { println!("hello!"); }"#); sleep_ms(1000); p.cargo("build") .with_stderr( @@ -376,15 +371,13 @@ fn deep_dependencies_trigger_rebuild() { // Make sure an update to bar doesn't trigger baz sleep_ms(1000); - File::create(&p.root().join("bar/src/bar.rs")) - .unwrap() - .write_all( - br#" - extern crate baz; - pub fn bar() { println!("hello!"); baz::baz(); } - "#, - ) - .unwrap(); + p.change_file( + "bar/src/bar.rs", + r#" + extern crate baz; + pub fn bar() { println!("hello!"); baz::baz(); } + "#, + ); sleep_ms(1000); p.cargo("build") .with_stderr( @@ -481,10 +474,7 @@ fn nested_deps_recompile() { .run(); sleep_ms(1000); - File::create(&p.root().join("src/main.rs")) - .unwrap() - .write_all(br#"fn main() {}"#) - .unwrap(); + p.change_file("src/main.rs", r#"fn main() {}"#); // This shouldn't recompile `bar` p.cargo("build") @@ -548,10 +538,7 @@ fn override_relative() { .build(); fs::create_dir(&paths::root().join(".cargo")).unwrap(); - File::create(&paths::root().join(".cargo/config")) - .unwrap() - .write_all(br#"paths = ["bar"]"#) - .unwrap(); + fs::write(&paths::root().join(".cargo/config"), r#"paths = ["bar"]"#).unwrap(); let p = project() .file( @@ -725,12 +712,7 @@ fn path_dep_build_cmd() { p.process(&p.bin("foo")).with_stdout("0\n").run(); // Touching bar.rs.in should cause the `build` command to run again. - { - let file = fs::File::create(&p.root().join("bar/src/bar.rs.in")); - file.unwrap() - .write_all(br#"pub fn gimme() -> i32 { 1 }"#) - .unwrap(); - } + p.change_file("bar/src/bar.rs.in", "pub fn gimme() -> i32 { 1 }"); p.cargo("build") .with_stderr( @@ -963,20 +945,18 @@ fn invalid_path_dep_in_workspace_with_lockfile() { p.cargo("build").run(); // Change the dependency on `bar` to an invalid path - File::create(&p.root().join("foo/Cargo.toml")) - .unwrap() - .write_all( - br#" - [project] - name = "foo" - version = "0.5.0" - authors = [] - - [dependencies] - bar = { path = "" } - "#, - ) - .unwrap(); + p.change_file( + "foo/Cargo.toml", + r#" + [project] + name = "foo" + version = "0.5.0" + authors = [] + + [dependencies] + bar = { path = "" } + "#, + ); // Make sure we get a nice error. In the past this actually stack // overflowed! diff --git a/tests/testsuite/publish.rs b/tests/testsuite/publish.rs index 345f269764d..7f4d1ad1854 100644 --- a/tests/testsuite/publish.rs +++ b/tests/testsuite/publish.rs @@ -1,12 +1,10 @@ //! Tests for the `cargo publish` command. -use std::fs::{self, File}; -use std::io::prelude::*; - use cargo_test_support::git::{self, repo}; use cargo_test_support::paths; use cargo_test_support::registry::{self, registry_path, registry_url, Package}; use cargo_test_support::{basic_manifest, project, publish}; +use std::fs; const CLEAN_FOO_JSON: &str = r#" { @@ -139,10 +137,7 @@ fn old_token_location() { .with_stderr_contains("[ERROR] no upload token found, please run `cargo login`") .run(); - File::create(&credentials) - .unwrap() - .write_all(br#"token = "api-token""#) - .unwrap(); + fs::write(&credentials, r#"token = "api-token""#).unwrap(); p.cargo("publish --no-verify --index") .arg(registry_url().to_string()) diff --git a/tests/testsuite/registry.rs b/tests/testsuite/registry.rs index 297884544d2..031064fba65 100644 --- a/tests/testsuite/registry.rs +++ b/tests/testsuite/registry.rs @@ -1,15 +1,13 @@ //! Tests for normal registry dependencies. -use std::fs::{self, File}; -use std::io::prelude::*; -use std::path::Path; - use cargo::util::paths::remove_dir_all; use cargo_test_support::cargo_process; use cargo_test_support::git; use cargo_test_support::paths::{self, CargoPathExt}; use cargo_test_support::registry::{self, registry_path, registry_url, Dependency, Package}; use cargo_test_support::{basic_manifest, project, t}; +use std::fs::{self, File}; +use std::path::Path; #[cargo_test] fn simple() { @@ -671,8 +669,9 @@ fn yanks_in_lockfiles_are_ok_with_new_dep() { Package::new("bar", "0.0.1").yanked(true).publish(); Package::new("baz", "0.0.1").publish(); - t!(t!(File::create(p.root().join("Cargo.toml"))).write_all( - br#" + p.change_file( + "Cargo.toml", + r#" [project] name = "foo" version = "0.0.1" @@ -681,8 +680,8 @@ fn yanks_in_lockfiles_are_ok_with_new_dep() { [dependencies] bar = "*" baz = "*" - "# - )); + "#, + ); p.cargo("build").with_stdout("").run(); } @@ -951,8 +950,9 @@ fn updating_a_dep() { ) .run(); - t!(t!(File::create(&p.root().join("a/Cargo.toml"))).write_all( - br#" + p.change_file( + "a/Cargo.toml", + r#" [project] name = "a" version = "0.0.1" @@ -960,8 +960,8 @@ fn updating_a_dep() { [dependencies] bar = "0.1.0" - "# - )); + "#, + ); Package::new("bar", "0.1.0").publish(); println!("second"); @@ -1608,8 +1608,9 @@ fn add_dep_dont_update_registry() { p.cargo("build").run(); - t!(t!(File::create(p.root().join("Cargo.toml"))).write_all( - br#" + p.change_file( + "Cargo.toml", + r#" [project] name = "bar" version = "0.5.0" @@ -1618,8 +1619,8 @@ fn add_dep_dont_update_registry() { [dependencies] baz = { path = "baz" } remote = "0.3" - "# - )); + "#, + ); p.cargo("build") .with_stderr( @@ -1666,8 +1667,9 @@ fn bump_version_dont_update_registry() { p.cargo("build").run(); - t!(t!(File::create(p.root().join("Cargo.toml"))).write_all( - br#" + p.change_file( + "Cargo.toml", + r#" [project] name = "bar" version = "0.6.0" @@ -1675,8 +1677,8 @@ fn bump_version_dont_update_registry() { [dependencies] baz = { path = "baz" } - "# - )); + "#, + ); p.cargo("build") .with_stderr( @@ -1933,15 +1935,14 @@ fn git_init_templatedir_missing() { p.cargo("build").run(); remove_dir_all(paths::home().join(".cargo/registry")).unwrap(); - File::create(paths::home().join(".gitconfig")) - .unwrap() - .write_all( - br#" + fs::write( + paths::home().join(".gitconfig"), + r#" [init] templatedir = nowhere "#, - ) - .unwrap(); + ) + .unwrap(); p.cargo("build").run(); p.cargo("build").run(); diff --git a/tests/testsuite/rustflags.rs b/tests/testsuite/rustflags.rs index 781935adee8..d4e7ce7678b 100644 --- a/tests/testsuite/rustflags.rs +++ b/tests/testsuite/rustflags.rs @@ -1,12 +1,10 @@ //! Tests for setting custom rustc flags. -use std::fs::{self, File}; -use std::io::Write; - use cargo_test_support::registry::Package; use cargo_test_support::{ basic_lib_manifest, basic_manifest, paths, project, project_in_home, rustc_host, }; +use std::fs; #[cargo_test] fn env_rustflags_normal_source() { @@ -871,8 +869,7 @@ fn build_rustflags_recompile() { "#; let config_file = paths::root().join("foo/.cargo/config"); fs::create_dir_all(config_file.parent().unwrap()).unwrap(); - let mut config_file = File::create(config_file).unwrap(); - config_file.write_all(config.as_bytes()).unwrap(); + fs::write(config_file, config).unwrap(); p.cargo("build") .with_status(101) @@ -893,8 +890,7 @@ fn build_rustflags_recompile2() { "#; let config_file = paths::root().join("foo/.cargo/config"); fs::create_dir_all(config_file.parent().unwrap()).unwrap(); - let mut config_file = File::create(config_file).unwrap(); - config_file.write_all(config.as_bytes()).unwrap(); + fs::write(config_file, config).unwrap(); p.cargo("build") .with_status(101) @@ -928,15 +924,14 @@ fn build_rustflags_with_home_config() { let home = paths::home(); let home_config = home.join(".cargo"); fs::create_dir(&home_config).unwrap(); - File::create(&home_config.join("config")) - .unwrap() - .write_all( - br#" - [build] - rustflags = ["-Cllvm-args=-x86-asm-syntax=intel"] - "#, - ) - .unwrap(); + fs::write( + &home_config.join("config"), + r#" + [build] + rustflags = ["-Cllvm-args=-x86-asm-syntax=intel"] + "#, + ) + .unwrap(); // And we need the project to be inside the home directory // so the walking process finds the home project twice. diff --git a/tests/testsuite/search.rs b/tests/testsuite/search.rs index 6229358e602..75ef3985d97 100644 --- a/tests/testsuite/search.rs +++ b/tests/testsuite/search.rs @@ -1,14 +1,12 @@ //! Tests for the `cargo search` command. -use std::collections::HashSet; -use std::fs::{self, File}; -use std::io::prelude::*; -use std::path::Path; - use cargo_test_support::cargo_process; use cargo_test_support::git::repo; use cargo_test_support::paths; use cargo_test_support::registry::{api_path, registry_path, registry_url}; +use std::collections::HashSet; +use std::fs; +use std::path::Path; use url::Url; fn api() -> Url { @@ -48,15 +46,13 @@ fn write_crates(dest: &Path) { // // On windows, though, `?` is an invalid character, but we always build curl // from source there anyway! - File::create(&dest) - .unwrap() - .write_all(content.as_bytes()) - .unwrap(); + fs::write(&dest, content).unwrap(); if !cfg!(windows) { - File::create(&dest.with_file_name("crates?q=postgres&per_page=10")) - .unwrap() - .write_all(content.as_bytes()) - .unwrap(); + fs::write( + &dest.with_file_name("crates?q=postgres&per_page=10"), + content, + ) + .unwrap(); } } @@ -80,11 +76,10 @@ fn setup() { fn set_cargo_config() { let config = paths::root().join(".cargo/config"); - File::create(&config) - .unwrap() - .write_all( - format!( - r#" + fs::write( + &config, + format!( + r#" [source.crates-io] registry = 'https://wut' replace-with = 'dummy-registry' @@ -92,11 +87,10 @@ replace-with = 'dummy-registry' [source.dummy-registry] registry = '{reg}' "#, - reg = registry_url(), - ) - .as_bytes(), - ) - .unwrap(); + reg = registry_url(), + ), + ) + .unwrap(); } #[cargo_test] diff --git a/tests/testsuite/update.rs b/tests/testsuite/update.rs index bd672f04022..86483257aa2 100644 --- a/tests/testsuite/update.rs +++ b/tests/testsuite/update.rs @@ -1,8 +1,5 @@ //! Tests for the `cargo update` command. -use std::fs::File; -use std::io::prelude::*; - use cargo_test_support::registry::Package; use cargo_test_support::{basic_manifest, project}; @@ -42,20 +39,18 @@ fn minor_update_two_places() { p.cargo("build").run(); Package::new("log", "0.1.1").publish(); - File::create(p.root().join("foo/Cargo.toml")) - .unwrap() - .write_all( - br#" - [package] - name = "foo" - version = "0.0.1" - authors = [] + p.change_file( + "foo/Cargo.toml", + r#" + [package] + name = "foo" + version = "0.0.1" + authors = [] - [dependencies] - log = "0.1.1" - "#, - ) - .unwrap(); + [dependencies] + log = "0.1.1" + "#, + ); p.cargo("build").run(); } @@ -630,7 +625,7 @@ fn dry_run_update() { .build(); p.cargo("build").run(); - let old_lockfile = p.read_file("Cargo.lock"); + let old_lockfile = p.read_lockfile(); Package::new("log", "0.1.1").publish(); Package::new("serde", "0.1.1").dep("log", "0.1").publish(); @@ -644,6 +639,6 @@ fn dry_run_update() { ", ) .run(); - let new_lockfile = p.read_file("Cargo.lock"); + let new_lockfile = p.read_lockfile(); assert_eq!(old_lockfile, new_lockfile) } diff --git a/tests/testsuite/workspaces.rs b/tests/testsuite/workspaces.rs index a3390f16cd5..0dfb43b6d0a 100644 --- a/tests/testsuite/workspaces.rs +++ b/tests/testsuite/workspaces.rs @@ -1,12 +1,9 @@ //! Tests for workspaces. -use std::env; -use std::fs::{self, File}; -use std::io::{Read, Write}; - use cargo_test_support::registry::Package; -use cargo_test_support::{basic_lib_manifest, basic_manifest, git, project}; -use cargo_test_support::{sleep_ms, t}; +use cargo_test_support::{basic_lib_manifest, basic_manifest, git, project, sleep_ms}; +use std::env; +use std::fs; #[cargo_test] fn simple_explicit() { @@ -1113,13 +1110,11 @@ fn lock_doesnt_change_depending_on_crate() { p.cargo("build").run(); - let mut lockfile = String::new(); - t!(t!(File::open(p.root().join("Cargo.lock"))).read_to_string(&mut lockfile)); + let lockfile = p.read_lockfile(); p.cargo("build").cwd("baz").run(); - let mut lockfile2 = String::new(); - t!(t!(File::open(p.root().join("Cargo.lock"))).read_to_string(&mut lockfile2)); + let lockfile2 = p.read_lockfile(); assert_eq!(lockfile, lockfile2); } @@ -1168,8 +1163,7 @@ fn rebuild_please() { sleep_ms(1000); - t!(t!(File::create(p.root().join("lib/src/lib.rs"))) - .write_all(br#"pub fn foo() -> u32 { 1 }"#)); + p.change_file("lib/src/lib.rs", "pub fn foo() -> u32 { 1 }"); p.cargo("build").cwd("lib").run();