ruby: Fix .new method (RHBZ#1046509).

The .new method was unintentionally broken in
commit 9466060201.

This fixes the .new method and allows it to be called with multiple
parameters, so you can use:

  Guestfs::Guestfs.new
  Guestfs::Guestfs.new()
  Guestfs::Guestfs.new(:close_on_exit => false)
  etc.

For backwards compatibility, Guestfs::create may still be used.

This commit also adds regression tests:

 - Use .new method in regular tests.  (Because this was not done
   before, we didn't catch the breakage.)

 - Test that ::create still works.

 - Test that args can be passed to .new method.
This commit is contained in:
Richard W.M. Jones
2013-12-27 09:45:49 +00:00
parent 480bd3a6d8
commit ee4ce2a029
14 changed files with 254 additions and 28 deletions

View File

@@ -451,7 +451,7 @@ and generate_ruby_bindtests () =
pr "\
require 'guestfs'
g = Guestfs::create()
g = Guestfs::Guestfs.new()
";
let mkargs args optargs =

View File

@@ -42,6 +42,7 @@ let rec generate_ruby_c () =
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored \"-Wstrict-prototypes\"
@@ -135,28 +136,33 @@ ruby_guestfs_free (void *gvp)
}
}
/*
* call-seq:
* Guestfs::Guestfs.new([{:environment => false, :close_on_exit => false}]) -> Guestfs::Guestfs
*
* Call
* +guestfs_create+[http://libguestfs.org/guestfs.3.html#guestfs_create]
* to create a new libguestfs handle. The handle is represented in
* Ruby as an instance of the Guestfs::Guestfs class.
/* This is the ruby internal alloc function for the class. We do nothing
* here except allocate an object containing a NULL guestfs handle.
* Note we cannot call guestfs_create here because we need the extra
* parameters, which ruby passes via the initialize method (see next
* function).
*/
static VALUE
ruby_guestfs_create (int argc, VALUE *argv, VALUE m)
ruby_guestfs_alloc (VALUE klass)
{
guestfs_h *g;
guestfs_h *g = NULL;
if (argc > 1)
rb_raise (rb_eArgError, \"expecting 0 or 1 arguments\");
volatile VALUE optargsv = argc == 1 ? argv[0] : rb_hash_new ();
Check_Type (optargsv, T_HASH);
/* Wrap it, and make sure the close function is called when the
* handle goes away.
*/
return Data_Wrap_Struct (c_guestfs, NULL, ruby_guestfs_free, g);
}
static unsigned
parse_flags (int argc, VALUE *argv)
{
volatile VALUE optargsv;
unsigned flags = 0;
volatile VALUE v;
optargsv = argc == 1 ? argv[0] : rb_hash_new ();
Check_Type (optargsv, T_HASH);
v = rb_hash_lookup (optargsv, ID2SYM (rb_intern (\"environment\")));
if (v != Qnil && !RTEST (v))
flags |= GUESTFS_CREATE_NO_ENVIRONMENT;
@@ -164,6 +170,56 @@ ruby_guestfs_create (int argc, VALUE *argv, VALUE m)
if (v != Qnil && !RTEST (v))
flags |= GUESTFS_CREATE_NO_CLOSE_ON_EXIT;
return flags;
}
/*
* call-seq:
* Guestfs::Guestfs.new([{:environment => false, :close_on_exit => false}]) -> Guestfs::Guestfs
*
* Call
* +guestfs_create_flags+[http://libguestfs.org/guestfs.3.html#guestfs_create_flags]
* to create a new libguestfs handle. The handle is represented in
* Ruby as an instance of the Guestfs::Guestfs class.
*/
static VALUE
ruby_guestfs_initialize (int argc, VALUE *argv, VALUE m)
{
guestfs_h *g;
unsigned flags;
if (argc > 1)
rb_raise (rb_eArgError, \"expecting 0 or 1 arguments\");
/* Should have been set to NULL by prior call to alloc function. */
assert (DATA_PTR (m) == NULL);
flags = parse_flags (argc, argv);
g = guestfs_create_flags (flags);
if (!g)
rb_raise (e_Error, \"failed to create guestfs handle\");
DATA_PTR (m) = g;
/* Don't print error messages to stderr by default. */
guestfs_set_error_handler (g, NULL, NULL);
return m;
}
/* For backwards compatibility. */
static VALUE
ruby_guestfs_create (int argc, VALUE *argv, VALUE module)
{
guestfs_h *g;
unsigned flags;
if (argc > 1)
rb_raise (rb_eArgError, \"expecting 0 or 1 arguments\");
flags = parse_flags (argc, argv);
g = guestfs_create_flags (flags);
if (!g)
rb_raise (e_Error, \"failed to create guestfs handle\");
@@ -171,9 +227,6 @@ ruby_guestfs_create (int argc, VALUE *argv, VALUE m)
/* Don't print error messages to stderr by default. */
guestfs_set_error_handler (g, NULL, NULL);
/* Wrap it, and make sure the close function is called when the
* handle goes away.
*/
return Data_Wrap_Struct (c_guestfs, NULL, ruby_guestfs_free, g);
}
@@ -707,10 +760,10 @@ Init__guestfs (void)
#ifndef HAVE_TYPE_RB_ALLOC_FUNC_T
#define rb_alloc_func_t void*
#endif
rb_define_alloc_func (c_guestfs, (rb_alloc_func_t) ruby_guestfs_create);
rb_define_alloc_func (c_guestfs, (rb_alloc_func_t) ruby_guestfs_alloc);
#endif
rb_define_module_function (m_guestfs, \"create\", ruby_guestfs_create, -1);
rb_define_method (c_guestfs, \"initialize\", ruby_guestfs_initialize, -1);
rb_define_method (c_guestfs, \"close\", ruby_guestfs_close, 0);
rb_define_method (c_guestfs, \"set_event_callback\",
ruby_set_event_callback, 2);
@@ -719,6 +772,11 @@ Init__guestfs (void)
rb_define_module_function (m_guestfs, \"event_to_string\",
ruby_event_to_string, 1);
/* For backwards compatibility with older code, define a ::create
* module function.
*/
rb_define_module_function (m_guestfs, \"create\", ruby_guestfs_create, -1);
";
(* Constants. *)

View File

@@ -22,7 +22,7 @@ require 'guestfs'
class TestLoad < Test::Unit::TestCase
def test_create
g = Guestfs::create()
g = Guestfs::Guestfs.new()
assert_not_nil (g)
end
end

View File

@@ -0,0 +1,29 @@
# libguestfs Ruby bindings -*- ruby -*-
# Copyright (C) 2013 Red Hat Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
require 'test/unit'
$:.unshift(File::join(File::dirname(__FILE__), "..", "lib"))
$:.unshift(File::join(File::dirname(__FILE__), "..", "ext", "guestfs"))
require 'guestfs'
class TestLoad < Test::Unit::TestCase
def test_create_flags
g = Guestfs::Guestfs.new(:environment => false, :close_on_exit => true)
assert_not_nil (g)
g.parse_environment()
end
end

View File

@@ -0,0 +1,32 @@
# libguestfs Ruby bindings -*- ruby -*-
# Copyright (C) 2013 Red Hat Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
require 'test/unit'
$:.unshift(File::join(File::dirname(__FILE__), "..", "lib"))
$:.unshift(File::join(File::dirname(__FILE__), "..", "ext", "guestfs"))
require 'guestfs'
class TestLoad < Test::Unit::TestCase
def test_create_multiple
g1 = Guestfs::Guestfs.new()
g2 = Guestfs::Guestfs.new()
g3 = Guestfs::Guestfs.new()
assert_not_nil (g1)
assert_not_nil (g2)
assert_not_nil (g3)
end
end

View File

@@ -0,0 +1,36 @@
# libguestfs Ruby bindings -*- ruby -*-
# Copyright (C) 2013 Red Hat Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
require 'test/unit'
$:.unshift(File::join(File::dirname(__FILE__), "..", "lib"))
$:.unshift(File::join(File::dirname(__FILE__), "..", "ext", "guestfs"))
require 'guestfs'
class TestLoad < Test::Unit::TestCase
def test_handle_properties
g = Guestfs::Guestfs.new()
assert_not_nil (g)
v = g.get_verbose()
g.set_verbose(v)
v = g.get_trace()
g.set_trace(v)
v = g.get_memsize()
g.set_memsize(v)
v = g.get_path()
g.set_path(v)
end
end

View File

@@ -0,0 +1,29 @@
# libguestfs Ruby bindings -*- ruby -*-
# Copyright (C) 2013 Red Hat Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
require 'test/unit'
$:.unshift(File::join(File::dirname(__FILE__), "..", "lib"))
$:.unshift(File::join(File::dirname(__FILE__), "..", "ext", "guestfs"))
require 'guestfs'
class TestLoad < Test::Unit::TestCase
def test_explicit_close
g = Guestfs::Guestfs.new()
assert_not_nil (g)
g.close()
end
end

View File

@@ -22,7 +22,7 @@ require 'guestfs'
class TestLoad < Test::Unit::TestCase
def test_optargs
g = Guestfs::create()
g = Guestfs::Guestfs.new()
g.add_drive("/dev/null", {})
g.add_drive("/dev/null", :readonly => 1)

View File

@@ -22,7 +22,7 @@ require 'guestfs'
class TestLoad < Test::Unit::TestCase
def test_launch
g = Guestfs::create()
g = Guestfs::Guestfs.new()
g.add_drive_scratch(500*1024*1024)
g.launch()

View File

@@ -22,7 +22,7 @@ require 'guestfs'
class TestLoad < Test::Unit::TestCase
def test_events
g = Guestfs::create()
g = Guestfs::Guestfs.new()
close_invoked = 0
close = Proc.new {| event, event_handle, buf, array |

View File

@@ -22,7 +22,7 @@ require 'guestfs'
class TestLoad < Test::Unit::TestCase
def test_events
g = Guestfs::create()
g = Guestfs::Guestfs.new()
log_invoked = 0
log = Proc.new {| event, event_handle, buf, array |

View File

@@ -22,7 +22,7 @@ require 'guestfs'
class TestLoad < Test::Unit::TestCase
def test_rhbz507346
g = Guestfs::create()
g = Guestfs::Guestfs.new()
g.add_drive_scratch(10*1024*1024)
g.launch()

View File

@@ -26,7 +26,7 @@ require 'guestfs'
class TestLoad < Test::Unit::TestCase
def test_rhbz664558c6
g = Guestfs::create()
g = Guestfs::Guestfs.new()
close_invoked = 0
close = Proc.new {| event, event_handle, buf, array |

View File

@@ -0,0 +1,42 @@
# libguestfs Ruby bindings -*- ruby -*-
# Copyright (C) 2013 Red Hat Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
# Test that we don't break the old ::create module function while
# fixing https://bugzilla.redhat.com/show_bug.cgi?id=1046509
require 'test/unit'
$:.unshift(File::join(File::dirname(__FILE__), "..", "lib"))
$:.unshift(File::join(File::dirname(__FILE__), "..", "ext", "guestfs"))
require 'guestfs'
class TestLoad < Test::Unit::TestCase
def _handleok(g)
g.add_drive("/dev/null")
g.close()
end
def test_rhbz1046509
g = Guestfs::create()
_handleok(g)
g = Guestfs::create(:close_on_exit => true)
_handleok(g)
g = Guestfs::create(:close_on_exit => true, :environment => true)
_handleok(g)
end
end