/
command_line_interface_unittest.cc
4105 lines (3458 loc) · 143 KB
/
command_line_interface_unittest.cc
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
// Author: kenton@google.com (Kenton Varda)
// Based on original Protocol Buffers design by
// Sanjay Ghemawat, Jeff Dean, and others.
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <cstdint>
#include <gmock/gmock.h>
#include "absl/log/absl_check.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
#include "google/protobuf/compiler/command_line_interface_tester.h"
#include "google/protobuf/unittest_features.pb.h"
#include "google/protobuf/unittest_invalid_features.pb.h"
#ifndef _MSC_VER
#include <unistd.h>
#endif
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "google/protobuf/testing/file.h"
#include "google/protobuf/any.pb.h"
#include "google/protobuf/descriptor.pb.h"
#include "google/protobuf/testing/googletest.h"
#include <gtest/gtest.h>
#include "absl/strings/str_split.h"
#include "absl/strings/string_view.h"
#include "absl/strings/substitute.h"
#include "google/protobuf/compiler/code_generator.h"
#include "google/protobuf/compiler/command_line_interface.h"
#include "google/protobuf/compiler/cpp/names.h"
#include "google/protobuf/compiler/mock_code_generator.h"
#include "google/protobuf/compiler/plugin.pb.h"
#include "google/protobuf/compiler/subprocess.h"
#include "google/protobuf/io/io_win32.h"
#include "google/protobuf/test_textproto.h"
#include "google/protobuf/test_util2.h"
#include "google/protobuf/unittest.pb.h"
#include "google/protobuf/unittest_custom_options.pb.h"
#ifdef GOOGLE_PROTOBUF_USE_BAZEL_GENERATED_PLUGIN_PATHS
// This is needed because of https://github.com/bazelbuild/bazel/issues/19124.
#include "google/protobuf/compiler/test_plugin_paths.h"
#endif // GOOGLE_PROTOBUF_USE_BAZEL_GENERATED_PLUGIN_PATHS
// Must be included last.
#include "google/protobuf/port_def.inc"
namespace google {
namespace protobuf {
namespace compiler {
#if defined(_WIN32)
// DO NOT include <io.h>, instead create functions in io_win32.{h,cc} and import
// them like we do below.
using google::protobuf::io::win32::access;
using google::protobuf::io::win32::close;
using google::protobuf::io::win32::dup;
using google::protobuf::io::win32::dup2;
using google::protobuf::io::win32::open;
using google::protobuf::io::win32::write;
#endif
// Disable the whole test when we use tcmalloc for "draconian" heap checks, in
// which case tcmalloc will print warnings that fail the plugin tests.
#if !GOOGLE_PROTOBUF_HEAP_CHECK_DRACONIAN
namespace {
std::string CreatePluginArg() {
std::string plugin_path;
#ifdef GOOGLE_PROTOBUF_TEST_PLUGIN_PATH
plugin_path = GOOGLE_PROTOBUF_TEST_PLUGIN_PATH;
#else
const char* possible_paths[] = {
// When building with shared libraries, libtool hides the real
// executable
// in .libs and puts a fake wrapper in the current directory.
// Unfortunately, due to an apparent bug on Cygwin/MinGW, if one program
// wrapped in this way (e.g. protobuf-tests.exe) tries to execute
// another
// program wrapped in this way (e.g. test_plugin.exe), the latter fails
// with error code 127 and no explanation message. Presumably the
// problem
// is that the wrapper for protobuf-tests.exe set some environment
// variables that confuse the wrapper for test_plugin.exe. Luckily, it
// turns out that if we simply invoke the wrapped test_plugin.exe
// directly, it works -- I guess the environment variables set by the
// protobuf-tests.exe wrapper happen to be correct for it too. So we do
// that.
".libs/test_plugin.exe", // Win32 w/autotool (Cygwin / MinGW)
"test_plugin.exe", // Other Win32 (MSVC)
"test_plugin", // Unix
};
for (int i = 0; i < ABSL_ARRAYSIZE(possible_paths); i++) {
if (access(possible_paths[i], F_OK) == 0) {
plugin_path = possible_paths[i];
break;
}
}
#endif
if (plugin_path.empty() || !File::Exists(plugin_path)) {
ABSL_LOG(ERROR)
<< "Plugin executable not found. Plugin tests are likely to fail. "
<< plugin_path;
return "";
}
return absl::StrCat("--plugin=prefix-gen-plug=", plugin_path);
}
class CommandLineInterfaceTest : public CommandLineInterfaceTester {
protected:
CommandLineInterfaceTest();
// Runs the CommandLineInterface with the given command line. The
// command is automatically split on spaces, and the string "$tmpdir"
// is replaced with TestTempDir().
void Run(std::string command);
void RunWithArgs(std::vector<std::string> args);
// -----------------------------------------------------------------
// Methods to set up the test (called before Run()).
class NullCodeGenerator;
// Normally plugins are allowed for all tests. Call this to explicitly
// disable them.
void DisallowPlugins() { disallow_plugins_ = true; }
// Checks that MockCodeGenerator::Generate() was called in the given
// context (or the generator in test_plugin.cc, which produces the same
// output). That is, this tests if the generator with the given name
// was called with the given parameter and proto file and produced the
// given output file. This is checked by reading the output file and
// checking that it contains the content that MockCodeGenerator would
// generate given these inputs. message_name is the name of the first
// message that appeared in the proto file; this is just to make extra
// sure that the correct file was parsed.
void ExpectGenerated(const std::string& generator_name,
const std::string& parameter,
const std::string& proto_name,
const std::string& message_name);
void ExpectGenerated(const std::string& generator_name,
const std::string& parameter,
const std::string& proto_name,
const std::string& message_name,
const std::string& output_directory);
void ExpectGeneratedWithMultipleInputs(const std::string& generator_name,
const std::string& all_proto_names,
const std::string& proto_name,
const std::string& message_name);
void ExpectGeneratedWithInsertions(const std::string& generator_name,
const std::string& parameter,
const std::string& insertions,
const std::string& proto_name,
const std::string& message_name);
void CheckGeneratedAnnotations(const std::string& name,
const std::string& file);
#if defined(_WIN32)
void ExpectNullCodeGeneratorCalled(const std::string& parameter);
#endif // _WIN32
std::string ReadFile(absl::string_view filename);
void ReadDescriptorSet(absl::string_view filename,
FileDescriptorSet* descriptor_set);
void WriteDescriptorSet(absl::string_view filename,
const FileDescriptorSet* descriptor_set);
FeatureSetDefaults ReadEditionDefaults(absl::string_view filename);
// The default code generators support all features. Use this to create a
// code generator that omits the given feature(s).
void CreateGeneratorWithMissingFeatures(const std::string& name,
const std::string& description,
uint64_t features) {
auto generator = std::make_unique<MockCodeGenerator>(name);
generator->SuppressFeatures(features);
RegisterGenerator(name, std::move(generator), description);
}
void SetMockGeneratorTestCase(absl::string_view name) {
#ifdef _WIN32
::_putenv(absl::StrCat("TEST_CASE", "=", name).c_str());
#else
::setenv("TEST_CASE", name.data(), 1);
#endif
}
MockCodeGenerator* mock_generator_ = nullptr;
private:
// Was DisallowPlugins() called?
bool disallow_plugins_ = false;
NullCodeGenerator* null_generator_ = nullptr;
};
class CommandLineInterfaceTest::NullCodeGenerator : public CodeGenerator {
public:
NullCodeGenerator() : called_(false) {}
~NullCodeGenerator() override {}
mutable bool called_;
mutable std::string parameter_;
// implements CodeGenerator ----------------------------------------
bool Generate(const FileDescriptor* file, const std::string& parameter,
GeneratorContext* context, std::string* error) const override {
called_ = true;
parameter_ = parameter;
return true;
}
};
// ===================================================================
CommandLineInterfaceTest::CommandLineInterfaceTest() {
// Reset the mock generator's test case environment variable.
SetMockGeneratorTestCase("");
// Register generators.
auto mock_generator = std::make_unique<MockCodeGenerator>("test_generator");
mock_generator_ = mock_generator.get();
RegisterGenerator("--test_out", "--test_opt", std::move(mock_generator),
"Test output.");
RegisterGenerator("-t", std::make_unique<MockCodeGenerator>("test_generator"),
"Test output.");
RegisterGenerator("--alt_out",
std::make_unique<MockCodeGenerator>("alt_generator"),
"Alt output.");
auto null_generator = std::make_unique<NullCodeGenerator>();
null_generator_ = null_generator.get();
RegisterGenerator("--null_out", std::move(null_generator), "Null output.");
}
void CommandLineInterfaceTest::Run(std::string command) {
if (!disallow_plugins_) {
AllowPlugins("prefix-");
absl::StrAppend(&command, " ", CreatePluginArg());
}
RunProtoc(command);
}
void CommandLineInterfaceTest::RunWithArgs(std::vector<std::string> args) {
if (!disallow_plugins_) {
AllowPlugins("prefix-");
args.push_back(CreatePluginArg());
}
RunProtocWithArgs(std::move(args));
}
// -------------------------------------------------------------------
void CommandLineInterfaceTest::ExpectGenerated(
const std::string& generator_name, const std::string& parameter,
const std::string& proto_name, const std::string& message_name) {
MockCodeGenerator::ExpectGenerated(generator_name, parameter, "", proto_name,
message_name, proto_name,
temp_directory());
}
void CommandLineInterfaceTest::ExpectGenerated(
const std::string& generator_name, const std::string& parameter,
const std::string& proto_name, const std::string& message_name,
const std::string& output_directory) {
MockCodeGenerator::ExpectGenerated(
generator_name, parameter, "", proto_name, message_name, proto_name,
absl::StrCat(temp_directory(), "/", output_directory));
}
void CommandLineInterfaceTest::ExpectGeneratedWithMultipleInputs(
const std::string& generator_name, const std::string& all_proto_names,
const std::string& proto_name, const std::string& message_name) {
MockCodeGenerator::ExpectGenerated(generator_name, "", "", proto_name,
message_name, all_proto_names,
temp_directory());
}
void CommandLineInterfaceTest::ExpectGeneratedWithInsertions(
const std::string& generator_name, const std::string& parameter,
const std::string& insertions, const std::string& proto_name,
const std::string& message_name) {
MockCodeGenerator::ExpectGenerated(generator_name, parameter, insertions,
proto_name, message_name, proto_name,
temp_directory());
}
void CommandLineInterfaceTest::CheckGeneratedAnnotations(
const std::string& name, const std::string& file) {
MockCodeGenerator::CheckGeneratedAnnotations(name, file, temp_directory());
}
#if defined(_WIN32)
void CommandLineInterfaceTest::ExpectNullCodeGeneratorCalled(
const std::string& parameter) {
EXPECT_TRUE(null_generator_->called_);
EXPECT_EQ(parameter, null_generator_->parameter_);
}
#endif // _WIN32
std::string CommandLineInterfaceTest::ReadFile(absl::string_view filename) {
std::string path = absl::StrCat(temp_directory(), "/", filename);
std::string file_contents;
ABSL_CHECK_OK(File::GetContents(path, &file_contents, true));
return file_contents;
}
void CommandLineInterfaceTest::ReadDescriptorSet(
absl::string_view filename, FileDescriptorSet* descriptor_set) {
std::string file_contents = ReadFile(filename);
if (!descriptor_set->ParseFromString(file_contents)) {
FAIL() << "Could not parse file contents: " << filename;
}
}
FeatureSetDefaults CommandLineInterfaceTest::ReadEditionDefaults(
absl::string_view filename) {
FeatureSetDefaults defaults;
std::string file_contents = ReadFile(filename);
ABSL_CHECK(defaults.ParseFromString(file_contents))
<< "Could not parse file contents: " << filename;
return defaults;
}
void CommandLineInterfaceTest::WriteDescriptorSet(
absl::string_view filename, const FileDescriptorSet* descriptor_set) {
std::string binary_proto;
ABSL_CHECK(descriptor_set->SerializeToString(&binary_proto));
CreateTempFile(filename, binary_proto);
}
// ===================================================================
TEST_F(CommandLineInterfaceTest, BasicOutput) {
// Test that the common case works.
CreateTempFile("foo.proto",
"syntax = \"proto2\";\n"
"message Foo {}\n");
Run("protocol_compiler --test_out=$tmpdir "
"--proto_path=$tmpdir foo.proto");
ExpectNoErrors();
ExpectGenerated("test_generator", "", "foo.proto", "Foo");
}
TEST_F(CommandLineInterfaceTest, BasicOutput_DescriptorSetIn) {
// Test that the common case works.
FileDescriptorSet file_descriptor_set;
FileDescriptorProto* file_descriptor_proto = file_descriptor_set.add_file();
file_descriptor_proto->set_name("foo.proto");
file_descriptor_proto->add_message_type()->set_name("Foo");
WriteDescriptorSet("foo.bin", &file_descriptor_set);
Run("protocol_compiler --test_out=$tmpdir "
"--descriptor_set_in=$tmpdir/foo.bin foo.proto");
ExpectNoErrors();
ExpectGenerated("test_generator", "", "foo.proto", "Foo");
}
TEST_F(CommandLineInterfaceTest, BasicPlugin) {
// Test that basic plugins work.
CreateTempFile("foo.proto",
"syntax = \"proto2\";\n"
"message Foo {}\n");
Run("protocol_compiler --plug_out=$tmpdir "
"--proto_path=$tmpdir foo.proto");
ExpectNoErrors();
ExpectGenerated("test_plugin", "", "foo.proto", "Foo");
}
TEST_F(CommandLineInterfaceTest, BasicPlugin_DescriptorSetIn) {
// Test that basic plugins work.
FileDescriptorSet file_descriptor_set;
FileDescriptorProto* file_descriptor_proto = file_descriptor_set.add_file();
file_descriptor_proto->set_name("foo.proto");
file_descriptor_proto->add_message_type()->set_name("Foo");
WriteDescriptorSet("foo.bin", &file_descriptor_set);
Run("protocol_compiler --plug_out=$tmpdir "
"--descriptor_set_in=$tmpdir/foo.bin foo.proto");
ExpectNoErrors();
ExpectGenerated("test_plugin", "", "foo.proto", "Foo");
}
TEST_F(CommandLineInterfaceTest, Plugin_OptionRetention) {
CreateTempFile("foo.proto",
R"pb(syntax = "proto2"
;
import "bar.proto";
package foo;
message Foo {
optional bar.Bar b = 1;
extensions 1000 to max [
declaration = {
number: 1000
full_name: ".foo.my_ext"
type: ".foo.MyType"
}
];
})pb");
CreateTempFile("bar.proto",
R"pb(syntax = "proto2"
;
package bar;
message Bar {
extensions 1000 to max [
declaration = {
number: 1000
full_name: ".baz.my_ext"
type: ".baz.MyType"
}
];
})pb");
#ifdef GOOGLE_PROTOBUF_FAKE_PLUGIN_PATH
std::string plugin_path = GOOGLE_PROTOBUF_FAKE_PLUGIN_PATH;
#else
std::string plugin_path = absl::StrCat(
TestUtil::TestSourceDir(), "/google/protobuf/compiler/fake_plugin");
#endif
// Invoke protoc with fake_plugin to get ahold of the CodeGeneratorRequest
// sent by protoc.
Run(absl::StrCat(
"protocol_compiler --fake_plugin_out=$tmpdir --proto_path=$tmpdir "
"foo.proto --plugin=prefix-gen-fake_plugin=",
plugin_path));
ExpectNoErrors();
std::string base64_output = ReadFile("foo.proto.request");
std::string binary_request;
ASSERT_TRUE(absl::Base64Unescape(base64_output, &binary_request));
CodeGeneratorRequest request;
ASSERT_TRUE(request.ParseFromString(binary_request));
// request.proto_file() should include source-retention options for bar.proto
// but not for foo.proto. Protoc should strip source-retention options from
// the immediate proto files being built, but not for all dependencies.
ASSERT_EQ(request.proto_file_size(), 2);
{
EXPECT_EQ(request.proto_file(0).name(), "bar.proto");
ASSERT_EQ(request.proto_file(0).message_type_size(), 1);
const DescriptorProto& m = request.proto_file(0).message_type(0);
ASSERT_EQ(m.extension_range_size(), 1);
EXPECT_EQ(m.extension_range(0).options().declaration_size(), 1);
}
{
EXPECT_EQ(request.proto_file(1).name(), "foo.proto");
ASSERT_EQ(request.proto_file(1).message_type_size(), 1);
const DescriptorProto& m = request.proto_file(1).message_type(0);
ASSERT_EQ(m.extension_range_size(), 1);
EXPECT_TRUE(m.extension_range(0).options().declaration().empty());
}
}
TEST_F(CommandLineInterfaceTest, Plugin_SourceFileDescriptors) {
CreateTempFile("foo.proto",
R"pb(syntax = "proto2"
;
import "bar.proto";
package foo;
message Foo {
optional bar.Bar b = 1;
extensions 1000 to max [
declaration = {
number: 1000
full_name: ".foo.my_ext"
type: ".foo.MyType"
}
];
})pb");
CreateTempFile("bar.proto",
R"pb(syntax = "proto2"
;
package bar;
message Bar {
extensions 1000 to max [
declaration = {
number: 1000
full_name: ".baz.my_ext"
type: ".baz.MyType"
}
];
})pb");
#ifdef GOOGLE_PROTOBUF_FAKE_PLUGIN_PATH
std::string plugin_path = GOOGLE_PROTOBUF_FAKE_PLUGIN_PATH;
#else
std::string plugin_path = absl::StrCat(
TestUtil::TestSourceDir(), "/google/protobuf/compiler/fake_plugin");
#endif
// Invoke protoc with fake_plugin to get ahold of the CodeGeneratorRequest
// sent by protoc.
Run(absl::StrCat(
"protocol_compiler --fake_plugin_out=$tmpdir --proto_path=$tmpdir "
"foo.proto --plugin=prefix-gen-fake_plugin=",
plugin_path));
ExpectNoErrors();
std::string base64_output = ReadFile("foo.proto.request");
std::string binary_request;
ASSERT_TRUE(absl::Base64Unescape(base64_output, &binary_request));
CodeGeneratorRequest request;
ASSERT_TRUE(request.ParseFromString(binary_request));
// request.source_file_descriptors() should consist of a descriptor for
// foo.proto that includes source-retention options.
ASSERT_EQ(request.source_file_descriptors_size(), 1);
EXPECT_EQ(request.source_file_descriptors(0).name(), "foo.proto");
ASSERT_EQ(request.source_file_descriptors(0).message_type_size(), 1);
const DescriptorProto& m = request.source_file_descriptors(0).message_type(0);
ASSERT_EQ(m.extension_range_size(), 1);
EXPECT_EQ(m.extension_range(0).options().declaration_size(), 1);
}
TEST_F(CommandLineInterfaceTest, GeneratorAndPlugin) {
// Invoke a generator and a plugin at the same time.
CreateTempFile("foo.proto",
"syntax = \"proto2\";\n"
"message Foo {}\n");
Run("protocol_compiler --test_out=$tmpdir --plug_out=$tmpdir "
"--proto_path=$tmpdir foo.proto");
ExpectNoErrors();
ExpectGenerated("test_generator", "", "foo.proto", "Foo");
ExpectGenerated("test_plugin", "", "foo.proto", "Foo");
}
TEST_F(CommandLineInterfaceTest, GeneratorAndPlugin_DescriptorSetIn) {
// Invoke a generator and a plugin at the same time.
FileDescriptorSet file_descriptor_set;
FileDescriptorProto* file_descriptor_proto = file_descriptor_set.add_file();
file_descriptor_proto->set_name("foo.proto");
file_descriptor_proto->add_message_type()->set_name("Foo");
WriteDescriptorSet("foo.bin", &file_descriptor_set);
Run("protocol_compiler --test_out=$tmpdir --plug_out=$tmpdir "
"--descriptor_set_in=$tmpdir/foo.bin foo.proto");
ExpectNoErrors();
ExpectGenerated("test_generator", "", "foo.proto", "Foo");
ExpectGenerated("test_plugin", "", "foo.proto", "Foo");
}
TEST_F(CommandLineInterfaceTest, MultipleInputs) {
// Test parsing multiple input files.
CreateTempFile("foo.proto",
"syntax = \"proto2\";\n"
"message Foo {}\n");
CreateTempFile("bar.proto",
"syntax = \"proto2\";\n"
"message Bar {}\n");
Run("protocol_compiler --test_out=$tmpdir --plug_out=$tmpdir "
"--proto_path=$tmpdir foo.proto bar.proto");
ExpectNoErrors();
ExpectGeneratedWithMultipleInputs("test_generator", "foo.proto,bar.proto",
"foo.proto", "Foo");
ExpectGeneratedWithMultipleInputs("test_generator", "foo.proto,bar.proto",
"bar.proto", "Bar");
ExpectGeneratedWithMultipleInputs("test_plugin", "foo.proto,bar.proto",
"foo.proto", "Foo");
ExpectGeneratedWithMultipleInputs("test_plugin", "foo.proto,bar.proto",
"bar.proto", "Bar");
}
TEST_F(CommandLineInterfaceTest, MultipleInputs_DescriptorSetIn) {
// Test parsing multiple input files.
FileDescriptorSet file_descriptor_set;
FileDescriptorProto* file_descriptor_proto = file_descriptor_set.add_file();
file_descriptor_proto->set_name("foo.proto");
file_descriptor_proto->add_message_type()->set_name("Foo");
file_descriptor_proto = file_descriptor_set.add_file();
file_descriptor_proto->set_name("bar.proto");
file_descriptor_proto->add_message_type()->set_name("Bar");
WriteDescriptorSet("foo.bin", &file_descriptor_set);
Run("protocol_compiler --test_out=$tmpdir --plug_out=$tmpdir "
"--descriptor_set_in=$tmpdir/foo.bin foo.proto bar.proto");
ExpectNoErrors();
ExpectGeneratedWithMultipleInputs("test_generator", "foo.proto,bar.proto",
"foo.proto", "Foo");
ExpectGeneratedWithMultipleInputs("test_generator", "foo.proto,bar.proto",
"bar.proto", "Bar");
ExpectGeneratedWithMultipleInputs("test_plugin", "foo.proto,bar.proto",
"foo.proto", "Foo");
ExpectGeneratedWithMultipleInputs("test_plugin", "foo.proto,bar.proto",
"bar.proto", "Bar");
}
TEST_F(CommandLineInterfaceTest, MultipleInputs_UnusedImport_DescriptorSetIn) {
// Test unused import warning is not raised when descriptor_set_in is called
// and custom options are in unknown field instead of uninterpreted_options.
FileDescriptorSet file_descriptor_set;
const FileDescriptor* descriptor_file =
FileDescriptorProto::descriptor()->file();
descriptor_file->CopyTo(file_descriptor_set.add_file());
FileDescriptorProto& any_proto = *file_descriptor_set.add_file();
google::protobuf::Any::descriptor()->file()->CopyTo(&any_proto);
const FileDescriptor* custom_file =
protobuf_unittest::AggregateMessage::descriptor()->file();
FileDescriptorProto* file_descriptor_proto = file_descriptor_set.add_file();
custom_file->CopyTo(file_descriptor_proto);
file_descriptor_proto->set_name("custom_options.proto");
// Add a custom message option.
FieldDescriptorProto* extension_option =
file_descriptor_proto->add_extension();
extension_option->set_name("unknown_option");
extension_option->set_extendee(".google.protobuf.MessageOptions");
extension_option->set_number(1111);
extension_option->set_label(FieldDescriptorProto::LABEL_OPTIONAL);
extension_option->set_type(FieldDescriptorProto::TYPE_INT64);
file_descriptor_proto = file_descriptor_set.add_file();
file_descriptor_proto->set_name("import_custom_unknown_options.proto");
file_descriptor_proto->add_dependency("custom_options.proto");
// Add custom message option to unknown field. This custom option is
// not known in generated pool, thus option will be in unknown fields.
file_descriptor_proto->add_message_type()->set_name("Bar");
file_descriptor_proto->mutable_message_type(0)
->mutable_options()
->mutable_unknown_fields()
->AddVarint(1111, 2222);
WriteDescriptorSet("foo.bin", &file_descriptor_set);
Run("protocol_compiler --test_out=$tmpdir --plug_out=$tmpdir "
"--descriptor_set_in=$tmpdir/foo.bin "
"import_custom_unknown_options.proto");
// TODO: Fix this test. This test case only happens when
// CommandLineInterface::Run() is used instead of invoke protoc combined
// with descriptor_set_in, and same custom options are defined in both
// generated pool and descriptor_set_in. There's no such uages for now but
// still need to be fixed.
/*
file_descriptor_proto = file_descriptor_set.add_file();
file_descriptor_proto->set_name("import_custom_extension_options.proto");
file_descriptor_proto->add_dependency("custom_options.proto");
// Add custom message option to unknown field. This custom option is
// also defined in generated pool, thus option will be in extensions.
file_descriptor_proto->add_message_type()->set_name("Foo");
file_descriptor_proto->mutable_message_type(0)
->mutable_options()
->mutable_unknown_fields()
->AddVarint(protobuf_unittest::message_opt1.number(), 2222);
WriteDescriptorSet("foo.bin", &file_descriptor_set);
Run("protocol_compiler --test_out=$tmpdir --plug_out=$tmpdir "
"--descriptor_set_in=$tmpdir/foo.bin import_custom_unknown_options.proto "
"import_custom_extension_options.proto");
*/
ExpectNoErrors();
}
TEST_F(CommandLineInterfaceTest, MultipleInputsWithImport) {
// Test parsing multiple input files with an import of a separate file.
CreateTempFile("foo.proto",
"syntax = \"proto2\";\n"
"message Foo {}\n");
CreateTempFile("bar.proto",
"syntax = \"proto2\";\n"
"import \"baz.proto\";\n"
"message Bar {\n"
" optional Baz a = 1;\n"
"}\n");
CreateTempFile("baz.proto",
"syntax = \"proto2\";\n"
"message Baz {}\n");
Run("protocol_compiler --test_out=$tmpdir --plug_out=$tmpdir "
"--proto_path=$tmpdir foo.proto bar.proto");
ExpectNoErrors();
ExpectGeneratedWithMultipleInputs("test_generator", "foo.proto,bar.proto",
"foo.proto", "Foo");
ExpectGeneratedWithMultipleInputs("test_generator", "foo.proto,bar.proto",
"bar.proto", "Bar");
ExpectGeneratedWithMultipleInputs("test_plugin", "foo.proto,bar.proto",
"foo.proto", "Foo");
ExpectGeneratedWithMultipleInputs("test_plugin", "foo.proto,bar.proto",
"bar.proto", "Bar");
}
TEST_F(CommandLineInterfaceTest, MultipleInputsWithImport_DescriptorSetIn) {
// Test parsing multiple input files with an import of a separate file.
FileDescriptorSet file_descriptor_set;
FileDescriptorProto* file_descriptor_proto = file_descriptor_set.add_file();
file_descriptor_proto->set_name("foo.proto");
file_descriptor_proto->add_message_type()->set_name("Foo");
file_descriptor_proto = file_descriptor_set.add_file();
file_descriptor_proto->set_name("bar.proto");
file_descriptor_proto->add_dependency("baz.proto");
DescriptorProto* message = file_descriptor_proto->add_message_type();
message->set_name("Bar");
FieldDescriptorProto* field = message->add_field();
field->set_type_name("Baz");
field->set_name("a");
field->set_number(1);
WriteDescriptorSet("foo_and_bar.bin", &file_descriptor_set);
file_descriptor_set.clear_file();
file_descriptor_proto = file_descriptor_set.add_file();
file_descriptor_proto->set_name("baz.proto");
file_descriptor_proto->add_message_type()->set_name("Baz");
file_descriptor_proto = file_descriptor_set.add_file();
file_descriptor_proto->set_name("bat.proto");
file_descriptor_proto->add_dependency("baz.proto");
message = file_descriptor_proto->add_message_type();
message->set_name("Bat");
field = message->add_field();
field->set_type_name("Baz");
field->set_name("a");
field->set_number(1);
WriteDescriptorSet("baz_and_bat.bin", &file_descriptor_set);
Run(absl::Substitute(
"protocol_compiler --test_out=$$tmpdir --plug_out=$$tmpdir "
"--descriptor_set_in=$0 foo.proto bar.proto",
absl::StrCat("$tmpdir/foo_and_bar.bin",
CommandLineInterface::kPathSeparator,
"$tmpdir/baz_and_bat.bin")));
ExpectNoErrors();
ExpectGeneratedWithMultipleInputs("test_generator", "foo.proto,bar.proto",
"foo.proto", "Foo");
ExpectGeneratedWithMultipleInputs("test_generator", "foo.proto,bar.proto",
"bar.proto", "Bar");
ExpectGeneratedWithMultipleInputs("test_plugin", "foo.proto,bar.proto",
"foo.proto", "Foo");
ExpectGeneratedWithMultipleInputs("test_plugin", "foo.proto,bar.proto",
"bar.proto", "Bar");
Run(absl::Substitute(
"protocol_compiler --test_out=$$tmpdir --plug_out=$$tmpdir "
"--descriptor_set_in=$0 baz.proto bat.proto",
absl::StrCat("$tmpdir/foo_and_bar.bin",
CommandLineInterface::kPathSeparator,
"$tmpdir/baz_and_bat.bin")));
ExpectNoErrors();
ExpectGeneratedWithMultipleInputs("test_generator", "baz.proto,bat.proto",
"baz.proto", "Baz");
ExpectGeneratedWithMultipleInputs("test_generator", "baz.proto,bat.proto",
"bat.proto", "Bat");
ExpectGeneratedWithMultipleInputs("test_plugin", "baz.proto,bat.proto",
"baz.proto", "Baz");
ExpectGeneratedWithMultipleInputs("test_plugin", "baz.proto,bat.proto",
"bat.proto", "Bat");
}
TEST_F(CommandLineInterfaceTest,
MultipleInputsWithImport_DescriptorSetIn_DuplicateFileDescriptor) {
// Test parsing multiple input files with an import of a separate file.
FileDescriptorSet file_descriptor_set;
FileDescriptorProto foo_file_descriptor_proto;
foo_file_descriptor_proto.set_name("foo.proto");
foo_file_descriptor_proto.add_message_type()->set_name("Foo");
file_descriptor_set.add_file()->CopyFrom(foo_file_descriptor_proto);
FileDescriptorProto* file_descriptor_proto = file_descriptor_set.add_file();
file_descriptor_proto->set_name("bar.proto");
file_descriptor_proto->add_dependency("baz.proto");
file_descriptor_proto->add_dependency("foo.proto");
DescriptorProto* message = file_descriptor_proto->add_message_type();
message->set_name("Bar");
FieldDescriptorProto* field = message->add_field();
field->set_type_name("Baz");
field->set_name("a");
field->set_number(1);
field = message->add_field();
field->set_type_name("Foo");
field->set_name("f");
field->set_number(2);
WriteDescriptorSet("foo_and_bar.bin", &file_descriptor_set);
file_descriptor_set.clear_file();
file_descriptor_set.add_file()->CopyFrom(foo_file_descriptor_proto);
file_descriptor_proto = file_descriptor_set.add_file();
file_descriptor_proto->set_name("baz.proto");
file_descriptor_proto->add_dependency("foo.proto");
message = file_descriptor_proto->add_message_type();
message->set_name("Baz");
field = message->add_field();
field->set_type_name("Foo");
field->set_name("f");
field->set_number(1);
WriteDescriptorSet("foo_and_baz.bin", &file_descriptor_set);
Run(absl::Substitute(
"protocol_compiler --test_out=$$tmpdir --plug_out=$$tmpdir "
"--descriptor_set_in=$0 bar.proto",
absl::StrCat("$tmpdir/foo_and_bar.bin",
CommandLineInterface::kPathSeparator,
"$tmpdir/foo_and_baz.bin")));
ExpectNoErrors();
ExpectGenerated("test_generator", "", "bar.proto", "Bar");
ExpectGenerated("test_plugin", "", "bar.proto", "Bar");
}
TEST_F(CommandLineInterfaceTest,
MultipleInputsWithImport_DescriptorSetIn_MissingImport) {
// Test parsing multiple input files with an import of a separate file.
FileDescriptorSet file_descriptor_set;
FileDescriptorProto* file_descriptor_proto = file_descriptor_set.add_file();
file_descriptor_proto->set_name("foo.proto");
file_descriptor_proto->add_message_type()->set_name("Foo");
file_descriptor_proto = file_descriptor_set.add_file();
file_descriptor_proto->set_name("bar.proto");
file_descriptor_proto->add_dependency("baz.proto");
DescriptorProto* message = file_descriptor_proto->add_message_type();
message->set_name("Bar");
FieldDescriptorProto* field = message->add_field();
field->set_type_name("Baz");
field->set_name("a");
field->set_number(1);
WriteDescriptorSet("foo_and_bar.bin", &file_descriptor_set);
file_descriptor_set.clear_file();
file_descriptor_proto = file_descriptor_set.add_file();
file_descriptor_proto->set_name("baz.proto");
file_descriptor_proto->add_message_type()->set_name("Baz");
WriteDescriptorSet("baz.bin", &file_descriptor_set);
Run("protocol_compiler --test_out=$tmpdir --plug_out=$tmpdir "
"--descriptor_set_in=$tmpdir/foo_and_bar.bin "
"foo.proto bar.proto");
ExpectErrorSubstring(
"bar.proto: Import \"baz.proto\" was not found or had errors.");
ExpectErrorSubstring("bar.proto: \"Baz\" is not defined.");
}
TEST_F(CommandLineInterfaceTest,
InputsOnlyFromDescriptorSetIn_UnusedImportIsNotReported) {
FileDescriptorSet file_descriptor_set;
FileDescriptorProto* file_descriptor_proto = file_descriptor_set.add_file();
file_descriptor_proto->set_name("unused.proto");
file_descriptor_proto->add_message_type()->set_name("Unused");
file_descriptor_proto = file_descriptor_set.add_file();
file_descriptor_proto->set_name("bar.proto");
file_descriptor_proto->add_dependency("unused.proto");
file_descriptor_proto->add_message_type()->set_name("Bar");
WriteDescriptorSet("unused_and_bar.bin", &file_descriptor_set);
Run("protocol_compiler --test_out=$tmpdir --plug_out=$tmpdir "
"--descriptor_set_in=$tmpdir/unused_and_bar.bin unused.proto bar.proto");
ExpectNoErrors();
}
TEST_F(CommandLineInterfaceTest,
InputsFromDescriptorSetInAndFileSystem_UnusedImportIsReported) {
FileDescriptorSet file_descriptor_set;
FileDescriptorProto* file_descriptor_proto = file_descriptor_set.add_file();
file_descriptor_proto->set_name("unused.proto");
file_descriptor_proto->add_message_type()->set_name("Unused");
file_descriptor_proto = file_descriptor_set.add_file();
file_descriptor_proto->set_name("bar.proto");
file_descriptor_proto->add_dependency("unused.proto");
file_descriptor_proto->add_message_type()->set_name("Bar");
WriteDescriptorSet("unused_and_bar.bin", &file_descriptor_set);
CreateTempFile("foo.proto",
"syntax = \"proto2\";\n"
"import \"bar.proto\";\n"
"message Foo {\n"
" optional Bar bar = 1;\n"
"}\n");
Run("protocol_compiler --test_out=$tmpdir --plug_out=$tmpdir "
"--descriptor_set_in=$tmpdir/unused_and_bar.bin "
"--proto_path=$tmpdir unused.proto bar.proto foo.proto");
// Reporting unused imports here is unfair, since it's unactionable. Notice
// the lack of a line number.
// TODO: If the file with unused import is from the descriptor
// set and not from the file system, suppress the warning.
ExpectWarningSubstring("bar.proto: warning: Import unused.proto is unused.");
}
TEST_F(CommandLineInterfaceTest,
OnlyReportsUnusedImportsForFilesBeingGenerated) {
CreateTempFile("unused.proto",
"syntax = \"proto2\";\n"
"message Unused {}\n");
CreateTempFile("bar.proto",
"syntax = \"proto2\";\n"
"import \"unused.proto\";\n"
"message Bar {}\n");
CreateTempFile("foo.proto",
"syntax = \"proto2\";\n"
"import \"bar.proto\";\n"
"message Foo {\n"
" optional Bar bar = 1;\n"
"}\n");
Run("protocol_compiler --test_out=$tmpdir "
"--proto_path=$tmpdir foo.proto");
ExpectNoErrors();
}
TEST_F(CommandLineInterfaceTest, ReportsTransitiveMisingImports_LeafFirst) {
CreateTempFile("unused.proto",
"syntax = \"proto2\";\n"
"message Unused {}\n");
CreateTempFile("bar.proto",
"syntax = \"proto2\";\n"
"import \"unused.proto\";\n"
"message Bar {}\n");
CreateTempFile("foo.proto",
"syntax = \"proto2\";\n"
"import \"bar.proto\";\n"
"message Foo {\n"
" optional Bar bar = 1;\n"
"}\n");
Run("protocol_compiler --test_out=$tmpdir "
"--proto_path=$tmpdir bar.proto foo.proto");
ExpectWarningSubstring(
"bar.proto:2:1: warning: Import unused.proto is unused.");
}
TEST_F(CommandLineInterfaceTest, ReportsTransitiveMisingImports_LeafLast) {
CreateTempFile("unused.proto",
"syntax = \"proto2\";\n"
"message Unused {}\n");
CreateTempFile("bar.proto",
"syntax = \"proto2\";\n"
"import \"unused.proto\";\n"
"message Bar {}\n");
CreateTempFile("foo.proto",
"syntax = \"proto2\";\n"
"import \"bar.proto\";\n"
"message Foo {\n"
" optional Bar bar = 1;\n"