Skip to content

Commit

Permalink
Merge pull request #154 from DarkEld3r/key-value-matcher-test
Browse files Browse the repository at this point in the history
Add a test for key-value matcher
  • Loading branch information
stanislav-tkach authored Mar 10, 2020
2 parents 4f341a5 + a3d592f commit 0b7182f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
1 change: 1 addition & 0 deletions cspell-dictionary.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
bitness
centos
concat
emscripten
libntdll
macos
Expand Down
1 change: 0 additions & 1 deletion src/linux/file_release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ struct ReleaseInfo<'a> {
/// List of all supported distributions and the information on how to parse their version from the
/// release file.
const DISTRIBUTIONS: [ReleaseInfo; 5] = [
// IMPORTANT IMPORTANT IMPORTANT
// Due to shenanigans with Oracle Linux including an /etc/redhat-release file that states
// that the OS is Red Hat Enterprise Linux, this /etc/os-release file MUST be checked
// before this code checks /etc/redhat-release. If it does not get run first,
Expand Down
31 changes: 23 additions & 8 deletions src/matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ pub enum Matcher {
/// Similar to `PrefixedWord`, but only if the word is a valid version.
PrefixedVersion { prefix: &'static str },

/// Key/Value file - principally seen on Oracle Linux with the /etc/os-release file. The `key`
/// field represents the key name for the field we're extracting the version from.
/// Takes a set of lines (separated by `\n`) and searches for the value in a key/value pair
/// separated by the `=` character.
#[allow(dead_code)]
KeyValue { key: &'static str },
}
Expand All @@ -32,12 +32,9 @@ impl Matcher {
}

fn find_by_key<'a>(string: &'a str, key: &str) -> Option<&'a str> {
let lines: Vec<&str> = string.split('\n').collect();

for line in lines {
let kv: Vec<&str> = line.split('=').collect();
if kv[0] == key {
return Some(kv[1]);
for line in string.lines() {
if let Some(val) = find_prefixed_word(line, &[key, "="].concat()) {
return Some(val);
}
}

Expand Down Expand Up @@ -93,6 +90,7 @@ mod tests {
let data = [
("", None),
("test", Some("")),
("test1", Some("1")),
("test 1", Some("1")),
(" test 1", Some("1")),
("test 1.2.3", Some("1.2.3")),
Expand Down Expand Up @@ -128,4 +126,21 @@ mod tests {
assert_eq!(result.as_deref(), *expected);
}
}

#[test]
fn key_value() {
let data = [
("", None),
("key", None),
("key=value", Some("value")),
("key=1", Some("1")),
];

let matcher = Matcher::KeyValue { key: "key" };

for (input, expected) in &data {
let result = matcher.find(input);
assert_eq!(result.as_deref(), *expected);
}
}
}
12 changes: 6 additions & 6 deletions src/os_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ pub enum Type {
Arch,
/// CentOS (<https://en.wikipedia.org/wiki/CentOS>).
Centos,
/// Fedora (<https://en.wikipedia.org/wiki/Fedora_(operating_system)>)
/// Fedora (<https://en.wikipedia.org/wiki/Fedora_(operating_system)>).
Fedora,
/// Amazon (<https://en.wikipedia.org/wiki/Amazon_Machine_Image#Amazon_Linux_AMI>)
/// Amazon (<https://en.wikipedia.org/wiki/Amazon_Machine_Image#Amazon_Linux_AMI>).
Amazon,
/// SUSE Linux Enterprise (<https://en.wikipedia.org/wiki/SUSE_Linux_Enterprise>)
/// SUSE Linux Enterprise (<https://en.wikipedia.org/wiki/SUSE_Linux_Enterprise>).
SUSE,
///openSUSE Linux (<https://en.wikipedia.org/wiki/OpenSUSE>)
///openSUSE Linux (<https://en.wikipedia.org/wiki/OpenSUSE>).
openSUSE,
/// Alpine Linux (<https://en.wikipedia.org/wiki/Alpine_Linux>)
/// Alpine Linux (<https://en.wikipedia.org/wiki/Alpine_Linux>).
Alpine,
/// Oracle Linux Server (<https://en.wikipedia.org/wiki/Oracle_Linux>)
/// Oracle Linux Server (<https://en.wikipedia.org/wiki/Oracle_Linux>).
OracleLinux,
/// Mac OS X/OS X/macOS (<https://en.wikipedia.org/wiki/MacOS>).
Macos,
Expand Down

0 comments on commit 0b7182f

Please sign in to comment.