Repository navigation
fix(engine): refuse feature and table exports over their own source #3582
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
Open
elpidioassinaturas
wants to merge
3
commits into
opengeos:main
Choose a base branch
from
elpidioassinaturas:fix/3567-export-own-source
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+229
−10
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| //! Feature and table exports refuse to write over the file a layer or table was read from (#3567), | ||
| //! as tool outputs already do: a row limit or a definition query would cut the source itself. | ||
|
|
||
| use crate::{Project, Session}; | ||
| use serde_json::json; | ||
|
|
||
| /// A folder removed however the test ends. | ||
| struct Dir(std::path::PathBuf); | ||
| impl Drop for Dir { | ||
| fn drop(&mut self) { | ||
| let _ = std::fs::remove_dir_all(&self.0); | ||
| } | ||
| } | ||
|
|
||
| fn dir(name: &str) -> Dir { | ||
| let d = std::env::temp_dir().join(format!("rustgis-export-own-source-{name}-{}", std::process::id())); | ||
| let _ = std::fs::remove_dir_all(&d); | ||
| std::fs::create_dir_all(&d).unwrap(); | ||
| Dir(d) | ||
| } | ||
|
|
||
| const ROWS: &str = "code,name,value\n1,Alpha,10\n2,Beta,20\n3,Gamma,30\n"; | ||
| const POINTS: &str = r#"{"type":"FeatureCollection","features":[ | ||
| {"type":"Feature","properties":{"n":1},"geometry":{"type":"Point","coordinates":[1,1]}}, | ||
| {"type":"Feature","properties":{"n":2},"geometry":{"type":"Point","coordinates":[2,2]}}]}"#; | ||
|
|
||
| #[test] | ||
| fn export_table_refuses_its_own_csv_and_writes_another() { | ||
| let d = dir("table"); | ||
| let source = d.0.join("origem.csv"); | ||
| std::fs::write(&source, ROWS).unwrap(); | ||
| let mut s = Session::new(Project::default()); | ||
| s.run("layer.addData", &json!({"path": source})).unwrap(); | ||
| assert!(s.project.tables.iter().any(|t| t.dataset.name == "origem")); | ||
|
|
||
| let error = s.run("share.exportTable", &json!({"table": "origem", "path": source, "limit": 1})).unwrap_err(); | ||
| assert!(error.contains("destination is the source of table 'origem'; choose a different output file"), "{error}"); | ||
| assert_eq!(std::fs::read_to_string(&source).unwrap(), ROWS); | ||
|
|
||
| let other = d.0.join("first.csv"); | ||
| s.run("share.exportTable", &json!({"table": "origem", "path": other, "limit": 1})).unwrap(); | ||
| assert_eq!(std::fs::read_to_string(&other).unwrap().lines().count(), 2); | ||
| assert_eq!(std::fs::read_to_string(&source).unwrap(), ROWS); | ||
| } | ||
|
|
||
| #[test] | ||
| fn export_layer_refuses_its_own_geojson_and_writes_another() { | ||
| let d = dir("layer"); | ||
| let source = d.0.join("pontos.geojson"); | ||
| std::fs::write(&source, POINTS).unwrap(); | ||
| let mut s = Session::new(Project::default()); | ||
| s.run("layer.addData", &json!({"path": source})).unwrap(); | ||
| s.run("layer.definitionQuery", &json!({"layer": "pontos", "where": "n = 1"})).unwrap(); | ||
|
|
||
| let error = s.run("share.exportLayer", &json!({"layer": "pontos", "path": source})).unwrap_err(); | ||
| assert!(error.contains("destination is the source of layer 'pontos'; choose a different output file"), "{error}"); | ||
| assert_eq!(std::fs::read_to_string(&source).unwrap(), POINTS); | ||
|
|
||
| let other = d.0.join("one.geojson"); | ||
| s.run("share.exportLayer", &json!({"layer": "pontos", "path": other})).unwrap(); | ||
| let written: serde_json::Value = serde_json::from_str(&std::fs::read_to_string(&other).unwrap()).unwrap(); | ||
| assert_eq!(written["features"].as_array().unwrap().len(), 1); | ||
| } | ||
|
|
||
| /// The rows of `table` in GeoPackage `path`, read apart from the session. | ||
| fn rows(path: &std::path::Path, table: &str) -> i64 { | ||
| let db = rusqlite::Connection::open(path).unwrap(); | ||
| db.query_row(&format!("SELECT count(*) FROM \"{table}\""), [], |r| r.get(0)).unwrap() | ||
| } | ||
|
|
||
| /// In a GeoPackage a layer was read from, a new file and the layer's own table replaced are | ||
| /// refused; a table added beside it, or another table replaced, leaves its rows alone. A layer | ||
| /// read whole doesn't keep its table's name: any table it replaces there is refused. | ||
| #[test] | ||
| fn export_layer_into_its_own_geopackage_keeps_its_table() { | ||
| let d = dir("gpkg"); | ||
| let geojson = d.0.join("pontos.geojson"); | ||
| std::fs::write(&geojson, POINTS).unwrap(); | ||
| let source = d.0.join("pontos.gpkg"); | ||
| let mut s = Session::new(Project::default()); | ||
| s.run("layer.addData", &json!({"path": geojson})).unwrap(); | ||
| s.run("share.exportLayer", &json!({"layer": "pontos", "path": source})).unwrap(); | ||
| s.run("layer.addData", &json!({"path": source, "name": "Whole"})).unwrap(); | ||
| s.run("layer.addData", &json!({"path": source, "layer": "pontos", "stream": true, "name": "Streamed"})).unwrap(); | ||
| for layer in ["Whole", "Streamed"] { | ||
| s.run("layer.definitionQuery", &json!({"layer": layer, "where": "n = 1"})).unwrap(); | ||
| } | ||
|
|
||
| let error = s.run("share.exportLayer", &json!({"layer": "pontos", "path": source})).unwrap_err(); | ||
| assert!(error.contains("destination is the source of layer"), "{error}"); | ||
| for layer in ["Whole", "Streamed"] { | ||
| let own = json!({"layer": layer, "path": source, "mode": "replaceTable", "tableName": "pontos"}); | ||
| let error = s.run("share.exportLayer", &own).unwrap_err(); | ||
| assert!(error.contains(&format!("destination is the source of layer '{layer}'")), "{error}"); | ||
| } | ||
| assert_eq!(rows(&source, "pontos"), 2); | ||
|
|
||
| s.run("share.exportLayer", &json!({"layer": "pontos", "path": source, "mode": "addTable", "tableName": "copia"})).unwrap(); | ||
|
elpidioassinaturas marked this conversation as resolved.
Resolved
|
||
| assert_eq!((rows(&source, "pontos"), rows(&source, "copia")), (2, 2)); | ||
| let other = json!({"layer": "Whole", "path": source, "mode": "replaceTable", "tableName": "copia"}); | ||
| assert!(s.run("share.exportLayer", &other).unwrap_err().contains("source of layer 'Whole'")); | ||
| s.run("share.exportLayer", &json!({"layer": "Streamed", "path": source, "mode": "replaceTable", "tableName": "copia"})).unwrap(); | ||
| assert_eq!((rows(&source, "pontos"), rows(&source, "copia")), (2, 1)); | ||
| } | ||
|
|
||
| /// A Shapefile export writes its `.dbf` beside the `.shp`: data read from that `.dbf`, as the | ||
| /// Shapefile's layer or as a table on its own, refuses it. | ||
| #[test] | ||
| fn export_layer_to_a_shapefile_beside_a_loaded_dbf_is_refused() { | ||
| let d = dir("shp"); | ||
| let geojson = d.0.join("pontos.geojson"); | ||
| std::fs::write(&geojson, POINTS).unwrap(); | ||
| let shp = d.0.join("roads.shp"); | ||
| let dbf = shp.with_extension("dbf"); | ||
| for alone in [false, true] { | ||
| let mut s = Session::new(Project::default()); | ||
| s.run("layer.addData", &json!({"path": geojson})).unwrap(); | ||
| s.run("share.exportLayer", &json!({"layer": "pontos", "path": shp})).unwrap(); | ||
| if alone { | ||
| for ext in ["shp", "shx", "prj", "cpg"] { | ||
| let _ = std::fs::remove_file(shp.with_extension(ext)); | ||
| } | ||
| } | ||
| let before = std::fs::read(&dbf).unwrap(); | ||
| let added = s.run("layer.addData", &json!({"path": dbf})).unwrap(); | ||
| let (kind, name) = match added["table"].as_str() { | ||
| Some(table) => ("table", table.to_string()), | ||
| None => ("layer", s.map().layer(added["layer"].as_u64().unwrap_or_else(|| panic!("{added}")) as u32).unwrap().name.clone()), | ||
| }; | ||
| assert_eq!(kind, if alone { "table" } else { "layer" }, "{added}"); | ||
| s.run("layer.definitionQuery", &json!({"layer": "pontos", "where": "n = 1"})).unwrap(); | ||
|
|
||
| let error = s.run("share.exportLayer", &json!({"layer": "pontos", "path": shp})).unwrap_err(); | ||
| assert!(error.contains(&format!("destination is the source of {kind} '{name}'")), "{error}"); | ||
| assert_eq!(std::fs::read(&dbf).unwrap(), before); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.