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 30, 2024
1 parent 32dfa96 commit c6682f0
Show file tree
Hide file tree
Showing 54 changed files with 6,557 additions and 3 deletions.
5 changes: 4 additions & 1 deletion pilota-build/src/codegen/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ where
None
}
})
.sorted()
.join(",\n");

let mut cargo_toml = toml::from_str::<toml::Value>(&unsafe {
Expand Down Expand Up @@ -184,6 +185,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 +249,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
99 changes: 97 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,35 @@ fn test_thrift(source: impl AsRef<Path>, target: impl AsRef<Path>) {
});
}

fn test_thrift_workspace(input_dir: impl AsRef<Path>, output_dir: impl AsRef<Path>, service_names: Vec<&str>) {
let services: Vec<IdlService> = service_names.iter()
.map(|name| IdlService::from_path(input_dir.as_ref().join(format!("{}.thrift", name))))
.collect();
test_with_builder_workspace(input_dir, output_dir, |source, target| {
crate::Builder::thrift()
.ignore_unused(false)
.compile_with_config(
services,
crate::Output::Workspace(target.into()),
)
});
}

fn test_thrift_workspace_with_split(input_dir: impl AsRef<Path>, output_dir: impl AsRef<Path>, service_names: Vec<&str>) {
let services: Vec<IdlService> = service_names.iter()
.map(|name| IdlService::from_path(input_dir.as_ref().join(format!("{}.thrift", name))))
.collect();
test_with_builder_workspace(input_dir, output_dir, |source, target| {
crate::Builder::thrift()
.ignore_unused(false)
.split_generated_files(true)
.compile_with_config(
services,
crate::Output::Workspace(target.into()),
)
});
}

fn test_thrift_with_split(
source: impl AsRef<Path>,
target: impl AsRef<Path>,
Expand Down Expand Up @@ -186,6 +257,30 @@ 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");

let input_dir = test_data_dir.join("input");
let output_dir = test_data_dir.join("output");

test_thrift_workspace(input_dir, output_dir, vec!["article", "author", "image"]);
}

#[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");

let input_dir = test_data_dir.join("input");
let output_dir = test_data_dir.join("output");

test_thrift_workspace_with_split(input_dir, output_dir, vec!["article", "author", "image"]);
}

#[test]
fn test_thrift_gen_with_split() {
let test_data_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
Expand Down
32 changes: 32 additions & 0 deletions pilota-build/test_data/thrift_workspace/input/article.thrift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
include "image.thrift"
include "author.thrift"
include "common.thrift"

namespace rs article

enum Status {
NORMAL = 0,
DELETED = 1,
}

struct Article {
1: required i64 id,
2: required string title,
3: required string content,
4: required author.Author author,
5: required Status status,
6: required list<image.Image> images,
7: required common.CommonData common_data,
}

struct GetArticleRequest {
1: required i64 id,
}

struct GetArticleResponse {
1: required Article article,
}

service ArticleService {
GetArticleResponse GetArticle(1: GetArticleRequest req),
}
24 changes: 24 additions & 0 deletions pilota-build/test_data/thrift_workspace/input/author.thrift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
include "image.thrift"
include "common.thrift"

namespace rs author

struct Author {
1: required i64 id,
2: required string username,
3: required string email,
4: required image.Image avatar,
5: required common.CommonData common_data,
}

struct GetAuthorRequest {
1: required i64 id,
}

struct GetAuthorResponse {
1: required Author author,
}

service AuthorService {
GetAuthorResponse GetAuthor(1: GetAuthorRequest req),
}
9 changes: 9 additions & 0 deletions pilota-build/test_data/thrift_workspace/input/cdn.thrift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
include "common.thrift"

namespace rs article.image.cdn

struct CDN {
1: required i64 id,
2: required string url,
3: required common.CommonData common_data,
}
7 changes: 7 additions & 0 deletions pilota-build/test_data/thrift_workspace/input/common.thrift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace rs common

struct CommonData {
1: required i64 id,
2: required string name,
3: required string description,
}
23 changes: 23 additions & 0 deletions pilota-build/test_data/thrift_workspace/input/image.thrift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
include "common.thrift"
include "cdn.thrift"

namespace rs article.image

struct Image {
1: required i64 id,
2: required string url,
3: required cdn.CDN cdn,
4: required common.CommonData common_data,
}

struct GetImageRequest {
1: required i64 id,
}

struct GetImageResponse {
1: required Image image,
}

service ImageService {
GetImageResponse GetImage(1: GetImageRequest req),
}
12 changes: 12 additions & 0 deletions pilota-build/test_data/thrift_workspace/output/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[workspace]
members = [
"article",
"author", "common",
"image",
]

[workspace.dependencies]
anyhow = "1"
pilota = "*"
volo = "*"
volo-thrift = "*"
19 changes: 19 additions & 0 deletions pilota-build/test_data/thrift_workspace/output/article/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[dependencies.anyhow]
workspace = true

[dependencies.common]
path = "../common"

[dependencies.pilota]
workspace = true

[dependencies.volo]
workspace = true

[dependencies.volo-thrift]
workspace = true

[package]
edition = "2021"
name = "article"
version = "0.1.0"
Loading

0 comments on commit c6682f0

Please sign in to comment.