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
132 #[test]
133 fn example_files() {
134 assert!(geoarrow_data_dir().is_ok());
136 assert!(test_geoparquet("natural-earth", "countries").is_ok());
137
138 let err = test_geoparquet("invalid group", "invalid name").unwrap_err();
140 assert!(err.message().contains("geoarrow-data test file"));
141
142 env::set_var("SEDONA_GEOARROW_DATA_DIR", "this_directory_does_not_exist");
144 let err = geoarrow_data_dir();
145 env::remove_var("SEDONA_GEOARROW_DATA_DIR");
146 assert!(err
147 .unwrap_err()
148 .message()
149 .contains("the value of the SEDONA_GEOARROW_DATA_DIR"));
150
151 env::set_var("SEDONA_GEOARROW_DATA_DIR", geoarrow_data_dir().unwrap());
153 let maybe_file = test_geoparquet("natural-earth", "countries");
154 env::remove_var("SEDONA_GEOARROW_DATA_DIR");
155 assert!(maybe_file.is_ok());
156 }
157
158 #[test]
159 fn sedona_testing_dir_resolves() {
160 assert!(sedona_testing_dir().is_ok());
161
162 env::set_var("SEDONA_TESTING_DIR", "this_directory_does_not_exist");
163 let err = sedona_testing_dir();
164 env::remove_var("SEDONA_TESTING_DIR");
165 assert!(err
166 .unwrap_err()
167 .message()
168 .contains("the value of the SEDONA_TESTING_DIR"));
169
170 env::set_var("SEDONA_TESTING_DIR", sedona_testing_dir().unwrap());
171 let maybe_dir = sedona_testing_dir();
172 env::remove_var("SEDONA_TESTING_DIR");
173 assert!(maybe_dir.is_ok());
174 }
175}