Skip to content

Commit

Permalink
refactor: replace tempdir with tempfile (GreptimeTeam#1123)
Browse files Browse the repository at this point in the history
* refactor: replace tempdir with tempfile

* refactor(query): move tempfile dependency under the workspace's Cargo.toml

* refactor(tempfile): create common-test-util

* refactor(tempfile): fix toml format

* refactor(tempfile): remove tempfile out of dependencies

* refactor(tempfile): fix incorrect toml
  • Loading branch information
etolbakov authored Mar 8, 2023
1 parent 9509059 commit b31a6cb
Show file tree
Hide file tree
Showing 56 changed files with 198 additions and 154 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ members = [
"src/common/runtime",
"src/common/substrait",
"src/common/telemetry",
"src/common/test-util",
"src/common/time",
"src/datanode",
"src/datatypes",
Expand Down
2 changes: 1 addition & 1 deletion src/catalog/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ table = { path = "../table" }
tokio.workspace = true

[dev-dependencies]
common-test-util = { path = "../common/test-util" }
chrono.workspace = true
log-store = { path = "../log-store" }
mito = { path = "../mito", features = ["test"] }
object-store = { path = "../object-store" }
storage = { path = "../storage" }
tempdir = "0.3"
tokio.workspace = true
4 changes: 2 additions & 2 deletions src/catalog/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ pub struct TableEntryValue {
#[cfg(test)]
mod tests {
use common_recordbatch::RecordBatches;
use common_test_util::temp_dir::{create_temp_dir, TempDir};
use datatypes::value::Value;
use log_store::NoopLogStore;
use mito::config::EngineConfig;
Expand All @@ -405,7 +406,6 @@ mod tests {
use storage::EngineImpl;
use table::metadata::TableType;
use table::metadata::TableType::Base;
use tempdir::TempDir;

use super::*;

Expand Down Expand Up @@ -480,7 +480,7 @@ mod tests {
}

pub async fn prepare_table_engine() -> (TempDir, TableEngineRef) {
let dir = TempDir::new("system-table-test").unwrap();
let dir = create_temp_dir("system-table-test");
let store_dir = dir.path().to_string_lossy();
let accessor = object_store::services::Fs::default()
.root(&store_dir)
Expand Down
3 changes: 1 addition & 2 deletions src/cmd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@ toml = "0.5"


[dev-dependencies]
common-test-util = { path = "../common/test-util" }
rexpect = "0.5"
serde.workspace = true
tempdir = "0.3"
tempfile.workspace = true

[build-dependencies]
build-data = "0.1.3"
4 changes: 2 additions & 2 deletions src/cmd/src/datanode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,15 @@ mod tests {
use std::io::Write;
use std::time::Duration;

use common_test_util::temp_dir::create_named_temp_file;
use datanode::datanode::{CompactionConfig, ObjectStoreConfig};
use servers::Mode;
use tempfile::NamedTempFile;

use super::*;

#[test]
fn test_read_from_config_file() {
let mut file = NamedTempFile::new().unwrap();
let mut file = create_named_temp_file();
let toml_str = r#"
mode = "distributed"
enable_memory_catalog = false
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/src/frontend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ mod tests {
use std::io::Write;
use std::time::Duration;

use common_test_util::temp_dir::create_named_temp_file;
use servers::auth::{Identity, Password, UserProviderRef};
use tempfile::NamedTempFile;

use super::*;

Expand Down Expand Up @@ -241,7 +241,7 @@ mod tests {

#[test]
fn test_read_from_config_file() {
let mut file = NamedTempFile::new().unwrap();
let mut file = create_named_temp_file();
let toml_str = r#"
mode = "distributed"
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/src/metasrv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ impl TryFrom<StartCommand> for MetaSrvOptions {
mod tests {
use std::io::Write;

use common_test_util::temp_dir::create_named_temp_file;
use meta_srv::selector::SelectorType;
use tempfile::NamedTempFile;

use super::*;

Expand All @@ -139,7 +139,7 @@ mod tests {

#[test]
fn test_read_from_config_file() {
let mut file = NamedTempFile::new().unwrap();
let mut file = create_named_temp_file();
let toml_str = r#"
bind_addr = "127.0.0.1:3002"
server_addr = "127.0.0.1:3002"
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/src/toml_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ mod tests {
use std::fs::File;
use std::io::Write;

use common_test_util::temp_dir::create_temp_dir;
use serde::{Deserialize, Serialize};
use snafu::ResultExt;
use tempdir::TempDir;

use super::*;
use crate::error::Result;
Expand Down Expand Up @@ -62,7 +62,7 @@ mod tests {
host: "greptime.test".to_string(),
};

let dir = TempDir::new("test_from_file").unwrap();
let dir = create_temp_dir("test_from_file");
let test_file = format!("{}/test.toml", dir.path().to_str().unwrap());

let s = toml::to_string(&config).unwrap();
Expand Down
6 changes: 3 additions & 3 deletions src/cmd/tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ mod tests {
use std::process::{Command, Stdio};
use std::time::Duration;

use common_test_util::temp_dir::create_temp_dir;
use rexpect::session::PtyReplSession;
use tempdir::TempDir;

struct Repl {
repl: PtyReplSession,
Expand Down Expand Up @@ -48,8 +48,8 @@ mod tests {

#[test]
fn test_repl() {
let data_dir = TempDir::new_in("/tmp", "data").unwrap();
let wal_dir = TempDir::new_in("/tmp", "wal").unwrap();
let data_dir = create_temp_dir("data");
let wal_dir = create_temp_dir("wal");

let mut bin_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
bin_path.push("../../target/debug");
Expand Down
1 change: 0 additions & 1 deletion src/common/catalog/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,4 @@ snafu = { version = "0.7", features = ["backtraces"] }

[dev-dependencies]
chrono.workspace = true
tempdir = "0.3"
tokio.workspace = true
2 changes: 1 addition & 1 deletion src/common/procedure/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ tokio.workspace = true
uuid.workspace = true

[dev-dependencies]
common-test-util = { path = "../test-util" }
futures-util.workspace = true
tempdir = "0.3"
12 changes: 6 additions & 6 deletions src/common/procedure/src/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,9 +409,9 @@ impl ProcedureManager for LocalManager {
/// Create a new [ProcedureMeta] for test purpose.
#[cfg(test)]
mod test_util {
use common_test_util::temp_dir::TempDir;
use object_store::services::Fs as Builder;
use object_store::ObjectStoreBuilder;
use tempdir::TempDir;

use super::*;

Expand All @@ -430,7 +430,7 @@ mod test_util {
mod tests {
use common_error::mock::MockError;
use common_error::prelude::StatusCode;
use tempdir::TempDir;
use common_test_util::temp_dir::create_temp_dir;

use super::*;
use crate::error::Error;
Expand Down Expand Up @@ -540,7 +540,7 @@ mod tests {

#[test]
fn test_register_loader() {
let dir = TempDir::new("register").unwrap();
let dir = create_temp_dir("register");
let config = ManagerConfig {
object_store: test_util::new_object_store(&dir),
};
Expand All @@ -558,7 +558,7 @@ mod tests {

#[tokio::test]
async fn test_recover() {
let dir = TempDir::new("recover").unwrap();
let dir = create_temp_dir("recover");
let object_store = test_util::new_object_store(&dir);
let config = ManagerConfig {
object_store: object_store.clone(),
Expand Down Expand Up @@ -603,7 +603,7 @@ mod tests {

#[tokio::test]
async fn test_submit_procedure() {
let dir = TempDir::new("submit").unwrap();
let dir = create_temp_dir("submit");
let config = ManagerConfig {
object_store: test_util::new_object_store(&dir),
};
Expand Down Expand Up @@ -649,7 +649,7 @@ mod tests {

#[tokio::test]
async fn test_state_changed_on_err() {
let dir = TempDir::new("on_err").unwrap();
let dir = create_temp_dir("on_err");
let config = ManagerConfig {
object_store: test_util::new_object_store(&dir),
};
Expand Down
14 changes: 7 additions & 7 deletions src/common/procedure/src/local/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,10 +395,10 @@ mod tests {
use common_error::ext::PlainError;
use common_error::mock::MockError;
use common_error::prelude::StatusCode;
use common_test_util::temp_dir::create_temp_dir;
use futures_util::future::BoxFuture;
use futures_util::{FutureExt, TryStreamExt};
use object_store::ObjectStore;
use tempdir::TempDir;

use super::*;
use crate::local::test_util;
Expand Down Expand Up @@ -511,7 +511,7 @@ mod tests {
exec_fn,
};

let dir = TempDir::new("normal").unwrap();
let dir = create_temp_dir("normal");
let meta = normal.new_meta(ROOT_ID);
let ctx = context_without_provider(meta.id);
let object_store = test_util::new_object_store(&dir);
Expand Down Expand Up @@ -559,7 +559,7 @@ mod tests {
exec_fn,
};

let dir = TempDir::new("suspend").unwrap();
let dir = create_temp_dir("suspend");
let meta = suspend.new_meta(ROOT_ID);
let ctx = context_without_provider(meta.id);
let object_store = test_util::new_object_store(&dir);
Expand Down Expand Up @@ -658,7 +658,7 @@ mod tests {
exec_fn,
};

let dir = TempDir::new("parent").unwrap();
let dir = create_temp_dir("parent");
let meta = parent.new_meta(ROOT_ID);
let procedure_id = meta.id;

Expand Down Expand Up @@ -700,7 +700,7 @@ mod tests {
exec_fn,
};

let dir = TempDir::new("fail").unwrap();
let dir = create_temp_dir("fail");
let meta = fail.new_meta(ROOT_ID);
let ctx = context_without_provider(meta.id);
let object_store = test_util::new_object_store(&dir);
Expand Down Expand Up @@ -735,7 +735,7 @@ mod tests {
exec_fn,
};

let dir = TempDir::new("retry_later").unwrap();
let dir = create_temp_dir("retry_later");
let meta = retry_later.new_meta(ROOT_ID);
let ctx = context_without_provider(meta.id);
let object_store = test_util::new_object_store(&dir);
Expand Down Expand Up @@ -806,7 +806,7 @@ mod tests {
exec_fn,
};

let dir = TempDir::new("child_err").unwrap();
let dir = create_temp_dir("child_err");
let meta = parent.new_meta(ROOT_ID);

let object_store = test_util::new_object_store(&dir);
Expand Down
10 changes: 5 additions & 5 deletions src/common/procedure/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,9 @@ impl ParsedKey {
#[cfg(test)]
mod tests {
use async_trait::async_trait;
use common_test_util::temp_dir::{create_temp_dir, TempDir};
use object_store::services::Fs as Builder;
use object_store::ObjectStoreBuilder;
use tempdir::TempDir;

use super::*;
use crate::{Context, LockKey, Procedure, Status};
Expand Down Expand Up @@ -373,7 +373,7 @@ mod tests {

#[tokio::test]
async fn test_store_procedure() {
let dir = TempDir::new("store_procedure").unwrap();
let dir = create_temp_dir("store_procedure");
let store = procedure_store_for_test(&dir);

let procedure_id = ProcedureId::random();
Expand All @@ -398,7 +398,7 @@ mod tests {

#[tokio::test]
async fn test_commit_procedure() {
let dir = TempDir::new("commit_procedure").unwrap();
let dir = create_temp_dir("commit_procedure");
let store = procedure_store_for_test(&dir);

let procedure_id = ProcedureId::random();
Expand All @@ -416,7 +416,7 @@ mod tests {

#[tokio::test]
async fn test_rollback_procedure() {
let dir = TempDir::new("rollback_procedure").unwrap();
let dir = create_temp_dir("rollback_procedure");
let store = procedure_store_for_test(&dir);

let procedure_id = ProcedureId::random();
Expand All @@ -434,7 +434,7 @@ mod tests {

#[tokio::test]
async fn test_load_messages() {
let dir = TempDir::new("load_messages").unwrap();
let dir = create_temp_dir("load_messages");
let store = procedure_store_for_test(&dir);

// store 3 steps
Expand Down
4 changes: 2 additions & 2 deletions src/common/procedure/src/store/state_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,15 @@ impl StateStore for ObjectStateStore {

#[cfg(test)]
mod tests {
use common_test_util::temp_dir::create_temp_dir;
use object_store::services::Fs as Builder;
use object_store::ObjectStoreBuilder;
use tempdir::TempDir;

use super::*;

#[tokio::test]
async fn test_object_state_store() {
let dir = TempDir::new("state_store").unwrap();
let dir = create_temp_dir("state_store");
let store_dir = dir.path().to_str().unwrap();
let accessor = Builder::default().root(store_dir).build().unwrap();
let object_store = ObjectStore::new(accessor).finish();
Expand Down
8 changes: 8 additions & 0 deletions src/common/test-util/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "common-test-util"
version.workspace = true
edition.workspace = true
license.workspace = true

[dependencies]
tempfile.workspace = true
15 changes: 15 additions & 0 deletions src/common/test-util/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2023 Greptime Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

pub mod temp_dir;
Loading

0 comments on commit b31a6cb

Please sign in to comment.