Set default output_file for jobs when loaded from file
This commit is contained in:
		
							parent
							
								
									38b5d5bb32
								
							
						
					
					
						commit
						fef475c8ad
					
				
							
								
								
									
										36
									
								
								src/job.rs
								
								
								
								
							
							
						
						
									
										36
									
								
								src/job.rs
								
								
								
								
							|  | @ -31,15 +31,19 @@ impl Job { | |||
|         }; | ||||
|     } | ||||
| 
 | ||||
|     fn set_default_output_file(&mut self, conf: &Conf) { | ||||
|         let mut output_file = conf.output_dir.clone(); | ||||
|         let mut file_name = self.url.clone().replace("/", "-"); | ||||
|         file_name.push_str(".rss"); | ||||
|         output_file = output_file.join(Path::new(&file_name)); | ||||
|         self.output_file = Some(output_file); | ||||
|     } | ||||
| 
 | ||||
|     pub fn new(url: &str, selector: &str, conf: &Conf) -> Job { | ||||
|         let mut job = Job::default(conf); | ||||
|         job.url = url.to_string(); | ||||
|         job.set_default_output_file(conf); | ||||
|         job.selector = selector.to_string(); | ||||
|         let mut output_file = conf.output_dir.clone(); | ||||
|         let mut file_name = job.url.clone().replace("/", "-"); | ||||
|         file_name.push_str(".rss"); | ||||
|         output_file = output_file.join(Path::new(&file_name)); | ||||
|         job.output_file = Some(output_file); | ||||
| 
 | ||||
|         match std::fs::File::open(job.output_file.as_ref().unwrap()) { | ||||
|             Err(why) => { | ||||
|  | @ -99,6 +103,9 @@ impl Job { | |||
|                 } | ||||
|             } | ||||
|         } | ||||
|         if job.output_file.is_none() { | ||||
|             job.set_default_output_file(conf); | ||||
|         } | ||||
|         return Ok(job); | ||||
|     } | ||||
| 
 | ||||
|  | @ -184,4 +191,23 @@ selector = section.listing:nth-child(2) > ul:nth-child(1) > li:nth-child(3) > he | |||
|         assert_eq!(job.selector, "section.listing:nth-child(2) > ul:nth-child(1) > li:nth-child(3) > header:nth-child(3) > h2:nth-child(1) > a:nth-child(1)"); | ||||
|     } | ||||
| 
 | ||||
|     #[test] | ||||
|     fn create_from_file_default_output_file() { | ||||
|         let conf = Conf::get_default_conf(); | ||||
|         let mut tf = NamedTempFile::new().unwrap(); | ||||
|         let job_conf = r#" | ||||
| url = http://example.com/test
 | ||||
| every=7200 | ||||
| 
 | ||||
| selector = section.listing:nth-child(2) > ul:nth-child(1) > li:nth-child(3) > header:nth-child(3) > h2:nth-child(1) > a:nth-child(1) | ||||
| "#;
 | ||||
|         tf.write_all(job_conf.as_bytes()).expect("Failed to write configuration to file"); | ||||
| 
 | ||||
|         let job = Job::from_file(tf.path(), &conf).expect("Failed to read configuration file"); | ||||
|         assert_eq!(job.url, "http://example.com/test"); | ||||
|         assert_eq!(job.output_file.unwrap().to_str().unwrap(), "results.d/http:--example.com-test.rss"); | ||||
|         assert_eq!(job.every.as_secs(), 7200); | ||||
|         assert_eq!(job.selector, "section.listing:nth-child(2) > ul:nth-child(1) > li:nth-child(3) > header:nth-child(3) > h2:nth-child(1) > a:nth-child(1)"); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue