Skip to content

Commit

Permalink
Fix lift-to-workspace (#87)
Browse files Browse the repository at this point in the history
* Add source location filtering

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Delete old test

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Update UI test lockfiles

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Fix issue with lift-to-workspace

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Clippy

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

---------

Signed-off-by: Oliver Tale-Yazdi <[email protected]>
  • Loading branch information
ggwpez authored Mar 6, 2024
1 parent d820f85 commit dbcce3c
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 20 deletions.
25 changes: 13 additions & 12 deletions src/autofix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -450,23 +450,24 @@ impl AutoFixer {
pub fn lift_dependency(
&mut self,
dname: &str,
kind: &DependencyKind,
default_feats: Option<bool>,
) -> 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(())
}

Expand Down
4 changes: 2 additions & 2 deletions src/cmd/transpose/lift_to_workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,14 @@ impl<R, E: std::fmt::Display> ErrToStr<R> for Result<R, E> {
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!(),
}
}
12 changes: 6 additions & 6 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -1363,10 +1362,11 @@ fn lift_to_workspace_works(
#[case] default: Option<bool>,
#[case] output: Result<Option<&str>, &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) => {
Expand Down
7 changes: 7 additions & 0 deletions tests/integration/runtimes/transpose.yaml
Original file line number Diff line number Diff line change
@@ -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!

0 comments on commit dbcce3c

Please sign in to comment.