datafusion_comet_spark_expr/test_common/file_util.rs
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
18use std::{env, fs, io::Write, path::PathBuf};
19
20/// Returns file handle for a temp file in 'target' directory with a provided content
21pub fn get_temp_file(file_name: &str, content: &[u8]) -> fs::File {
22 // build tmp path to a file in "target/debug/testdata"
23 let mut path_buf = env::current_dir().unwrap();
24 path_buf.push("target");
25 path_buf.push("debug");
26 path_buf.push("testdata");
27 fs::create_dir_all(&path_buf).unwrap();
28 path_buf.push(file_name);
29
30 // write file content
31 let mut tmp_file = fs::File::create(path_buf.as_path()).unwrap();
32 tmp_file.write_all(content).unwrap();
33 tmp_file.sync_all().unwrap();
34
35 // return file handle for both read and write
36 let file = fs::OpenOptions::new()
37 .read(true)
38 .write(true)
39 .open(path_buf.as_path());
40 assert!(file.is_ok());
41 file.unwrap()
42}
43
44pub fn get_temp_filename() -> PathBuf {
45 let mut path_buf = env::current_dir().unwrap();
46 path_buf.push("target");
47 path_buf.push("debug");
48 path_buf.push("testdata");
49 fs::create_dir_all(&path_buf).unwrap();
50 path_buf.push(rand::random::<i16>().to_string());
51
52 path_buf
53}