aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Richard Brown <rbrown@exherbo.org> 2007-05-02 23:36:24 +0000
committerAvatar Richard Brown <rbrown@exherbo.org> 2007-05-02 23:36:24 +0000
commitbf2b471b2bb33de86cb99e9b3bcece107cad18bf (patch)
tree7c3444f078256b9c492c2f2e8760b65098792a55
parenta871a64484604cd146d4ac109e5794917fe64c09 (diff)
downloadpaludis-bf2b471b2bb33de86cb99e9b3bcece107cad18bf.tar.gz
paludis-bf2b471b2bb33de86cb99e9b3bcece107cad18bf.tar.xz
Add AdaptedEnvironment to ruby binding.
-rw-r--r--ruby/Makefile.am3
-rw-r--r--ruby/environment.cc105
-rw-r--r--ruby/environment_TEST.rb70
3 files changed, 178 insertions, 0 deletions
diff --git a/ruby/Makefile.am b/ruby/Makefile.am
index 886d8b3..bc2223d 100644
--- a/ruby/Makefile.am
+++ b/ruby/Makefile.am
@@ -93,6 +93,7 @@ TESTS_ENVIRONMENT = env \
\`$(top_srcdir)/paludis/repositories/gentoo/ebuild/utils/canonicalise $(top_builddir)/paludis/environments/.libs\`: \
\`$(top_srcdir)/paludis/repositories/gentoo/ebuild/utils/canonicalise $(top_builddir)/paludis/environments/paludis/.libs\`: \
\`$(top_srcdir)/paludis/repositories/gentoo/ebuild/utils/canonicalise $(top_builddir)/paludis/environments/no_config/.libs\`: \
+ \`$(top_srcdir)/paludis/repositories/gentoo/ebuild/utils/canonicalise $(top_builddir)/paludis/environments/adapted/.libs\`: \
\`$(top_srcdir)/paludis/repositories/gentoo/ebuild/utils/canonicalise $(top_builddir)/paludis/dep_list/.libs\`: \
\`$(top_srcdir)/paludis/repositories/gentoo/ebuild/utils/canonicalise $(top_builddir)/paludis/repositories/.libs\`: \
\`$(top_srcdir)/paludis/repositories/gentoo/ebuild/utils/canonicalise $(top_builddir)/paludis/repositories/gentoo/.libs\`: \
@@ -123,6 +124,7 @@ libpaludisruby_la_LIBADD = \
$(top_builddir)/paludis/repositories/libpaludisrepositories.la \
$(top_builddir)/paludis/environments/paludis/libpaludispaludisenvironment.la \
$(top_builddir)/paludis/environments/no_config/libpaludisnoconfigenvironment.la \
+ $(top_builddir)/paludis/environments/adapted/libpaludisadaptedenvironment.la \
$(top_builddir)/paludis/environments/libpaludisenvironments.la \
$(top_builddir)/paludis/qa/libpaludisqa.la
@@ -139,6 +141,7 @@ libpaludisruby_la_LIBADD = \
$(top_builddir)/paludis/repositories/libpaludisrepositories.la \
$(top_builddir)/paludis/environments/paludis/libpaludispaludisenvironment.la \
$(top_builddir)/paludis/environments/no_config/libpaludisnoconfigenvironment.la \
+ $(top_builddir)/paludis/environments/adapted/libpaludisadaptedenvironment.la \
$(top_builddir)/paludis/environments/libpaludisenvironments.la
TESTS = $(IF_RUBY_TESTS)
diff --git a/ruby/environment.cc b/ruby/environment.cc
index 969c3c4..0678fc3 100644
--- a/ruby/environment.cc
+++ b/ruby/environment.cc
@@ -20,6 +20,7 @@
#include <paludis_ruby.hh>
#include <paludis/environments/paludis/paludis_environment.hh>
#include <paludis/environments/no_config/no_config_environment.hh>
+#include <paludis/environments/adapted/adapted_environment.hh>
#include <paludis/environments/environment_maker.hh>
#include <paludis/util/collection_concrete.hh>
#include <ruby.h>
@@ -34,8 +35,24 @@ namespace
static VALUE c_environment;
static VALUE c_paludis_environment;
static VALUE c_no_config_environment;
+ static VALUE c_adapted_environment;
static VALUE c_environment_maker;
+ std::tr1::shared_ptr<AdaptedEnvironment>
+ value_to_adapted_environment(VALUE v)
+ {
+ if (rb_obj_is_kind_of(v, c_adapted_environment))
+ {
+ std::tr1::shared_ptr<AdaptedEnvironment> * v_ptr;
+ Data_Get_Struct(v, std::tr1::shared_ptr<AdaptedEnvironment>, v_ptr);
+ return *v_ptr;
+ }
+ else
+ {
+ rb_raise(rb_eTypeError, "Can't convert %s into AdaptedEnvironment", rb_obj_classname(v));
+ }
+ }
+
/*
* call-seq:
* query_use(use_flag, package_database_entry) -> true or false
@@ -371,6 +388,80 @@ namespace
return Qnil;
}
+ VALUE
+ adapted_environment_init(int, VALUE *, VALUE self)
+ {
+ return self;
+ }
+
+ /*
+ * call-seq:
+ * AdaptedEnvironment.new(environment) -> AdaptedEnvironment
+ *
+ * Return a new AdaptedEnvironment based on the given Environment.
+ */
+ VALUE
+ adapted_environment_new(int argc, VALUE * argv, VALUE self)
+ {
+ if (1 != argc)
+ rb_raise(rb_eArgError, "AdaptedEnvironment.new expects one argument, but got %d", argc);
+ try
+ {
+ std::tr1::shared_ptr<AdaptedEnvironment> * e = new std::tr1::shared_ptr<AdaptedEnvironment>(new AdaptedEnvironment(value_to_environment(argv[0])));
+ VALUE tdata(Data_Wrap_Struct(self, 0, &Common<std::tr1::shared_ptr<AdaptedEnvironment> >::free, e));
+ rb_obj_call_init(tdata, argc, argv);
+ return tdata;
+ }
+ catch (const std::exception & e)
+ {
+ exception_to_ruby_exception(e);
+ }
+ }
+
+ /*
+ * call-seq:
+ * adapt_use(package_dep_spec, use_flag_name, use_flag_state) -> Nil
+ *
+ * Set the state of a USE flag for the given PackageDepSpec, enable == true, disabled == false.
+ */
+ VALUE
+ adapted_environment_adapt_use(VALUE self, VALUE package_dep_spec, VALUE use_flag_name, VALUE use_flag_state)
+ {
+ try
+ {
+ UseFlagState ufs;
+ if (use_flag_state == Qtrue)
+ ufs = use_enabled;
+ else if (use_flag_state == Qfalse)
+ ufs = use_disabled;
+ else
+ rb_raise(rb_eTypeError, "Can't convert %s into UseFlagState", rb_obj_classname(use_flag_state));
+
+ value_to_adapted_environment(self)->adapt_use(value_to_package_dep_spec(package_dep_spec),
+ UseFlagName(StringValuePtr(use_flag_name)),
+ ufs);
+ return Qnil;
+ }
+ catch (const std::exception & e)
+ {
+ exception_to_ruby_exception(e);
+ }
+ }
+
+ /*
+ * call-seq:
+ * clear_adaptions -> Nil
+ *
+ * Clear all adaptions from this Environment.
+ */
+ VALUE
+ adapted_environment_clear_adaptions(VALUE self)
+ {
+ value_to_adapted_environment(self)->clear_adaptions();
+ return Qnil;
+ }
+
+
/*
* call-seq:
* make_from_spec(spec) -> Environment
@@ -441,6 +532,20 @@ namespace
rb_define_method(c_no_config_environment, "accept_unstable=", RUBY_FUNC_CAST(&no_config_environment_set_accept_unstable), 0);
/*
+ * Document-class: Paludis::AdaptedEnvironment
+ *
+ * An Environment that allows you to change aspects of an
+ * existing Environment, e.g. the state of a USE flag for a
+ * package.
+ */
+
+ c_adapted_environment = rb_define_class_under(paludis_module(), "AdaptedEnvironment", c_environment);
+ rb_define_singleton_method(c_adapted_environment, "new", RUBY_FUNC_CAST(&adapted_environment_new), -1);
+ rb_define_method(c_adapted_environment, "initialize", RUBY_FUNC_CAST(&adapted_environment_init), -1);
+ rb_define_method(c_adapted_environment, "adapt_use", RUBY_FUNC_CAST(&adapted_environment_adapt_use), 3);
+ rb_define_method(c_adapted_environment, "clear_adaptions", RUBY_FUNC_CAST(&adapted_environment_clear_adaptions), 0);
+
+ /*
* Document-class: Paludis::EnvironmentMaker
*
* A class that holds methods to create environments.
diff --git a/ruby/environment_TEST.rb b/ruby/environment_TEST.rb
index e7626aa..49e7954 100644
--- a/ruby/environment_TEST.rb
+++ b/ruby/environment_TEST.rb
@@ -176,6 +176,76 @@ module Paludis
end
end
+ class TestCase_AdaptedEnvironment < Test::Unit::TestCase
+ def env
+ @env or @env = AdaptedEnvironment.new(EnvironmentMaker.instance.make_from_spec(""))
+ end
+
+ def test_create
+ assert_nothing_raised do
+ env
+ end
+ end
+
+ def test_create_bad
+ assert_raise ArgumentError do
+ AdaptedEnvironment.new
+ end
+ end
+ end
+
+ class TestCase_AdaptedEnvironmentAdaptUse < Test::Unit::TestCase
+ def env
+ @env or @env = AdaptedEnvironment.new(EnvironmentMaker.instance.make_from_spec(""))
+ end
+
+ def test_adapt_use
+ pde = PackageDatabaseEntry.new("foo/bar", VersionSpec.new("1.0"), "testrepo")
+
+ assert env.query_use("enabled", pde)
+ assert ! env.query_use("not_enabled", pde)
+ assert env.query_use("sometimes_enabled", pde)
+
+ env.adapt_use(PackageDepSpec.new('foo/bar', PackageDepSpecParseMode::Permissive), 'enabled', false);
+ assert ! env.query_use('enabled', pde);
+ env.adapt_use(PackageDepSpec.new('foo/bar', PackageDepSpecParseMode::Permissive), 'not_enabled', true);
+ assert env.query_use("not_enabled", pde)
+ env.adapt_use(PackageDepSpec.new('foo/bar', PackageDepSpecParseMode::Permissive), 'sometimes_enabled', false);
+ assert ! env.query_use("sometimes_enabled", pde)
+ end
+
+ def test_adapt_use_bad
+ assert_raise TypeError do
+ env.adapt_use(PackageDepSpec.new('foo/bar', PackageDepSpecParseMode::Permissive), 'not_enabled', 'lemon');
+ end
+ assert_raise TypeError do
+ env.adapt_use(PackageDepSpec.new(123, PackageDepSpecParseMode::Permissive), 'not_enabled', false);
+ end
+ assert_raise TypeError do
+ env.adapt_use(PackageDepSpec.new('foo/bar', PackageDepSpecParseMode::Permissive), 7, false);
+ end
+ end
+ end
+
+ class TestCase_AdaptedEnvironmentClearAdaptions < Test::Unit::TestCase
+ def env
+ @env or @env = AdaptedEnvironment.new(EnvironmentMaker.instance.make_from_spec(""))
+ end
+
+ def test_clear_adaptions
+ pde = PackageDatabaseEntry.new("foo/bar", VersionSpec.new("1.0"), "testrepo")
+
+ assert env.query_use("enabled", pde)
+
+ env.adapt_use(PackageDepSpec.new('foo/bar', PackageDepSpecParseMode::Permissive), 'enabled', false);
+ assert ! env.query_use('enabled', pde);
+
+ env.clear_adaptions;
+ assert env.query_use("enabled", pde)
+
+ end
+ end
+
class TestCase_NoConfigEnvironmentMaskReasons < Test::Unit::TestCase
def env
NoConfigEnvironment.new(Dir.getwd().to_s + "/environment_TEST_dir/testrepo")