Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: incremental lock file updates #140

Merged
merged 13 commits into from
Jun 29, 2023
Merged
22 changes: 12 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,7 @@ toml_edit = { version = "0.19.10", features = ["serde"] }
tracing = "0.1.37"
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
url = "2.4.0"

[dev-dependencies]
rattler_digest = { default-features = false, git = "https://github.com/mamba-org/rattler", branch = "main" }
serde_json = "1.0.96"
68 changes: 66 additions & 2 deletions src/project/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ use crate::project::manifest::{ProjectManifest, TargetMetadata, TargetSelector};
use crate::report_error::ReportError;
use anyhow::Context;
use ariadne::{Label, Report, ReportKind, Source};
use rattler_conda_types::{Channel, MatchSpec, NamelessMatchSpec, Platform, Version};
use rattler_conda_types::{
Channel, ChannelConfig, MatchSpec, NamelessMatchSpec, Platform, Version,
};
use rattler_virtual_packages::VirtualPackage;
use std::{
collections::HashMap,
env, fs,
path::{Path, PathBuf},
};
use toml_edit::{Document, Item, Table, TomlError};
use toml_edit::{Array, Document, Item, Table, TomlError, Value};

/// A project represented by a pixi.toml file.
#[derive(Debug)]
Expand Down Expand Up @@ -212,6 +214,68 @@ impl Project {
&self.manifest.project.channels
}

/// Adds the specified channels to the project.
pub fn add_channels(
&mut self,
channels: impl IntoIterator<Item = impl AsRef<str>>,
) -> anyhow::Result<()> {
let mut stored_channels = Vec::new();
for channel in channels {
self.manifest.project.channels.push(Channel::from_str(
channel.as_ref(),
&ChannelConfig::default(),
)?);
stored_channels.push(channel.as_ref().to_owned());
}

let channels_array = self.channels_array_mut()?;
for channel in stored_channels {
channels_array.push(channel);
}

Ok(())
}

/// Replaces all the channels in the project with the specified channels.
pub fn set_channels(
&mut self,
channels: impl IntoIterator<Item = impl AsRef<str>>,
) -> anyhow::Result<()> {
self.manifest.project.channels.clear();
let mut stored_channels = Vec::new();
for channel in channels {
self.manifest.project.channels.push(Channel::from_str(
channel.as_ref(),
&ChannelConfig::default(),
)?);
stored_channels.push(channel.as_ref().to_owned());
}

let channels_array = self.channels_array_mut()?;
channels_array.clear();
for channel in stored_channels {
channels_array.push(channel);
}
Ok(())
}

/// Returns a mutable reference to the channels array.
fn channels_array_mut(&mut self) -> anyhow::Result<&mut Array> {
let project = &mut self.doc["project"];
if project.is_none() {
*project = Item::Table(Table::new());
}

let channels = &mut project["channels"];
if channels.is_none() {
*channels = Item::Value(Value::Array(Array::new()))
}

channels
.as_array_mut()
.ok_or_else(|| anyhow::anyhow!("malformed channels array"))
}

/// Returns the platforms this project targets
pub fn platforms(&self) -> &[Platform] {
self.manifest.project.platforms.as_ref().as_slice()
Expand Down
12 changes: 11 additions & 1 deletion tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
pub mod package_database;

use pixi::cli::run::create_command;
use pixi::cli::{add, init, run};
use pixi::consts;
use pixi::{consts, Project};
use rattler_conda_types::conda_lock::CondaLock;
use rattler_conda_types::{MatchSpec, Version};
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -120,4 +122,12 @@ impl PixiControl {
pub async fn lock_file(&self) -> anyhow::Result<CondaLock> {
pixi::environment::load_lock_for_manifest_path(&self.manifest_path()).await
}

/// Set the project to use a specific channel
pub async fn set_channel(&mut self, channel: impl ToString) -> anyhow::Result<()> {
let mut project = Project::load(&self.manifest_path()).unwrap();
project.set_channels(&[channel.to_string()])?;
project.save()?;
Ok(())
}
}
Loading