Skip to content

Commit cdfb3a0

Browse files
authored
move test_util to ballista-examples package (#661)
1 parent a9fd64d commit cdfb3a0

File tree

3 files changed

+111
-1
lines changed

3 files changed

+111
-1
lines changed

examples/examples/standalone-sql.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// under the License.
1717

1818
use ballista::prelude::{BallistaConfig, BallistaContext, Result};
19+
use ballista_examples::test_util;
1920
use datafusion::prelude::CsvReadOptions;
2021

2122
#[tokio::main]
@@ -26,10 +27,12 @@ async fn main() -> Result<()> {
2627

2728
let ctx = BallistaContext::standalone(&config, 2).await?;
2829

30+
let testdata = test_util::examples_test_data();
31+
2932
// register csv file with the execution context
3033
ctx.register_csv(
3134
"test",
32-
"testdata/aggregate_test_100.csv",
35+
&format!("{testdata}/aggregate_test_100.csv"),
3336
CsvReadOptions::new(),
3437
)
3538
.await?;

examples/src/lib.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
pub mod test_util;

examples/src/test_util.rs

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
use std::env;
19+
use std::error::Error;
20+
use std::path::PathBuf;
21+
22+
/// Returns the parquet test data directory, which is by default
23+
/// stored in a git submodule rooted at
24+
/// `examples/testdata`.
25+
///
26+
/// The default can be overridden by the optional environment variable
27+
/// `EXAMPLES_TEST_DATA`
28+
///
29+
/// panics when the directory can not be found.
30+
///
31+
/// Example:
32+
/// ```
33+
/// use ballista_examples::test_util;
34+
/// let testdata = test_util::examples_test_data();
35+
/// let filename = format!("{testdata}/aggregate_test_100.csv");
36+
/// assert!(std::path::PathBuf::from(filename).exists());
37+
/// ```
38+
pub fn examples_test_data() -> String {
39+
match get_data_dir("EXAMPLES_TEST_DATA", "testdata") {
40+
Ok(pb) => pb.display().to_string(),
41+
Err(err) => panic!("failed to get examples test data dir: {err}"),
42+
}
43+
}
44+
45+
/// Returns a directory path for finding test data.
46+
///
47+
/// udf_env: name of an environment variable
48+
///
49+
/// submodule_dir: fallback path (relative to CARGO_MANIFEST_DIR)
50+
///
51+
/// Returns either:
52+
/// The path referred to in `udf_env` if that variable is set and refers to a directory
53+
/// The submodule_data directory relative to CARGO_MANIFEST_PATH
54+
fn get_data_dir(udf_env: &str, submodule_data: &str) -> Result<PathBuf, Box<dyn Error>> {
55+
// Try user defined env.
56+
if let Ok(dir) = env::var(udf_env) {
57+
let trimmed = dir.trim().to_string();
58+
if !trimmed.is_empty() {
59+
let pb = PathBuf::from(trimmed);
60+
if pb.is_dir() {
61+
return Ok(pb);
62+
} else {
63+
return Err(format!(
64+
"the data dir `{}` defined by env {udf_env} not found",
65+
pb.display()
66+
)
67+
.into());
68+
}
69+
}
70+
}
71+
72+
// The env is undefined or its value is trimmed to empty, let's try default dir.
73+
74+
// env "CARGO_MANIFEST_DIR" is "the directory containing the manifest of your package",
75+
// set by `cargo run` or `cargo test`, see:
76+
// https://doc.rust-lang.org/cargo/reference/environment-variables.html
77+
let dir = env!("CARGO_MANIFEST_DIR");
78+
79+
let pb = PathBuf::from(dir).join(submodule_data);
80+
if pb.is_dir() {
81+
Ok(pb)
82+
} else {
83+
Err(format!(
84+
"env `{udf_env}` is undefined or has empty value, and the pre-defined data dir `{}` not found\n\
85+
HINT: try running `git submodule update --init`",
86+
pb.display(),
87+
).into())
88+
}
89+
}

0 commit comments

Comments
 (0)