Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(121)

Side by Side Diff: third_party/protobuf/src/google/protobuf/compiler/command_line_interface_unittest.cc

Issue 2495533002: third_party/protobuf: Update to HEAD (83d681ee2c) (Closed)
Patch Set: Make chrome settings proto generated file a component Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Protocol Buffers - Google's data interchange format 1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved. 2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/ 3 // https://developers.google.com/protocol-buffers/
4 // 4 //
5 // Redistribution and use in source and binary forms, with or without 5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are 6 // modification, are permitted provided that the following conditions are
7 // met: 7 // met:
8 // 8 //
9 // * Redistributions of source code must retain the above copyright 9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer. 10 // notice, this list of conditions and the following disclaimer.
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
50 #include <google/protobuf/descriptor.h> 50 #include <google/protobuf/descriptor.h>
51 #include <google/protobuf/io/zero_copy_stream.h> 51 #include <google/protobuf/io/zero_copy_stream.h>
52 #include <google/protobuf/compiler/command_line_interface.h> 52 #include <google/protobuf/compiler/command_line_interface.h>
53 #include <google/protobuf/compiler/code_generator.h> 53 #include <google/protobuf/compiler/code_generator.h>
54 #include <google/protobuf/testing/file.h> 54 #include <google/protobuf/testing/file.h>
55 #include <google/protobuf/compiler/mock_code_generator.h> 55 #include <google/protobuf/compiler/mock_code_generator.h>
56 #include <google/protobuf/compiler/subprocess.h> 56 #include <google/protobuf/compiler/subprocess.h>
57 #include <google/protobuf/io/printer.h> 57 #include <google/protobuf/io/printer.h>
58 #include <google/protobuf/unittest.pb.h> 58 #include <google/protobuf/unittest.pb.h>
59 #include <google/protobuf/testing/file.h> 59 #include <google/protobuf/testing/file.h>
60 #include <google/protobuf/stubs/stringprintf.h>
60 #include <google/protobuf/stubs/strutil.h> 61 #include <google/protobuf/stubs/strutil.h>
61 #include <google/protobuf/stubs/substitute.h> 62 #include <google/protobuf/stubs/substitute.h>
62 63
63 #include <google/protobuf/testing/file.h> 64 #include <google/protobuf/testing/file.h>
64 #include <google/protobuf/testing/googletest.h> 65 #include <google/protobuf/testing/googletest.h>
65 #include <gtest/gtest.h> 66 #include <gtest/gtest.h>
66 67
67 68
68 namespace google { 69 namespace google {
69 namespace protobuf { 70 namespace protobuf {
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 // The result of Run(). 211 // The result of Run().
211 int return_code_; 212 int return_code_;
212 213
213 // The captured stderr output. 214 // The captured stderr output.
214 string error_text_; 215 string error_text_;
215 216
216 // The captured stdout. 217 // The captured stdout.
217 string captured_stdout_; 218 string captured_stdout_;
218 219
219 // Pointers which need to be deleted later. 220 // Pointers which need to be deleted later.
220 vector<CodeGenerator*> mock_generators_to_delete_; 221 std::vector<CodeGenerator*> mock_generators_to_delete_;
221 222
222 NullCodeGenerator* null_generator_; 223 NullCodeGenerator* null_generator_;
223 }; 224 };
224 225
225 class CommandLineInterfaceTest::NullCodeGenerator : public CodeGenerator { 226 class CommandLineInterfaceTest::NullCodeGenerator : public CodeGenerator {
226 public: 227 public:
227 NullCodeGenerator() : called_(false) {} 228 NullCodeGenerator() : called_(false) {}
228 ~NullCodeGenerator() {} 229 ~NullCodeGenerator() {}
229 230
230 mutable bool called_; 231 mutable bool called_;
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
284 } 285 }
285 286
286 // Delete all the MockCodeGenerators. 287 // Delete all the MockCodeGenerators.
287 for (int i = 0; i < mock_generators_to_delete_.size(); i++) { 288 for (int i = 0; i < mock_generators_to_delete_.size(); i++) {
288 delete mock_generators_to_delete_[i]; 289 delete mock_generators_to_delete_[i];
289 } 290 }
290 mock_generators_to_delete_.clear(); 291 mock_generators_to_delete_.clear();
291 } 292 }
292 293
293 void CommandLineInterfaceTest::Run(const string& command) { 294 void CommandLineInterfaceTest::Run(const string& command) {
294 vector<string> args = Split(command, " ", true); 295 std::vector<string> args = Split(command, " ", true);
295 296
296 if (!disallow_plugins_) { 297 if (!disallow_plugins_) {
297 cli_.AllowPlugins("prefix-"); 298 cli_.AllowPlugins("prefix-");
298 #ifndef GOOGLE_THIRD_PARTY_PROTOBUF 299 #ifndef GOOGLE_THIRD_PARTY_PROTOBUF
299 string plugin_path; 300 string plugin_path;
300 #ifdef GOOGLE_PROTOBUF_TEST_PLUGIN_PATH 301 #ifdef GOOGLE_PROTOBUF_TEST_PLUGIN_PATH
301 plugin_path = GOOGLE_PROTOBUF_TEST_PLUGIN_PATH; 302 plugin_path = GOOGLE_PROTOBUF_TEST_PLUGIN_PATH;
302 #else 303 #else
303 const char* possible_paths[] = { 304 const char* possible_paths[] = {
304 // When building with shared libraries, libtool hides the real executable 305 // When building with shared libraries, libtool hides the real executable
(...skipping 341 matching lines...) Expand 10 before | Expand all | Expand 10 after
646 "--test_opt=foo3 " 647 "--test_opt=foo3 "
647 "--proto_path=$tmpdir foo.proto"); 648 "--proto_path=$tmpdir foo.proto");
648 649
649 ExpectNoErrors(); 650 ExpectNoErrors();
650 ExpectGenerated( 651 ExpectGenerated(
651 "test_generator", "bar,foo1,foo2,foo3", "foo.proto", "Foo", "a"); 652 "test_generator", "bar,foo1,foo2,foo3", "foo.proto", "Foo", "a");
652 ExpectGenerated( 653 ExpectGenerated(
653 "test_generator", "baz,foo1,foo2,foo3", "foo.proto", "Foo", "b"); 654 "test_generator", "baz,foo1,foo2,foo3", "foo.proto", "Foo", "b");
654 } 655 }
655 656
657 TEST_F(CommandLineInterfaceTest, ExtraPluginParameters) {
658 // Test that generator parameters specified with the option flag are
659 // correctly passed to the code generator.
660
661 CreateTempFile("foo.proto",
662 "syntax = \"proto2\";\n"
663 "message Foo {}\n");
664 // Create the "a" and "b" sub-directories.
665 CreateTempDir("a");
666 CreateTempDir("b");
667
668 Run("protocol_compiler "
669 "--plug_opt=foo1 "
670 "--plug_out=bar:$tmpdir/a "
671 "--plug_opt=foo2 "
672 "--plug_out=baz:$tmpdir/b "
673 "--plug_opt=foo3 "
674 "--proto_path=$tmpdir foo.proto");
675
676 ExpectNoErrors();
677 ExpectGenerated(
678 "test_plugin", "bar,foo1,foo2,foo3", "foo.proto", "Foo", "a");
679 ExpectGenerated(
680 "test_plugin", "baz,foo1,foo2,foo3", "foo.proto", "Foo", "b");
681 }
682
683 TEST_F(CommandLineInterfaceTest, UnrecognizedExtraParameters) {
684 CreateTempFile("foo.proto",
685 "syntax = \"proto2\";\n"
686 "message Foo {}\n");
687
688 Run("protocol_compiler --plug_out=TestParameter:$tmpdir "
689 "--unknown_plug_opt=Foo "
690 "--proto_path=$tmpdir foo.proto");
691
692 ExpectErrorSubstring("Unknown flag: --unknown_plug_opt");
693 }
694
656 TEST_F(CommandLineInterfaceTest, Insert) { 695 TEST_F(CommandLineInterfaceTest, Insert) {
657 // Test running a generator that inserts code into another's output. 696 // Test running a generator that inserts code into another's output.
658 697
659 CreateTempFile("foo.proto", 698 CreateTempFile("foo.proto",
660 "syntax = \"proto2\";\n" 699 "syntax = \"proto2\";\n"
661 "message Foo {}\n"); 700 "message Foo {}\n");
662 701
663 Run("protocol_compiler " 702 Run("protocol_compiler "
664 "--test_out=TestParameter:$tmpdir " 703 "--test_out=TestParameter:$tmpdir "
665 "--plug_out=TestPluginParameter:$tmpdir " 704 "--plug_out=TestPluginParameter:$tmpdir "
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
712 "syntax = \"proto2\";\n" 751 "syntax = \"proto2\";\n"
713 "message Foo {}\n"); 752 "message Foo {}\n");
714 753
715 Run("protocol_compiler --test_out=$tmpdir\\ " 754 Run("protocol_compiler --test_out=$tmpdir\\ "
716 "--proto_path=$tmpdir\\ foo.proto"); 755 "--proto_path=$tmpdir\\ foo.proto");
717 756
718 ExpectNoErrors(); 757 ExpectNoErrors();
719 ExpectGenerated("test_generator", "", "foo.proto", "Foo"); 758 ExpectGenerated("test_generator", "", "foo.proto", "Foo");
720 } 759 }
721 760
761 TEST_F(CommandLineInterfaceTest, Win32ErrorMessage) {
762 EXPECT_EQ("The system cannot find the file specified.\r\n",
763 Subprocess::Win32ErrorMessage(ERROR_FILE_NOT_FOUND));
764 }
765
722 #endif // defined(_WIN32) || defined(__CYGWIN__) 766 #endif // defined(_WIN32) || defined(__CYGWIN__)
723 767
724 TEST_F(CommandLineInterfaceTest, PathLookup) { 768 TEST_F(CommandLineInterfaceTest, PathLookup) {
725 // Test that specifying multiple directories in the proto search path works. 769 // Test that specifying multiple directories in the proto search path works.
726 770
727 CreateTempFile("b/bar.proto", 771 CreateTempFile("b/bar.proto",
728 "syntax = \"proto2\";\n" 772 "syntax = \"proto2\";\n"
729 "message Bar {}\n"); 773 "message Bar {}\n");
730 CreateTempFile("a/foo.proto", 774 CreateTempFile("a/foo.proto",
731 "syntax = \"proto2\";\n" 775 "syntax = \"proto2\";\n"
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
779 "syntax = \"proto2\";\n" 823 "syntax = \"proto2\";\n"
780 "message Foo {}\n"); 824 "message Foo {}\n");
781 825
782 Run("protocol_compiler --test_out=$tmpdir " 826 Run("protocol_compiler --test_out=$tmpdir "
783 "--proto_path=bar=$tmpdir bar/foo.proto"); 827 "--proto_path=bar=$tmpdir bar/foo.proto");
784 828
785 ExpectNoErrors(); 829 ExpectNoErrors();
786 ExpectGenerated("test_generator", "", "bar/foo.proto", "Foo"); 830 ExpectGenerated("test_generator", "", "bar/foo.proto", "Foo");
787 } 831 }
788 832
833 TEST_F(CommandLineInterfaceTest, PathWithEqualsSign) {
834 // Test setting up a search path which happens to have '=' in it.
835
836 CreateTempDir("with=sign");
837 CreateTempFile("with=sign/foo.proto",
838 "syntax = \"proto2\";\n"
839 "message Foo {}\n");
840
841 Run("protocol_compiler --test_out=$tmpdir "
842 "--proto_path=$tmpdir/with=sign foo.proto");
843
844 ExpectNoErrors();
845 ExpectGenerated("test_generator", "", "foo.proto", "Foo");
846 }
847
789 TEST_F(CommandLineInterfaceTest, MultipleGenerators) { 848 TEST_F(CommandLineInterfaceTest, MultipleGenerators) {
790 // Test that we can have multiple generators and use both in one invocation, 849 // Test that we can have multiple generators and use both in one invocation,
791 // each with a different output directory. 850 // each with a different output directory.
792 851
793 CreateTempFile("foo.proto", 852 CreateTempFile("foo.proto",
794 "syntax = \"proto2\";\n" 853 "syntax = \"proto2\";\n"
795 "message Foo {}\n"); 854 "message Foo {}\n");
796 // Create the "a" and "b" sub-directories. 855 // Create the "a" and "b" sub-directories.
797 CreateTempDir("a"); 856 CreateTempDir("a");
798 CreateTempDir("b"); 857 CreateTempDir("b");
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
844 "message Foo {}\n" 903 "message Foo {}\n"
845 "service Bar {}\n"); 904 "service Bar {}\n");
846 905
847 Run("protocol_compiler --test_out=$tmpdir " 906 Run("protocol_compiler --test_out=$tmpdir "
848 "--proto_path=$tmpdir foo.proto"); 907 "--proto_path=$tmpdir foo.proto");
849 908
850 ExpectNoErrors(); 909 ExpectNoErrors();
851 ExpectGenerated("test_generator", "", "foo.proto", "Foo"); 910 ExpectGenerated("test_generator", "", "foo.proto", "Foo");
852 } 911 }
853 912
913 TEST_F(CommandLineInterfaceTest, DirectDependencies_Missing_EmptyList) {
914 CreateTempFile("foo.proto",
915 "syntax = \"proto2\";\n"
916 "import \"bar.proto\";\n"
917 "message Foo { optional Bar bar = 1; }");
918 CreateTempFile("bar.proto",
919 "syntax = \"proto2\";\n"
920 "message Bar { optional string text = 1; }");
921
922 Run("protocol_compiler --test_out=$tmpdir --proto_path=$tmpdir "
923 "--direct_dependencies= foo.proto");
924
925 ExpectErrorText(
926 "foo.proto: File is imported but not declared in --direct_dependencies: "
927 "bar.proto\n");
928 }
929
930 TEST_F(CommandLineInterfaceTest, DirectDependencies_Missing) {
931 CreateTempFile("foo.proto",
932 "syntax = \"proto2\";\n"
933 "import \"bar.proto\";\n"
934 "import \"bla.proto\";\n"
935 "message Foo { optional Bar bar = 1; optional Bla bla = 2; }");
936 CreateTempFile("bar.proto",
937 "syntax = \"proto2\";\n"
938 "message Bar { optional string text = 1; }");
939 CreateTempFile("bla.proto",
940 "syntax = \"proto2\";\n"
941 "message Bla { optional int64 number = 1; }");
942
943 Run("protocol_compiler --test_out=$tmpdir --proto_path=$tmpdir "
944 "--direct_dependencies=bla.proto foo.proto");
945
946 ExpectErrorText(
947 "foo.proto: File is imported but not declared in --direct_dependencies: "
948 "bar.proto\n");
949 }
950
951 TEST_F(CommandLineInterfaceTest, DirectDependencies_NoViolation) {
952 CreateTempFile("foo.proto",
953 "syntax = \"proto2\";\n"
954 "import \"bar.proto\";\n"
955 "message Foo { optional Bar bar = 1; }");
956 CreateTempFile("bar.proto",
957 "syntax = \"proto2\";\n"
958 "message Bar { optional string text = 1; }");
959
960 Run("protocol_compiler --test_out=$tmpdir --proto_path=$tmpdir "
961 "--direct_dependencies=bar.proto foo.proto");
962
963 ExpectNoErrors();
964 }
965
966 TEST_F(CommandLineInterfaceTest, DirectDependencies_NoViolation_MultiImports) {
967 CreateTempFile("foo.proto",
968 "syntax = \"proto2\";\n"
969 "import \"bar.proto\";\n"
970 "import \"bla.proto\";\n"
971 "message Foo { optional Bar bar = 1; optional Bla bla = 2; }");
972 CreateTempFile("bar.proto",
973 "syntax = \"proto2\";\n"
974 "message Bar { optional string text = 1; }");
975 CreateTempFile("bla.proto",
976 "syntax = \"proto2\";\n"
977 "message Bla { optional int64 number = 1; }");
978
979 Run("protocol_compiler --test_out=$tmpdir --proto_path=$tmpdir "
980 "--direct_dependencies=bar.proto:bla.proto foo.proto");
981
982 ExpectNoErrors();
983 }
984
985 TEST_F(CommandLineInterfaceTest, DirectDependencies_ProvidedMultipleTimes) {
986 CreateTempFile("foo.proto",
987 "syntax = \"proto2\";\n");
988
989 Run("protocol_compiler --test_out=$tmpdir --proto_path=$tmpdir "
990 "--direct_dependencies=bar.proto --direct_dependencies=bla.proto "
991 "foo.proto");
992
993 ExpectErrorText(
994 "--direct_dependencies may only be passed once. To specify multiple "
995 "direct dependencies, pass them all as a single parameter separated by "
996 "':'.\n");
997 }
998
854 TEST_F(CommandLineInterfaceTest, CwdRelativeInputs) { 999 TEST_F(CommandLineInterfaceTest, CwdRelativeInputs) {
855 // Test that we can accept working-directory-relative input files. 1000 // Test that we can accept working-directory-relative input files.
856 1001
857 SetInputsAreProtoPathRelative(false); 1002 SetInputsAreProtoPathRelative(false);
858 1003
859 CreateTempFile("foo.proto", 1004 CreateTempFile("foo.proto",
860 "syntax = \"proto2\";\n" 1005 "syntax = \"proto2\";\n"
861 "message Foo {}\n"); 1006 "message Foo {}\n");
862 1007
863 Run("protocol_compiler --test_out=$tmpdir " 1008 Run("protocol_compiler --test_out=$tmpdir "
(...skipping 567 matching lines...) Expand 10 before | Expand all | Expand 10 after
1431 "syntax = \"proto2\";\n" 1576 "syntax = \"proto2\";\n"
1432 "message MockCodeGenerator_HasJsonName {\n" 1577 "message MockCodeGenerator_HasJsonName {\n"
1433 " optional int32 value = 1;\n" 1578 " optional int32 value = 1;\n"
1434 "}\n"); 1579 "}\n");
1435 1580
1436 Run("protocol_compiler --plug_out=$tmpdir --proto_path=$tmpdir foo.proto"); 1581 Run("protocol_compiler --plug_out=$tmpdir --proto_path=$tmpdir foo.proto");
1437 1582
1438 ExpectErrorSubstring("Saw json_name: 1"); 1583 ExpectErrorSubstring("Saw json_name: 1");
1439 } 1584 }
1440 1585
1586 TEST_F(CommandLineInterfaceTest, PluginReceivesCompilerVersion) {
1587 CreateTempFile("foo.proto",
1588 "syntax = \"proto2\";\n"
1589 "message MockCodeGenerator_ShowVersionNumber {\n"
1590 " optional int32 value = 1;\n"
1591 "}\n");
1592
1593 Run("protocol_compiler --plug_out=$tmpdir --proto_path=$tmpdir foo.proto");
1594
1595 ExpectErrorSubstring(
1596 StringPrintf("Saw compiler_version: %d %s",
1597 GOOGLE_PROTOBUF_VERSION,
1598 GOOGLE_PROTOBUF_VERSION_SUFFIX));
1599 }
1600
1441 TEST_F(CommandLineInterfaceTest, GeneratorPluginNotFound) { 1601 TEST_F(CommandLineInterfaceTest, GeneratorPluginNotFound) {
1442 // Test what happens if the plugin isn't found. 1602 // Test what happens if the plugin isn't found.
1443 1603
1444 CreateTempFile("error.proto", 1604 CreateTempFile("error.proto",
1445 "syntax = \"proto2\";\n" 1605 "syntax = \"proto2\";\n"
1446 "message Foo {}\n"); 1606 "message Foo {}\n");
1447 1607
1448 Run("protocol_compiler --badplug_out=TestParameter:$tmpdir " 1608 Run("protocol_compiler --badplug_out=TestParameter:$tmpdir "
1449 "--plugin=prefix-gen-badplug=no_such_file " 1609 "--plugin=prefix-gen-badplug=no_such_file "
1450 "--proto_path=$tmpdir error.proto"); 1610 "--proto_path=$tmpdir error.proto");
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after
1705 } 1865 }
1706 } 1866 }
1707 1867
1708 return result; 1868 return result;
1709 } 1869 }
1710 1870
1711 enum Type { TEXT, BINARY }; 1871 enum Type { TEXT, BINARY };
1712 enum ReturnCode { SUCCESS, ERROR }; 1872 enum ReturnCode { SUCCESS, ERROR };
1713 1873
1714 bool Run(const string& command) { 1874 bool Run(const string& command) {
1715 vector<string> args; 1875 std::vector<string> args;
1716 args.push_back("protoc"); 1876 args.push_back("protoc");
1717 SplitStringUsing(command, " ", &args); 1877 SplitStringUsing(command, " ", &args);
1718 args.push_back("--proto_path=" + TestSourceDir()); 1878 args.push_back("--proto_path=" + TestSourceDir());
1719 1879
1720 google::protobuf::scoped_array<const char * > argv(new const char* [args.siz e()]); 1880 google::protobuf::scoped_array<const char * > argv(new const char* [args.siz e()]);
1721 for (int i = 0; i < args.size(); i++) { 1881 for (int i = 0; i < args.size(); i++) {
1722 argv[i] = args[i].c_str(); 1882 argv[i] = args[i].c_str();
1723 } 1883 }
1724 1884
1725 CommandLineInterface cli; 1885 CommandLineInterface cli;
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
1825 "google/protobuf/no_such_file.proto: File not found.\n"); 1985 "google/protobuf/no_such_file.proto: File not found.\n");
1826 } 1986 }
1827 1987
1828 } // anonymous namespace 1988 } // anonymous namespace
1829 1989
1830 #endif // !GOOGLE_PROTOBUF_HEAP_CHECK_DRACONIAN 1990 #endif // !GOOGLE_PROTOBUF_HEAP_CHECK_DRACONIAN
1831 1991
1832 } // namespace compiler 1992 } // namespace compiler
1833 } // namespace protobuf 1993 } // namespace protobuf
1834 } // namespace google 1994 } // namespace google
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698