Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Expose configuration options #834

Merged
merged 13 commits into from

4 participants

@JonRowe
Owner

An alternate to #832 this solves #732 by exposing readers for a few of the configuration options that don't already have them.

@soulcutter
Collaborator

Just a heads up, there will be a conflict with #867 specifically the requires= / requires bit.

Other than that, I think this looks good.

@myronmarston

Oh yeah, I still need to review this...will try to get to it soon :).

@soulcutter
Collaborator

Closing in favor of #874 (which is just this PR merged into master)

@soulcutter soulcutter closed this
@JonRowe
Owner

I prefer rebasing and force pushing over merging in another branch, as this preserves the discussion centered around the PR, and I only havent rebased it because I'm still awaiting @myronmarston's feedback...

@soulcutter
Collaborator

I can force push to your repo? I wasn't sure how to deal with that.

Sorry if I stepped on toes this way - my thinking was I'd fix this PR since I broke it (merge-conflict-wise).

FWIW this change gets :+1: to merge into master from me, but also didn't want to step on Myron's toes since this was on his radar.

@JonRowe
Owner

I just meant it was awaiting more feedback, then I was going to rebase it.

You can't push to my repo unless I add you all as collaborators, which actually might be reasonable.

@myronmarston

I prefer keeping the extra merge commit out as well, so I'll comment here.

@myronmarston myronmarston reopened this
lib/rspec/core/configuration.rb
@@ -423,6 +424,9 @@ def expect_with(*frameworks)
@expectation_frameworks.push(*modules)
end
+ def full_backtrace
+ @backtrace_clean_patterns.empty?
+ end
def full_backtrace=(true_or_false)
@myronmarston Owner

Please put blank lines between method defs -- I find that it helps readability a lot.

@JonRowe Owner
JonRowe added a note

Done, sorry, I suck at that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/rspec/core/configuration.rb
@@ -455,10 +459,17 @@ def color=(bool)
def libs=(libs)
libs.map {|lib| $LOAD_PATH.unshift lib}
end
+ def libs
+ $LOAD_PATH
+ end
@myronmarston Owner

I'm a little concerned that this isn't symmetric with libs= -- this will return all load paths, not just the ones the user has added using libs=. I think that these reader/writer methods should be symmetric; otherwise it gets confusing.

@JonRowe Owner
JonRowe added a note

Well, I could argue that I did this because the paths can be mutated from anywhere prior too... However I will defer and make this capture only the path's we've configured.

@myronmarston Owner

Well, I could argue that I did this because the paths can be mutated from anywhere prior too.

That's another reason I think this should only return the load paths that have been added through rspec config: because if the user wants all load paths they can already get it via $LOAD_PATH. However, they don't have a way to get the load paths that were configured via rspec, and that, to me, is the value proposition of having this method at all. (If it's just going to return $LOAD_PATH, I'd just encourage users to use $LOAD_PATH directly.)

@JonRowe Owner
JonRowe added a note

Heh the @github notification email couldn't cope with the outdated diff there, oh well, I've changed this in my most recent push anyhow.

@myronmarston Owner

Same here -- I think it would be more idiomatic as full_backtrace?.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/rspec/core/configuration.rb
@@ -480,15 +491,24 @@ def debug=(bool)
EOM
end
end
+ def debug
+ !!defined?(Debugger)
+ end
@myronmarston Owner

FWIW, I'm planning on removing the debug option in rspec 3...but it's fine to have this for now.

@JonRowe Owner
JonRowe added a note

Noted

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/rspec/core/configuration.rb
((7 lines not shown))
# Run examples defined on `line_numbers` in all files to run.
def line_numbers=(line_numbers)
filter_run :line_numbers => line_numbers.map{|l| l.to_i}
end
+ def line_numbers
+ filter.has_key? :line_numbers
+ end
@myronmarston Owner

Again, this isn't symmetric -- line_numbers= accepts an array of line numbers, but this returns a boolean...that seems odd.

@JonRowe Owner
JonRowe added a note

This one makes the most sense to return directly, so done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/rspec/core/configuration.rb
((15 lines not shown))
def full_description=(description)
filter_run :full_description => Regexp.union(*Array(description).map {|d| Regexp.new(d) })
end
+ def full_description
+ filter.has_key? :full_description
+ end
@myronmarston Owner

This isn't symmetric either -- any reason not to have this return the assigned full description rather than just a boolean?

@JonRowe Owner
JonRowe added a note

The thinking behind these non symmetrical readers was that they were filters, so they indicated wether the filters were on or not, (this thinking was caused by my reading of the specs, which are rather... oddly... structured when it comes to these 3 methods... )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@myronmarston

Woops, I accidentally got my chrome tabs confused and left a couple comments on @soulcutter's PR. I might move them over later but I don't have the time right now.

