Skip to content

Commit

Permalink
Auto merge of #10705 - Muscraft:workspace-source-fmt-key, r=epage
Browse files Browse the repository at this point in the history
fix key formatting when switching to a dotted `WorkspaceSource`

This fell out of #10697 see [this comment](#10697 (comment))

There was a small issue where changing the source of a `cargo_add::Dependency` to a `WorkspaceSource` would cause the dotted version to have extra space.

```toml
dep .workspace = true
dep.workspace = true
```

This makes sure the key is formatted as well as adds a unit test to make sure this doesn't come back up in the future.

r? `@epage`
  • Loading branch information
bors committed May 26, 2022
2 parents 39ad103 + 706e5b2 commit 8f5500b
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/cargo/ops/cargo_add/dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,9 +486,7 @@ impl Dependency {
if str_or_1_len_table(item) {
// Nothing to preserve
*item = self.to_toml(crate_root);
if self.source != Some(Source::Workspace(WorkspaceSource)) {
key.fmt();
}
key.fmt();
} else if let Some(table) = item.as_table_like_mut() {
match &self.source {
Some(Source::Registry(src)) => {
Expand Down Expand Up @@ -940,6 +938,7 @@ impl Display for WorkspaceSource {
mod tests {
use std::path::Path;

use crate::ops::cargo_add::manifest::LocalManifest;
use cargo_util::paths;

use super::*;
Expand Down Expand Up @@ -1123,6 +1122,25 @@ mod tests {
verify_roundtrip(&crate_root, key, &item);
}

#[test]
fn overwrite_with_workspace_source_fmt_key() {
let crate_root =
paths::normalize_path(&std::env::current_dir().unwrap().join(Path::new("./")));
let toml = "dep = \"1.0\"\n";
let manifest = toml.parse().unwrap();
let mut local = LocalManifest {
path: crate_root.clone(),
manifest,
};
assert_eq!(local.manifest.to_string(), toml);
for (key, item) in local.data.clone().iter() {
let dep = Dependency::from_toml(&crate_root, key, item).unwrap();
let dep = dep.set_source(WorkspaceSource::new());
local.insert_into_table(&vec![], &dep).unwrap();
assert_eq!(local.data.to_string(), "dep.workspace = true\n");
}
}

#[test]
#[cfg(windows)]
fn normalise_windows_style_paths() {
Expand Down

0 comments on commit 8f5500b

Please sign in to comment.