Skip to content

Commit ea87c94

Browse files
committed
Fix publish error with colored output and crate is already published
1 parent dc88862 commit ea87c94

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

crates/tracel-xtask/src/utils/cargo.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,16 @@ pub fn is_cargo_crate_installed(crate_name: &str) -> bool {
4949
output_str.lines().any(|line| line.contains(crate_name))
5050
}
5151

52-
pub fn parse_cargo_search_output(output: &str) -> Option<(&str, &str)> {
52+
pub fn parse_cargo_search_output(output: &str) -> Option<(String, String)> {
53+
// First strip ANSI color codes
54+
let ansi_re = Regex::new(r"\x1b\[[0-9;]*m").expect("should compile regex for ANSI codes");
55+
let cleaned_output = ansi_re.replace_all(output, "");
56+
// Then retrieve the crate name and version in the output
5357
let re = Regex::new(r#"(?P<name>[a-zA-Z0-9_-]+)\s*=\s*"(?P<version>\d+\.\d+\.\d+)""#)
5458
.expect("should compile regex");
55-
if let Some(captures) = re.captures(output) {
59+
if let Some(captures) = re.captures(&cleaned_output) {
5660
if let (Some(name), Some(version)) = (captures.name("name"), captures.name("version")) {
57-
return Some((name.as_str(), version.as_str()));
61+
return Some((name.as_str().to_owned(), version.as_str().to_owned()));
5862
}
5963
}
6064
None
@@ -67,12 +71,15 @@ mod tests {
6771

6872
#[rstest]
6973
#[case::valid_input("tracel-xtask-macros = \"1.0.1\"", Some(("tracel-xtask-macros", "1.0.1")))]
74+
#[case::valid_input_with_comments("heat-sdk-cli-macros = \"0.1.0\" # Macros for Tracel Heat SDK CLI.\n", Some(("heat-sdk-cli-macros", "0.1.0")))]
75+
#[case::valid_input_with_comments_and_color_codes("\u{1b}[1m\u{1b}[32mheat-sdk-cli-macros\u{1b}[0m = \"0.1.0\" # Macros for Tracel Heat SDK CLI.\n", Some(("heat-sdk-cli-macros", "0.1.0")))]
7076
#[case::missing_version("tracel-xtask-macros =", None)]
7177
#[case::invalid_format("tracel-xtask-macros: \"1.0.1\"", None)]
7278
#[case::extra_whitespace(" tracel-xtask-macros = \"1.0.1\" ", Some(("tracel-xtask-macros", "1.0.1")))]
7379
#[case::no_quotes("tracel-xtask-macros = 1.0.1", None)]
7480
#[case::wrong_version_format("tracel-xtask-macros = \"1.0\"", None)]
7581
fn test_parse_cargo_search_output(#[case] input: &str, #[case] expected: Option<(&str, &str)>) {
82+
let expected = expected.map(|(name, version)| (name.to_string(), version.to_string()));
7683
let result = parse_cargo_search_output(input);
7784
assert_eq!(result, expected);
7885
}

0 commit comments

Comments
 (0)