1use std::{env, fs};
18
19use datafusion_common::Result;
20use sedona_common::sedona_internal_err;
21
22pub fn test_geoparquet(group: &str, name: &str) -> Result<String> {
28 let geoarrow_data = geoarrow_data_dir()?;
29 let path = format!("{geoarrow_data}/{group}/files/{group}_{name}_geo.parquet");
30 if let Ok(exists) = fs::exists(&path) {
31 if exists {
32 return Ok(path);
33 }
34 }
35
36 sedona_internal_err!(
37 "geoarrow-data test file '{path}' does not exist.\n{}\n{}",
38 "You may need to check the value of the SEDONA_GEOARROW_DATA_DIR environment variable,",
39 "run submodules/download-assets.py, or check the name of the file you requested"
40 )
41}
42
43pub fn geoarrow_data_dir() -> Result<String> {
49 if let Ok(from_env) = env::var("SEDONA_GEOARROW_DATA_DIR") {
51 if fs::exists(&from_env)? {
52 return Ok(from_env);
53 } else {
54 return sedona_internal_err!(
55 "{}\n{}{}{}",
56 "Can't resolve geoarrow-data from the current working directory because",
57 "the value of the SEDONA_GEOARROW_DATA_DIR (",
58 from_env,
59 ") does not exist"
60 );
61 }
62 }
63
64 let likely_possibilities = [
65 "../../submodules/geoarrow-data".to_string(),
67 "submodules/geoarrow-data".to_string(),
69 ];
70
71 for possibility in likely_possibilities.into_iter().rev() {
72 if let Ok(exists) = fs::exists(&possibility) {
73 if exists {
74 return Ok(possibility);
75 }
76 }
77 }
78
79 sedona_internal_err!(
80 "{}\n{}\n{}",
81 "Can't resolve geoarrow-data from the current working directory",
82 "You may need to run `git submodule init && git submodule update --recursive` or",
83 "set the SEDONA_GEOARROW_DATA_DIR environment variable"
84 )
85}
86
87pub fn sedona_testing_dir() -> Result<String> {
93 if let Ok(from_env) = env::var("SEDONA_TESTING_DIR") {
94 if fs::exists(&from_env)? {
95 return Ok(from_env);
96 } else {
97 return sedona_internal_err!(
98 "{}\n{}{}{}",
99 "Can't resolve sedona-testing directory because",
100 "the value of the SEDONA_TESTING_DIR (",
101 from_env,
102 ") does not exist"
103 );
104 }
105 }
106
107 let likely_possibilities = [
108 "../../submodules/sedona-testing".to_string(),
109 "submodules/sedona-testing".to_string(),
110 ];
111
112 for possibility in likely_possibilities.into_iter().rev() {
113 if let Ok(exists) = fs::exists(&possibility) {
114 if exists {
115 return Ok(possibility);
116 }
117 }
118 }
119
120 sedona_internal_err!(
121 "{}\n{}\n{}",
122 "Can't resolve sedona-testing directory from the current working directory",
123 "You may need to run `git submodule init && git submodule update --recursive` or",
124 "set the SEDONA_TESTING_DIR environment variable"
125 )
126}
127
128#[cfg(test)]
129mod test {
130 use super::*;
131 use std::sync::Mutex;
132
133 static SERIAL_TEST: Mutex<()> = Mutex::new(());
137
138 #[test]
139 fn example_files() {
140 let _guard = SERIAL_TEST.lock().unwrap();
141
142 assert!(geoarrow_data_dir().is_ok());
144 assert!(test_geoparquet("natural-earth", "countries").is_ok());
145
146 let err = test_geoparquet("invalid group", "invalid name").unwrap_err();
148 assert!(err.message().contains("geoarrow-data test file"));
149
150 env::set_var("SEDONA_GEOARROW_DATA_DIR", "this_directory_does_not_exist");
152 let err = geoarrow_data_dir();
153 env::remove_var("SEDONA_GEOARROW_DATA_DIR");
154 assert!(err
155 .unwrap_err()
156 .message()
157 .contains("the value of the SEDONA_GEOARROW_DATA_DIR"));
158
159 env::set_var("SEDONA_GEOARROW_DATA_DIR", geoarrow_data_dir().unwrap());
161 let maybe_file = test_geoparquet("natural-earth", "countries");
162 env::remove_var("SEDONA_GEOARROW_DATA_DIR");
163 assert!(maybe_file.is_ok());
164 }
165
166 #[test]
167 fn sedona_testing_dir_resolves() {
168 let _guard = SERIAL_TEST.lock().unwrap();
169
170 assert!(sedona_testing_dir().is_ok());
171
172 env::set_var("SEDONA_TESTING_DIR", "this_directory_does_not_exist");
173 let err = sedona_testing_dir();
174 env::remove_var("SEDONA_TESTING_DIR");
175 assert!(err
176 .unwrap_err()
177 .message()
178 .contains("the value of the SEDONA_TESTING_DIR"));
179
180 env::set_var("SEDONA_TESTING_DIR", sedona_testing_dir().unwrap());
181 let maybe_dir = sedona_testing_dir();
182 env::remove_var("SEDONA_TESTING_DIR");
183 assert!(maybe_dir.is_ok());
184 }
185}