Skip to content

Commit

Permalink
fix: handle package version panic (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
Matan Kushner committed May 16, 2019
1 parent 2cf69a8 commit 876e7d3
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 29 deletions.
3 changes: 1 addition & 2 deletions src/modules/go.rs
@@ -1,5 +1,4 @@
use ansi_term::Color;
use std::path::PathBuf;
use std::process::Command;

use super::{Context, Module};
Expand Down Expand Up @@ -53,7 +52,7 @@ fn get_go_version() -> 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"]
.splitn(2, "go version go")
// return "1.12.4 linux/amd64"
Expand Down
89 changes: 62 additions & 27 deletions src/modules/package.rs
Expand Up @@ -49,51 +49,42 @@ fn read_file(file_name: &str) -> io::Result<String> {
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 raw_version = cargo_toml.get("package")?.get("version")?.as_str()?;

match cargo_toml["package"]["version"].as_str() {
Some(raw_version) => {
let version = format_version(raw_version.to_string());
Some(version)
}
None => None,
}
let formatted_version = format_version(raw_version);
Some(formatted_version)
}

fn extract_package_version(file_contents: String) -> Option<String> {
let json: Option<serde_json::Value> = serde_json::from_str(&file_contents).ok()?;

match json {
Some(json) => {
let raw_version = json["version"].to_string();
if raw_version == "null" {
None
} else {
Some(format_version(raw_version))
}
}
None => None,
}
fn extract_package_version(file_contents: &str) -> Option<String> {
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;
};

let formatted_version = format_version(raw_version);
Some(formatted_version)
}

fn get_package_version(context: &Context) -> Option<String> {
let has_cargo_toml = context.dir_files.iter().any(is_cargo_toml);
if has_cargo_toml {
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);
if has_package_json {
let file_contents = read_file("package.json").ok()?;
return extract_package_version(file_contents);
return extract_package_version(&file_contents);
}

None
}

fn format_version(version: String) -> String {
fn format_version(version: &str) -> String {
format!("v{}", version.replace('"', "").trim())
}

Expand All @@ -103,7 +94,51 @@ mod tests {

#[test]
fn test_format_version() {
let input = String::from("0.1.0");
assert_eq!(format_version(input), "v0.1.0");
assert_eq!(format_version("0.1.0"), "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
);
}
}

0 comments on commit 876e7d3

Please sign in to comment.