Skip to content

CLI: normalize_example_lines raises NoMethodError when @example tag has no body #15

Description

@jcouball

Summary

normalize_example_lines calls .gsub directly on text without guarding against nil. If a YARD @example tag has no body (e.g. # @example with no indented content following it), tag.text is nil and the call raises NoMethodError: undefined method 'gsub' for nil. This crashes the entire test-examples pipeline.

Location

lib/yard/cli/test_examples.rb — normalize_example_lines

def normalize_example_lines(text)
  text = text.gsub('# =>', '#=>')
  text = text.gsub('#=>', "\n#=>")
  text.split("\n").map(&:strip).reject(&:empty?)
end

Called from extract_expectations, which is called from build_spec:

spec.expectations = extract_expectations(example)

When can text be nil?

A bare @example tag with no indented content:

# @example
def some_method
  # ...
end

YARD parses this as a tag with text = nil.

Also possible with a titled-but-empty example:

# @example Some title
def some_method
  # ...
end

Current behavior

NoMethodError: undefined method 'gsub' for nil
  from lib/yard/cli/test_examples.rb:…:in 'normalize_example_lines'

The crash aborts the entire run — no tests execute.

Proposed solution

Return an empty array early when text is nil or blank:

def normalize_example_lines(text)
  return [] if text.nil? || text.strip.empty?

  text = text.gsub('# =>', '#=>')
  text = text.gsub('#=>', "\n#=>")
  text.split("\n").map(&:strip).reject(&:empty?)
end

This produces an empty expectations array, which means the example evaluates with zero assertions — consistent with how a body-less example would behave if the text were just whitespace.

Acceptance criteria

  • @example tags with no body do not crash the runner
  • @example Some title tags with no body do not crash the runner
  • The empty-body example is reported as a passing test with 0 assertions
  • All existing cucumber scenarios continue to pass

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions