Skip to content

Commit

Permalink
test: exercise project channel add/remove with priority
Browse files Browse the repository at this point in the history
  • Loading branch information
minrk committed Sep 20, 2024
1 parent d61fa31 commit 6b86e1c
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 7 deletions.
2 changes: 1 addition & 1 deletion crates/pixi_manifest/src/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl From<PrioritizedChannel> for Value {
table.insert("channel", channel.channel.to_string().into());
table.insert("priority", i64::from(priority).into());
Value::InlineTable(table)
},
}
None => Value::String(toml_edit::Formatted::new(channel.channel.to_string())),
}
}
Expand Down
7 changes: 3 additions & 4 deletions crates/pixi_manifest/src/manifests/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,22 +566,21 @@ impl Manifest {
FeatureName::Default => &mut self.parsed.project.channels,
FeatureName::Named(_) => self.feature_mut(feature_name)?.channels_mut(),
};

// Get the channels to remove, while checking if they exist
let to_remove: IndexSet<_> = channels
.into_iter()
.map(|c| {
current
.iter()
.position(|x| x.channel == c.channel)
.position(|x| x.channel.to_string() == c.channel.to_string())
.ok_or_else(|| miette::miette!("channel {} does not exist", c.channel.as_str()))
.map(|_| c.channel)
.map(|_| c.channel.to_string())
})
.collect::<Result<_, _>>()?;

let retained: IndexSet<_> = current
.iter()
.filter(|channel| !to_remove.contains(&channel.channel))
.filter(|channel| !to_remove.contains(&channel.channel.to_string()))
.cloned()
.collect();

Expand Down
38 changes: 38 additions & 0 deletions tests/common/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,11 @@ impl ProjectChannelAddBuilder {
self
}

pub fn with_priority(mut self, priority: Option<i32>) -> Self {
self.args.priority = priority.into();

Check failure on line 327 in tests/common/builders.rs

View workflow job for this annotation

GitHub Actions / Cargo Lint

useless conversion to the same type: `std::option::Option<i32>`
self
}

/// Alias to add a local channel.
pub fn with_local_channel(self, channel: impl AsRef<Path>) -> Self {
self.with_channel(Url::from_directory_path(channel).unwrap())
Expand All @@ -342,6 +347,39 @@ impl IntoFuture for ProjectChannelAddBuilder {
}
}

pub struct ProjectChannelRemoveBuilder {
pub manifest_path: Option<PathBuf>,
pub args: project::channel::AddRemoveArgs,
}

impl ProjectChannelRemoveBuilder {
/// Removes the specified channel
pub fn with_channel(mut self, name: impl Into<String>) -> Self {
self.args
.channel
.push(NamedChannelOrUrl::from_str(&name.into()).unwrap());
self
}

/// Alias to Remove a local channel.
pub fn with_local_channel(self, channel: impl AsRef<Path>) -> Self {
self.with_channel(Url::from_directory_path(channel).unwrap())
}
}

impl IntoFuture for ProjectChannelRemoveBuilder {
type Output = miette::Result<()>;
type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + 'static>>;

fn into_future(self) -> Self::IntoFuture {
project::channel::execute(project::channel::Args {
manifest_path: self.manifest_path,
command: project::channel::Command::Remove(self.args),
})
.boxed_local()
}
}

/// Contains the arguments to pass to [`install::execute()`]. Call `.await` to
/// call the CLI execute method and await the result at the same time.
pub struct InstallBuilder {
Expand Down
15 changes: 14 additions & 1 deletion tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use thiserror::Error;

use self::builders::{HasDependencyConfig, RemoveBuilder};
use crate::common::builders::{
AddBuilder, InitBuilder, InstallBuilder, ProjectChannelAddBuilder,
AddBuilder, InitBuilder, InstallBuilder, ProjectChannelAddBuilder, ProjectChannelRemoveBuilder,
ProjectEnvironmentAddBuilder, TaskAddBuilder, TaskAliasBuilder, UpdateBuilder,
};

Expand Down Expand Up @@ -339,6 +339,19 @@ impl PixiControl {
}
}

/// Add a new channel to the project.
pub fn project_channel_remove(&self) -> ProjectChannelRemoveBuilder {
ProjectChannelRemoveBuilder {
manifest_path: Some(self.manifest_path()),
args: project::channel::AddRemoveArgs {
channel: vec![],
no_install: true,
feature: None,
priority: None,
},
}
}

pub fn project_environment_add(&self, name: &str) -> ProjectEnvironmentAddBuilder {
ProjectEnvironmentAddBuilder {
manifest_path: Some(self.manifest_path()),
Expand Down
35 changes: 34 additions & 1 deletion tests/project_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use url::Url;
use crate::common::{package_database::PackageDatabase, PixiControl};

#[tokio::test]
async fn add_channel() {
async fn add_remove_channel() {
// Create a local package database with no entries and write it to disk. This
// ensures that we have a valid channel.
let package_database = PackageDatabase::default();
Expand Down Expand Up @@ -47,7 +47,40 @@ async fn add_channel() {
let local_channel =
NamedChannelOrUrl::Url(Url::from_file_path(additional_channel_dir.as_ref()).unwrap());
let channels = project.default_environment().channels();
assert!(channels.len() == 2);
assert!(channels.last().unwrap() == &&local_channel);
assert!(channels.contains(&local_channel));

// now add the same channel, with priority 2
pixi.project_channel_add()
.with_local_channel(additional_channel_dir.path())
.with_priority(Some(2i32))
.await
.unwrap();

// Load again
let project = Project::from_path(&pixi.manifest_path()).unwrap();
let channels = project.default_environment().channels();
// still present
assert!(channels.contains(&local_channel));
// didn't duplicate
assert!(channels.len() == 2);
// priority applied
assert!(channels.first().unwrap() == &&local_channel);

// now remove it
pixi.project_channel_remove()
.with_local_channel(additional_channel_dir.path())
.await
.unwrap();

// Load again
let project = Project::from_path(&pixi.manifest_path()).unwrap();
let channels = project.default_environment().channels();

// Channel has been removed
assert!(channels.len() == 1);
assert!(!channels.contains(&local_channel));
}

#[tokio::test]
Expand Down

0 comments on commit 6b86e1c

Please sign in to comment.