Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add --offline mode fallback to cargo fmt #3813

Merged
merged 3 commits into from
Oct 2, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 12 additions & 17 deletions src/cargo-fmt/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,6 @@ fn execute() -> i32 {
cargo_metadata_additional_opts.push(String::from("--offline"));
}

let cargo_metadata_opts = if !cargo_metadata_additional_opts.is_empty() {
Some(cargo_metadata_additional_opts.as_slice())
} else {
None
};

if let Some(specified_manifest_path) = opts.manifest_path {
if !specified_manifest_path.ends_with("Cargo.toml") {
print_usage_to_stderr("the manifest-path must be a path to a Cargo.toml file");
Expand All @@ -139,15 +133,15 @@ fn execute() -> i32 {
&strategy,
rustfmt_args,
Some(&manifest_path),
cargo_metadata_opts,
&cargo_metadata_additional_opts,
))
} else {
handle_command_status(format_crate(
verbosity,
&strategy,
rustfmt_args,
None,
cargo_metadata_opts,
&cargo_metadata_additional_opts,
))
}
}
Expand Down Expand Up @@ -252,7 +246,7 @@ fn format_crate(
strategy: &CargoFmtStrategy,
rustfmt_args: Vec<String>,
manifest_path: Option<&Path>,
cargo_metadata_additional_opts: Option<&[String]>,
cargo_metadata_additional_opts: &[String],
) -> Result<i32, io::Error> {
let targets = get_targets(strategy, manifest_path, cargo_metadata_additional_opts)?;

Expand Down Expand Up @@ -334,7 +328,7 @@ impl CargoFmtStrategy {
fn get_targets(
strategy: &CargoFmtStrategy,
manifest_path: Option<&Path>,
cargo_metadata_additional_opts: Option<&[String]>,
cargo_metadata_additional_opts: &[String],
) -> Result<BTreeSet<Target>, io::Error> {
let mut targets = BTreeSet::new();

Expand Down Expand Up @@ -369,7 +363,7 @@ fn get_targets(
fn get_targets_root_only(
manifest_path: Option<&Path>,
targets: &mut BTreeSet<Target>,
cargo_metadata_additional_opts: Option<&[String]>,
cargo_metadata_additional_opts: &[String],
) -> Result<(), io::Error> {
let metadata = get_cargo_metadata(manifest_path, false, cargo_metadata_additional_opts)?;
let workspace_root_path = PathBuf::from(&metadata.workspace_root).canonicalize()?;
Expand Down Expand Up @@ -414,7 +408,7 @@ fn get_targets_recursive(
manifest_path: Option<&Path>,
mut targets: &mut BTreeSet<Target>,
visited: &mut BTreeSet<String>,
cargo_metadata_additional_opts: Option<&[String]>,
cargo_metadata_additional_opts: &[String],
) -> Result<(), io::Error> {
let metadata = get_cargo_metadata(manifest_path, false, cargo_metadata_additional_opts)?;
let metadata_with_deps =
Expand Down Expand Up @@ -462,7 +456,7 @@ fn get_targets_with_hitlist(
manifest_path: Option<&Path>,
hitlist: &[String],
targets: &mut BTreeSet<Target>,
cargo_metadata_additional_opts: Option<&[String]>,
cargo_metadata_additional_opts: &[String],
) -> Result<(), io::Error> {
let metadata = get_cargo_metadata(manifest_path, false, cargo_metadata_additional_opts)?;

Expand Down Expand Up @@ -553,7 +547,7 @@ fn run_rustfmt(
fn get_cargo_metadata(
manifest_path: Option<&Path>,
include_deps: bool,
other_opts: Option<&[String]>,
other_opts: &[String],
) -> Result<cargo_metadata::Metadata, io::Error> {
let mut cmd = cargo_metadata::MetadataCommand::new();
if !include_deps {
Expand All @@ -562,9 +556,7 @@ fn get_cargo_metadata(
if let Some(manifest_path) = manifest_path {
cmd.manifest_path(manifest_path);
}
if let Some(opts) = other_opts {
cmd.other_options(opts);
}
cmd.other_options(other_opts);
match cmd.exec() {
Ok(metadata) => Ok(metadata),
Err(error) => Err(io::Error::new(io::ErrorKind::Other, error.to_string())),
Expand All @@ -587,6 +579,7 @@ mod cargo_fmt_tests {
assert_eq!(false, o.format_all);
assert_eq!(None, o.manifest_path);
assert_eq!(None, o.message_format);
assert_eq!(false, o.offline);
}

#[test]
Expand All @@ -598,13 +591,15 @@ mod cargo_fmt_tests {
"p1",
"-p",
"p2",
"--offline",
"--message-format",
"short",
"--",
"--edition",
"2018",
]);
assert_eq!(true, o.quiet);
assert_eq!(true, o.offline);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that unit test is good enough... @topecongiro ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be cool if one day we could have tests that validate that the correct options were passed to the cargo_metadata lib based on command line items passed to cargo fmt, though that seems like it might be a bit tricky

assert_eq!(false, o.verbose);
assert_eq!(false, o.version);
assert_eq!(vec!["p1", "p2"], o.packages);
Expand Down