Skip to content

Commit

Permalink
Add workspace tests
Browse files Browse the repository at this point in the history
  • Loading branch information
missingdays committed Sep 25, 2024
1 parent 32dfa96 commit d0a79d4
Show file tree
Hide file tree
Showing 38 changed files with 1,494 additions and 3 deletions.
4 changes: 3 additions & 1 deletion pilota-build/src/codegen/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ where
Command::new("cargo")
.arg("init")
.arg("--lib")
.arg("--vcs")
.arg("none")
.current_dir(base_dir.as_ref())
.arg(&*info.name),
)?;
Expand Down Expand Up @@ -246,7 +248,7 @@ where
def_id,
kind: super::CodegenKind::RePub,
})),
base_dir.as_ref(),
base_dir.as_ref().join(&*info.name).join("src").as_path(),
);
if let Some(main_mod_path) = info.main_mod_path {
gen_rs_stream.push_str(&format!(
Expand Down
119 changes: 117 additions & 2 deletions pilota-build/src/test/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![cfg(test)]

use std::{fs, path::Path};

use std::fs::File;
use tempfile::tempdir;

use crate::{plugin::SerdePlugin, IdlService};
Expand Down Expand Up @@ -45,7 +45,14 @@ fn diff_dir(old: impl AsRef<Path>, new: impl AsRef<Path>) {
if !corresponding_new_file.exists() {
panic!("File {:?} does not exist in the new directory", file_name);
}
diff_file(old_file, corresponding_new_file);

if old_file.is_file() && corresponding_new_file.is_file() {
diff_file(old_file, corresponding_new_file);
} else if !old_file.is_file() && !corresponding_new_file.is_file() {
diff_dir(old_file, corresponding_new_file)
} else {
panic!("{} and {} are not both files or directories", old_file.to_str().unwrap(), corresponding_new_file.to_str().unwrap());
}
}
}

Expand Down Expand Up @@ -85,6 +92,41 @@ fn test_with_builder<F: FnOnce(&Path, &Path)>(
}
}

fn test_with_builder_workspace<F: FnOnce(&Path, &Path)>(
source: impl AsRef<Path>,
target: impl AsRef<Path>,
f: F,
) {
if std::env::var("UPDATE_TEST_DATA").as_deref() == Ok("1") {
fs::remove_dir(&target);
fs::create_dir_all(&target).unwrap();
let cargo_toml_path = target.as_ref().join("Cargo.toml");
File::create(cargo_toml_path).unwrap();

f(source.as_ref(), target.as_ref());
} else {
let dir = tempdir().unwrap();
let path = dir.path().join(
target
.as_ref()
.file_name()
.and_then(|s| s.to_str())
.unwrap(),
);
let mut base_dir_tmp = path.clone();
base_dir_tmp.pop();
base_dir_tmp.push(path.file_stem().unwrap());
println!("{path:?}");

fs::create_dir_all(&path).unwrap();
let cargo_toml_path = path.join("Cargo.toml");
File::create(cargo_toml_path).unwrap();

f(source.as_ref(), &path);
diff_dir(target, base_dir_tmp);
}
}

fn test_with_split_builder<F: FnOnce(&Path, &Path)>(
source: impl AsRef<Path>,
target: impl AsRef<Path>,
Expand Down Expand Up @@ -125,6 +167,29 @@ fn test_thrift(source: impl AsRef<Path>, target: impl AsRef<Path>) {
});
}

fn test_thrift_workspace(source: impl AsRef<Path>, target: impl AsRef<Path>) {
test_with_builder_workspace(source, target, |source, target| {
crate::Builder::thrift()
.ignore_unused(false)
.compile_with_config(
vec![IdlService::from_path(source.to_owned())],
crate::Output::Workspace(target.into()),
)
});
}

fn test_thrift_workspace_with_split(source: impl AsRef<Path>, target: impl AsRef<Path>) {
test_with_builder_workspace(source, target, |source, target| {
crate::Builder::thrift()
.ignore_unused(false)
.split_generated_files(true)
.compile_with_config(
vec![IdlService::from_path(source.to_owned())],
crate::Output::Workspace(target.into()),
)
});
}

fn test_thrift_with_split(
source: impl AsRef<Path>,
target: impl AsRef<Path>,
Expand Down Expand Up @@ -186,6 +251,56 @@ fn test_thrift_gen() {
});
}

#[test]
fn test_thrift_workspace_gen() {
let test_data_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("test_data")
.join("thrift_workspace");

test_data_dir.read_dir().unwrap().for_each(|f| {
let f = f.unwrap();

let path = f.path();

if let Some(ext) = path.extension() {
if ext == "thrift" {
let mut dir_path = path.clone();
let buf = dir_path.clone();
let dir_name = buf.file_stem().unwrap();
dir_path.pop();
dir_path.push(dir_name);

test_thrift_workspace(path, dir_path);
}
}
});
}

#[test]
fn test_thrift_workspace_with_split_gen() {
let test_data_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("test_data")
.join("thrift_workspace_with_split");

test_data_dir.read_dir().unwrap().for_each(|f| {
let f = f.unwrap();

let path = f.path();

if let Some(ext) = path.extension() {
if ext == "thrift" {
let mut dir_path = path.clone();
let buf = dir_path.clone();
let dir_name = buf.file_stem().unwrap();
dir_path.pop();
dir_path.push(dir_name);

test_thrift_workspace_with_split(path, dir_path);
}
}
});
}

#[test]
fn test_thrift_gen_with_split() {
let test_data_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
Expand Down
Loading

0 comments on commit d0a79d4

Please sign in to comment.