From 728e3b4047a55ead0f5fbb3bc57095b2b273352b Mon Sep 17 00:00:00 2001 From: Yehuda Katz + Carl Lerche Date: Fri, 26 Jun 2009 11:21:17 -0700 Subject: Simple initial Paths impl --- railties/test/paths_test.rb | 68 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 railties/test/paths_test.rb (limited to 'railties/test/paths_test.rb') diff --git a/railties/test/paths_test.rb b/railties/test/paths_test.rb new file mode 100644 index 0000000000..0da31b467a --- /dev/null +++ b/railties/test/paths_test.rb @@ -0,0 +1,68 @@ +require 'abstract_unit' + +module Rails + class Application + class Path + attr_accessor :path, :root #, :glob, :load_once, :eager + + def initialize(path, root = nil) + @children = {} + @path = path + @root = root || self + end + + def method_missing(id, *args) + name = id.to_s + + if name =~ /^(.*)=$/ + @children[$1] = Path.new(args.first, @root) + elsif path = @children[name] + path + else + super + end + end + + def path + @path.index('/') == 0 ? @path : File.join(@root.path, @path) + end + + alias to_s path + end + end +end + +class PathsTest < ActiveSupport::TestCase + + def setup + @root = Rails::Application::Path.new("/foo/bar") + end + + test "the paths object is initialized with the root path" do + root = Rails::Application::Path.new("/fiz/baz") + assert_equal "/fiz/baz", root.to_s + end + + test "creating a root level path" do + @root.app = "/foo/bar" + assert_equal "/foo/bar", @root.app.to_s + end + + test "relative paths are relative to the paths root" do + @root.app = "app" + assert_equal "/foo/bar/app", @root.app.to_s + end + + test "creating a child level path" do + @root.app = "/foo/bar" + @root.app.models = "/foo/bar/baz" + assert_equal "/foo/bar/baz", @root.app.models.to_s + end + + test "child level paths are relative from the root" do + @root.app = "/app" + @root.app.models = "baz" + + assert_equal "/foo/bar/baz", @root.app.models.to_s + end +end \ No newline at end of file -- cgit v1.2.3 From b0774281ef4008f2c249b1e85bbdb1f28629d18f Mon Sep 17 00:00:00 2001 From: Yehuda Katz + Carl Lerche Date: Fri, 26 Jun 2009 12:12:20 -0700 Subject: Update paths to support an explicit root and multiple paths per category --- railties/test/paths_test.rb | 120 +++++++++++++++++++++++++++++++++----------- 1 file changed, 91 insertions(+), 29 deletions(-) (limited to 'railties/test/paths_test.rb') diff --git a/railties/test/paths_test.rb b/railties/test/paths_test.rb index 0da31b467a..1fad324015 100644 --- a/railties/test/paths_test.rb +++ b/railties/test/paths_test.rb @@ -2,18 +2,10 @@ require 'abstract_unit' module Rails class Application - class Path - attr_accessor :path, :root #, :glob, :load_once, :eager - - def initialize(path, root = nil) - @children = {} - @path = path - @root = root || self - end - + module PathParent def method_missing(id, *args) name = id.to_s - + if name =~ /^(.*)=$/ @children[$1] = Path.new(args.first, @root) elsif path = @children[name] @@ -22,47 +14,117 @@ module Rails super end end - - def path - @path.index('/') == 0 ? @path : File.join(@root.path, @path) + end + + class Root + include PathParent + + attr_reader :path + def initialize(path) + raise unless path.is_a?(String) + + @children = {} + + # TODO: Move logic from set_root_path initializer + @path = File.expand_path(path) + @root = self + end + end + + class Path + include PathParent + + attr_reader :path #, :glob, :load_once, :eager + + def initialize(path, root) + @children = {} + @root = root + @paths = [path].flatten + end + + def push(path) + @paths.push path + end + + alias << push + + def unshift(path) + @paths.unshift path + end + + + def paths + @paths.map do |path| + path.index('/') == 0 ? path : File.join(@root.path, path) + end end - - alias to_s path + + alias to_a paths end end end class PathsTest < ActiveSupport::TestCase - + def setup - @root = Rails::Application::Path.new("/foo/bar") + @root = Rails::Application::Root.new("/foo/bar") end - + test "the paths object is initialized with the root path" do - root = Rails::Application::Path.new("/fiz/baz") - assert_equal "/fiz/baz", root.to_s + root = Rails::Application::Root.new("/fiz/baz") + assert_equal "/fiz/baz", root.path end - + test "creating a root level path" do @root.app = "/foo/bar" - assert_equal "/foo/bar", @root.app.to_s + assert_equal ["/foo/bar"], @root.app.to_a end - + test "relative paths are relative to the paths root" do @root.app = "app" - assert_equal "/foo/bar/app", @root.app.to_s + assert_equal ["/foo/bar/app"], @root.app.to_a end - + test "creating a child level path" do @root.app = "/foo/bar" @root.app.models = "/foo/bar/baz" - assert_equal "/foo/bar/baz", @root.app.models.to_s + assert_equal ["/foo/bar/baz"], @root.app.models.to_a end - + test "child level paths are relative from the root" do @root.app = "/app" @root.app.models = "baz" - - assert_equal "/foo/bar/baz", @root.app.models.to_s + + assert_equal ["/foo/bar/baz"], @root.app.models.to_a + end + + test "adding multiple physical paths as an array" do + @root.app = ["/app", "/app2"] + assert_equal ["/app", "/app2"], @root.app.to_a + end + + test "adding multiple physical paths using #push" do + @root.app = "/app" + @root.app.push "/app2" + assert_equal ["/app", "/app2"], @root.app.to_a + end + + test "adding multiple physical paths using <<" do + @root.app = "/app" + @root.app << "/app2" + assert_equal ["/app", "/app2"], @root.app.to_a + end + + test "adding multiple physical paths using #unshift" do + @root.app = "/app" + @root.app.unshift "/app2" + assert_equal ["/app2", "/app"], @root.app.to_a + end + + test "the root can only have one physical path" do + assert_raise(RuntimeError) { Rails::Application::Root.new(["/fiz", "/biz"]) } + assert_raise(NoMethodError) { @root.push "/biz" } + assert_raise(NoMethodError) { @root.unshift "/biz" } + assert_raise(NoMethodError) { @root << "/biz" } end end \ No newline at end of file -- cgit v1.2.3 From 4153c6b720563e1c43bb96e95f0ff5fbd59d6be7 Mon Sep 17 00:00:00 2001 From: Yehuda Katz + Carl Lerche Date: Fri, 26 Jun 2009 15:36:38 -0700 Subject: Finished a first stab at the Rails application path object. --- railties/test/paths_test.rb | 104 +++++++++++++++++--------------------------- 1 file changed, 40 insertions(+), 64 deletions(-) (limited to 'railties/test/paths_test.rb') diff --git a/railties/test/paths_test.rb b/railties/test/paths_test.rb index 1fad324015..e7df58579f 100644 --- a/railties/test/paths_test.rb +++ b/railties/test/paths_test.rb @@ -1,68 +1,5 @@ require 'abstract_unit' - -module Rails - class Application - module PathParent - def method_missing(id, *args) - name = id.to_s - - if name =~ /^(.*)=$/ - @children[$1] = Path.new(args.first, @root) - elsif path = @children[name] - path - else - super - end - end - end - - class Root - include PathParent - - attr_reader :path - def initialize(path) - raise unless path.is_a?(String) - - @children = {} - - # TODO: Move logic from set_root_path initializer - @path = File.expand_path(path) - @root = self - end - end - - class Path - include PathParent - - attr_reader :path #, :glob, :load_once, :eager - - def initialize(path, root) - @children = {} - @root = root - @paths = [path].flatten - end - - def push(path) - @paths.push path - end - - alias << push - - def unshift(path) - @paths.unshift path - end - - - def paths - @paths.map do |path| - path.index('/') == 0 ? path : File.join(@root.path, path) - end - end - - alias to_a paths - end - end -end +require 'rails/paths' class PathsTest < ActiveSupport::TestCase @@ -127,4 +64,43 @@ class PathsTest < ActiveSupport::TestCase assert_raise(NoMethodError) { @root.unshift "/biz" } assert_raise(NoMethodError) { @root << "/biz" } end + + test "it is possible to add a path that should be loaded only once" do + @root.app = "/app" + @root.app.load_once! + assert @root.app.load_once? + assert @root.load_once.include?(@root.app) + end + + test "making a path load_once more than once only includes it once in @root.load_once" do + @root.app = "/app" + @root.app.load_once! + @root.app.load_once! + assert_equal 1, @root.load_once.select {|p| p == @root.app }.size + end + + test "it is possible to mark a path as eager" do + @root.app = "/app" + @root.app.eager_load! + assert @root.app.eager_load? + assert @root.eager_load.include?(@root.app) + end + + test "making a path eager more than once only includes it once in @root.eager_paths" do + @root.app = "/app" + @root.app.eager_load! + @root.app.eager_load! + assert_equal 1, @root.eager_load.select {|p| p == @root.app }.size + end + + test "a path should have a glob that defaults to **/*.rb" do + @root.app = "/app" + assert_equal "**/*.rb", @root.app.glob + end + + test "it should be possible to override a path's default glob" do + @root.app = "/app" + @root.app.glob = "*.rb" + assert_equal "*.rb", @root.app.glob + end end \ No newline at end of file -- cgit v1.2.3 From 188a892c5a097ee6d62249d048a6be7d2dfe9649 Mon Sep 17 00:00:00 2001 From: Yehuda Katz + Carl Lerche Date: Fri, 26 Jun 2009 17:32:05 -0700 Subject: Starting to replace scattered path configuration settings with the path object --- railties/test/paths_test.rb | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'railties/test/paths_test.rb') diff --git a/railties/test/paths_test.rb b/railties/test/paths_test.rb index e7df58579f..a1ed43ad6d 100644 --- a/railties/test/paths_test.rb +++ b/railties/test/paths_test.rb @@ -69,28 +69,28 @@ class PathsTest < ActiveSupport::TestCase @root.app = "/app" @root.app.load_once! assert @root.app.load_once? - assert @root.load_once.include?(@root.app) + assert @root.load_once.include?(@root.app.paths.first) end test "making a path load_once more than once only includes it once in @root.load_once" do @root.app = "/app" @root.app.load_once! @root.app.load_once! - assert_equal 1, @root.load_once.select {|p| p == @root.app }.size + assert_equal 1, @root.load_once.select {|p| p == @root.app.paths.first }.size end test "it is possible to mark a path as eager" do @root.app = "/app" @root.app.eager_load! assert @root.app.eager_load? - assert @root.eager_load.include?(@root.app) + assert @root.eager_load.include?(@root.app.paths.first) end test "making a path eager more than once only includes it once in @root.eager_paths" do @root.app = "/app" @root.app.eager_load! @root.app.eager_load! - assert_equal 1, @root.eager_load.select {|p| p == @root.app }.size + assert_equal 1, @root.eager_load.select {|p| p == @root.app.paths.first }.size end test "a path should have a glob that defaults to **/*.rb" do @@ -103,4 +103,17 @@ class PathsTest < ActiveSupport::TestCase @root.app.glob = "*.rb" assert_equal "*.rb", @root.app.glob end + + test "a path can be added to the load path" do + @root.app = "app" + @root.app.load_path! + @root.app.models = "app/models" + assert_equal ["/foo/bar/app"], @root.load_paths + end + + test "adding a path to the eager paths also adds it to the load path" do + @root.app = "app" + @root.app.eager_load! + assert_equal ["/foo/bar/app"], @root.load_paths + end end \ No newline at end of file -- cgit v1.2.3 From 132e6d00638dc6370fafa0f1377d3bca17eee2d1 Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Tue, 30 Jun 2009 13:55:11 -0700 Subject: Add #concat to Rails::Application::Path --- railties/test/paths_test.rb | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'railties/test/paths_test.rb') diff --git a/railties/test/paths_test.rb b/railties/test/paths_test.rb index a1ed43ad6d..fa2f6ceee2 100644 --- a/railties/test/paths_test.rb +++ b/railties/test/paths_test.rb @@ -52,6 +52,12 @@ class PathsTest < ActiveSupport::TestCase assert_equal ["/app", "/app2"], @root.app.to_a end + test "adding multiple physical paths using concat" do + @root.app = "/app" + @root.app.concat ["/app2", "/app3"] + assert_equal ["/app", "/app2", "/app3"], @root.app.to_a + end + test "adding multiple physical paths using #unshift" do @root.app = "/app" @root.app.unshift "/app2" @@ -62,6 +68,7 @@ class PathsTest < ActiveSupport::TestCase assert_raise(RuntimeError) { Rails::Application::Root.new(["/fiz", "/biz"]) } assert_raise(NoMethodError) { @root.push "/biz" } assert_raise(NoMethodError) { @root.unshift "/biz" } + assert_raise(NoMethodError) { @root.concat ["/biz"]} assert_raise(NoMethodError) { @root << "/biz" } end -- cgit v1.2.3