Skip to content

Commit

Permalink
fix(poetry): was not clearly translating poetry dep groups to uv ones…
Browse files Browse the repository at this point in the history
… in many circumstances
  • Loading branch information
stvnksslr committed Nov 15, 2024
1 parent 0f814c9 commit 8f3a904
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 18 deletions.
44 changes: 28 additions & 16 deletions src/migrators/poetry.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// In migrators/poetry.rs

use super::{MigrationSource, Dependency, DependencyType};
use super::{Dependency, DependencyType, MigrationSource};
use crate::types::PyProject;
use std::path::Path;
use std::fs;
use std::path::Path;

pub struct PoetryMigrationSource;

Expand All @@ -13,8 +13,13 @@ impl MigrationSource for PoetryMigrationSource {
let contents = fs::read_to_string(&pyproject_path)
.map_err(|e| format!("Error reading file '{}': {}", pyproject_path.display(), e))?;

let pyproject: PyProject = toml::from_str(&contents)
.map_err(|e| format!("Error parsing TOML in '{}': {}", pyproject_path.display(), e))?;
let pyproject: PyProject = toml::from_str(&contents).map_err(|e| {
format!(
"Error parsing TOML in '{}': {}",
pyproject_path.display(),
e
)
})?;

let mut dependencies = Vec::new();

Expand All @@ -23,7 +28,8 @@ impl MigrationSource for PoetryMigrationSource {
// Handle main dependencies
if let Some(deps) = &poetry.dependencies {
for (name, value) in deps {
if let Some(dep) = self.format_dependency(name, value, DependencyType::Main) {
if let Some(dep) = self.format_dependency(name, value, DependencyType::Main)
{
dependencies.push(dep);
}
}
Expand All @@ -33,13 +39,15 @@ impl MigrationSource for PoetryMigrationSource {
if let Some(groups) = &poetry.group {
for (group_name, group) in groups {
// Determine dependency type based on group name
// Only "dev" group should be converted to DependencyType::Dev
let dep_type = match group_name.as_str() {
"dev" | "test" => DependencyType::Dev,
_ => DependencyType::Group(group_name.clone())
"dev" => DependencyType::Dev,
_ => DependencyType::Group(group_name.clone()),
};

for (name, value) in &group.dependencies {
if let Some(dep) = self.format_dependency(name, value, dep_type.clone()) {
if let Some(dep) = self.format_dependency(name, value, dep_type.clone())
{
dependencies.push(dep);
}
}
Expand All @@ -53,7 +61,12 @@ impl MigrationSource for PoetryMigrationSource {
}

impl PoetryMigrationSource {
fn format_dependency(&self, name: &str, value: &toml::Value, dep_type: DependencyType) -> Option<Dependency> {
fn format_dependency(
&self,
name: &str,
value: &toml::Value,
dep_type: DependencyType,
) -> Option<Dependency> {
if name == "python" {
return None;
}
Expand All @@ -66,13 +79,12 @@ impl PoetryMigrationSource {
} else {
Some(v.to_string())
}
},
toml::Value::Table(t) => {
t.get("version")
.and_then(|v| v.as_str())
.map(|v| v.trim().to_string())
.filter(|v| v != "*")
}
toml::Value::Table(t) => t
.get("version")
.and_then(|v| v.as_str())
.map(|v| v.trim().to_string())
.filter(|v| v != "*"),
_ => None,
};

Expand All @@ -83,4 +95,4 @@ impl PoetryMigrationSource {
environment_markers: None,
})
}
}
}
29 changes: 27 additions & 2 deletions tests/poetry_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,17 +261,42 @@ python = "^3.11"
[tool.poetry.group.test.dependencies]
blue = ">=0.9.1"
pytest = "^8.0.0"
pytest-cov = "^4.1.0"
"#;
let (_temp_dir, project_dir) = create_test_project(content);

let source = PoetryMigrationSource;
let dependencies = source.extract_dependencies(&project_dir).unwrap();

assert_eq!(dependencies.len(), 1);
// Should have all test dependencies (excluding python)
assert_eq!(dependencies.len(), 3, "Should have three test dependencies");

// Verify each dependency is in the test group
for dep in &dependencies {
match &dep.dep_type {
DependencyType::Group(name) => {
assert_eq!(name, "test", "Group name should be 'test'");
}
other => panic!("Expected Group(\"test\"), got {:?}", other),
}
}

// Check specific dependency details
let blue_dep = dependencies.iter().find(|d| d.name == "blue").unwrap();
assert_eq!(blue_dep.version, Some(">=0.9.1".to_string()));
assert_eq!(blue_dep.dep_type, DependencyType::Dev); // All group dependencies should be marked as Dev
assert!(matches!(blue_dep.dep_type, DependencyType::Group(ref name) if name == "test"));

let pytest_dep = dependencies.iter().find(|d| d.name == "pytest").unwrap();
assert_eq!(pytest_dep.version, Some("^8.0.0".to_string()));
assert!(matches!(pytest_dep.dep_type, DependencyType::Group(ref name) if name == "test"));

let pytest_cov_dep = dependencies
.iter()
.find(|d| d.name == "pytest-cov")
.unwrap();
assert_eq!(pytest_cov_dep.version, Some("^4.1.0".to_string()));
assert!(matches!(pytest_cov_dep.dep_type, DependencyType::Group(ref name) if name == "test"));
}

#[cfg(test)]
Expand Down

0 comments on commit 8f3a904

Please sign in to comment.