diff --git a/src/ntfs/mod.rs b/src/ntfs/mod.rs index e13d38cdb..caf23b76b 100644 --- a/src/ntfs/mod.rs +++ b/src/ntfs/mod.rs @@ -29,6 +29,7 @@ use crate::{ use chrono::{DateTime, FixedOffset}; use chrono_tz::Tz; use derivative::Derivative; +use failure::ResultExt; use log::info; use serde::{Deserialize, Serialize}; use std::path; @@ -222,9 +223,10 @@ pub fn read>(path: P) -> Result { let p = path.as_ref(); if p.is_file() { // if it's a file, we consider it to be a zip (and an error will be returned if it is not) - read_from_zip(path) + Ok(read_from_zip(p).with_context(|_| format!("impossible to read ziped ntfs {:?}", p))?) } else if p.is_dir() { - read_from_path(path) + Ok(read_from_path(p) + .with_context(|_| format!("impossible to read ntfs directory from {:?}", p))?) } else { Err(failure::format_err!( "file {:?} is neither a file nor a directory, cannot read a ntfs from it", diff --git a/tests/fixtures/ziped_ntfs/minimal_ntfs.zip b/tests/fixtures/ziped_ntfs/minimal_ntfs.zip new file mode 100644 index 000000000..e9e749260 Binary files /dev/null and b/tests/fixtures/ziped_ntfs/minimal_ntfs.zip differ diff --git a/tests/read_ntfs.rs b/tests/read_ntfs.rs index 73ec044a8..f5354c1d9 100644 --- a/tests/read_ntfs.rs +++ b/tests/read_ntfs.rs @@ -34,10 +34,7 @@ where ids } -#[test] -fn minimal() { - let ntm = transit_model::ntfs::read("tests/fixtures/minimal_ntfs/").unwrap(); - +fn test_minimal_ntfs(ntm: &Model) { assert_eq!(8, ntm.stop_areas.len()); assert_eq!(12, ntm.stop_points.len()); assert_eq!(3, ntm.commercial_modes.len()); @@ -88,6 +85,46 @@ fn minimal() { ); } +#[test] +fn minimal() { + let ntm = transit_model::ntfs::read("tests/fixtures/minimal_ntfs/").unwrap(); + test_minimal_ntfs(&ntm); +} + +#[test] +fn ziped_minimal() { + let ntm = transit_model::ntfs::read("tests/fixtures/ziped_ntfs/minimal_ntfs.zip").unwrap(); + test_minimal_ntfs(&ntm); +} + +#[test] +#[should_panic( + expected = "ErrorMessage { msg: \"file \\\"tests/fixtures/i_m_not_here\\\" is neither a file nor a directory, cannot read a ntfs from it\" }" +)] +fn unexistent_file() { + // reading a file that does not exists will lead to an error + let _ = transit_model::ntfs::read("tests/fixtures/i_m_not_here").unwrap(); +} + +#[test] +#[should_panic( + expected = "InvalidArchive(\"Could not find central directory end\")\n\nimpossible to read ziped ntfs \"tests/fixtures/ntfs/stops.txt\"" +)] +fn file_not_a_ntfs() { + // reading a file that is not either a directory with the ntfs files nor a zip archive will lead to an error + // here we read the stops.txt + let _ = transit_model::ntfs::read("tests/fixtures/ntfs/stops.txt").unwrap(); +} + +#[test] +#[should_panic( + expected = "ErrorMessage { msg: \"file \\\"tests/fixtures/netex_france/contributors.txt\\\" not found\" }\n\nimpossible to read ntfs directory from \"tests/fixtures/netex_france\"" +)] +fn directory_not_a_ntfs() { + // reading a directory that does not contain the ntfs files will lead to an error + let _ = transit_model::ntfs::read("tests/fixtures/netex_france").unwrap(); +} + #[test] fn ntfs_stop_zones() { let ntm = transit_model::ntfs::read("tests/fixtures/minimal_ntfs/").unwrap(); @@ -139,10 +176,6 @@ fn test_minimal_platforms_stay_same() { #[test] fn test_minimal_fares_stay_same_with_empty_of_fares() { let ntm = transit_model::ntfs::read("tests/fixtures/ntfs2ntfs/empty_od_fares").unwrap(); - println!("prices: {}", ntm.prices_v1.len()); - for p in &ntm.prices_v1 { - println!("p == {:?}", p); - } test_in_tmp_dir(|output_dir| { transit_model::ntfs::write(&ntm, output_dir, get_test_datetime()).unwrap(); compare_output_dir_with_expected(