Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove pathname require from ruby_project.rb #1703

Merged
merged 2 commits into from Oct 12, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Changelog.md
Expand Up @@ -10,6 +10,7 @@ Enhancements:
spec runs, whilst retaining user configuration. (Alexey Fedorov, #1706)
* Reduce string allocations when defining and running examples by 70%
and 50% respectively. (Myron Marston, #1738)
* Removed dependency on pathname from stdlib. (Sam Phippen, #1703)

### 3.1.7 Development
[Full Changelog](http://github.com/rspec/rspec-core/compare/v3.1.6...3-1-maintenance)
Expand Down
16 changes: 12 additions & 4 deletions lib/rspec/core/ruby_project.rb
@@ -1,9 +1,6 @@
# This is borrowed (slightly modified) from Scott Taylor's
# project_path project:
# http://github.com/smtlaissezfaire/project_path

require 'pathname'

module RSpec
module Core
# @private
Expand All @@ -29,8 +26,19 @@ def find_first_parent_containing(dir)
end

def ascend_until
Pathname(File.expand_path('.')).ascend do |path|
fs = File::SEPARATOR
escaped_slash = "\\#{fs}"
special = "_RSPEC_ESCAPED_SLASH_"
project_path = File.expand_path(".")
parts = project_path.gsub(escaped_slash, special).squeeze(fs).split(fs).map do |x|
x.gsub(special, escaped_slash)
end

until parts.empty?
path = parts.join(fs)
path = fs if path == ""
return path if yield(path)
parts.pop
end
end

Expand Down
27 changes: 27 additions & 0 deletions spec/rspec/core/ruby_project_spec.rb
Expand Up @@ -21,6 +21,33 @@ module Core
end

end

describe "#ascend_until" do
subject { RubyProject }

def expect_ascend(source_path, *yielded_paths)
expect { |probe|
allow(File).to receive(:expand_path).with('.') { source_path }
subject.ascend_until(&probe)
}.to yield_successive_args(*yielded_paths)
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this before block needed? You explicitly stub expand_path in each of the specs below...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears that JRuby calls expand_path as part of it's require mechanism, so this is required to allow JRuby to require files.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going to ask "what is requiring a file here?"...but then I tried it locally and I see it now: it's our autoload mechanism for matchers in rspec-expectations, when yield_successive_args is called. IMO, it would be better to change expect_ascend to this:

def expect_ascend(source_path, *yielded_paths)
  expect { |probe|
    allow(File).to receive(:expand_path).with('.') { source_path }
    subject.ascend_until(&probe)
  }.to yield_successive_args(*yielded_paths)
end

With that change in place, you don't need this odd stub.


it "works with a normal path" do
expect_ascend("/var//ponies/", "/var/ponies", "/var", "/")
end

it "works with a path with a trailing slash" do
expect_ascend("/var//ponies/", "/var/ponies", "/var", "/")
end

it "works with a path with double slashes" do
expect_ascend("/var//ponies/", "/var/ponies", "/var", "/")
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The three paths in test are all the same. According to the descriptions, the paths should be "/var/ponies", "/var/ponies/" and "/var//ponies", right?


it "works with a path with escaped slashes" do
expect_ascend("/var\\/ponies/", "/var\\/ponies", "/")
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These specs all follow the same form. I think it's worth extracting a common helper method:

def expect_ascend(source_path, *yielded_paths)
  allow(File).to receive(:expand_path).with('.') { source_path }
  expect { |probe|
    subject.ascend_until(&probe)
  }.to yield_successive_args(*yielded_paths)
end

# call like:
expect_ascend("/var//ponies/", "/var/ponies", "/var", "/")

Thoughts?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

end
end
end
end