@@ -22,6 +22,7 @@ use std::thread;
22
22
23
23
use log:: info;
24
24
use sqllogictest:: strict_column_validator;
25
+ use tempfile:: TempDir ;
25
26
26
27
use datafusion:: prelude:: { SessionConfig , SessionContext } ;
27
28
@@ -83,7 +84,8 @@ async fn run_test_file(
83
84
relative_path : PathBuf ,
84
85
) -> Result < ( ) , Box < dyn Error > > {
85
86
info ! ( "Running with DataFusion runner: {}" , path. display( ) ) ;
86
- let ctx = context_for_test_file ( & relative_path) . await ;
87
+ let test_ctx = context_for_test_file ( & relative_path) . await ;
88
+ let ctx = test_ctx. session_ctx ( ) . clone ( ) ;
87
89
let mut runner = sqllogictest:: Runner :: new ( DataFusion :: new ( ctx, relative_path) ) ;
88
90
runner. with_column_validator ( strict_column_validator) ;
89
91
runner. run_file_async ( path) . await ?;
@@ -110,7 +112,8 @@ async fn run_complete_file(
110
112
111
113
info ! ( "Using complete mode to complete: {}" , path. display( ) ) ;
112
114
113
- let ctx = context_for_test_file ( & relative_path) . await ;
115
+ let test_ctx = context_for_test_file ( & relative_path) . await ;
116
+ let ctx = test_ctx. session_ctx ( ) . clone ( ) ;
114
117
let mut runner = sqllogictest:: Runner :: new ( DataFusion :: new ( ctx, relative_path) ) ;
115
118
let col_separator = " " ;
116
119
runner
@@ -160,27 +163,67 @@ fn read_dir_recursive<P: AsRef<Path>>(path: P) -> Box<dyn Iterator<Item = PathBu
160
163
}
161
164
162
165
/// Create a SessionContext, configured for the specific test
163
- async fn context_for_test_file ( relative_path : & Path ) -> SessionContext {
166
+ async fn context_for_test_file ( relative_path : & Path ) -> TestContext {
164
167
let config = SessionConfig :: new ( )
165
168
// hardcode target partitions so plans are deterministic
166
169
. with_target_partitions ( 4 ) ;
167
170
168
- let ctx = SessionContext :: with_config ( config) ;
171
+ let mut test_ctx = TestContext :: new ( SessionContext :: with_config ( config) ) ;
169
172
170
173
match relative_path. file_name ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) {
171
174
"aggregate.slt" => {
172
175
info ! ( "Registering aggregate tables" ) ;
173
- setup:: register_aggregate_tables ( & ctx ) . await ;
176
+ setup:: register_aggregate_tables ( test_ctx . session_ctx ( ) ) . await ;
174
177
}
175
178
"scalar.slt" => {
176
179
info ! ( "Registering scalar tables" ) ;
177
- setup:: register_scalar_tables ( & ctx) . await ;
180
+ setup:: register_scalar_tables ( test_ctx. session_ctx ( ) ) . await ;
181
+ }
182
+ "avro.slt" => {
183
+ info ! ( "Registering avro tables" ) ;
184
+ setup:: register_avro_tables ( & mut test_ctx) . await ;
178
185
}
179
186
_ => {
180
187
info ! ( "Using default SessionContext" ) ;
181
188
}
182
189
} ;
183
- ctx
190
+ test_ctx
191
+ }
192
+
193
+ /// Context for running tests
194
+ pub struct TestContext {
195
+ /// Context for running queries
196
+ ctx : SessionContext ,
197
+ /// Temporary directory created and cleared at the end of the test
198
+ test_dir : Option < TempDir > ,
199
+ }
200
+
201
+ impl TestContext {
202
+ fn new ( ctx : SessionContext ) -> Self {
203
+ Self {
204
+ ctx,
205
+ test_dir : None ,
206
+ }
207
+ }
208
+
209
+ /// Enables the test directory feature. If not enabled,
210
+ /// calling `testdir_path` will result in a panic.
211
+ fn enable_testdir ( & mut self ) {
212
+ if self . test_dir . is_none ( ) {
213
+ self . test_dir = Some ( TempDir :: new ( ) . expect ( "failed to create testdir" ) ) ;
214
+ }
215
+ }
216
+
217
+ /// Returns the path to the test directory. Panics if the test
218
+ /// directory feature is not enabled via `enable_testdir`.
219
+ fn testdir_path ( & self ) -> & Path {
220
+ self . test_dir . as_ref ( ) . expect ( "testdir not enabled" ) . path ( )
221
+ }
222
+
223
+ /// Returns a reference to the internal SessionContext
224
+ fn session_ctx ( & self ) -> & SessionContext {
225
+ & self . ctx
226
+ }
184
227
}
185
228
186
229
/// Parsed command line options
0 commit comments