Skip to content

Commit

Permalink
Fix indent in yaml not producing results
Browse files Browse the repository at this point in the history
  • Loading branch information
mihaigalos committed Oct 20, 2019
1 parent 4af9ebf commit 6c52cf8
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 37 deletions.
23 changes: 15 additions & 8 deletions src/dot_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use crate::yaml_parser::YamlParser;

pub struct DotGenerator {}
impl DotGenerator {
pub fn run(&self, args: Vec<String>) -> String{
pub fn run(&self, args: Vec<String>) -> String {
let yaml_parser = YamlParser {};
let mut result : String;
let mut result: String;
result = OutputWriter::prologue();

for file in &args[1..] {
Expand All @@ -18,18 +18,25 @@ impl DotGenerator {
result
}

fn generate_dot(&self, jobs: yaml_rust::Yaml) -> String{
let mut result : String = "".to_string();
fn generate_dot(&self, jobs: yaml_rust::Yaml) -> String {
let mut result: String = "".to_string();

let hexified_rgb = RandomColorGenerator::get_random_color().to_owned();
let hex_color = "#".to_owned() + &hexified_rgb;

let mut job_name: String = "".to_string();
let mut job_parent: String = "".to_string();

for job in jobs.as_vec().unwrap() {
for field in job.as_hash().unwrap() {
result += &self.generate_node_and_edge(field, &hex_color, &mut job_name, &mut job_parent);
for hashed_job in jobs.as_vec().unwrap() {
for job in hashed_job.as_hash().unwrap() {
for field in job.1.as_hash().unwrap() {
result += &self.generate_node_and_edge(
field,
&hex_color,
&mut job_name,
&mut job_parent,
);
}
}
}
result
Expand All @@ -42,7 +49,7 @@ impl DotGenerator {
job_name: &mut String,
job_parent: &mut String,
) -> String {
let mut result : String = "".to_string();
let mut result: String = "".to_string();
if field.0.as_str() == Some("name") {
*job_name = field.1.as_str().unwrap().to_owned();
} else if field.0.as_str() == Some("parent") {
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mod test_unit;
fn main() {
let args = get_program_arguments();
let dot_generator = DotGenerator {};
println!("{}",dot_generator.run(args));
println!("{}", dot_generator.run(args));
}

fn get_program_arguments() -> Vec<String> {
Expand Down
11 changes: 7 additions & 4 deletions src/output_writer.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
pub struct OutputWriter {}
impl OutputWriter {
pub fn prologue() -> String{
pub fn prologue() -> String {
"digraph D {\n".to_string()
}

pub fn epilogue() -> String{
pub fn epilogue() -> String {
"}\n".to_string()
}

pub fn write_output(job_name: &String, job_parent: &String, hex_color: &String) -> String {
let mut result = format!(" {} -> {}\n", job_name, job_parent);
result += &format!(" \"{}\" [style=filled, fillcolor=\"{}\"]\n", job_name, hex_color);
let mut result = format!(" \"{}\" -> \"{}\"\n", job_name, job_parent);
result += &format!(
" \"{}\" [style=filled, fillcolor=\"{}\"]\n",
job_name, hex_color
);
result
}
}
38 changes: 23 additions & 15 deletions src/test_unit.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
#[cfg(test)]
mod tests {
use regex::Regex;
use crate::dot_generator::DotGenerator;
use regex::Regex;

#[test]
fn test_when_typical() {
let dot_generator = DotGenerator {};
let args = ["zuulgraph","/home/mihai/git/zuulgraph/test/yaml/demo_file1.yaml","/home/mihai/git/zuulgraph/test/yaml/demo_file2.yaml"].iter().map(|&s| s.into()).collect();
let expected = concat!("digraph D {\n",
" JobB_file1 -> A_file2\n",
" \"JobB_file1\" [style=filled, fillcolor=\"\"]\n",
" JobC_file1 -> JobB_file1\n",
" \"JobC_file1\" [style=filled, fillcolor=\"\"]\n",
" JobD_file1 -> JobB_file1\n",
" \"JobD_file1\" [style=filled, fillcolor=\"\"]\n",
" A_file2 -> JobA_file1\n",
" \"A_file2\" [style=filled, fillcolor=\"\"]\n",
"}\n");
let args = [
"zuulgraph",
"/home/mihai/git/zuulgraph/test/yaml/demo_file1.yaml",
"/home/mihai/git/zuulgraph/test/yaml/demo_file2.yaml",
]
.iter()
.map(|&s| s.into())
.collect();
let expected = concat!(
"digraph D {\n",
" JobB_file1 -> A_file2\n",
" \"JobB_file1\" [style=filled, fillcolor=\"\"]\n",
" JobC_file1 -> JobB_file1\n",
" \"JobC_file1\" [style=filled, fillcolor=\"\"]\n",
" JobD_file1 -> JobB_file1\n",
" \"JobD_file1\" [style=filled, fillcolor=\"\"]\n",
" A_file2 -> JobA_file1\n",
" \"A_file2\" [style=filled, fillcolor=\"\"]\n",
"}\n"
);

let result = dot_generator.run(args);
let re = Regex::new(r"#[A-Z0-9]+").unwrap();
let postprocessed_result = re.replace_all(result.as_str(), "");

assert_eq!(postprocessed_result, expected);

assert_eq!(postprocessed_result, expected);
}
}
}
14 changes: 7 additions & 7 deletions test/yaml/demo_file1.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
- job:
name: JobA_file1
name: JobA_file1

- job:
name: JobB_file1
parent: A_file2
name: JobB_file1
parent: A_file2

- job:
name: JobC_file1
parent: JobB_file1
name: JobC_file1
parent: JobB_file1

- job:
name: JobD_file1
parent: JobB_file1
name: JobD_file1
parent: JobB_file1
4 changes: 2 additions & 2 deletions test/yaml/demo_file2.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
- job:
name: A_file2
parent: JobA_file1
name: A_file2
parent: JobA_file1

0 comments on commit 6c52cf8

Please sign in to comment.