Skip to content

Commit f0a93b2

Browse files
authored
fix: channel name display (#531)
channel wasn't displayed if there was no `name`. Using `name()` now and formatting the `:` correctly Useful to simplify the generation of pixi.toml from environment yaml
1 parent 8f0ced2 commit f0a93b2

File tree

2 files changed

+38
-12
lines changed

2 files changed

+38
-12
lines changed

crates/rattler_conda_types/src/match_spec/mod.rs

+28-12
Original file line numberDiff line numberDiff line change
@@ -146,27 +146,25 @@ pub struct MatchSpec {
146146
impl Display for MatchSpec {
147147
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
148148
if let Some(channel) = &self.channel {
149-
if let Some(name) = &channel.name {
150-
// TODO: namespace
151-
write!(f, "{name}")?;
149+
let name = channel.name();
150+
write!(f, "{name}")?;
151+
152+
if let Some(subdir) = &self.subdir {
153+
write!(f, "/{subdir}")?;
152154
}
153155
}
154156

155-
if let Some(subdir) = &self.subdir {
156-
write!(f, "/{subdir}")?;
157+
if let Some(namespace) = &self.namespace {
158+
write!(f, ":{namespace}:")?;
159+
} else if self.channel.is_some() || self.subdir.is_some() {
160+
write!(f, "::")?;
157161
}
158162

159163
match &self.name {
160164
Some(name) => write!(f, "{}", name.as_normalized())?,
161165
None => write!(f, "*")?,
162166
}
163167

164-
if let Some(namespace) = &self.namespace {
165-
write!(f, ":{namespace}:")?;
166-
} else if self.channel.is_some() || self.subdir.is_some() {
167-
write!(f, "::")?;
168-
}
169-
170168
if let Some(version) = &self.version {
171169
write!(f, " {version}")?;
172170
}
@@ -407,11 +405,12 @@ mod tests {
407405
use rattler_digest::{parse_digest_from_hex, Md5, Sha256};
408406

409407
use crate::{MatchSpec, NamelessMatchSpec, PackageName, PackageRecord, Version};
408+
use insta::assert_snapshot;
410409
use std::hash::{Hash, Hasher};
411410

412411
#[test]
413412
fn test_matchspec_format_eq() {
414-
let spec = MatchSpec::from_str("mamba[version==1.0, sha256=aaac4bc9c6916ecc0e33137431645b029ade22190c7144eead61446dcbcc6f97, md5=dede6252c964db3f3e41c7d30d07f6bf]").unwrap();
413+
let spec = MatchSpec::from_str("conda-forge::mamba[version==1.0, sha256=aaac4bc9c6916ecc0e33137431645b029ade22190c7144eead61446dcbcc6f97, md5=dede6252c964db3f3e41c7d30d07f6bf]").unwrap();
415414
let spec_as_string = spec.to_string();
416415
let rebuild_spec = MatchSpec::from_str(&spec_as_string).unwrap();
417416

@@ -495,4 +494,21 @@ mod tests {
495494
let spec = MatchSpec::from_str("mamba[version==1.0, md5=dede6252c964db3f3e41c7d30d07f6bf, sha256=aaac4bc9c6916ecc0e33137431645b029ade22190c7144eead61446dcbcc6f97]").unwrap();
496495
assert!(!spec.matches(&record));
497496
}
497+
498+
#[test]
499+
fn test_serialize_matchspec() {
500+
let specs = ["mamba 1.0 py37_0",
501+
"conda-forge::pytest[version=1.0, sha256=aaac4bc9c6916ecc0e33137431645b029ade22190c7144eead61446dcbcc6f97, md5=dede6252c964db3f3e41c7d30d07f6bf]",
502+
"conda-forge/linux-64::pytest",
503+
"conda-forge/linux-64::pytest[version=1.0]",
504+
"conda-forge/linux-64::pytest[version=1.0, build=py37_0]",
505+
"conda-forge/linux-64::pytest 1.2.3"];
506+
507+
assert_snapshot!(specs
508+
.into_iter()
509+
.map(|s| MatchSpec::from_str(s).unwrap())
510+
.map(|s| s.to_string())
511+
.collect::<Vec<String>>()
512+
.join("\n"));
513+
}
498514
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
source: crates/rattler_conda_types/src/match_spec/mod.rs
3+
expression: "specs.into_iter().map(|s|\n MatchSpec::from_str(s).unwrap()).map(|s|\n s.to_string()).collect::<Vec<String>>().join(\"\\n\")"
4+
---
5+
mamba ==1.0 py37_0
6+
conda-forge::pytest ==1.0[md5=dede6252c964db3f3e41c7d30d07f6bf, sha256=aaac4bc9c6916ecc0e33137431645b029ade22190c7144eead61446dcbcc6f97]
7+
conda-forge/linux-64::pytest
8+
conda-forge/linux-64::pytest ==1.0
9+
conda-forge/linux-64::pytest ==1.0 py37_0
10+
conda-forge/linux-64::pytest ==1.2.3

0 commit comments

Comments
 (0)