1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-12-01 01:04:00 +00:00

fix: handle package version panic (#57)

This commit is contained in:
Matan Kushner 2019-05-16 10:06:34 -06:00 committed by GitHub
parent 2cf69a82b7
commit 876e7d36b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 62 additions and 28 deletions

View File

@ -1,5 +1,4 @@
use ansi_term::Color; use ansi_term::Color;
use std::path::PathBuf;
use std::process::Command; use std::process::Command;
use super::{Context, Module}; use super::{Context, Module};
@ -53,7 +52,7 @@ fn get_go_version() -> Option<String> {
} }
fn format_go_version(go_stdout: String) -> Option<String> { fn format_go_version(go_stdout: String) -> Option<String> {
let mut version = go_stdout let version = go_stdout
// split into ["", "1.12.4 linux/amd64"] // split into ["", "1.12.4 linux/amd64"]
.splitn(2, "go version go") .splitn(2, "go version go")
// return "1.12.4 linux/amd64" // return "1.12.4 linux/amd64"

View File

@ -49,51 +49,42 @@ fn read_file(file_name: &str) -> io::Result<String> {
Ok(data) Ok(data)
} }
fn extract_cargo_version(file_contents: String) -> Option<String> { fn extract_cargo_version(file_contents: &str) -> Option<String> {
let cargo_toml = file_contents.parse::<toml::Value>().ok()?; let cargo_toml = file_contents.parse::<toml::Value>().ok()?;
let raw_version = cargo_toml.get("package")?.get("version")?.as_str()?;
match cargo_toml["package"]["version"].as_str() { let formatted_version = format_version(raw_version);
Some(raw_version) => { Some(formatted_version)
let version = format_version(raw_version.to_string());
Some(version)
}
None => None,
}
} }
fn extract_package_version(file_contents: String) -> Option<String> { fn extract_package_version(file_contents: &str) -> Option<String> {
let json: Option<serde_json::Value> = serde_json::from_str(&file_contents).ok()?; let package_json: serde_json::Value = serde_json::from_str(&file_contents).ok()?;
let raw_version = package_json.get("version")?.as_str()?;
if raw_version == "null" {
return None;
};
match json { let formatted_version = format_version(raw_version);
Some(json) => { Some(formatted_version)
let raw_version = json["version"].to_string();
if raw_version == "null" {
None
} else {
Some(format_version(raw_version))
}
}
None => None,
}
} }
fn get_package_version(context: &Context) -> Option<String> { fn get_package_version(context: &Context) -> Option<String> {
let has_cargo_toml = context.dir_files.iter().any(is_cargo_toml); let has_cargo_toml = context.dir_files.iter().any(is_cargo_toml);
if has_cargo_toml { if has_cargo_toml {
let file_contents = read_file("Cargo.toml").ok()?; let file_contents = read_file("Cargo.toml").ok()?;
return extract_cargo_version(file_contents); return extract_cargo_version(&file_contents);
} }
let has_package_json = context.dir_files.iter().any(is_package_json); let has_package_json = context.dir_files.iter().any(is_package_json);
if has_package_json { if has_package_json {
let file_contents = read_file("package.json").ok()?; let file_contents = read_file("package.json").ok()?;
return extract_package_version(file_contents); return extract_package_version(&file_contents);
} }
None None
} }
fn format_version(version: String) -> String { fn format_version(version: &str) -> String {
format!("v{}", version.replace('"', "").trim()) format!("v{}", version.replace('"', "").trim())
} }
@ -103,7 +94,51 @@ mod tests {
#[test] #[test]
fn test_format_version() { fn test_format_version() {
let input = String::from("0.1.0"); assert_eq!(format_version("0.1.0"), "v0.1.0");
assert_eq!(format_version(input), "v0.1.0"); }
#[test]
fn test_extract_cargo_version() {
let cargo_with_version = "[package]
name = \"starship\"
version = \"0.1.0\"";
let expected_version = Some("v0.1.0".to_string());
assert_eq!(extract_cargo_version(&cargo_with_version), expected_version);
let cargo_without_version = "[package]
name = \"starship\"";
let expected_version = None;
assert_eq!(
extract_cargo_version(&cargo_without_version),
expected_version
);
}
#[test]
fn test_extract_package_version() {
let package_with_version = serde_json::json!({
"name": "spacefish",
"version": "0.1.0"
})
.to_string();
let expected_version = Some("v0.1.0".to_string());
assert_eq!(
extract_package_version(&package_with_version),
expected_version
);
let package_without_version = serde_json::json!({
"name": "spacefish"
})
.to_string();
let expected_version = None;
assert_eq!(
extract_package_version(&package_without_version),
expected_version
);
} }
} }