Mailinglist Archive: yast-commit (1914 mails)

< Previous Next >
[yast-commit] <rest-service> basesystem : Fix setting RAILS_ENV to 'test' early (bnc#533178)
  • From: Klaus Kämpf <kkaempf@xxxxxxx>
  • Date: Wed, 9 Sep 2009 15:26:13 +0200
  • Message-id: <E1Mlf03-0004GV-SP@xxxxxxxxxxxxxxxx>
ref: refs/heads/basesystem
commit f3976a983540abc789f693930267f9bd538fd11a
Author: Klaus Kämpf <kkaempf@xxxxxxx>
Date: Wed Sep 9 15:26:13 2009 +0200

Fix setting RAILS_ENV to 'test' early (bnc#533178)

Apparently, using RAILS_ENV (the constant) vs. ENV['RAILS_ENV'] (the
environment) isn't completely standardized leading to many confusions.

config/environment.rb now sets RAILS_ENV (the constant) early.

Loading test resources is now controlled outside of environment.rb,
within the respective testsuites. Setting RESOURCE_REGISTRATION_TESTING
prevents registering all plugin resources now.

This finally allowed to fix bnc#533178 in setting ENV['RAILS_ENV'] to
"test" sufficiently early.

This commit also fixes various test cases to use relative pathes
instead of absolute ones.
---
plugins/basesystem/test/test_helper.rb | 2 +-
plugins/commandline/test/test_helper.rb | 1 +
.../test/functional/language_controller_test.rb | 2 +-
plugins/language/test/test_helper.rb | 2 +-
plugins/network/test/test_helper.rb | 1 +
plugins/patches/test/test_helper.rb | 1 +
plugins/samba_server/test/test_helper.rb | 1 +
plugins/securities/test/test_helper.rb | 1 +
plugins/services/test/test_helper.rb | 1 +
plugins/status/test/test_helper.rb | 1 +
plugins/system/test/test_helper.rb | 1 +
plugins/time/test/test_helper.rb | 1 +
plugins/users/test/test_helper.rb | 1 +
webservice/config/environment.rb | 51 +++++++------------
webservice/lib/resource_registration.rb | 28 ++++++++++-
.../test/functional/resources_controller_test.rb | 14 ++++--
.../test/functional/resources_registration_test.rb | 49 +++++++++++--------
webservice/test/functional/resources_route_test.rb | 22 +++------
18 files changed, 102 insertions(+), 78 deletions(-)

diff --git a/plugins/basesystem/test/test_helper.rb
b/plugins/basesystem/test/test_helper.rb
index 9433250..0d099c3 100644
--- a/plugins/basesystem/test/test_helper.rb
+++ b/plugins/basesystem/test/test_helper.rb
@@ -1,9 +1,9 @@
+ENV["RAILS_ENV"] = "test"
# find the rails parent
require File.join(File.dirname(__FILE__), '..', 'config', 'rails_parent')
# first config rails
require File.expand_path( File.join("config","environment"),
RailsParent.parent )
# then enable testing, this will get the routing right
-ENV["RAILS_ENV"] = "test"
require 'test_help'

class ActiveSupport::TestCase
diff --git a/plugins/commandline/test/test_helper.rb
b/plugins/commandline/test/test_helper.rb
index 7d67910..f2b67f9 100644
--- a/plugins/commandline/test/test_helper.rb
+++ b/plugins/commandline/test/test_helper.rb
@@ -1,3 +1,4 @@
+ENV["RAILS_ENV"] = "test"
# find the rails parent
require File.join(File.dirname(__FILE__), '..', 'config', 'rails_parent')
# first config rails
diff --git a/plugins/language/test/functional/language_controller_test.rb
b/plugins/language/test/functional/language_controller_test.rb
index 10eaee1..bb934b0 100644
--- a/plugins/language/test/functional/language_controller_test.rb
+++ b/plugins/language/test/functional/language_controller_test.rb
@@ -8,7 +8,7 @@ class LanguageControllerTest < ActionController::TestCase
fixtures :accounts

def setup
- @model_class = Language
+# @model_class = Language
@controller = LanguageController.new
@request = ActionController::TestRequest.new
# http://railsforum.com/viewtopic.php?id=1719
diff --git a/plugins/language/test/test_helper.rb
b/plugins/language/test/test_helper.rb
index 7d67910..54d2979 100644
--- a/plugins/language/test/test_helper.rb
+++ b/plugins/language/test/test_helper.rb
@@ -1,9 +1,9 @@
+ENV["RAILS_ENV"] = "test"
# find the rails parent
require File.join(File.dirname(__FILE__), '..', 'config', 'rails_parent')
# first config rails
require File.expand_path( File.join("config","environment"),
RailsParent.parent )
# then enable testing, this will get the routing right
-ENV["RAILS_ENV"] = "test"
require 'test_help'

class ActiveSupport::TestCase
diff --git a/plugins/network/test/test_helper.rb
b/plugins/network/test/test_helper.rb
index 7d67910..f2b67f9 100644
--- a/plugins/network/test/test_helper.rb
+++ b/plugins/network/test/test_helper.rb
@@ -1,3 +1,4 @@
+ENV["RAILS_ENV"] = "test"
# find the rails parent
require File.join(File.dirname(__FILE__), '..', 'config', 'rails_parent')
# first config rails
diff --git a/plugins/patches/test/test_helper.rb
b/plugins/patches/test/test_helper.rb
index 7d67910..f2b67f9 100644
--- a/plugins/patches/test/test_helper.rb
+++ b/plugins/patches/test/test_helper.rb
@@ -1,3 +1,4 @@
+ENV["RAILS_ENV"] = "test"
# find the rails parent
require File.join(File.dirname(__FILE__), '..', 'config', 'rails_parent')
# first config rails
diff --git a/plugins/samba_server/test/test_helper.rb
b/plugins/samba_server/test/test_helper.rb
index 7d67910..f2b67f9 100644
--- a/plugins/samba_server/test/test_helper.rb
+++ b/plugins/samba_server/test/test_helper.rb
@@ -1,3 +1,4 @@
+ENV["RAILS_ENV"] = "test"
# find the rails parent
require File.join(File.dirname(__FILE__), '..', 'config', 'rails_parent')
# first config rails
diff --git a/plugins/securities/test/test_helper.rb
b/plugins/securities/test/test_helper.rb
index 7d67910..f2b67f9 100644
--- a/plugins/securities/test/test_helper.rb
+++ b/plugins/securities/test/test_helper.rb
@@ -1,3 +1,4 @@
+ENV["RAILS_ENV"] = "test"
# find the rails parent
require File.join(File.dirname(__FILE__), '..', 'config', 'rails_parent')
# first config rails
diff --git a/plugins/services/test/test_helper.rb
b/plugins/services/test/test_helper.rb
index 7d67910..f2b67f9 100644
--- a/plugins/services/test/test_helper.rb
+++ b/plugins/services/test/test_helper.rb
@@ -1,3 +1,4 @@
+ENV["RAILS_ENV"] = "test"
# find the rails parent
require File.join(File.dirname(__FILE__), '..', 'config', 'rails_parent')
# first config rails
diff --git a/plugins/status/test/test_helper.rb
b/plugins/status/test/test_helper.rb
index 7d67910..f2b67f9 100644
--- a/plugins/status/test/test_helper.rb
+++ b/plugins/status/test/test_helper.rb
@@ -1,3 +1,4 @@
+ENV["RAILS_ENV"] = "test"
# find the rails parent
require File.join(File.dirname(__FILE__), '..', 'config', 'rails_parent')
# first config rails
diff --git a/plugins/system/test/test_helper.rb
b/plugins/system/test/test_helper.rb
index 7d67910..f2b67f9 100644
--- a/plugins/system/test/test_helper.rb
+++ b/plugins/system/test/test_helper.rb
@@ -1,3 +1,4 @@
+ENV["RAILS_ENV"] = "test"
# find the rails parent
require File.join(File.dirname(__FILE__), '..', 'config', 'rails_parent')
# first config rails
diff --git a/plugins/time/test/test_helper.rb b/plugins/time/test/test_helper.rb
index 7d67910..f2b67f9 100644
--- a/plugins/time/test/test_helper.rb
+++ b/plugins/time/test/test_helper.rb
@@ -1,3 +1,4 @@
+ENV["RAILS_ENV"] = "test"
# find the rails parent
require File.join(File.dirname(__FILE__), '..', 'config', 'rails_parent')
# first config rails
diff --git a/plugins/users/test/test_helper.rb
b/plugins/users/test/test_helper.rb
index 7d67910..f2b67f9 100644
--- a/plugins/users/test/test_helper.rb
+++ b/plugins/users/test/test_helper.rb
@@ -1,3 +1,4 @@
+ENV["RAILS_ENV"] = "test"
# find the rails parent
require File.join(File.dirname(__FILE__), '..', 'config', 'rails_parent')
# first config rails
diff --git a/webservice/config/environment.rb b/webservice/config/environment.rb
index ec75b87..d1af80e 100644
--- a/webservice/config/environment.rb
+++ b/webservice/config/environment.rb
@@ -4,6 +4,10 @@
# you don't control web/app server and can't set it the proper way
# ENV['RAILS_ENV'] ||= 'production'

+# Hmm, don't know if this is the right place for this
+# but
http://groups.google.com/group/sdruby/browse_thread/thread/5239824b058ac936
doesn't know any better
+RAILS_ENV = ENV['RAILS_ENV']
+
# Specifies gem version of Rails to use when vendor/rails is not present
# RAILS_GEM_VERSION = '2.1.0' unless defined? RAILS_GEM_VERSION

@@ -20,11 +24,7 @@ init = Rails::Initializer.run do |config|
# you must remove the Active Record framework.
# config.frameworks -= [ :active_record, :active_resource, :action_mailer ]

- config.gem 'test-unit', :lib => 'test/unit' if ENV['RAILS_ENV'] == 'test' or
- # FIXME: 'rake test' in plugins sets test mode too late, script/server
sets both variables
- # to 'development' so this should not be set in real development mode
- (RAILS_ENV == 'development' and ENV['RAILS_ENV'].nil?)
-
+ config.gem 'test-unit', :lib => 'test/unit' if ENV['RAILS_ENV'] == 'test'

# Specify gems that this application depends on.
# They can then be installed with "rake gems:install" on new installations.
@@ -80,34 +80,21 @@ init = Rails::Initializer.run do |config|

# allows to find plugin in development tree locations
# avoiding installing plugins to see them
- config.plugin_paths << File.join(RAILS_ROOT, '..', 'plugins') if RAILS_ENV
== "development"
+ config.plugin_paths << File.join(RAILS_ROOT, '..', 'plugins') if
ENV['RAILS_ENV'] == "development" or ENV['RAILS_ENV'] == "test"
end

-# load lib/resource_registration.rb
-require "resource_registration"
-
-case RAILS_ENV
- when "test"
- class TestPlugin
- def directory
- "test/resource_fixtures/good"
- end
- end
- ResourceRegistration.register_plugin TestPlugin.new
-
- USER_ROLES_CONFIG = File.join(File.dirname(__FILE__),
"../test/fixtures/yast_user_roles")
-
- when "development", "production"
- init.loaded_plugins.each do |plugin|
- ResourceRegistration.register_plugin(plugin)
- end
-
- USER_ROLES_CONFIG = "/etc/yast_user_roles"
-
- else
- $stderr.puts "No ResourceRegistration triggered"
+# don't load all plugins while just testing resource registration
+unless defined? RESOURCE_REGISTRATION_TESTING
+ STDERR.puts "\n*** registering plugins ***\n"
+ # load lib/resource_registration.rb
+ require "resource_registration"
+
+ init.loaded_plugins.each do |plugin|
+ ResourceRegistration.register_plugin(plugin)
+ end

-end
-
-ResourceRegistration.route ResourceRegistration.resources
+ USER_ROLES_CONFIG = "/etc/yast_user_roles"

+ ResourceRegistration.route ResourceRegistration.resources
+
+end
diff --git a/webservice/lib/resource_registration.rb
b/webservice/lib/resource_registration.rb
index 92a0c0d..a2fb733 100644
--- a/webservice/lib/resource_registration.rb
+++ b/webservice/lib/resource_registration.rb
@@ -1,6 +1,18 @@
# load resources and populate database
+
+class ResourceRegistrationError < StandardError
+end
+
+class ResourceRegistrationPathError < ResourceRegistrationError
+end
+
+class ResourceRegistrationFormatError < ResourceRegistrationError
+end
+
class ResourceRegistration

+ @@in_production = (ENV['RAILS_ENV'] == "production")
+
@@resources = Hash.new
def self.resources
@@resources
@@ -12,7 +24,7 @@ private
log.error msg
return
else
- raise msg
+ raise ResourceRegistrationFormatError.new( msg )
end
end

@@ -33,7 +45,6 @@ public

def self.register(file, interface = nil, controller = nil)
require 'yaml'
- @@in_production = (RAILS_ENV == "production")
name = name || File.basename(file, ".*")
begin
resource = YAML.load(File.open(file)) || Hash.new
@@ -76,18 +87,29 @@ public
#
def self.register_plugin(plugin)
res_path = File.join(plugin.directory, 'config')
+ if defined? RESOURCE_REGISTRATION_TESTING
+ raise ResourceRegistrationPathError.new("Could not access plugin
directory: #{res_path}") unless File.exists?( res_path )
+ end
# $stderr.puts "checking #{res_path}"
Dir.glob(File.join(res_path, 'routes.rb')).each do |route|
basename = File.basename(plugin.directory)
-# $stderr.puts "***Error: Plugin #{basename} does private routing, please
remove #{basename}/config/routes.rb."
+ raise ResourceRegistrationFormatError.new "Plugin #{basename} does
private routing, please remove #{basename}/config/routes.rb."
end
res_path = File.join(res_path, 'resources')
+ if defined? RESOURCE_REGISTRATION_TESTING
+ raise ResourceRegistrationPathError.new("Could not access plugin
directory: #{res_path}") unless File.exists?( res_path )
+ end
# $stderr.puts "self.register_plugin #{res_path}"
+ registration_count = 0
Dir.glob(File.join(res_path, '**/*.y*ml')).each do |descriptor|
# $stderr.puts "checking #{descriptor}"
next unless descriptor =~ %r{#{res_path}/((\w+)/)?(\w+)\.y(a)?ml$}
# $stderr.puts "registering #{descriptor}"
self.register(descriptor)
+ registration_count += 1
+ end
+ if defined? RESOURCE_REGISTRATION_TESTING
+ raise ResourceRegistrationPathError.new("Could not find any YAML file
with resource description below #{res_path}") unless registration_count > 0
end
end

diff --git a/webservice/test/functional/resources_controller_test.rb
b/webservice/test/functional/resources_controller_test.rb
index 3b652af..0c41226 100644
--- a/webservice/test/functional/resources_controller_test.rb
+++ b/webservice/test/functional/resources_controller_test.rb
@@ -3,24 +3,28 @@
#
# This tests proper returns for resource inspection
#
-require File.expand_path(File.dirname(__FILE__) + "/../test_helper")

class TestPlugin
attr_reader :directory
def initialize path
- @directory = path
+ @directory = File.join(File.dirname(__FILE__), "..", path)
end
end

-class ResourcesControllerTest < ActionController::TestCase
+USER_ROLES_CONFIG = File.join(File.dirname(__FILE__), "..", "fixtures",
"yast_user_roles")
+
+RESOURCE_REGISTRATION_TESTING = true # prevent plugin registration in
environment.rb
+require File.join(File.dirname(__FILE__), "..", "test_helper")
+require File.join(File.dirname(__FILE__), "..", "..", "lib",
"resource_registration")

- require File.expand_path(File.dirname(__FILE__) +
"/../../lib/resource_registration")
+class ResourcesControllerTest < ActionController::TestCase

def setup
# set up test routing
ResourceRegistration.reset
- plugin = TestPlugin.new "test/resource_fixtures/good"
+ plugin = TestPlugin.new "resource_fixtures/good"
ResourceRegistration.register_plugin plugin
+ ResourceRegistration.route ResourceRegistration.resources
end

test "resources access root" do
diff --git a/webservice/test/functional/resources_registration_test.rb
b/webservice/test/functional/resources_registration_test.rb
index 4507b17..99e7c71 100644
--- a/webservice/test/functional/resources_registration_test.rb
+++ b/webservice/test/functional/resources_registration_test.rb
@@ -5,23 +5,30 @@
#
# See resource_route_test.rb for resource route tests.
#
-require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
-
class TestPlugin
attr_reader :directory
def initialize path
- @directory = path
+ @directory = File.join(File.dirname(__FILE__), "..", path)
end
end

+unless defined? USER_ROLES_CONFIG
+ USER_ROLES_CONFIG = File.join(File.dirname(__FILE__), "..", "fixtures",
"yast_user_roles")
+end
+
+unless defined? RESOURCE_REGISTRATION_TESTING
+ RESOURCE_REGISTRATION_TESTING = true # prevent plugin registration in
environment.rb
+end
+
+require File.join(File.dirname(__FILE__), "..", "test_helper")
+require File.join(File.dirname(__FILE__), "..", "..", "lib",
"resource_registration")
+
class ResourceRegistrationTest < ActiveSupport::TestCase

- require File.expand_path(File.dirname(__FILE__) +
"/../../lib/resource_registration")
-
# Create resources from .yml file

test "resource creation" do
- plugin = TestPlugin.new "test/resource_fixtures/good"
+ plugin = TestPlugin.new "resource_fixtures/good"
ResourceRegistration.register_plugin plugin

assert !ResourceRegistration.resources.empty?
@@ -30,7 +37,7 @@ class ResourceRegistrationTest < ActiveSupport::TestCase
# Create nested resources from .yml file

test "resource creation nested" do
- plugin = TestPlugin.new "test/resource_fixtures/nested"
+ plugin = TestPlugin.new "resource_fixtures/nested"
ResourceRegistration.register_plugin plugin

assert !ResourceRegistration.resources.empty?
@@ -39,15 +46,15 @@ class ResourceRegistrationTest < ActiveSupport::TestCase
# Catch errors in interface

test "bad interface" do
- plugin = TestPlugin.new "test/resource_fixtures/bad_interface"
- assert_raise RuntimeError do
+ plugin = TestPlugin.new "resource_fixtures/bad_interface"
+ assert_raise ResourceRegistrationFormatError do
ResourceRegistration.register_plugin plugin
end
end

test "no interface" do
- plugin = TestPlugin.new "test/resource_fixtures/no_interface"
- assert_raise RuntimeError do
+ plugin = TestPlugin.new "resource_fixtures/no_interface"
+ assert_raise ResourceRegistrationFormatError do
ResourceRegistration.register_plugin plugin
end
end
@@ -55,14 +62,14 @@ class ResourceRegistrationTest < ActiveSupport::TestCase
# Catch errors in controller

test "no controller" do
- plugin = TestPlugin.new "test/resource_fixtures/no_controller"
- assert_raise RuntimeError do
+ plugin = TestPlugin.new "resource_fixtures/no_controller"
+ assert_raise ResourceRegistrationFormatError do
ResourceRegistration.register_plugin plugin
end
end

test "bad controller, go fix web-client to use modules" do
- plugin = TestPlugin.new "test/resource_fixtures/bad_controller"
+ plugin = TestPlugin.new "resource_fixtures/bad_controller"
# assert_raise RuntimeError do
ResourceRegistration.register_plugin plugin
# end
@@ -71,8 +78,8 @@ class ResourceRegistrationTest < ActiveSupport::TestCase
# Catch pluralization error

test "interface is singular but not flagged as such" do
- plugin = TestPlugin.new "test/resource_fixtures/bad_singular"
- assert_raise RuntimeError do
+ plugin = TestPlugin.new "resource_fixtures/bad_singular"
+ assert_raise ResourceRegistrationFormatError do
ResourceRegistration.register_plugin plugin
end
end
@@ -80,8 +87,8 @@ class ResourceRegistrationTest < ActiveSupport::TestCase
# Catch nested singular, which is not supported (yet?)

test "nesting inside a singular resource" do
- plugin = TestPlugin.new "test/resource_fixtures/nested_singular"
- assert_raise RuntimeError do
+ plugin = TestPlugin.new "resource_fixtures/nested_singular"
+ assert_raise ResourceRegistrationFormatError do
ResourceRegistration.register_plugin plugin
end
end
@@ -125,8 +132,10 @@ class ResourceRegistrationTest < ActiveSupport::TestCase
# Complain about private routing

test "complain about private routing" do
- plugin = TestPlugin.new "test/resource_fixtures/private_routing"
- assert ResourceRegistration.register_plugin plugin
+ plugin = TestPlugin.new "resource_fixtures/private_routing"
+ assert_raise ResourceRegistrationFormatError do
+ ResourceRegistration.register_plugin plugin
+ end
end

end
diff --git a/webservice/test/functional/resources_route_test.rb
b/webservice/test/functional/resources_route_test.rb
index d7c0c42..33930d2 100644
--- a/webservice/test/functional/resources_route_test.rb
+++ b/webservice/test/functional/resources_route_test.rb
@@ -3,29 +3,30 @@
#
# This tests route creation from the resource database
#
-require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
-
class TestPlugin
attr_reader :directory
def initialize path
- @directory = path
+ @directory = File.join(File.dirname(__FILE__), "..", path)
end
end

+RESOURCE_REGISTRATION_TESTING = true # prevent plugin registration in
environment.rb
+require File.join(File.dirname(__FILE__), "..", "test_helper")
+require File.join(File.dirname(__FILE__), "..", "..", "lib",
"resource_registration")
+
class ResourceRouteTest < ActiveSupport::TestCase

# See http://pennysmalls.com/2009/03/04/rails-23-breakage-and-fixage/
include ActionController::Assertions::RoutingAssertions

- require "lib/resource_registration"
-
# config/initializers/resource_registration.rb sets it up

test "resource route initialization" do

- plugin = TestPlugin.new "test/resource_fixtures/good"
+ plugin = TestPlugin.new "resource_fixtures/good"
ResourceRegistration.reset
ResourceRegistration.register_plugin plugin
+ ResourceRegistration.route ResourceRegistration.resources

# $stderr.puts ActionController::Routing::Routes.routes

@@ -45,14 +46,5 @@ class ResourceRouteTest < ActiveSupport::TestCase
end
end
end
-
- test "plugin private routing" do
- searchdir = File.join(File.dirname(__FILE__),"..","..","..","plugins")
- Dir.foreach(searchdir) { |filename|
- unless filename[0].chr == "."
- assert
!File.exist?(File.join(searchdir,filename,"config","routes.rb")), "Plugin
#{filename} contains private routing"
- end
- }
- end

end
--
To unsubscribe, e-mail: yast-commit+unsubscribe@xxxxxxxxxxxx
For additional commands, e-mail: yast-commit+help@xxxxxxxxxxxx

< Previous Next >
This Thread
  • No further messages