@myronmarston myronmarston commented on the diff
lib/rspec/core/backtrace_cleaner.rb
@@ -32,6 +32,10 @@ def full_backtrace=(true_or_false)
@exclusion_patterns = true_or_false ? [] : DEFAULT_EXCLUSION_PATTERNS.dup
end
+ def full_backtrace
+ @exclusion_patterns.empty?
+ end
+
@myronmarston Owner

Given that this is a boolean predicate, maybe it should be full_backtrace?.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@myronmarston myronmarston commented on the diff
lib/rspec/core/configuration.rb
@@ -523,15 +534,27 @@ def debug=(bool)
end
end
+ def debug
+ !!defined?(Debugger)
+ end
@myronmarston Owner

Maybe here, too? (debug?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@myronmarston myronmarston commented on the diff
lib/rspec/core/configuration.rb
((17 lines not shown))
def full_description=(description)
filter_run :full_description => Regexp.union(*Array(description).map {|d| Regexp.new(d) })
end
+ def full_description
+ filter.fetch :full_description, false
+ end
@myronmarston Owner

Do you think nil makes more sense here than false? Usually false is paired with true, and other wise nil is used...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@myronmarston myronmarston merged commit de6723d into from
@myronmarston

I left a couple more minor comments, (sorry, some of this stuf I didn't notice until just now), but went ahead and merged because this is definitely a big improvement. If the other comments are deemed worthy of addressing it can always be done post-merge.

Can you add a change log entry, though?

Thanks!

@JonRowe
Owner

Added changelog over on #876

@JonRowe JonRowe deleted the branch
@dchelimsky
Owner

I just noticed this because I was working on BacktraceCleaner and wondered where full_backtrace? gets used and it turns out it doesn't (at least I couldn't find any use of it in rspec-core). What's the motivation to add these config properties?

PS I realize this is 5 months old :)

I believe the motivation for this was solving #732. (See #832 and #834 for further discussion).

A user wanted to be able to do a conditional thing based on if the --backtrace and there wasn't a public API available for that...so @JonRowe added full_backtrace?.

Ha! Turns out I recommended it!

Thx @myronmarston, and thx @JonRowe for implementing it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
4 lib/rspec/core/backtrace_cleaner.rb
@@ -32,6 +32,10 @@ def full_backtrace=(true_or_false)
@exclusion_patterns = true_or_false ? [] : DEFAULT_EXCLUSION_PATTERNS.dup
end
+ def full_backtrace
+ @exclusion_patterns.empty?
+ end
+
@myronmarston Owner

Given that this is a boolean predicate, maybe it should be full_backtrace?.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
private
def matches_an_exclusion_pattern?(line)
View
25 lib/rspec/core/configuration.rb
@@ -111,6 +111,9 @@ def self.add_setting(name, opts={})
# Indicates files configured to be required
define_reader :requires
+ # Returns dirs that have been prepended to the load path by #lib=
+ define_reader :libs
+
# Default: `$stdout`.
# Also known as `output` and `out`
add_setting :output_stream, :alias_with => [:output, :out]
@@ -204,6 +207,7 @@ def initialize
@detail_color = :cyan
@profile_examples = false
@requires = []
+ @libs = []
end
# @private
@@ -462,6 +466,10 @@ def expect_with(*frameworks)
@expectation_frameworks.push(*modules)
end
+ def full_backtrace
+ @backtrace_cleaner.full_backtrace
+ end
@myronmarston Owner

Same here -- I think it would be more idiomatic as full_backtrace?.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+
def full_backtrace=(true_or_false)
@backtrace_cleaner.full_backtrace = true_or_false
end
@@ -492,7 +500,10 @@ def color=(bool)
define_predicate_for :color_enabled, :color
def libs=(libs)
- libs.map {|lib| $LOAD_PATH.unshift lib}
+ libs.map do |lib|
+ @libs.unshift lib
+ $LOAD_PATH.unshift lib
+ end
end
def requires=(paths)
@@ -523,15 +534,27 @@ def debug=(bool)
end
end
+ def debug
+ !!defined?(Debugger)
+ end
@myronmarston Owner

