Skip to content

Commit

Permalink
chore: small code fixes for pyproject code (#1075)
Browse files Browse the repository at this point in the history
Some small minor code changes to the pyproject implementation:

* Move some setters to the `ManifestSource` enum.
* `Manifest::from_str` determine the type of manifest from the path
argument instead of taking an additional argument.
* Move detection of valid pyproject file in project/mod.rs into a
function.
  • Loading branch information
baszalmstra authored Mar 28, 2024
1 parent 8f485c6 commit 263a0c0
Show file tree
Hide file tree
Showing 13 changed files with 114 additions and 139 deletions.
4 changes: 2 additions & 2 deletions src/activation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ mod tests {
[environments]
test = ["test"]
"#;
let project = Project::from_str(Path::new(""), multi_env_project).unwrap();
let project = Project::from_str(Path::new("pixi.toml"), multi_env_project).unwrap();

let default_env = project.default_environment();
let env = default_env.get_metadata_env();
Expand All @@ -270,7 +270,7 @@ mod tests {
channels = ["conda-forge"]
platforms = ["linux-64", "osx-64", "win-64"]
"#;
let project = Project::from_str(Path::new(""), project).unwrap();
let project = Project::from_str(Path::new("pixi.toml"), project).unwrap();
let env = project.get_metadata_env();

assert_eq!(env.get("PIXI_PROJECT_NAME").unwrap(), project.name());
Expand Down
2 changes: 1 addition & 1 deletion src/cli/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ pub async fn execute(args: Args) -> miette::Result<()> {
// - Implement it for `[crate::project::manifest::ProjectManifest]` to do this for other filetypes, e.g. (pyproject.toml, requirements.txt)
let (conda_deps, pypi_deps, channels) = conda_env_to_manifest(conda_env_file, &config)?;
let rv = render_project(&env, name, version, &author, channels, &platforms);
let mut project = Project::from_str(&dir, &rv)?;
let mut project = Project::from_str(&dir.join("pixi.toml"), &rv)?;
for spec in conda_deps {
for platform in platforms.iter() {
// TODO: fix serialization of channels in rattler_conda_types::MatchSpec
Expand Down
12 changes: 6 additions & 6 deletions src/project/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ mod tests {
#[test]
fn test_default_channels() {
let manifest = Project::from_str(
Path::new(""),
Path::new("pixi.toml"),
r#"
[project]
name = "foobar"
Expand Down Expand Up @@ -393,7 +393,7 @@ mod tests {
#[test]
fn test_default_platforms() {
let manifest = Project::from_str(
Path::new(""),
Path::new("pixi.toml"),
r#"
[project]
name = "foobar"
Expand All @@ -413,7 +413,7 @@ mod tests {
#[test]
fn test_default_tasks() {
let manifest = Project::from_str(
Path::new(""),
Path::new("pixi.toml"),
r#"
[project]
name = "foobar"
Expand Down Expand Up @@ -463,7 +463,7 @@ mod tests {
#[test]
fn test_dependencies() {
let manifest = Project::from_str(
Path::new(""),
Path::new("pixi.toml"),
r#"
[project]
name = "foobar"
Expand Down Expand Up @@ -502,7 +502,7 @@ mod tests {
#[test]
fn test_activation() {
let manifest = Project::from_str(
Path::new(""),
Path::new("pixi.toml"),
r#"
[project]
name = "foobar"
Expand Down Expand Up @@ -538,7 +538,7 @@ mod tests {
#[test]
fn test_channel_priorities() {
let manifest = Project::from_str(
Path::new(""),
Path::new("pixi.toml"),
r#"
[project]
name = "foobar"
Expand Down
20 changes: 14 additions & 6 deletions src/project/manifest/document.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use miette::{miette, Report};
use rattler_conda_types::{NamelessMatchSpec, PackageName, Platform};
use std::{fmt, path::Path};
use toml_edit::{Array, Item, Table, Value};
use toml_edit::{value, Array, Item, Table, Value};

use crate::{consts, FeatureName, SpecType, Task};

Expand Down Expand Up @@ -315,12 +315,22 @@ impl ManifestSource {

Ok(())
}

/// Sets the description of the project
pub fn set_description(&mut self, description: &str) {
self.as_table_mut()["project"]["description"] = value(description);
}

/// Sets the version of the project
pub fn set_version(&mut self, version: &str) {
self.as_table_mut()["project"]["version"] = value(version);
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::project::manifest::{Manifest, ManifestKind};
use crate::project::manifest::Manifest;
use insta::assert_snapshot;
use std::path::Path;

Expand All @@ -334,8 +344,7 @@ mod tests {

#[test]
fn test_get_or_insert_toml_table() {
let mut manifest =
Manifest::from_str(Path::new(""), PROJECT_BOILERPLATE, ManifestKind::Pixi).unwrap();
let mut manifest = Manifest::from_str(Path::new("pixi.toml"), PROJECT_BOILERPLATE).unwrap();
let _ = manifest
.document
.get_or_insert_toml_table(None, &FeatureName::Default, "tasks");
Expand Down Expand Up @@ -369,8 +378,7 @@ platforms = ["linux-64", "win-64"]
"#;

let manifest =
Manifest::from_str(Path::new(""), file_contents, ManifestKind::Pixi).unwrap();
let manifest = Manifest::from_str(Path::new("pixi.toml"), file_contents).unwrap();
// Test all different options for the feature name and platform
assert_eq!(
"dependencies".to_string(),
Expand Down
8 changes: 3 additions & 5 deletions src/project/manifest/feature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,14 +290,14 @@ impl<'de> Deserialize<'de> for Feature {
#[cfg(test)]
mod tests {
use super::*;
use crate::project::manifest::{Manifest, ManifestKind};
use crate::project::manifest::Manifest;
use assert_matches::assert_matches;
use std::path::Path;

#[test]
fn test_dependencies_borrowed() {
let manifest = Manifest::from_str(
Path::new(""),
Path::new("pixi.toml"),
r#"
[project]
name = "foo"
Expand All @@ -316,7 +316,6 @@ mod tests {
[feature.bla.host-dependencies]
# empty on purpose
"#,
ManifestKind::Pixi,
)
.unwrap();

Expand Down Expand Up @@ -365,7 +364,7 @@ mod tests {
#[test]
fn test_activation() {
let manifest = Manifest::from_str(
Path::new(""),
Path::new("pixi.toml"),
r#"
[project]
name = "foo"
Expand All @@ -378,7 +377,6 @@ mod tests {
[target.linux-64.activation]
scripts = ["linux-64.bat"]
"#,
ManifestKind::Pixi,
)
.unwrap();

Expand Down
Loading

0 comments on commit 263a0c0

Please sign in to comment.