diff --git a/CHANGES.md b/CHANGES.md index 6c37e00bf..a3d0d4422 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -96,6 +96,10 @@ - +- Added a workaround in multi-dim tests to not access files multiple times + + - + ## 0.12 - Bump Rust edition to 2021 diff --git a/src/lib.rs b/src/lib.rs index 5fc0decd7..90a5908a6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -33,6 +33,8 @@ mod metadata; pub mod programs; pub mod raster; pub mod spatial_ref; +#[cfg(test)] +pub mod test_utils; mod utils; pub mod vector; pub mod version; diff --git a/src/programs/raster/mdimtranslate.rs b/src/programs/raster/mdimtranslate.rs index c59404541..f6b561f6b 100644 --- a/src/programs/raster/mdimtranslate.rs +++ b/src/programs/raster/mdimtranslate.rs @@ -227,19 +227,22 @@ fn _multi_dim_translate( #[cfg(test)] mod tests { + use super::*; - use crate::{DatasetOptions, Driver, GdalOpenFlags}; + use crate::{test_utils::TempFixture, DatasetOptions, Driver, GdalOpenFlags}; #[test] fn test_build_tiff_from_path() { + let fixture = TempFixture::fixture("cf_nasa_4326.nc"); + let dataset_options = DatasetOptions { open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER, allowed_drivers: None, open_options: None, sibling_files: None, }; - let dataset = Dataset::open_ex("fixtures/cf_nasa_4326.nc", dataset_options).unwrap(); + let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap(); let mem_file_path = "/vsimem/2d3e9124-a7a0-413e-97b5-e79d46e50ff8"; @@ -263,13 +266,15 @@ mod tests { #[test] fn test_build_tiff_from_dataset() { + let fixture = TempFixture::fixture("cf_nasa_4326.nc"); + let dataset_options = DatasetOptions { open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER, allowed_drivers: None, open_options: None, sibling_files: None, }; - let dataset = Dataset::open_ex("fixtures/cf_nasa_4326.nc", dataset_options).unwrap(); + let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap(); let driver = Driver::get_by_name("MEM").unwrap(); let output_dataset = driver.create("", 5, 7, 1).unwrap(); diff --git a/src/raster/mdarray.rs b/src/raster/mdarray.rs index 5cb453d6c..e53e91ce4 100644 --- a/src/raster/mdarray.rs +++ b/src/raster/mdarray.rs @@ -782,19 +782,22 @@ impl Attribute { #[cfg(test)] mod tests { + use super::*; - use crate::{Dataset, DatasetOptions, GdalOpenFlags}; + use crate::{test_utils::TempFixture, Dataset, DatasetOptions, GdalOpenFlags}; #[test] fn test_root_group_name() { + let fixture = TempFixture::fixture("byte_no_cf.nc"); + let options = DatasetOptions { open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER, allowed_drivers: None, open_options: None, sibling_files: None, }; - let dataset = Dataset::open_ex("fixtures/byte_no_cf.nc", options).unwrap(); + let dataset = Dataset::open_ex(&fixture, options).unwrap(); let root_group = dataset.root_group().unwrap(); let root_group_name = root_group.name(); assert_eq!(root_group_name, "/"); @@ -802,13 +805,15 @@ mod tests { #[test] fn test_array_names() { + let fixture = TempFixture::fixture("byte_no_cf.nc"); + let dataset_options = DatasetOptions { open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER, allowed_drivers: None, open_options: None, sibling_files: None, }; - let dataset = Dataset::open_ex("fixtures/byte_no_cf.nc", dataset_options).unwrap(); + let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap(); let root_group = dataset.root_group().unwrap(); let options = CslStringList::new(); //Driver specific options determining how groups should be retrieved. Pass nullptr for default behavior. let array_names = root_group.array_names(options); @@ -817,13 +822,15 @@ mod tests { #[test] fn test_n_dimension() { + let fixture = TempFixture::fixture("byte_no_cf.nc"); + let dataset_options = DatasetOptions { open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER, allowed_drivers: None, open_options: None, sibling_files: None, }; - let dataset = Dataset::open_ex("fixtures/byte_no_cf.nc", dataset_options).unwrap(); + let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap(); let root_group = dataset.root_group().unwrap(); let array_name = "Band1".to_string(); let options = CslStringList::new(); //Driver specific options determining how the array should be opened. Pass nullptr for default behavior. @@ -834,13 +841,15 @@ mod tests { #[test] fn test_n_elements() { + let fixture = TempFixture::fixture("byte_no_cf.nc"); + let dataset_options = DatasetOptions { open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER, allowed_drivers: None, open_options: None, sibling_files: None, }; - let dataset = Dataset::open_ex("fixtures/byte_no_cf.nc", dataset_options).unwrap(); + let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap(); let root_group = dataset.root_group().unwrap(); let array_name = "Band1".to_string(); let options = CslStringList::new(); //Driver specific options determining how the array should be opened. Pass nullptr for default behavior. @@ -851,13 +860,15 @@ mod tests { #[test] fn test_dimension_name() { + let fixture = TempFixture::fixture("byte_no_cf.nc"); + let dataset_options = DatasetOptions { open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER, allowed_drivers: None, open_options: None, sibling_files: None, }; - let dataset = Dataset::open_ex("fixtures/byte_no_cf.nc", dataset_options).unwrap(); + let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap(); let root_group = dataset.root_group().unwrap(); // group dimensions @@ -883,13 +894,15 @@ mod tests { #[test] fn test_dimension_size() { + let fixture = TempFixture::fixture("byte_no_cf.nc"); + let dataset_options = DatasetOptions { open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER, allowed_drivers: None, open_options: None, sibling_files: None, }; - let dataset = Dataset::open_ex("fixtures/byte_no_cf.nc", dataset_options).unwrap(); + let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap(); let root_group = dataset.root_group().unwrap(); let array_name = "Band1".to_string(); let options = CslStringList::new(); //Driver specific options determining how the array should be opened. Pass nullptr for default behavior. @@ -904,13 +917,15 @@ mod tests { #[test] fn test_read_data() { + let fixture = TempFixture::fixture("byte_no_cf.nc"); + let dataset_options = DatasetOptions { open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER, allowed_drivers: None, open_options: None, sibling_files: None, }; - let dataset = Dataset::open_ex("fixtures/byte_no_cf.nc", dataset_options).unwrap(); + let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap(); let root_group = dataset.root_group().unwrap(); let md_array = root_group @@ -924,13 +939,15 @@ mod tests { #[test] fn test_read_string_array() { + let fixture = TempFixture::fixture("alldatatypes.nc"); + let dataset_options = DatasetOptions { open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER, allowed_drivers: None, open_options: None, sibling_files: None, }; - let dataset = Dataset::open_ex("fixtures/alldatatypes.nc", dataset_options).unwrap(); + let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap(); let root_group = dataset.root_group().unwrap(); @@ -950,13 +967,15 @@ mod tests { #[test] fn test_datatype() { + let fixture = TempFixture::fixture("byte_no_cf.nc"); + let dataset_options = DatasetOptions { open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER, allowed_drivers: None, open_options: None, sibling_files: None, }; - let dataset = Dataset::open_ex("fixtures/byte_no_cf.nc", dataset_options).unwrap(); + let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap(); let root_group = dataset.root_group().unwrap(); @@ -973,13 +992,15 @@ mod tests { #[test] fn test_spatial_ref() { + let fixture = TempFixture::fixture("byte_no_cf.nc"); + let dataset_options = DatasetOptions { open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER, allowed_drivers: None, open_options: None, sibling_files: None, }; - let dataset = Dataset::open_ex("fixtures/byte_no_cf.nc", dataset_options).unwrap(); + let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap(); let root_group = dataset.root_group().unwrap(); let md_array = root_group @@ -995,13 +1016,15 @@ mod tests { #[test] fn test_no_data_value() { + let fixture = TempFixture::fixture("byte_no_cf.nc"); + let dataset_options = DatasetOptions { open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER, allowed_drivers: None, open_options: None, sibling_files: None, }; - let dataset = Dataset::open_ex("fixtures/byte_no_cf.nc", dataset_options).unwrap(); + let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap(); let root_group = dataset.root_group().unwrap(); let md_array = root_group @@ -1013,13 +1036,15 @@ mod tests { #[test] fn test_attributes() { + let fixture = TempFixture::fixture("cf_nasa_4326.nc"); + let dataset_options = DatasetOptions { open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER, allowed_drivers: None, open_options: None, sibling_files: None, }; - let dataset = Dataset::open_ex("fixtures/cf_nasa_4326.nc", dataset_options).unwrap(); + let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap(); let root_group = dataset.root_group().unwrap(); @@ -1064,13 +1089,15 @@ mod tests { #[test] fn test_unit() { + let fixture = TempFixture::fixture("cf_nasa_4326.nc"); + let dataset_options = DatasetOptions { open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER, allowed_drivers: None, open_options: None, sibling_files: None, }; - let dataset = Dataset::open_ex("fixtures/cf_nasa_4326.nc", dataset_options).unwrap(); + let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap(); let root_group = dataset.root_group().unwrap(); @@ -1101,34 +1128,31 @@ mod tests { #[test] fn test_stats() { - { - let dataset_options = DatasetOptions { - open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER, - allowed_drivers: None, - open_options: None, - sibling_files: None, - }; - let dataset = Dataset::open_ex("fixtures/byte_no_cf.nc", dataset_options).unwrap(); - let root_group = dataset.root_group().unwrap(); - let array_name = "Band1".to_string(); - let options = CslStringList::new(); //Driver specific options determining how the array should be opened. Pass nullptr for default behavior. - let md_array = root_group.open_md_array(&array_name, options).unwrap(); - - assert!(md_array.get_statistics(false, true).unwrap().is_none()); - - assert_eq!( - md_array.get_statistics(true, true).unwrap().unwrap(), - MdStatisticsAll { - min: 74.0, - max: 255.0, - mean: 126.76500000000001, - std_dev: 22.928470838675654, - valid_count: 400, - } - ); - } + let fixture = TempFixture::fixture("byte_no_cf.nc"); + + let dataset_options = DatasetOptions { + open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER, + allowed_drivers: None, + open_options: None, + sibling_files: None, + }; + let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap(); + let root_group = dataset.root_group().unwrap(); + let array_name = "Band1".to_string(); + let options = CslStringList::new(); //Driver specific options determining how the array should be opened. Pass nullptr for default behavior. + let md_array = root_group.open_md_array(&array_name, options).unwrap(); - // clean up aux file - std::fs::remove_file("fixtures/byte_no_cf.nc.aux.xml").unwrap(); + assert!(md_array.get_statistics(false, true).unwrap().is_none()); + + assert_eq!( + md_array.get_statistics(true, true).unwrap().unwrap(), + MdStatisticsAll { + min: 74.0, + max: 255.0, + mean: 126.76500000000001, + std_dev: 22.928470838675654, + valid_count: 400, + } + ); } } diff --git a/src/raster/tests.rs b/src/raster/tests.rs index 3f6a1c722..9f99c17ff 100644 --- a/src/raster/tests.rs +++ b/src/raster/tests.rs @@ -4,6 +4,7 @@ use crate::raster::rasterband::ResampleAlg; use crate::raster::{ ByteBuffer, ColorInterpretation, RasterCreationOption, StatisticsAll, StatisticsMinMax, }; +use crate::test_utils::TempFixture; use crate::vsi::unlink_mem_file; use crate::Driver; use gdal_sys::GDALDataType; @@ -806,7 +807,9 @@ fn test_color_table() { #[test] fn test_raster_stats() { - let dataset = Dataset::open(fixture!("tinymarble.tif")).unwrap(); + let fixture = TempFixture::fixture("tinymarble.tif"); + + let dataset = Dataset::open(&fixture).unwrap(); let rb = dataset.rasterband(1).unwrap(); assert!(rb.get_statistics(false, false).unwrap().is_none()); @@ -828,8 +831,4 @@ fn test_raster_stats() { max: 255.0, } ); - - // clean up aux file - drop(dataset); - std::fs::remove_file(fixture!("tinymarble.tif.aux.xml")).unwrap(); } diff --git a/src/test_utils.rs b/src/test_utils.rs new file mode 100644 index 000000000..a4b120d65 --- /dev/null +++ b/src/test_utils.rs @@ -0,0 +1,37 @@ +use std::path::{Path, PathBuf}; + +/// A struct that contains a temporary directory and a path to a file in that directory. +pub struct TempFixture { + _temp_dir: tempfile::TempDir, + temp_path: PathBuf, +} + +impl TempFixture { + /// Creates a copy of the test file in a temporary directory. + /// Returns the struct `TempFixture` that contains the temp dir (for clean-up on `drop`) as well as the path to the file. + /// + /// This can potentially be removed when is resolved. + pub fn fixture(name: &str) -> Self { + let path = std::path::Path::new("fixtures").join(name); + + let _temp_dir = tempfile::tempdir().unwrap(); + let temp_path = _temp_dir.path().join(path.file_name().unwrap()); + + std::fs::copy(&path, &temp_path).unwrap(); + + Self { + _temp_dir, + temp_path, + } + } + + pub fn path(&self) -> &Path { + &self.temp_path + } +} + +impl AsRef for TempFixture { + fn as_ref(&self) -> &Path { + self.path() + } +}