Maybe here, too? (debug?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+
# Run examples defined on `line_numbers` in all files to run.
def line_numbers=(line_numbers)
filter_run :line_numbers => line_numbers.map{|l| l.to_i}
end
+ def line_numbers
+ filter.fetch(:line_numbers,[])
+ end
+
def full_description=(description)
filter_run :full_description => Regexp.union(*Array(description).map {|d| Regexp.new(d) })
end
+ def full_description
+ filter.fetch :full_description, false
+ end
@myronmarston Owner

Do you think nil makes more sense here than false? Usually false is paired with true, and other wise nil is used...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+
# @overload add_formatter(formatter)
#
# Adds a formatter to the formatters collection. `formatter` can be a
View
12 spec/rspec/core/backtrace_cleaner_spec.rb
@@ -8,6 +8,10 @@ module RSpec::Core
cleaner = BacktraceCleaner.new([], [])
expect(lines.all? {|line| cleaner.exclude? line}).to be_false
end
+
+ it 'is considered a full backtrace' do
+ expect(BacktraceCleaner.new([], []).full_backtrace).to be_true
+ end
end
context "with an exclusion pattern but no inclusion patterns" do
@@ -20,6 +24,10 @@ module RSpec::Core
cleaner = BacktraceCleaner.new([], [/remove/])
expect(cleaner.exclude? "apple").to be_false
end
+
+ it 'is considered a partial backtrace' do
+ expect(BacktraceCleaner.new([], [/remove/]).full_backtrace).to be_false
+ end
end
context "with an exclusion pattern and an inclusion pattern" do
@@ -37,6 +45,10 @@ module RSpec::Core
cleaner = BacktraceCleaner.new([/hi/], [/delete/])
expect(cleaner.exclude? "fish").to be_false
end
+
+ it 'is considered a partial backtrace' do
+ expect(BacktraceCleaner.new([], [/remove/]).full_backtrace).to be_false
+ end
end
context "with an exclusion pattern that matches the current working directory" do
View
65 spec/rspec/core/configuration_spec.rb
@@ -493,16 +493,26 @@ def specify_consistent_ordering_of_files_to_run
end
end
- context "with full_description" do
+ context "with full_description set" do
it "overrides filters" do
config.filter_run :focused => true
config.full_description = "foo"
expect(config.filter).not_to have_key(:focused)
end
+
+ it 'is possible to access the full description regular expression' do
+ config.full_description = "foo"
+ expect(config.full_description).to eq /foo/
+ end
end
- context "with line number" do
+ context "without full_description having been set" do
+ it 'returns false from #full_description' do
+ expect(config.full_description).to eq false
+ end
+ end
+ context "with line number" do
it "assigns the file and line number as a location filter" do
config.files_or_directories_to_run = "path/to/a_spec.rb:37"
expect(config.filter).to eq({:locations => {File.expand_path("path/to/a_spec.rb") => [37]}})
@@ -537,6 +547,11 @@ def specify_consistent_ordering_of_files_to_run
expect(config.filter).to eq({:full_description => Regexp.union(/foo/, /bar/)})
end
+ it 'is possible to access the full description regular expression' do
+ config.full_description = "foo","bar"
+ expect(config.full_description).to eq Regexp.union(/foo/,/bar/)
+ end
+
describe "#default_path" do
it 'defaults to "spec"' do
expect(config.default_path).to eq('spec')
@@ -954,6 +969,17 @@ def metadata_hash(*args)
end
end
+ describe "line_numbers" do
+ it "returns the line numbers from the filter" do
+ config.line_numbers = ['42']
+ expect(config.line_numbers).to eq [42]
+ end
+
+ it "defaults to empty" do
+ expect(config.line_numbers).to eq []
+ end
+ end
+
describe "#full_backtrace=" do
context "given true" do
it "clears the backtrace exclusion patterns" do
@@ -987,6 +1013,18 @@ def metadata_hash(*args)
end
end
+ describe 'full_backtrace' do
+ it 'returns true when backtrace patterns is empty' do
+ config.backtrace_exclusion_patterns = []
+ expect(config.full_backtrace).to eq true
+ end
+
+ it 'returns false when backtrace patterns isnt empty' do
+ config.backtrace_exclusion_patterns = [:lib]
+ expect(config.full_backtrace).to eq false
+ end
+ end
+
describe "#backtrace_clean_patterns" do
it "is deprecated" do
RSpec.stub(:warn_deprecation)
@@ -1037,6 +1075,7 @@ def metadata_hash(*args)
else
@orig_debugger = nil
end
+ config.stub(:require)
Object.const_set("Debugger", debugger)
end
@@ -1053,10 +1092,14 @@ def metadata_hash(*args)
end
it "starts the debugger" do
- config.stub(:require)
debugger.should_receive(:start)
config.debug = true
end
+
+ it 'sets the reader to true' do
+ config.debug = true
+ expect(config.debug).to eq true
+ end
end
describe "#debug=false" do
@@ -1064,6 +1107,11 @@ def metadata_hash(*args)
config.should_not_receive(:require).with('ruby-debug')
config.debug = false
end
+
+ it 'sets the reader to false' do
+ config.debug = false
+ expect(config.debug).to eq false
+ end
end
describe "#output=" do
@@ -1083,6 +1131,15 @@ def metadata_hash(*args)
end
end
+ describe "libs" do
+ include_context "isolate load path mutation"
+
+ it 'records paths added to the load path' do
+ config.libs = ["a/dir"]
+ expect(config.libs).to eq ["a/dir"]
+ end
+ end
+
describe "#requires=" do
before { RSpec.should_receive :deprecate }
@@ -1095,7 +1152,7 @@ def metadata_hash(*args)
it "stores require paths" do
config.should_receive(:require).with("a/path")
config.requires = ["a/path"]
- expect(config.requires).to eq(['a/path'])
+ expect(config.requires).to eq ['a/path']
end
end
Something went wrong with that request. Please try again.