File: wkt.rs

package info (click to toggle)
rust-protox 0.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 468 kB
  • sloc: makefile: 2
file content (62 lines) | stat: -rw-r--r-- 2,605 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use prost_reflect::ReflectMessage;
use prost_types::FileDescriptorSet;
use protox::{file::GoogleFileResolver, Compiler};

#[test]
fn prost_reflect_wkt_matches_compiled_wkt() {
    use prost::Message;

    let desc = FileDescriptorSet::decode(expected_well_known_types().as_slice()).unwrap();
    let prost_wkt_desc = FileDescriptorSet {
        file: ().descriptor().parent_pool().file_descriptor_protos().cloned().collect(),
    };

    if desc != prost_wkt_desc {
        let actual = format!("{prost_wkt_desc:#?}");
        let expected = format!("{desc:#?}");
        let diff = similar_asserts::SimpleDiff::from_str(&actual, &expected, "actual", "expected");

        // If this fails with a non-trivial diff it's reasonable to just dump `desc` via
        // the debug representation and afterwards adjust the output to be valid rust code
        // The following steps were done for the intial version:
        //
        // * replace `[` with `vec![`
        // * Call `.into()` on strings and enum variants
        // * Wrap all options field into `Options::from_prost`
        //
        // The first two steps can be easily done with a bunch of search and
        // replace queries for almost all instances. There are a few cases
        // that need to be manually adjusted afterwards
        //
        // The last step requires manually going through these fields, but
        // that's only ~10 instances.
        panic!(
            "The well known file descriptor returned by `make_description()` \
             does not match the expected file descriptor parsed from `src/well_known_types_bytes.bin`: \
             {diff}"
        );
    }
}

fn expected_well_known_types() -> Vec<u8> {
    // protox can output a FileDescriptorSet directly, but by going through bytes, this should still work
    // when upgrading to a newer prost-types version.
    Compiler::with_file_resolver(GoogleFileResolver::new())
        .include_source_info(false)
        .open_files([
            "google/protobuf/any.proto",
            "google/protobuf/api.proto",
            "google/protobuf/descriptor.proto",
            "google/protobuf/duration.proto",
            "google/protobuf/empty.proto",
            "google/protobuf/field_mask.proto",
            "google/protobuf/source_context.proto",
            "google/protobuf/struct.proto",
            "google/protobuf/timestamp.proto",
            "google/protobuf/type.proto",
            "google/protobuf/wrappers.proto",
            "google/protobuf/compiler/plugin.proto",
        ])
        .unwrap()
        .encode_file_descriptor_set()
}