|
| 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