浏览代码

Fix golden-file Ruby test to work with out-of-tree builds.

Chris Fallin 10 年之前
父节点
当前提交
b0670ddae7
共有 2 个文件被更改,包括 41 次插入34 次删除
  1. 5 1
      Makefile.am
  2. 36 33
      src/google/protobuf/compiler/ruby/ruby_generator_unittest.cc

+ 5 - 1
Makefile.am

@@ -245,6 +245,7 @@ ruby_EXTRA_DIST=                                                             \
   ruby/ext/google/protobuf_c/defs.c                                          \
   ruby/ext/google/protobuf_c/defs.c                                          \
   ruby/ext/google/protobuf_c/encode_decode.c                                 \
   ruby/ext/google/protobuf_c/encode_decode.c                                 \
   ruby/ext/google/protobuf_c/extconf.rb                                      \
   ruby/ext/google/protobuf_c/extconf.rb                                      \
+  ruby/ext/google/protobuf_c/map.c                                           \
   ruby/ext/google/protobuf_c/message.c                                       \
   ruby/ext/google/protobuf_c/message.c                                       \
   ruby/ext/google/protobuf_c/protobuf.c                                      \
   ruby/ext/google/protobuf_c/protobuf.c                                      \
   ruby/ext/google/protobuf_c/protobuf.h                                      \
   ruby/ext/google/protobuf_c/protobuf.h                                      \
@@ -255,7 +256,10 @@ ruby_EXTRA_DIST=                                                             \
   ruby/google-protobuf.gemspec                                               \
   ruby/google-protobuf.gemspec                                               \
   ruby/lib/google/protobuf.rb                                                \
   ruby/lib/google/protobuf.rb                                                \
   ruby/tests/basic.rb                                                        \
   ruby/tests/basic.rb                                                        \
-  ruby/tests/stress.rb
+  ruby/tests/stress.rb                                                       \
+  ruby/tests/generated_code.proto                                            \
+  ruby/tests/generated_code.rb                                               \
+  ruby/tests/generated_code_test.rb
 
 
 all_EXTRA_DIST=$(java_EXTRA_DIST) $(python_EXTRA_DIST) $(ruby_EXTRA_DIST)
 all_EXTRA_DIST=$(java_EXTRA_DIST) $(python_EXTRA_DIST) $(ruby_EXTRA_DIST)
 
 

+ 36 - 33
src/google/protobuf/compiler/ruby/ruby_generator_unittest.cc

@@ -42,33 +42,22 @@
 namespace google {
 namespace google {
 namespace protobuf {
 namespace protobuf {
 namespace compiler {
 namespace compiler {
-namespace python {
+namespace ruby {
 namespace {
 namespace {
 
 
-class TestGenerator : public CodeGenerator {
- public:
-  TestGenerator() {}
-  ~TestGenerator() {}
-
-  virtual bool Generate(const FileDescriptor* file,
-                        const string& parameter,
-                        GeneratorContext* context,
-                        string* error) const {
-    TryInsert("test_pb2.py", "imports", context);
-    TryInsert("test_pb2.py", "module_scope", context);
-    TryInsert("test_pb2.py", "class_scope:foo.Bar", context);
-    TryInsert("test_pb2.py", "class_scope:foo.Bar.Baz", context);
-    return true;
-  }
-
-  void TryInsert(const string& filename, const string& insertion_point,
-                 GeneratorContext* context) const {
-    google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output(
-        context->OpenForInsert(filename, insertion_point));
-    io::Printer printer(output.get(), '$');
-    printer.Print("// inserted $name$\n", "name", insertion_point);
+string FindRubyTestDir() {
+  // Inspired by TestSourceDir() in src/google/protobuf/testing/googletest.cc.
+  string prefix = ".";
+  while (!File::Exists(prefix + "/ruby/tests")) {
+    if (!File::Exists(prefix)) {
+      GOOGLE_LOG(FATAL)
+          << "Could not find Ruby test directory. Please run tests from "
+             "somewhere within the protobuf source package.";
+    }
+    prefix += "/..";
   }
   }
-};
+  return prefix + "/ruby/tests";
+}
 
 
 // This test is a simple golden-file test over the output of the Ruby code
 // This test is a simple golden-file test over the output of the Ruby code
 // generator. When we make changes to the Ruby extension and alter the Ruby code
 // generator. When we make changes to the Ruby extension and alter the Ruby code
@@ -78,39 +67,53 @@ class TestGenerator : public CodeGenerator {
 // extensions to the point where we can do this test in a more automated way.
 // extensions to the point where we can do this test in a more automated way.
 
 
 TEST(RubyGeneratorTest, GeneratorTest) {
 TEST(RubyGeneratorTest, GeneratorTest) {
+  string ruby_tests = FindRubyTestDir();
+
   google::protobuf::compiler::CommandLineInterface cli;
   google::protobuf::compiler::CommandLineInterface cli;
   cli.SetInputsAreProtoPathRelative(true);
   cli.SetInputsAreProtoPathRelative(true);
 
 
   ruby::Generator ruby_generator;
   ruby::Generator ruby_generator;
   cli.RegisterGenerator("--ruby_out", &ruby_generator, "");
   cli.RegisterGenerator("--ruby_out", &ruby_generator, "");
 
 
-  string path_arg = "-I" + TestSourceDir() + "/ruby/tests";
+  // Copy generated_code.proto to the temporary test directory.
+  string test_input;
+  GOOGLE_CHECK_OK(File::GetContents(
+      ruby_tests + "/generated_code.proto",
+      &test_input,
+      true));
+  GOOGLE_CHECK_OK(File::SetContents(
+      TestTempDir() + "/generated_code.proto",
+      test_input,
+      true));
+
+  // Invoke the proto compiler (we will be inside TestTempDir() at this point).
   string ruby_out = "--ruby_out=" + TestTempDir();
   string ruby_out = "--ruby_out=" + TestTempDir();
+  string proto_path = "--proto_path=" + TestTempDir();
   const char* argv[] = {
   const char* argv[] = {
     "protoc",
     "protoc",
-    path_arg.c_str(),
     ruby_out.c_str(),
     ruby_out.c_str(),
+    proto_path.c_str(),
     "generated_code.proto",
     "generated_code.proto",
   };
   };
 
 
   EXPECT_EQ(0, cli.Run(4, argv));
   EXPECT_EQ(0, cli.Run(4, argv));
 
 
+  // Load the generated output and compare to the expected result.
   string output;
   string output;
-  GOOGLE_CHECK_OK(File::GetContents(TestTempDir() + "/generated_code.rb",
-                                    &output,
-                                    true));
-
+  GOOGLE_CHECK_OK(File::GetContents(
+      TestTempDir() + "/generated_code.rb",
+      &output,
+      true));
   string expected_output;
   string expected_output;
   GOOGLE_CHECK_OK(File::GetContents(
   GOOGLE_CHECK_OK(File::GetContents(
-      TestSourceDir() + "/ruby/tests/generated_code.rb",
+      ruby_tests + "/generated_code.rb",
       &expected_output,
       &expected_output,
       true));
       true));
-
   EXPECT_EQ(expected_output, output);
   EXPECT_EQ(expected_output, output);
 }
 }
 
 
 }  // namespace
 }  // namespace
-}  // namespace python
+}  // namespace ruby
 }  // namespace compiler
 }  // namespace compiler
 }  // namespace protobuf
 }  // namespace protobuf
 }  // namespace google
 }  // namespace google