Skip to content

Commit

Permalink
Adding configuration to a linter
Browse files Browse the repository at this point in the history
Made configurable the step threshold for the TestWithTooManyStepsLinter.
  • Loading branch information
enkessler committed Apr 7, 2019
1 parent 23ec1f7 commit 59f4b55
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 5 deletions.
1 change: 1 addition & 0 deletions environments/rspec_env.rb
Expand Up @@ -7,6 +7,7 @@
require 'yaml'

require_relative '../testing/rspec/spec/unit/formatters/formatter_unit_specs'
require_relative '../testing/rspec/spec/unit/linters/configurable_linter_unit_specs'
require_relative '../testing/rspec/spec/unit/linters/linter_unit_specs'
require_relative '../testing/rspec/spec/integration/formatters/formatter_integration_specs'
require_relative '../testing/rspec/spec/integration/linters/linter_integration_specs'
Expand Down
11 changes: 8 additions & 3 deletions lib/cuke_linter/linters/test_with_too_many_steps_linter.rb
Expand Up @@ -9,14 +9,19 @@ def name
'TestWithTooManyStepsLinter'
end

def configure(options)
@step_threshold = options['StepThreshold'] if options['StepThreshold']
end

# Lints the given model and returns linting data about said model
def lint(model)
return [] unless model.is_a?(CukeModeler::Scenario) || model.is_a?(CukeModeler::Outline)

step_count = model.steps.nil? ? 0 : model.steps.count
step_count = model.steps.nil? ? 0 : model.steps.count
step_threshold = @step_threshold || 10

if step_count > 10
[{ problem: "Test has too many steps. #{step_count} steps found (max 10)", location: "#{model.get_ancestor(:feature_file).path}:#{model.source_line}" }]
if step_count > step_threshold
[{ problem: "Test has too many steps. #{step_count} steps found (max #{step_threshold})", location: "#{model.get_ancestor(:feature_file).path}:#{model.source_line}" }]
else
[]
end
Expand Down
Expand Up @@ -32,5 +32,25 @@ Feature: Test with too many steps linter
| linter | problem | location |
| TestWithTooManyStepsLinter | Test has too many steps. 11 steps found (max 10) | <path_to_file>:3 |

@wip
Scenario: Configuration of step count threshold
Given a linter for tests with too many steps has been registered
And the following configuration file:
"""
TestWithTooManyStepsLinter:
StepThreshold: 3
"""
And the following feature:
"""
Feature:
Scenario:
* step 1
* step 2
* step 3
* step one too many...
"""
When the configuration file is loaded
And the feature is linted
Then an error is reported
| linter | problem | location |
| TestWithTooManyStepsLinter | Test has too many steps. 4 steps found (max 3) | <path_to_file>:3 |
2 changes: 1 addition & 1 deletion testing/cucumber/step_definitions/action_steps.rb
Expand Up @@ -16,7 +16,7 @@
@results = CukeLinter.lint(options)
end

When(/^the configuration file is used$/) do
When(/^the configuration file is (?:used|loaded)$/) do
CukeLinter.load_configuration(config_file_path: @configuration_file_path)
end

Expand Down
4 changes: 4 additions & 0 deletions testing/cucumber/step_definitions/setup_steps.rb
Expand Up @@ -41,6 +41,10 @@
@linter = CukeLinter::TestWithTooManyStepsLinter.new
end

Given(/^a linter for tests with too many steps has been registered$/) do
CukeLinter.register_linter(linter: CukeLinter::TestWithTooManyStepsLinter.new, name: 'TestWithTooManyStepsLinter')
end

Given(/^the following configuration file(?: "([^"]*)")?:$/) do |file_name, text|
file_name ||= '.cuke_linter'

Expand Down
11 changes: 11 additions & 0 deletions testing/rspec/spec/unit/linters/configurable_linter_unit_specs.rb
@@ -0,0 +1,11 @@
shared_examples_for 'a configurable linter at the unit level' do

it 'is configurable' do
expect(subject).to respond_to(:configure)
end

it 'is configured via a set of options' do
expect(subject.method(:configure).arity).to eq(1)
end

end
Expand Up @@ -29,6 +29,7 @@


it_should_behave_like 'a linter at the unit level'
it_should_behave_like 'a configurable linter at the unit level'


it 'has a name' do
Expand Down Expand Up @@ -139,6 +140,78 @@

end


describe 'configuration' do

context 'with no configuration' do

let(:default_step_threshhold) { 10 }

context 'because configuration never happened' do

let(:unconfigured_test_model) do
model = CukeLinter::ModelFactory.send("generate_#{model_type}_model")
model.steps = []
(default_step_threshhold + 1).times { model.steps << :a_step }

model
end

it 'defaults to a step threshold of 10 steps' do
results = subject.lint(unconfigured_test_model)

expect(results.first[:problem]).to match(/^Test has too many steps. #{unconfigured_test_model.steps.count} steps found \(max 10\)/)
end

end

context 'because configuration did not set a step threshold' do
let(:configuration) { {} }
let(:configured_test_model) do
model = CukeLinter::ModelFactory.send("generate_#{model_type}_model")
model.steps = []
(default_step_threshhold + 1).times { model.steps << :a_step }

subject.configure(configuration)

model
end

it 'defaults to a step threshold of 10 steps' do
results = subject.lint(configured_test_model)

expect(results.first[:problem]).to match(/^Test has too many steps. #{configured_test_model.steps.count} steps found \(max 10\)/)
end

end

end


context 'with configuration' do

let(:step_threshhold) { 3 }
let(:configuration) { { 'StepThreshold' => step_threshhold } }
let(:configured_test_model) do
model = CukeLinter::ModelFactory.send("generate_#{model_type}_model")
model.steps = []
(step_threshhold + 1).times { model.steps << :a_step }

subject.configure(configuration)

model
end

it 'the step threshold used is the configured value' do
results = subject.lint(configured_test_model)

expect(results.first[:problem]).to match(/^Test has too many steps. #{configured_test_model.steps.count} steps found \(max #{step_threshhold}\)/)
end

end

end

end

context 'a non-test model' do
Expand Down

0 comments on commit 59f4b55

Please sign in to comment.