From cad4727afc2adaf3d4fc96f88cf28748aabae25f Mon Sep 17 00:00:00 2001 From: Christian Beilschmidt Date: Fri, 2 Sep 2022 16:47:34 +0200 Subject: [PATCH 1/4] use tempfiles to not access file multiple times --- src/programs/raster/mdimtranslate.rs | 46 ++++++++- src/raster/mdarray.rs | 143 +++++++++++++++++++-------- 2 files changed, 146 insertions(+), 43 deletions(-) diff --git a/src/programs/raster/mdimtranslate.rs b/src/programs/raster/mdimtranslate.rs index c59404541..1e47ccfc8 100644 --- a/src/programs/raster/mdimtranslate.rs +++ b/src/programs/raster/mdimtranslate.rs @@ -231,15 +231,55 @@ mod tests { use crate::{DatasetOptions, Driver, GdalOpenFlags}; + /// Create a copy of the test file in a temporary directory + /// and returns a tuple of the temp dir (for clean-up) as well as the path to the file. + /// We can remove this when is resolved. + struct TempDataset { + _temp_dir: tempfile::TempDir, + temp_path: PathBuf, + } + + impl TempDataset { + pub fn fixture(name: &str) -> Self { + let path = std::path::Path::new(file!()) + .parent() + .unwrap() + .parent() + .unwrap() + .parent() + .unwrap() + .parent() + .unwrap() + .join("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_dir, + temp_path, + } + } + + pub fn path(&self) -> &Path { + &self.temp_path + } + } + #[test] fn test_build_tiff_from_path() { + let fixture = TempDataset::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.path(), dataset_options).unwrap(); let mem_file_path = "/vsimem/2d3e9124-a7a0-413e-97b5-e79d46e50ff8"; @@ -263,13 +303,15 @@ mod tests { #[test] fn test_build_tiff_from_dataset() { + let fixture = TempDataset::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.path(), 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..09dbb7e65 100644 --- a/src/raster/mdarray.rs +++ b/src/raster/mdarray.rs @@ -782,19 +782,59 @@ impl Attribute { #[cfg(test)] mod tests { + use std::path::{Path, PathBuf}; + use super::*; use crate::{Dataset, DatasetOptions, GdalOpenFlags}; + /// Create a copy of the test file in a temporary directory + /// and returns a tuple of the temp dir (for clean-up) as well as the path to the file. + /// We can remove this when is resolved. + struct TempDataset { + _temp_dir: tempfile::TempDir, + temp_path: PathBuf, + } + + impl TempDataset { + pub fn fixture(name: &str) -> Self { + let path = std::path::Path::new(file!()) + .parent() + .unwrap() + .parent() + .unwrap() + .parent() + .unwrap() + .join("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_dir, + temp_path, + } + } + + pub fn path(&self) -> &Path { + &self.temp_path + } + } + #[test] fn test_root_group_name() { + let fixture = TempDataset::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.path(), options).unwrap(); let root_group = dataset.root_group().unwrap(); let root_group_name = root_group.name(); assert_eq!(root_group_name, "/"); @@ -802,13 +842,15 @@ mod tests { #[test] fn test_array_names() { + let fixture = TempDataset::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.path(), 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 +859,15 @@ mod tests { #[test] fn test_n_dimension() { + let fixture = TempDataset::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.path(), 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 +878,15 @@ mod tests { #[test] fn test_n_elements() { + let fixture = TempDataset::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.path(), 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 +897,15 @@ mod tests { #[test] fn test_dimension_name() { + let fixture = TempDataset::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.path(), dataset_options).unwrap(); let root_group = dataset.root_group().unwrap(); // group dimensions @@ -883,13 +931,15 @@ mod tests { #[test] fn test_dimension_size() { + let fixture = TempDataset::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.path(), 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 +954,15 @@ mod tests { #[test] fn test_read_data() { + let fixture = TempDataset::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.path(), dataset_options).unwrap(); let root_group = dataset.root_group().unwrap(); let md_array = root_group @@ -924,13 +976,15 @@ mod tests { #[test] fn test_read_string_array() { + let fixture = TempDataset::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.path(), dataset_options).unwrap(); let root_group = dataset.root_group().unwrap(); @@ -950,13 +1004,15 @@ mod tests { #[test] fn test_datatype() { + let fixture = TempDataset::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.path(), dataset_options).unwrap(); let root_group = dataset.root_group().unwrap(); @@ -973,13 +1029,15 @@ mod tests { #[test] fn test_spatial_ref() { + let fixture = TempDataset::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.path(), dataset_options).unwrap(); let root_group = dataset.root_group().unwrap(); let md_array = root_group @@ -995,13 +1053,15 @@ mod tests { #[test] fn test_no_data_value() { + let fixture = TempDataset::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.path(), dataset_options).unwrap(); let root_group = dataset.root_group().unwrap(); let md_array = root_group @@ -1013,13 +1073,15 @@ mod tests { #[test] fn test_attributes() { + let fixture = TempDataset::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.path(), dataset_options).unwrap(); let root_group = dataset.root_group().unwrap(); @@ -1064,13 +1126,15 @@ mod tests { #[test] fn test_unit() { + let fixture = TempDataset::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.path(), dataset_options).unwrap(); let root_group = dataset.root_group().unwrap(); @@ -1101,34 +1165,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 = TempDataset::fixture("byte_no_cf.nc"); - // clean up aux file - std::fs::remove_file("fixtures/byte_no_cf.nc.aux.xml").unwrap(); + 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.path(), 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, + } + ); } } From 3886bb23a0738ad3d22d992ebd4c1745b1332b6e Mon Sep 17 00:00:00 2001 From: Christian Beilschmidt Date: Fri, 2 Sep 2022 16:49:00 +0200 Subject: [PATCH 2/4] changelog entry --- CHANGES.md | 4 ++++ 1 file changed, 4 insertions(+) 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 From cb5cf9f52e54c7c564647737752665ab78a91596 Mon Sep 17 00:00:00 2001 From: Christian Beilschmidt Date: Fri, 2 Sep 2022 17:02:17 +0200 Subject: [PATCH 3/4] test util with `TempFixture` --- src/lib.rs | 2 + src/programs/raster/mdimtranslate.rs | 49 ++------------ src/raster/mdarray.rs | 95 +++++++++------------------- src/test_utils.rs | 37 +++++++++++ 4 files changed, 74 insertions(+), 109 deletions(-) create mode 100644 src/test_utils.rs 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 1e47ccfc8..f6b561f6b 100644 --- a/src/programs/raster/mdimtranslate.rs +++ b/src/programs/raster/mdimtranslate.rs @@ -227,51 +227,14 @@ fn _multi_dim_translate( #[cfg(test)] mod tests { - use super::*; - - use crate::{DatasetOptions, Driver, GdalOpenFlags}; - /// Create a copy of the test file in a temporary directory - /// and returns a tuple of the temp dir (for clean-up) as well as the path to the file. - /// We can remove this when is resolved. - struct TempDataset { - _temp_dir: tempfile::TempDir, - temp_path: PathBuf, - } - - impl TempDataset { - pub fn fixture(name: &str) -> Self { - let path = std::path::Path::new(file!()) - .parent() - .unwrap() - .parent() - .unwrap() - .parent() - .unwrap() - .parent() - .unwrap() - .join("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_dir, - temp_path, - } - } + use super::*; - pub fn path(&self) -> &Path { - &self.temp_path - } - } + use crate::{test_utils::TempFixture, DatasetOptions, Driver, GdalOpenFlags}; #[test] fn test_build_tiff_from_path() { - let fixture = TempDataset::fixture("cf_nasa_4326.nc"); + let fixture = TempFixture::fixture("cf_nasa_4326.nc"); let dataset_options = DatasetOptions { open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER, @@ -279,7 +242,7 @@ mod tests { open_options: None, sibling_files: None, }; - let dataset = Dataset::open_ex(fixture.path(), dataset_options).unwrap(); + let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap(); let mem_file_path = "/vsimem/2d3e9124-a7a0-413e-97b5-e79d46e50ff8"; @@ -303,7 +266,7 @@ mod tests { #[test] fn test_build_tiff_from_dataset() { - let fixture = TempDataset::fixture("cf_nasa_4326.nc"); + let fixture = TempFixture::fixture("cf_nasa_4326.nc"); let dataset_options = DatasetOptions { open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER, @@ -311,7 +274,7 @@ mod tests { open_options: None, sibling_files: None, }; - let dataset = Dataset::open_ex(fixture.path(), 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 09dbb7e65..e53e91ce4 100644 --- a/src/raster/mdarray.rs +++ b/src/raster/mdarray.rs @@ -782,51 +782,14 @@ impl Attribute { #[cfg(test)] mod tests { - use std::path::{Path, PathBuf}; use super::*; - use crate::{Dataset, DatasetOptions, GdalOpenFlags}; - - /// Create a copy of the test file in a temporary directory - /// and returns a tuple of the temp dir (for clean-up) as well as the path to the file. - /// We can remove this when is resolved. - struct TempDataset { - _temp_dir: tempfile::TempDir, - temp_path: PathBuf, - } - - impl TempDataset { - pub fn fixture(name: &str) -> Self { - let path = std::path::Path::new(file!()) - .parent() - .unwrap() - .parent() - .unwrap() - .parent() - .unwrap() - .join("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_dir, - temp_path, - } - } - - pub fn path(&self) -> &Path { - &self.temp_path - } - } + use crate::{test_utils::TempFixture, Dataset, DatasetOptions, GdalOpenFlags}; #[test] fn test_root_group_name() { - let fixture = TempDataset::fixture("byte_no_cf.nc"); + let fixture = TempFixture::fixture("byte_no_cf.nc"); let options = DatasetOptions { open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER, @@ -834,7 +797,7 @@ mod tests { open_options: None, sibling_files: None, }; - let dataset = Dataset::open_ex(fixture.path(), 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, "/"); @@ -842,7 +805,7 @@ mod tests { #[test] fn test_array_names() { - let fixture = TempDataset::fixture("byte_no_cf.nc"); + let fixture = TempFixture::fixture("byte_no_cf.nc"); let dataset_options = DatasetOptions { open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER, @@ -850,7 +813,7 @@ mod tests { open_options: None, sibling_files: None, }; - let dataset = Dataset::open_ex(fixture.path(), 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); @@ -859,7 +822,7 @@ mod tests { #[test] fn test_n_dimension() { - let fixture = TempDataset::fixture("byte_no_cf.nc"); + let fixture = TempFixture::fixture("byte_no_cf.nc"); let dataset_options = DatasetOptions { open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER, @@ -867,7 +830,7 @@ mod tests { open_options: None, sibling_files: None, }; - let dataset = Dataset::open_ex(fixture.path(), 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. @@ -878,7 +841,7 @@ mod tests { #[test] fn test_n_elements() { - let fixture = TempDataset::fixture("byte_no_cf.nc"); + let fixture = TempFixture::fixture("byte_no_cf.nc"); let dataset_options = DatasetOptions { open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER, @@ -886,7 +849,7 @@ mod tests { open_options: None, sibling_files: None, }; - let dataset = Dataset::open_ex(fixture.path(), 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. @@ -897,7 +860,7 @@ mod tests { #[test] fn test_dimension_name() { - let fixture = TempDataset::fixture("byte_no_cf.nc"); + let fixture = TempFixture::fixture("byte_no_cf.nc"); let dataset_options = DatasetOptions { open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER, @@ -905,7 +868,7 @@ mod tests { open_options: None, sibling_files: None, }; - let dataset = Dataset::open_ex(fixture.path(), dataset_options).unwrap(); + let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap(); let root_group = dataset.root_group().unwrap(); // group dimensions @@ -931,7 +894,7 @@ mod tests { #[test] fn test_dimension_size() { - let fixture = TempDataset::fixture("byte_no_cf.nc"); + let fixture = TempFixture::fixture("byte_no_cf.nc"); let dataset_options = DatasetOptions { open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER, @@ -939,7 +902,7 @@ mod tests { open_options: None, sibling_files: None, }; - let dataset = Dataset::open_ex(fixture.path(), 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. @@ -954,7 +917,7 @@ mod tests { #[test] fn test_read_data() { - let fixture = TempDataset::fixture("byte_no_cf.nc"); + let fixture = TempFixture::fixture("byte_no_cf.nc"); let dataset_options = DatasetOptions { open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER, @@ -962,7 +925,7 @@ mod tests { open_options: None, sibling_files: None, }; - let dataset = Dataset::open_ex(fixture.path(), dataset_options).unwrap(); + let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap(); let root_group = dataset.root_group().unwrap(); let md_array = root_group @@ -976,7 +939,7 @@ mod tests { #[test] fn test_read_string_array() { - let fixture = TempDataset::fixture("alldatatypes.nc"); + let fixture = TempFixture::fixture("alldatatypes.nc"); let dataset_options = DatasetOptions { open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER, @@ -984,7 +947,7 @@ mod tests { open_options: None, sibling_files: None, }; - let dataset = Dataset::open_ex(fixture.path(), dataset_options).unwrap(); + let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap(); let root_group = dataset.root_group().unwrap(); @@ -1004,7 +967,7 @@ mod tests { #[test] fn test_datatype() { - let fixture = TempDataset::fixture("byte_no_cf.nc"); + let fixture = TempFixture::fixture("byte_no_cf.nc"); let dataset_options = DatasetOptions { open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER, @@ -1012,7 +975,7 @@ mod tests { open_options: None, sibling_files: None, }; - let dataset = Dataset::open_ex(fixture.path(), dataset_options).unwrap(); + let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap(); let root_group = dataset.root_group().unwrap(); @@ -1029,7 +992,7 @@ mod tests { #[test] fn test_spatial_ref() { - let fixture = TempDataset::fixture("byte_no_cf.nc"); + let fixture = TempFixture::fixture("byte_no_cf.nc"); let dataset_options = DatasetOptions { open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER, @@ -1037,7 +1000,7 @@ mod tests { open_options: None, sibling_files: None, }; - let dataset = Dataset::open_ex(fixture.path(), dataset_options).unwrap(); + let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap(); let root_group = dataset.root_group().unwrap(); let md_array = root_group @@ -1053,7 +1016,7 @@ mod tests { #[test] fn test_no_data_value() { - let fixture = TempDataset::fixture("byte_no_cf.nc"); + let fixture = TempFixture::fixture("byte_no_cf.nc"); let dataset_options = DatasetOptions { open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER, @@ -1061,7 +1024,7 @@ mod tests { open_options: None, sibling_files: None, }; - let dataset = Dataset::open_ex(fixture.path(), dataset_options).unwrap(); + let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap(); let root_group = dataset.root_group().unwrap(); let md_array = root_group @@ -1073,7 +1036,7 @@ mod tests { #[test] fn test_attributes() { - let fixture = TempDataset::fixture("cf_nasa_4326.nc"); + let fixture = TempFixture::fixture("cf_nasa_4326.nc"); let dataset_options = DatasetOptions { open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER, @@ -1081,7 +1044,7 @@ mod tests { open_options: None, sibling_files: None, }; - let dataset = Dataset::open_ex(fixture.path(), dataset_options).unwrap(); + let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap(); let root_group = dataset.root_group().unwrap(); @@ -1126,7 +1089,7 @@ mod tests { #[test] fn test_unit() { - let fixture = TempDataset::fixture("cf_nasa_4326.nc"); + let fixture = TempFixture::fixture("cf_nasa_4326.nc"); let dataset_options = DatasetOptions { open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER, @@ -1134,7 +1097,7 @@ mod tests { open_options: None, sibling_files: None, }; - let dataset = Dataset::open_ex(fixture.path(), dataset_options).unwrap(); + let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap(); let root_group = dataset.root_group().unwrap(); @@ -1165,7 +1128,7 @@ mod tests { #[test] fn test_stats() { - let fixture = TempDataset::fixture("byte_no_cf.nc"); + let fixture = TempFixture::fixture("byte_no_cf.nc"); let dataset_options = DatasetOptions { open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER, @@ -1173,7 +1136,7 @@ mod tests { open_options: None, sibling_files: None, }; - let dataset = Dataset::open_ex(fixture.path(), 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. 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() + } +} From 499cac488fa4d00025e0d9926b5f0830940dec1d Mon Sep 17 00:00:00 2001 From: Christian Beilschmidt Date: Fri, 2 Sep 2022 17:04:31 +0200 Subject: [PATCH 4/4] use `TempFixture` for rasterband get_statistics test --- src/raster/tests.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) 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(); }