Skip to content

Commit

Permalink
fix tokio-rs#1001 and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MixusMinimax committed Mar 13, 2024
1 parent 7c04d97 commit 4bd5337
Show file tree
Hide file tree
Showing 12 changed files with 198 additions and 1 deletion.
25 changes: 24 additions & 1 deletion prost-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1174,6 +1174,25 @@ impl Config {
let mut written = 0;
entries.sort();

if depth == 0 {
let (empty, remaining): (Vec<&Module>, Vec<&Module>) =
entries.into_iter().partition(|m| m.is_empty());
for module in empty {
let modname = module.to_partial_file_name(..=depth);
if basepath.is_some() {
self.write_line(outfile, depth, &format!("include!(\"{}.rs\");", modname))?;
} else {
self.write_line(
outfile,
depth,
&format!("include!(concat!(env!(\"OUT_DIR\"), \"/{}.rs\"));", modname),
)?;
}
written += 1;
}
entries = remaining;
}

while !entries.is_empty() {
let modident = entries[0].part(depth);
let matching: Vec<&Module> = entries
Expand Down Expand Up @@ -1414,7 +1433,11 @@ impl Module {
}

fn to_partial_file_name(&self, range: RangeToInclusive<usize>) -> String {
self.components[range].join(".")
if self.components.is_empty() {
"_".to_string()
} else {
self.components[range].join(".")
}
}

fn part(&self, idx: usize) -> &str {
Expand Down
29 changes: 29 additions & 0 deletions tests/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,35 @@ fn main() {
.compile_protos(&[src.join("well_known_types.proto")], includes)
.unwrap();

{
let out = std::env::var("OUT_DIR").unwrap();
let out_path = PathBuf::from(out).join("no_package");

std::fs::create_dir_all(&out_path).unwrap();

prost_build::Config::new()
.out_dir(out_path)
.include_file("_includes.rs")
.compile_protos(&[src.join("no_package_with_message.proto")], includes)
.unwrap();
}

{
let out = std::env::var("OUT_DIR").unwrap();
let out_path = PathBuf::from(out).join("complex_package_structure");

std::fs::create_dir_all(&out_path).unwrap();

prost_build::Config::new()
.out_dir(out_path)
.include_file("__.rs")
.compile_protos(
&[src.join("complex_package_structure/proto/post/post.proto")],
&[src.join("complex_package_structure/proto")],
)
.unwrap();
}

config
.compile_protos(
&[src.join("packages/widget_factory.proto")],
Expand Down
61 changes: 61 additions & 0 deletions tests/src/complex_package_structure/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use self::proto::image::Image;
use self::proto::post::content::post_content_fragment;
use self::proto::post::content::PostContentFragment;
use self::proto::post::Post;
use self::proto::user::User;
use self::proto::Timestamp;

mod proto {
include!(concat!(env!("OUT_DIR"), "/complex_package_structure/__.rs"));
}

#[test]
fn it_works() {
let user = User {
id: "69a4cd96-b956-4fb1-9a97-b222eac33b8a".to_string(),
name: "Test User".to_string(),
created_at: Some(Timestamp {
seconds: 1710366135,
nanos: 0,
}),
..User::default()
};
let posts = vec![
Post::default(),
Post {
id: "aa1e751f-e287-4c6e-aa0f-f838f96a1a60".to_string(),
author: Some(user),
content: vec![
PostContentFragment {
content: Some(post_content_fragment::Content::Text(
"Hello, world!".to_string(),
)),
},
PostContentFragment {
content: Some(post_content_fragment::Content::Image(Image {
name: "doggo.jpg".to_string(),
description: Some("A dog".to_string()),
data: vec![0, 1, 2, 3],
})),
},
PostContentFragment {
content: Some(post_content_fragment::Content::Link(
"https://example.com".to_string(),
)),
},
],
..Post::default()
},
Post::default(),
];
assert_eq!(posts.len(), 3);
assert_eq!(posts[1].content.len(), 3);
if let PostContentFragment {
content: Some(post_content_fragment::Content::Image(Image { name, .. })),
} = &posts[1].content[1]
{
assert_eq!(name, "doggo.jpg");
} else {
assert!(false, "Expected an image")
}
}
3 changes: 3 additions & 0 deletions tests/src/complex_package_structure/proto/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[*]
ij_protobuf_keep_blank_lines_in_code = 0
insert_final_newline = true
13 changes: 13 additions & 0 deletions tests/src/complex_package_structure/proto/post/content.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
syntax = "proto3";

package post.content;

import "wrong/image.proto";

message PostContentFragment {
oneof content {
string text = 1;
image.Image image = 2;
string link = 3;
}
}
17 changes: 17 additions & 0 deletions tests/src/complex_package_structure/proto/post/post.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
syntax = "proto3";

package post;

import "std/time.proto";
import "user/user.proto";
import "post/content.proto";

message Post {
string id = 1;
string title = 2;
reserved 3;
user.User author = 4;
Timestamp created_at = 5;
Timestamp updated_at = 6;
repeated content.PostContentFragment content = 7;
}
6 changes: 6 additions & 0 deletions tests/src/complex_package_structure/proto/std/time.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
syntax = "proto3";

message Timestamp {
int64 seconds = 1;
int32 nanos = 2;
}
13 changes: 13 additions & 0 deletions tests/src/complex_package_structure/proto/user/user.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
syntax = "proto3";

package user;

import "std/time.proto";

message User {
string id = 1;
Timestamp created_at = 2;
Timestamp updated_at = 3;
string name = 4;
string email = 5;
}
12 changes: 12 additions & 0 deletions tests/src/complex_package_structure/proto/wrong/image.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// This file makes sure that the file path of the proto file has nothing to do with the package name,
// therefore testing the validity of write_includes.

syntax = "proto3";

package image;

message Image {
string name = 1;
optional string description = 2;
bytes data = 3;
}
4 changes: 4 additions & 0 deletions tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ pub mod unittest;
#[cfg(test)]
mod bootstrap;
#[cfg(test)]
mod complex_package_structure;
#[cfg(test)]
mod debug;
#[cfg(test)]
mod deprecated_field;
Expand All @@ -41,6 +43,8 @@ mod generic_derive;
#[cfg(test)]
mod message_encoding;
#[cfg(test)]
mod no_package_with_message;
#[cfg(test)]
mod no_unused_results;
#[cfg(test)]
#[cfg(feature = "std")]
Expand Down
5 changes: 5 additions & 0 deletions tests/src/no_package_with_message.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
syntax = "proto3";

message NoPackageWithMessageExampleMsg {
string some_field = 1;
}
11 changes: 11 additions & 0 deletions tests/src/no_package_with_message.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
mod proto {
include!(concat!(env!("OUT_DIR"), "/no_package/_includes.rs"));
}

#[test]
fn it_works() {
assert_eq!(
proto::NoPackageWithMessageExampleMsg::default(),
proto::NoPackageWithMessageExampleMsg::default()
);
}

0 comments on commit 4bd5337

Please sign in to comment.