Index: third_party/protobuf/src/google/protobuf/compiler/command_line_interface_unittest.cc |
diff --git a/third_party/protobuf/src/google/protobuf/compiler/command_line_interface_unittest.cc b/third_party/protobuf/src/google/protobuf/compiler/command_line_interface_unittest.cc |
index e5b77c3363609ec34baca4ed3d6f22e72ad4b086..165599231647d718144e4a6d3b4d2c4476a768e8 100644 |
--- a/third_party/protobuf/src/google/protobuf/compiler/command_line_interface_unittest.cc |
+++ b/third_party/protobuf/src/google/protobuf/compiler/command_line_interface_unittest.cc |
@@ -1,6 +1,6 @@ |
// Protocol Buffers - Google's data interchange format |
// Copyright 2008 Google Inc. All rights reserved. |
-// https://developers.google.com/protocol-buffers/ |
+// http://code.google.com/p/protobuf/ |
// |
// Redistribution and use in source and binary forms, with or without |
// modification, are permitted provided that the following conditions are |
@@ -40,10 +40,6 @@ |
#else |
#include <unistd.h> |
#endif |
-#include <memory> |
-#ifndef _SHARED_PTR_H |
-#include <google/protobuf/stubs/shared_ptr.h> |
-#endif |
#include <vector> |
#include <google/protobuf/descriptor.pb.h> |
@@ -51,7 +47,6 @@ |
#include <google/protobuf/io/zero_copy_stream.h> |
#include <google/protobuf/compiler/command_line_interface.h> |
#include <google/protobuf/compiler/code_generator.h> |
-#include <google/protobuf/testing/file.h> |
#include <google/protobuf/compiler/mock_code_generator.h> |
#include <google/protobuf/compiler/subprocess.h> |
#include <google/protobuf/io/printer.h> |
@@ -63,10 +58,6 @@ |
#include <google/protobuf/testing/googletest.h> |
#include <gtest/gtest.h> |
- |
-// 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 google { |
namespace protobuf { |
namespace compiler { |
@@ -85,10 +76,6 @@ namespace compiler { |
namespace { |
-bool FileExists(const string& path) { |
- return File::Exists(path); |
-} |
- |
class CommandLineInterfaceTest : public testing::Test { |
protected: |
virtual void SetUp(); |
@@ -115,16 +102,6 @@ class CommandLineInterfaceTest : public testing::Test { |
// Create a subdirectory within temp_directory_. |
void CreateTempDir(const string& name); |
-#ifdef PROTOBUF_OPENSOURCE |
- // Change working directory to temp directory. |
- void SwitchToTempDirectory() { |
- File::ChangeWorkingDirectory(temp_directory_); |
- } |
-#else // !PROTOBUF_OPENSOURCE |
- // TODO(teboring): Figure out how to change and get working directory in |
- // google3. |
-#endif // !PROTOBUF_OPENSOURCE |
- |
void SetInputsAreProtoPathRelative(bool enable) { |
cli_.SetInputsAreProtoPathRelative(enable); |
} |
@@ -149,9 +126,6 @@ class CommandLineInterfaceTest : public testing::Test { |
void ExpectErrorSubstringWithZeroReturnCode( |
const string& expected_substring); |
- // Checks that the captured stdout is the same as the expected_text. |
- void ExpectCapturedStdout(const string& expected_text); |
- |
// Returns true if ExpectErrorSubstring(expected_substring) would pass, but |
// does not fail otherwise. |
bool HasAlternateErrorSubstring(const string& expected_substring); |
@@ -189,9 +163,6 @@ class CommandLineInterfaceTest : public testing::Test { |
void ReadDescriptorSet(const string& filename, |
FileDescriptorSet* descriptor_set); |
- void ExpectFileContent(const string& filename, |
- const string& content); |
- |
private: |
// The object we are testing. |
CommandLineInterface cli_; |
@@ -211,9 +182,6 @@ class CommandLineInterfaceTest : public testing::Test { |
// The captured stderr output. |
string error_text_; |
- // The captured stdout. |
- string captured_stdout_; |
- |
// Pointers which need to be deleted later. |
vector<CodeGenerator*> mock_generators_to_delete_; |
@@ -251,12 +219,12 @@ void CommandLineInterfaceTest::SetUp() { |
// If the temp directory already exists, it must be left over from a |
// previous run. Delete it. |
- if (FileExists(temp_directory_)) { |
+ if (File::Exists(temp_directory_)) { |
File::DeleteRecursively(temp_directory_, NULL, NULL); |
} |
// Create the temp directory. |
- GOOGLE_CHECK_OK(File::CreateDir(temp_directory_, 0777)); |
+ GOOGLE_CHECK(File::CreateDir(temp_directory_.c_str(), DEFAULT_FILE_MODE)); |
// Register generators. |
CodeGenerator* generator = new MockCodeGenerator("test_generator"); |
@@ -277,9 +245,7 @@ void CommandLineInterfaceTest::SetUp() { |
void CommandLineInterfaceTest::TearDown() { |
// Delete the temp directory. |
- if (FileExists(temp_directory_)) { |
- File::DeleteRecursively(temp_directory_, NULL, NULL); |
- } |
+ File::DeleteRecursively(temp_directory_, NULL, NULL); |
// Delete all the MockCodeGenerators. |
for (int i = 0; i < mock_generators_to_delete_.size(); i++) { |
@@ -289,11 +255,11 @@ void CommandLineInterfaceTest::TearDown() { |
} |
void CommandLineInterfaceTest::Run(const string& command) { |
- vector<string> args = Split(command, " ", true); |
+ vector<string> args; |
+ SplitStringUsing(command, " ", &args); |
if (!disallow_plugins_) { |
cli_.AllowPlugins("prefix-"); |
-#ifndef GOOGLE_THIRD_PARTY_PROTOBUF |
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. |
@@ -322,11 +288,6 @@ void CommandLineInterfaceTest::Run(const string& command) { |
} |
if (plugin_path.empty()) { |
-#else |
- string plugin_path = "third_party/protobuf/test_plugin"; |
- |
- if (access(plugin_path.c_str(), F_OK) != 0) { |
-#endif // GOOGLE_THIRD_PARTY_PROTOBUF |
GOOGLE_LOG(ERROR) |
<< "Plugin executable not found. Plugin tests are likely to fail."; |
} else { |
@@ -334,27 +295,18 @@ void CommandLineInterfaceTest::Run(const string& command) { |
} |
} |
- google::protobuf::scoped_array<const char * > argv(new const char* [args.size()]); |
+ scoped_array<const char*> argv(new const char*[args.size()]); |
for (int i = 0; i < args.size(); i++) { |
args[i] = StringReplace(args[i], "$tmpdir", temp_directory_, true); |
argv[i] = args[i].c_str(); |
} |
- // TODO(jieluo): Cygwin doesn't work well if we try to capture stderr and |
- // stdout at the same time. Need to figure out why and add this capture back |
- // for Cygwin. |
-#if !defined(__CYGWIN__) |
- CaptureTestStdout(); |
-#endif |
CaptureTestStderr(); |
return_code_ = cli_.Run(args.size(), argv.get()); |
error_text_ = GetCapturedTestStderr(); |
-#if !defined(__CYGWIN__) |
- captured_stdout_ = GetCapturedTestStdout(); |
-#endif |
} |
// ------------------------------------------------------------------- |
@@ -366,20 +318,16 @@ void CommandLineInterfaceTest::CreateTempFile( |
string::size_type slash_pos = name.find_last_of('/'); |
if (slash_pos != string::npos) { |
string dir = name.substr(0, slash_pos); |
- if (!FileExists(temp_directory_ + "/" + dir)) { |
- GOOGLE_CHECK_OK(File::RecursivelyCreateDir(temp_directory_ + "/" + dir, |
- 0777)); |
- } |
+ File::RecursivelyCreateDir(temp_directory_ + "/" + dir, 0777); |
} |
// Write file. |
string full_name = temp_directory_ + "/" + name; |
- GOOGLE_CHECK_OK(File::SetContents(full_name, contents, true)); |
+ File::WriteStringToFileOrDie(contents, full_name); |
} |
void CommandLineInterfaceTest::CreateTempDir(const string& name) { |
- GOOGLE_CHECK_OK(File::RecursivelyCreateDir(temp_directory_ + "/" + name, |
- 0777)); |
+ File::RecursivelyCreateDir(temp_directory_ + "/" + name, 0777); |
} |
// ------------------------------------------------------------------- |
@@ -466,29 +414,14 @@ void CommandLineInterfaceTest::ReadDescriptorSet( |
const string& filename, FileDescriptorSet* descriptor_set) { |
string path = temp_directory_ + "/" + filename; |
string file_contents; |
- GOOGLE_CHECK_OK(File::GetContents(path, &file_contents, true)); |
- |
+ if (!File::ReadFileToString(path, &file_contents)) { |
+ FAIL() << "File not found: " << path; |
+ } |
if (!descriptor_set->ParseFromString(file_contents)) { |
FAIL() << "Could not parse file contents: " << path; |
} |
} |
-void CommandLineInterfaceTest::ExpectCapturedStdout( |
- const string& expected_text) { |
- EXPECT_EQ(expected_text, captured_stdout_); |
-} |
- |
- |
-void CommandLineInterfaceTest::ExpectFileContent( |
- const string& filename, const string& content) { |
- string path = temp_directory_ + "/" + filename; |
- string file_contents; |
- GOOGLE_CHECK_OK(File::GetContents(path, &file_contents, true)); |
- |
- EXPECT_EQ(StringReplace(content, "$tmpdir", temp_directory_, true), |
- file_contents); |
-} |
- |
// =================================================================== |
TEST_F(CommandLineInterfaceTest, BasicOutput) { |
@@ -758,7 +691,7 @@ TEST_F(CommandLineInterfaceTest, ColonDelimitedPath) { |
#endif |
Run("protocol_compiler --test_out=$tmpdir " |
- "--proto_path=$tmpdir/a" PATH_SEPARATOR "$tmpdir/b foo.proto"); |
+ "--proto_path=$tmpdir/a"PATH_SEPARATOR"$tmpdir/b foo.proto"); |
#undef PATH_SEPARATOR |
@@ -880,7 +813,7 @@ TEST_F(CommandLineInterfaceTest, WriteDescriptorSet) { |
FileDescriptorSet descriptor_set; |
ReadDescriptorSet("descriptor_set", &descriptor_set); |
if (HasFatalFailure()) return; |
- EXPECT_EQ(1, descriptor_set.file_size()); |
+ ASSERT_EQ(1, descriptor_set.file_size()); |
EXPECT_EQ("bar.proto", descriptor_set.file(0).name()); |
// Descriptor set should not have source code info. |
EXPECT_FALSE(descriptor_set.file(0).has_source_code_info()); |
@@ -905,7 +838,7 @@ TEST_F(CommandLineInterfaceTest, WriteDescriptorSetWithSourceInfo) { |
FileDescriptorSet descriptor_set; |
ReadDescriptorSet("descriptor_set", &descriptor_set); |
if (HasFatalFailure()) return; |
- EXPECT_EQ(1, descriptor_set.file_size()); |
+ ASSERT_EQ(1, descriptor_set.file_size()); |
EXPECT_EQ("bar.proto", descriptor_set.file(0).name()); |
// Source code info included. |
EXPECT_TRUE(descriptor_set.file(0).has_source_code_info()); |
@@ -930,7 +863,7 @@ TEST_F(CommandLineInterfaceTest, WriteTransitiveDescriptorSet) { |
FileDescriptorSet descriptor_set; |
ReadDescriptorSet("descriptor_set", &descriptor_set); |
if (HasFatalFailure()) return; |
- EXPECT_EQ(2, descriptor_set.file_size()); |
+ ASSERT_EQ(2, descriptor_set.file_size()); |
if (descriptor_set.file(0).name() == "bar.proto") { |
std::swap(descriptor_set.mutable_file()->mutable_data()[0], |
descriptor_set.mutable_file()->mutable_data()[1]); |
@@ -961,7 +894,7 @@ TEST_F(CommandLineInterfaceTest, WriteTransitiveDescriptorSetWithSourceInfo) { |
FileDescriptorSet descriptor_set; |
ReadDescriptorSet("descriptor_set", &descriptor_set); |
if (HasFatalFailure()) return; |
- EXPECT_EQ(2, descriptor_set.file_size()); |
+ ASSERT_EQ(2, descriptor_set.file_size()); |
if (descriptor_set.file(0).name() == "bar.proto") { |
std::swap(descriptor_set.mutable_file()->mutable_data()[0], |
descriptor_set.mutable_file()->mutable_data()[1]); |
@@ -973,80 +906,6 @@ TEST_F(CommandLineInterfaceTest, WriteTransitiveDescriptorSetWithSourceInfo) { |
EXPECT_TRUE(descriptor_set.file(1).has_source_code_info()); |
} |
-#ifdef _WIN32 |
-// TODO(teboring): Figure out how to write test on windows. |
-#else |
-TEST_F(CommandLineInterfaceTest, WriteDependencyManifestFileGivenTwoInputs) { |
- CreateTempFile("foo.proto", |
- "syntax = \"proto2\";\n" |
- "message Foo {}\n"); |
- CreateTempFile("bar.proto", |
- "syntax = \"proto2\";\n" |
- "import \"foo.proto\";\n" |
- "message Bar {\n" |
- " optional Foo foo = 1;\n" |
- "}\n"); |
- |
- Run("protocol_compiler --dependency_out=$tmpdir/manifest " |
- "--test_out=$tmpdir --proto_path=$tmpdir bar.proto foo.proto"); |
- |
- ExpectErrorText( |
- "Can only process one input file when using --dependency_out=FILE.\n"); |
-} |
- |
-#ifdef PROTOBUF_OPENSOURCE |
-TEST_F(CommandLineInterfaceTest, WriteDependencyManifestFile) { |
- CreateTempFile("foo.proto", |
- "syntax = \"proto2\";\n" |
- "message Foo {}\n"); |
- CreateTempFile("bar.proto", |
- "syntax = \"proto2\";\n" |
- "import \"foo.proto\";\n" |
- "message Bar {\n" |
- " optional Foo foo = 1;\n" |
- "}\n"); |
- |
- string current_working_directory = getcwd(NULL, 0); |
- SwitchToTempDirectory(); |
- |
- Run("protocol_compiler --dependency_out=manifest --test_out=. " |
- "bar.proto"); |
- |
- ExpectNoErrors(); |
- |
- ExpectFileContent("manifest", |
- "bar.proto.MockCodeGenerator.test_generator: " |
- "foo.proto\\\n bar.proto"); |
- |
- File::ChangeWorkingDirectory(current_working_directory); |
-} |
-#else // !PROTOBUF_OPENSOURCE |
-// TODO(teboring): Figure out how to change and get working directory in |
-// google3. |
-#endif // !PROTOBUF_OPENSOURCE |
- |
-TEST_F(CommandLineInterfaceTest, WriteDependencyManifestFileForAbsolutePath) { |
- CreateTempFile("foo.proto", |
- "syntax = \"proto2\";\n" |
- "message Foo {}\n"); |
- CreateTempFile("bar.proto", |
- "syntax = \"proto2\";\n" |
- "import \"foo.proto\";\n" |
- "message Bar {\n" |
- " optional Foo foo = 1;\n" |
- "}\n"); |
- |
- Run("protocol_compiler --dependency_out=$tmpdir/manifest " |
- "--test_out=$tmpdir --proto_path=$tmpdir bar.proto"); |
- |
- ExpectNoErrors(); |
- |
- ExpectFileContent("manifest", |
- "$tmpdir/bar.proto.MockCodeGenerator.test_generator: " |
- "$tmpdir/foo.proto\\\n $tmpdir/bar.proto"); |
-} |
-#endif // !_WIN32 |
- |
// ------------------------------------------------------------------- |
TEST_F(CommandLineInterfaceTest, ParseErrors) { |
@@ -1534,76 +1393,6 @@ TEST_F(CommandLineInterfaceTest, MissingValueAtEndError) { |
ExpectErrorText("Missing value for flag: --test_out\n"); |
} |
-TEST_F(CommandLineInterfaceTest, PrintFreeFieldNumbers) { |
- CreateTempFile( |
- "foo.proto", |
- "syntax = \"proto2\";\n" |
- "package foo;\n" |
- "message Foo {\n" |
- " optional int32 a = 2;\n" |
- " optional string b = 4;\n" |
- " optional string c = 5;\n" |
- " optional int64 d = 8;\n" |
- " optional double e = 10;\n" |
- "}\n"); |
- CreateTempFile( |
- "bar.proto", |
- "syntax = \"proto2\";\n" |
- "message Bar {\n" |
- " optional int32 a = 2;\n" |
- " extensions 4 to 5;\n" |
- " optional int64 d = 8;\n" |
- " extensions 10;\n" |
- "}\n"); |
- CreateTempFile( |
- "baz.proto", |
- "syntax = \"proto2\";\n" |
- "message Baz {\n" |
- " optional int32 a = 2;\n" |
- " optional int64 d = 8;\n" |
- " extensions 15 to max;\n" // unordered. |
- " extensions 13;\n" |
- " extensions 10 to 12;\n" |
- " extensions 5;\n" |
- " extensions 4;\n" |
- "}\n"); |
- CreateTempFile( |
- "quz.proto", |
- "syntax = \"proto2\";\n" |
- "message Quz {\n" |
- " message Foo {}\n" // nested message |
- " optional int32 a = 2;\n" |
- " optional group C = 4 {\n" |
- " optional int32 d = 5;\n" |
- " }\n" |
- " extensions 8 to 10;\n" |
- " optional group E = 11 {\n" |
- " optional int32 f = 9;\n" // explicitly reuse extension range 8-10 |
- " optional group G = 15 {\n" // nested group |
- " message Foo {}\n" // nested message inside nested group |
- " }\n" |
- " }\n" |
- "}\n"); |
- |
- Run("protocol_compiler --print_free_field_numbers --proto_path=$tmpdir " |
- "foo.proto bar.proto baz.proto quz.proto"); |
- |
- ExpectNoErrors(); |
- |
- // TODO(jieluo): Cygwin doesn't work well if we try to capture stderr and |
- // stdout at the same time. Need to figure out why and add this test back |
- // for Cygwin. |
-#if !defined(__CYGWIN__) |
- ExpectCapturedStdout( |
- "foo.Foo free: 1 3 6-7 9 11-INF\n" |
- "Bar free: 1 3 6-7 9 11-INF\n" |
- "Baz free: 1 3 6-7 9 14\n" |
- "Quz.Foo free: 1-INF\n" |
- "Quz.E.G.Foo free: 1-INF\n" |
- "Quz free: 1 3 6-7 12-14 16-INF\n"); |
-#endif |
-} |
- |
// =================================================================== |
// Test for --encode and --decode. Note that it would be easier to do this |
@@ -1623,7 +1412,7 @@ class EncodeDecodeTest : public testing::Test { |
void RedirectStdinFromText(const string& input) { |
string filename = TestTempDir() + "/test_stdin"; |
- GOOGLE_CHECK_OK(File::SetContents(filename, input, true)); |
+ File::WriteStringToFileOrDie(input, filename); |
GOOGLE_CHECK(RedirectStdinFromFile(filename)); |
} |
@@ -1657,7 +1446,7 @@ class EncodeDecodeTest : public testing::Test { |
SplitStringUsing(command, " ", &args); |
args.push_back("--proto_path=" + TestSourceDir()); |
- google::protobuf::scoped_array<const char * > argv(new const char* [args.size()]); |
+ scoped_array<const char*> argv(new const char*[args.size()]); |
for (int i = 0; i < args.size(); i++) { |
argv[i] = args[i].c_str(); |
} |
@@ -1678,7 +1467,7 @@ class EncodeDecodeTest : public testing::Test { |
void ExpectStdoutMatchesBinaryFile(const string& filename) { |
string expected_output; |
- GOOGLE_CHECK_OK(File::GetContents(filename, &expected_output, true)); |
+ ASSERT_TRUE(File::ReadFileToString(filename, &expected_output)); |
// Don't use EXPECT_EQ because we don't want to print raw binary data to |
// stdout on failure. |
@@ -1687,7 +1476,7 @@ class EncodeDecodeTest : public testing::Test { |
void ExpectStdoutMatchesTextFile(const string& filename) { |
string expected_output; |
- GOOGLE_CHECK_OK(File::GetContents(filename, &expected_output, true)); |
+ ASSERT_TRUE(File::ReadFileToString(filename, &expected_output)); |
ExpectStdoutMatchesText(expected_output); |
} |
@@ -1707,23 +1496,22 @@ class EncodeDecodeTest : public testing::Test { |
}; |
TEST_F(EncodeDecodeTest, Encode) { |
- RedirectStdinFromFile(TestSourceDir() + "/google/protobuf/" |
- "testdata/text_format_unittest_data_oneof_implemented.txt"); |
+ RedirectStdinFromFile(TestSourceDir() + |
+ "/google/protobuf/testdata/text_format_unittest_data.txt"); |
EXPECT_TRUE(Run("google/protobuf/unittest.proto " |
"--encode=protobuf_unittest.TestAllTypes")); |
ExpectStdoutMatchesBinaryFile(TestSourceDir() + |
- "/google/protobuf/testdata/golden_message_oneof_implemented"); |
+ "/google/protobuf/testdata/golden_message"); |
ExpectStderrMatchesText(""); |
} |
TEST_F(EncodeDecodeTest, Decode) { |
RedirectStdinFromFile(TestSourceDir() + |
- "/google/protobuf/testdata/golden_message_oneof_implemented"); |
+ "/google/protobuf/testdata/golden_message"); |
EXPECT_TRUE(Run("google/protobuf/unittest.proto " |
"--decode=protobuf_unittest.TestAllTypes")); |
ExpectStdoutMatchesTextFile(TestSourceDir() + |
- "/google/protobuf/" |
- "testdata/text_format_unittest_data_oneof_implemented.txt"); |
+ "/google/protobuf/testdata/text_format_unittest_data.txt"); |
ExpectStderrMatchesText(""); |
} |
@@ -1769,6 +1557,4 @@ TEST_F(EncodeDecodeTest, ProtoParseError) { |
} // namespace compiler |
} // namespace protobuf |
- |
-#endif // !GOOGLE_PROTOBUF_HEAP_CHECK_DRACONIAN |
} // namespace google |