Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use tempfiles to not access file multiple times #302

Merged
merged 4 commits into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@

- <https://github.com/georust/gdal/pull/292>

- Added a workaround in multi-dim tests to not access files multiple times

- <https://github.com/georust/gdal/pull/302>

## 0.12

- Bump Rust edition to 2021
Expand Down
46 changes: 44 additions & 2 deletions src/programs/raster/mdimtranslate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/OSGeo/gdal/issues/6253> 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!())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really better than Path::new("fixtures")? The tests should run in the root directory of the project.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can revert that, just copied it from fixtures!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.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";

Expand All @@ -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();
Expand Down
Loading