From eb36f3e038ff334b54cd4ba8f140ccaf3a1357a4 Mon Sep 17 00:00:00 2001 From: Per Lundberg Date: Wed, 9 Dec 2020 22:58:37 +0200 Subject: [PATCH] Fixes to make the --fatal_warnings flag work with current master --- .../compiler/command_line_interface.cc | 4 ++++ .../command_line_interface_unittest.cc | 22 ++++++++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/google/protobuf/compiler/command_line_interface.cc b/src/google/protobuf/compiler/command_line_interface.cc index 7b66f01103e3..b0b2963d2dcd 100644 --- a/src/google/protobuf/compiler/command_line_interface.cc +++ b/src/google/protobuf/compiler/command_line_interface.cc @@ -2054,6 +2054,10 @@ Parse PROTO_FILES and generate output based on the options given: --error_format=FORMAT Set the format in which to print errors. FORMAT may be 'gcc' (the default) or 'msvs' (Microsoft Visual Studio format). + --fatal_warnings Make warnings be fatal (similar to -Werr in + gcc). This flag will make protoc return + with a non-zero exit code if any warnings + are generated. --print_free_field_numbers Print the free field numbers of the messages defined in the given proto files. Groups share the same field number space with the parent diff --git a/src/google/protobuf/compiler/command_line_interface_unittest.cc b/src/google/protobuf/compiler/command_line_interface_unittest.cc index a0e79874adfb..effab542f5b5 100644 --- a/src/google/protobuf/compiler/command_line_interface_unittest.cc +++ b/src/google/protobuf/compiler/command_line_interface_unittest.cc @@ -160,6 +160,11 @@ class CommandLineInterfaceTest : public testing::Test { void ExpectCapturedStdoutSubstringWithZeroReturnCode( const std::string& expected_substring); + // Checks that Run() returned zero and the stderr contains the given + // substring. + void ExpectCapturedStderrSubstringWithZeroReturnCode( + const std::string& expected_substring); + #if defined(_WIN32) && !defined(__CYGWIN__) // Returns true if ExpectErrorSubstring(expected_substring) would pass, but // does not fail otherwise. @@ -522,6 +527,13 @@ void CommandLineInterfaceTest::ExpectCapturedStdoutSubstringWithZeroReturnCode( captured_stdout_); } +void CommandLineInterfaceTest::ExpectCapturedStderrSubstringWithZeroReturnCode( + const std::string& expected_substring) { + EXPECT_EQ(0, return_code_); + EXPECT_PRED_FORMAT2(testing::IsSubstring, expected_substring, + error_text_); +} + void CommandLineInterfaceTest::ExpectFileContent(const std::string& filename, const std::string& content) { std::string path = temp_directory_ + "/" + filename; @@ -2310,7 +2322,7 @@ TEST_F(CommandLineInterfaceTest, MsvsFormatErrors) { } TEST_F(CommandLineInterfaceTest, InvalidErrorFormat) { - // Test --error_format=msvs + // Test implicit (default) --error_format CreateTempFile("foo.proto", "syntax = \"proto2\";\n" @@ -2318,7 +2330,7 @@ TEST_F(CommandLineInterfaceTest, InvalidErrorFormat) { Run("protocol_compiler --test_out=$tmpdir --proto_path=$tmpdir foo.proto"); - ExpectErrorText("Unknown error format: invalid\n"); + ExpectErrorText("foo.proto:2:1: Expected top-level statement (e.g. \"message\").\n"); } TEST_F(CommandLineInterfaceTest, Warnings) { @@ -2333,13 +2345,13 @@ TEST_F(CommandLineInterfaceTest, Warnings) { Run("protocol_compiler --test_out=$tmpdir " "--proto_path=$tmpdir foo.proto"); ExpectReturnCode(0); - ExpectErrorSubstringWithZeroReturnCode( - "foo.proto: warning: Import bar.proto but not used."); + ExpectCapturedStderrSubstringWithZeroReturnCode( + "foo.proto:2:1: warning: Import bar.proto is unused."); Run("protocol_compiler --test_out=$tmpdir --fatal_warnings " "--proto_path=$tmpdir foo.proto"); ExpectErrorSubstring( - "foo.proto: warning: Import bar.proto but not used."); + "foo.proto:2:1: warning: Import bar.proto is unused."); } // -------------------------------------------------------------------