Class: Cucumber::FeatureFile

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

Defined Under Namespace

Modules: FixRuby21Bug9285

Constant Summary collapse

FILE_COLON_LINE_PATTERN =

:nodoc:

/^([\w\W]*?):([\d:]+)$/
DEFAULT_ENCODING =

:nodoc:

"UTF-8"
COMMENT_OR_EMPTY_LINE_PATTERN =

:nodoc:

/^\s*#|^\s*$/
ENCODING_PATTERN =

:nodoc:

/^\s*#\s*encoding\s*:\s*([^\s]+)/

Instance Method Summary collapse

Constructor Details

#initialize(uri, source = nil) ⇒ FeatureFile

The uri argument is the location of the source. It can be a path or a path:line1:line2 etc. If source is passed, uri is ignored.



15
16
17
18
19
20
21
22
23
# File 'lib/cucumber/feature_file.rb', line 15

def initialize(uri, source=nil)
  @source = source
  _, @path, @lines = *FILE_COLON_LINE_PATTERN.match(uri)
  if @path
    @lines = @lines.split(':').map { |line| line.to_i }
  else
    @path = uri
  end
end

Instance Method Details

#parse(configuration_filters, tag_counts) ⇒ Object

Parses a file and returns a Cucumber::Ast::Feature If configuration_filters contains any filters, the result will be filtered.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/cucumber/feature_file.rb', line 28

def parse(configuration_filters, tag_counts)
  filters = @lines || configuration_filters

  builder             = Cucumber::Parser::GherkinBuilder.new(@path)
  filter_formatter    = filters.empty? ? builder : Gherkin::Formatter::FilterFormatter.new(builder, filters)
  tag_count_formatter = Gherkin::Formatter::TagCountFormatter.new(filter_formatter, tag_counts)
  parser              = Gherkin::Parser::Parser.new(tag_count_formatter, true, "root", false)

  begin
    parser.parse(source, @path, 0)
    builder.language = parser.i18n_language
    builder.result
  rescue Gherkin::Lexer::LexingError, Gherkin::Parser::ParseError => e
    e.message.insert(0, "#{@path}: ")
    raise e
  end
end

#sourceObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/cucumber/feature_file.rb', line 52

def source
  @source ||= if @path =~ /^http/
    require 'open-uri'
    open(@path).read
  else
    begin
      source = File.open(@path, Cucumber.file_mode('r', DEFAULT_ENCODING)).read
      encoding = encoding_for(source)
      if(DEFAULT_ENCODING.downcase != encoding.downcase)
        # Read the file again - it's explicitly declaring a different encoding
        source = File.open(@path, Cucumber.file_mode('r', encoding)).read
        source = to_default_encoding(source, encoding)
      end
      source
    rescue Errno::EACCES => e
      e.message << "\nCouldn't open #{File.expand_path(@path)}"
      raise e
    rescue Errno::ENOENT => e
      # special-case opening features, because this could be a new user:
      e.message << ". Please create a #{@path} directory to get started."
      e.extend FixRuby21Bug9285 if Cucumber::RUBY_2_1
      raise e
    end
  end
end