Skip to content

Commit

Permalink
citnames: draft execution processing
Browse files Browse the repository at this point in the history
  • Loading branch information
rizsotto committed Aug 29, 2023
1 parent 0ba950c commit 57504be
Show file tree
Hide file tree
Showing 6 changed files with 284 additions and 184 deletions.
1 change: 1 addition & 0 deletions source/citnames_rs/src/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ impl TryFrom<CompilerCall> for Vec<Entry> {
fn try_from(value: CompilerCall) -> Result<Self, Self::Error> {
match value {
CompilerCall::Compile { working_dir, compiler, flags, sources, output } =>
// todo: should error when sources are empty?
sources.iter()
.map(|source| -> Result<Entry, Self::Error> {
let mut arguments: Vec<String> = vec![];
Expand Down
283 changes: 136 additions & 147 deletions source/citnames_rs/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,6 @@ pub enum DuplicateFilterFields {
All,
}



impl TryFrom<String> for DuplicateFilterFields {
type Error = String;

Expand All @@ -150,173 +148,164 @@ impl TryFrom<String> for DuplicateFilterFields {
}
}

pub mod io {
#[cfg(test)]
mod test {
use crate::{vec_of_pathbuf, vec_of_strings};
use super::*;

/// Load the content of the given stream and parse it as Configuration.
pub fn from_reader(reader: impl std::io::Read) -> Result<Configuration, serde_json::Error> {
serde_json::from_reader(reader)
#[test]
fn test_full_config() {
let content: &[u8] = br#"{
"output": {
"format": {
"command_as_array": true,
"drop_output_field": false
},
"content": {
"include_only_existing_source": false,
"duplicate_filter_fields": "all",
"paths_to_include": ["sources"],
"paths_to_exclude": ["tests"]
}
},
"compilation": {
"compilers_to_recognize": [
{
"executable": "/usr/local/bin/clang",
"flags_to_add": ["-Dfoo=bar"],
"flags_to_remove": ["-Wall"]
}
],
"compilers_to_exclude": [
"clang"
]
}
}"#;

let result = serde_json::from_reader(content).unwrap();

let expected = Configuration {
output: Output {
format: Format {
command_as_array: true,
drop_output_field: false,
},
content: Content {
include_only_existing_source: false,
duplicate_filter_fields: DuplicateFilterFields::All,
paths_to_include: vec_of_pathbuf!["sources"],
paths_to_exclude: vec_of_pathbuf!["tests"],
},
},
compilation: Compilation {
compilers_to_recognize: vec![
CompilerToRecognize {
executable: PathBuf::from("/usr/local/bin/clang"),
flags_to_add: vec_of_strings!["-Dfoo=bar"],
flags_to_remove: vec_of_strings!["-Wall"],
}
],
compilers_to_exclude: vec_of_pathbuf!["clang"],
},
};

assert_eq!(expected, result);
}

#[cfg(test)]
mod test {
use crate::{vec_of_pathbuf, vec_of_strings};
use super::*;

#[test]
fn test_full_config() {
let content: &[u8] = br#"{
"output": {
"format": {
"command_as_array": true,
"drop_output_field": false
},
"content": {
"include_only_existing_source": false,
"duplicate_filter_fields": "all",
"paths_to_include": ["sources"],
"paths_to_exclude": ["tests"]
}
#[test]
fn test_only_output_config() {
let content: &[u8] = br#"{
"output": {
"format": {
"command_as_array": false
},
"compilation": {
"compilers_to_recognize": [
{
"executable": "/usr/local/bin/clang",
"flags_to_add": ["-Dfoo=bar"],
"flags_to_remove": ["-Wall"]
}
],
"compilers_to_exclude": [
"clang"
]
"content": {
"duplicate_filter_fields": "file"
}
}"#;
}
}"#;

let result = from_reader(content).unwrap();
let result = serde_json::from_reader(content).unwrap();

let expected = Configuration {
output: Output {
format: Format {
command_as_array: true,
drop_output_field: false,
},
content: Content {
include_only_existing_source: false,
duplicate_filter_fields: DuplicateFilterFields::All,
paths_to_include: vec_of_pathbuf!["sources"],
paths_to_exclude: vec_of_pathbuf!["tests"],
},
let expected = Configuration {
output: Output {
format: Format {
command_as_array: false,
drop_output_field: false,
},
compilation: Compilation {
compilers_to_recognize: vec![
CompilerToRecognize {
executable: PathBuf::from("/usr/local/bin/clang"),
flags_to_add: vec_of_strings!["-Dfoo=bar"],
flags_to_remove: vec_of_strings!["-Wall"],
}
],
compilers_to_exclude: vec_of_pathbuf!["clang"],
content: Content {
include_only_existing_source: false,
duplicate_filter_fields: DuplicateFilterFields::FileOnly,
paths_to_include: vec_of_pathbuf![],
paths_to_exclude: vec_of_pathbuf![],
},
};
},
compilation: Compilation::default(),
};

assert_eq!(expected, result);
}
assert_eq!(expected, result);
}

#[test]
fn test_only_output_config() {
let content: &[u8] = br#"{
"output": {
"format": {
"command_as_array": false
#[test]
fn test_compilation_only_config() {
let content: &[u8] = br#"{
"compilation": {
"compilers_to_recognize": [
{
"executable": "/usr/local/bin/clang"
},
"content": {
"duplicate_filter_fields": "file"
{
"executable": "/usr/local/bin/clang++"
}
}
}"#;

let result = from_reader(content).unwrap();

let expected = Configuration {
output: Output {
format: Format {
command_as_array: false,
drop_output_field: false,
],
"compilers_to_exclude": [
"clang", "clang++"
]
}
}"#;

let result = serde_json::from_reader(content).unwrap();

let expected = Configuration {
output: Output::default(),
compilation: Compilation {
compilers_to_recognize: vec![
CompilerToRecognize {
executable: PathBuf::from("/usr/local/bin/clang"),
flags_to_add: vec![],
flags_to_remove: vec![],
},
content: Content {
include_only_existing_source: false,
duplicate_filter_fields: DuplicateFilterFields::FileOnly,
paths_to_include: vec_of_pathbuf![],
paths_to_exclude: vec_of_pathbuf![],
CompilerToRecognize {
executable: PathBuf::from("/usr/local/bin/clang++"),
flags_to_add: vec![],
flags_to_remove: vec![],
},
},
compilation: Compilation::default(),
};
],
compilers_to_exclude: vec_of_pathbuf!["clang", "clang++"],
},
};

assert_eq!(expected, result);
}
assert_eq!(expected, result);
}

#[test]
fn test_compilation_only_config() {
let content: &[u8] = br#"{
"compilation": {
"compilers_to_recognize": [
{
"executable": "/usr/local/bin/clang"
},
{
"executable": "/usr/local/bin/clang++"
}
],
"compilers_to_exclude": [
"clang", "clang++"
]
}
}"#;

let result = from_reader(content).unwrap();

let expected = Configuration {
output: Output::default(),
compilation: Compilation {
compilers_to_recognize: vec![
CompilerToRecognize {
executable: PathBuf::from("/usr/local/bin/clang"),
flags_to_add: vec![],
flags_to_remove: vec![],
},
CompilerToRecognize {
executable: PathBuf::from("/usr/local/bin/clang++"),
flags_to_add: vec![],
flags_to_remove: vec![],
},
],
compilers_to_exclude: vec_of_pathbuf!["clang", "clang++"],
#[test]
fn test_failing_config() {
let content: &[u8] = br#"{
"output": {
"format": {
"command_as_array": false
},
};

assert_eq!(expected, result);
}

#[test]
fn test_failing_config() {
let content: &[u8] = br#"{
"output": {
"format": {
"command_as_array": false
},
"content": {
"duplicate_filter_fields": "files"
}
"content": {
"duplicate_filter_fields": "files"
}
}"#;
}
}"#;

let result = from_reader(content);
let result: Result<Configuration, serde_json::Error> = serde_json::from_reader(content);

assert!(result.is_err());
assert!(result.is_err());

let message = result.unwrap_err().to_string();
assert_eq!("Unknown value \"files\" for duplicate filter at line 8 column 21", message);
}
let message = result.unwrap_err().to_string();
assert_eq!("Unknown value \"files\" for duplicate filter at line 8 column 17", message);
}
}
6 changes: 3 additions & 3 deletions source/citnames_rs/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ pub fn from_reader(reader: impl std::io::Read) -> impl Iterator<Item=Result<Exec
match value {
Ok(value) => {
match into_execution(value) {
None => vec![],
Some(result) => vec![Ok(result)]
None => None,
Some(result) => Some(Ok(result))
}
}
Err(error) =>
vec![Err(error)],
Some(Err(error)),
}
})
}
Expand Down
Loading

0 comments on commit 57504be

Please sign in to comment.