Class: GherkinLint::GherkinLint

Inherits:
Object
  • Object
show all
Defined in:
lib/gherkin_lint.rb

Overview

gherkin linter

Constant Summary collapse

LINTER =
[
  AvoidOutlineForSingleExample,
  AvoidPeriod,
  AvoidScripting,
  BackgroundDoesMoreThanSetup,
  BackgroundRequiresMultipleScenarios,
  BadScenarioName,
  BeDeclarative,
  FileNameDiffersFeatureName,
  MissingExampleName,
  MissingFeatureDescription,
  MissingFeatureName,
  MissingScenarioName,
  MissingTestAction,
  MissingVerification,
  InvalidFileName,
  InvalidStepFlow,
  SameTagForAllScenarios,
  TooClumsy,
  TooManyDifferentTags,
  TooManySteps,
  TooManyTags,
  TooLongStep,
  UniqueScenarioNames,
  UnknownVariable,
  UnusedVariable,
  UseBackground,
  UseOutline
].freeze

Instance Method Summary collapse

Constructor Details

#initializeGherkinLint

Returns a new instance of GherkinLint.



69
70
71
72
73
# File 'lib/gherkin_lint.rb', line 69

def initialize
  @files = {}
  @linter = []
  enable_all
end

Instance Method Details

#analyze(file) ⇒ Object



100
101
102
# File 'lib/gherkin_lint.rb', line 100

def analyze(file)
  @files[file] = parse file
end

#disable(disabled_linter) ⇒ Object



83
84
85
# File 'lib/gherkin_lint.rb', line 83

def disable(disabled_linter)
  set_linter(LINTER.map { |linter| linter.new.name.split('::').last }, disabled_linter)
end

#disable_tagsObject



121
122
123
# File 'lib/gherkin_lint.rb', line 121

def disable_tags
  LINTER.map { |lint| "disable#{lint.new.class.name.split('::').last}" }
end

#enable(enabled_linter) ⇒ Object



79
80
81
# File 'lib/gherkin_lint.rb', line 79

def enable(enabled_linter)
  set_linter(enabled_linter)
end

#enable_allObject



75
76
77
# File 'lib/gherkin_lint.rb', line 75

def enable_all
  disable []
end

#parse(file) ⇒ Object



104
105
106
107
# File 'lib/gherkin_lint.rb', line 104

def parse(file)
  content = File.read file
  to_json(content, file)
end


134
135
136
137
# File 'lib/gherkin_lint.rb', line 134

def print(issues)
  puts "There are #{issues.length} Issues" unless issues.empty?
  issues.each { |issue| puts issue }
end

#reportObject



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/gherkin_lint.rb', line 109

def report
  issues = @linter.map do |linter|
    linter.lint_files(@files, disable_tags)
    linter.issues
  end.flatten

  issues.each { |issue| puts issue.render }

  return 0 if issues.select { |issue| issue.class == Error }.empty?
  -1
end

#set_linter(enabled_linter, disabled_linter = []) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/gherkin_lint.rb', line 87

def set_linter(enabled_linter, disabled_linter = [])
  @linter = []
  enabled_linter = Set.new enabled_linter
  disabled_linter = Set.new disabled_linter
  LINTER.each do |linter|
    new_linter = linter.new
    name = new_linter.class.name.split('::').last
    next unless enabled_linter.include? name
    next if disabled_linter.include? name
    @linter.push new_linter
  end
end

#to_json(input, file = 'generated.feature') ⇒ Object



125
126
127
128
129
130
131
132
# File 'lib/gherkin_lint.rb', line 125

def to_json(input, file = 'generated.feature')
  io = StringIO.new
  formatter = Gherkin::Formatter::JSONFormatter.new(io)
  parser = Gherkin::Parser::Parser.new(formatter, true)
  parser.parse(input, file, 0)
  formatter.done
  MultiJson.load io.string
end