diff --git a/src/autofix.rs b/src/autofix.rs index 3d40634..ec4ffdd 100644 --- a/src/autofix.rs +++ b/src/autofix.rs @@ -4,7 +4,7 @@ //! Automatically fix problems by modifying `Cargo.toml` files. use crate::{cmd::fmt::Mode, log}; -use cargo_metadata::Dependency; +use cargo_metadata::{Dependency, DependencyKind}; use std::{ collections::BTreeMap as Map, path::{Path, PathBuf}, @@ -450,23 +450,24 @@ impl AutoFixer { pub fn lift_dependency( &mut self, dname: &str, + kind: &DependencyKind, default_feats: Option, ) -> Result<(), String> { + let kind = crate::kind_to_str(kind); let doc: &mut Document = self.doc.as_mut().unwrap(); - for kind in &["dependencies", "dev-dependencies", "build-dependencies"] { - if !doc.contains_table(kind) { - continue - } - let deps: &mut Table = doc[kind].as_table_mut().unwrap(); - - if !deps.contains_key(dname) { - continue - } + if !doc.contains_table(kind) { + return Ok(()) + } + let deps: &mut Table = doc[&kind].as_table_mut().unwrap(); - let dep = deps.get_mut(dname).unwrap(); - Self::lift_some_dependency(dep, default_feats)?; + if !deps.contains_key(dname) { + return Ok(()) } + + let dep = deps.get_mut(dname).unwrap(); + Self::lift_some_dependency(dep, default_feats)?; + Ok(()) } diff --git a/src/cmd/transpose/lift_to_workspace.rs b/src/cmd/transpose/lift_to_workspace.rs index 36db339..4a1a6ee 100644 --- a/src/cmd/transpose/lift_to_workspace.rs +++ b/src/cmd/transpose/lift_to_workspace.rs @@ -191,9 +191,9 @@ impl LiftToWorkspaceCmd { let (_, fixer) = fixers.get_mut(&pkg.name).unwrap(); if dep.uses_default_features != workspace_default_features_enabled { - fixer.lift_dependency(&dep.name, Some(dep.uses_default_features))?; + fixer.lift_dependency(&dep.name, &dep.kind, Some(dep.uses_default_features))?; } else { - fixer.lift_dependency(&dep.name, None)?; + fixer.lift_dependency(&dep.name, &dep.kind, None)?; } } diff --git a/src/lib.rs b/src/lib.rs index 8059ae8..7567b35 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -90,3 +90,14 @@ impl ErrToStr for Result { self.map_err(|e| format!("{}", e)) } } + +use cargo_metadata::DependencyKind; + +pub(crate) fn kind_to_str(kind: &DependencyKind) -> &'static str { + match kind { + DependencyKind::Development => "dev-dependencies", + DependencyKind::Build => "build-dependencies", + DependencyKind::Normal => "dependencies", + _ => unreachable!(), + } +} diff --git a/src/tests.rs b/src/tests.rs index adb2ce9..1f55685 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -5,11 +5,10 @@ use crate::{ autofix::AutoFixer, - cmd::fmt::{ - Mode, - Mode::{Canonicalize, Dedub, Sort}, - }, + cmd::fmt::Mode::{self, Canonicalize, Dedub, Sort}, + kind_to_str, }; +use cargo_metadata::DependencyKind::*; use rstest::*; use std::{collections::BTreeMap as Map, vec}; @@ -1363,10 +1362,11 @@ fn lift_to_workspace_works( #[case] default: Option, #[case] output: Result, &str>, ) { - for table in ["dependencies", "dev-dependencies", "build-dependencies"] { + for kind in [Normal, Build, Development] { + let table = kind_to_str(&kind); let input = &input.replace("dependencies", table); let mut fixer = AutoFixer::from_raw(input).unwrap(); - let res = fixer.lift_dependency("log", default); + let res = fixer.lift_dependency("log", &kind, default); match output { Ok(modify) => { diff --git a/tests/integration/runtimes/transpose.yaml b/tests/integration/runtimes/transpose.yaml new file mode 100644 index 0000000..06667ed --- /dev/null +++ b/tests/integration/runtimes/transpose.yaml @@ -0,0 +1,7 @@ +repo: + name: polkadot-fellows/runtimes + ref: 71ddd5fb0d3bebb90731f921e793b007697d1a6c +cases: +- cmd: transpose dependency lift-to-workspace "regex:bridge-runtime-common" --version-resolver unambiguous + stderr: | + [WARN] Unstable feature - do not rely on this!