Repository navigation
Expand file tree
/
Copy pathmetadata.rs
More file actions
92 lines (79 loc) · 2.59 KB
/
Copy pathmetadata.rs
File metadata and controls
92 lines (79 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use std::ffi::OsStr;
use std::path::Path;
use anyhow::{Context, Result};
use serde::Deserialize;
use super::util;
/// Metadata file name.
pub(crate) const METADATA_FILE: &str = ".clomonitor.yml";
/// CLOMonitor metadata.
#[derive(Debug, Clone, Default, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub(crate) struct Metadata {
pub agent_readiness: Option<AgentReadiness>,
pub exemptions: Option<Vec<Exemption>>,
pub license_scanning: Option<LicenseScanning>,
}
impl Metadata {
/// Create a new metadata instance from the contents of the file located at
/// the path provided.
pub(crate) fn from<P: AsRef<OsStr>>(path: P) -> Result<Option<Self>> {
if !Path::new(&path).exists() {
return Ok(None);
}
let content = util::fs::read_to_string(path.as_ref())
.context("error reading clomonitor metadata file")?;
Ok(serde_yaml::from_str(&content)?)
}
}
/// Agent readiness section of the metadata.
#[derive(Debug, Clone, Deserialize, PartialEq)]
pub(crate) struct AgentReadiness {
pub url: Option<String>,
}
/// Metadata check exemption entry.
#[derive(Debug, Clone, Deserialize, PartialEq)]
pub(crate) struct Exemption {
pub check: String,
pub reason: String,
}
/// License scanning section of the metadata.
#[derive(Debug, Clone, Deserialize, PartialEq)]
pub(crate) struct LicenseScanning {
pub url: Option<String>,
}
#[cfg(test)]
mod tests {
use super::*;
const TESTDATA_PATH: &str = "src/testdata";
#[test]
fn metadata_from_path_success() {
assert_eq!(
Metadata::from(Path::new(TESTDATA_PATH).join(METADATA_FILE))
.unwrap()
.unwrap(),
Metadata {
agent_readiness: Some(AgentReadiness {
url: Some("https://docs.project.url".to_string()),
}),
exemptions: Some(vec![Exemption {
check: "artifacthub_badge".to_string(),
reason: "this is a sample reason".to_string(),
}]),
license_scanning: Some(LicenseScanning {
url: Some("https://license-scanning-results.url".to_string()),
}),
},
);
}
#[test]
fn metadata_from_path_not_found() {
assert!(matches!(
Metadata::from(Path::new(TESTDATA_PATH).join("not-found")),
Ok(None)
));
}
#[test]
fn metadata_from_path_invalid_metadata_file() {
assert!(Metadata::from(Path::new(TESTDATA_PATH).join(".clomonitor-invalid.yaml")).is_err());
}
}