jnicklas/turnip — pull request #141 by gongo (2014-09-09 12:35:15+00:00)
Title: Failure/Error message is wrong (fix backtrace)

## Motivation

There is wrong with execution result of the current example.

```
$ bundle exec rspec examples

(snip...)

Failures:

1) Ambiguous This is an ambiguous feature Given this is ambiguous
Failure/Error: Unable to find matching line in /path/to/jnicklas/turnip/examples/ambiguous.feature
Turnip::Ambiguous:
Ambiguous step definitions
- "this is ambiguous" (/path/to/jnicklas/turnip/lib/turnip/dsl.rb:10:in `step')
- "this is :ambiguous" (/path/to/jnicklas/turnip/lib/turnip/dsl.rb:10:in `step')
# ./lib/turnip/execute.rb:22:in `step'
# ./lib/turnip/rspec.rb:44:in `run_step'
# ./examples/ambiguous.feature:4:in `block (6 levels) in run'
# ./examples/ambiguous.feature:3:in `each'
# ./examples/ambiguous.feature:3:in `block (5 levels) in run'
# ./examples/ambiguous.feature:3:in `this is ambiguous'

2) raises errors Step raises error When raise error
Failure/Error: Scenario: Incorrect expectation
RuntimeError:
foobar
# ./examples/steps/steps.rb:107:in `block in '
# ./lib/turnip/execute.rb:25:in `step'
# ./lib/turnip/rspec.rb:44:in `run_step'
# ./examples/errors.feature:6:in `block (6 levels) in run'
# ./examples/errors.feature:5:in `each'
# ./examples/errors.feature:5:in `block (5 levels) in run'
# ./examples/errors.feature:5:in `raise error'

3) raises errors Incorrect expectation Given there is a monster -> Then it should die
Failure/Error: Then it should die

expected: 0
got: 1

(compared using ==)
# ./examples/steps/steps.rb:14:in `block in '
# ./lib/turnip/execute.rb:25:in `step'
# ./lib/turnip/rspec.rb:44:in `run_step'
# ./examples/errors.feature:8:in `block (6 levels) in run'
# ./examples/errors.feature:7:in `each'
# ./examples/errors.feature:7:in `block (5 levels) in run'
# ./examples/errors.feature:8:in `it should die'

4) raises errors Step missing and raise_error_for_unimplemented_steps is set When a step just does not exist
Failure/Error: Unable to find matching line in /path/to/jnicklas/turnip/examples/errors.feature
Turnip::Pending:
a step just does not exist
# ./lib/turnip/execute.rb:17:in `step'
# ./lib/turnip/rspec.rb:44:in `run_step'
# ./examples/errors.feature:12:in `block (6 levels) in run'
# ./examples/errors.feature:11:in `each'
# ./examples/errors.feature:11:in `block (5 levels) in run'
# ./examples/errors.feature:11:in `a step just does not exist'

(snip...)
```

`Failure/Error` message of..

- `1)` and `4)` -> `Unable to find matching line`
- `2)` -> `Scenario: Incorrect expectation` but `2)` scenario is `Step raises error`

## Causing

Current backtrace `2)`:

```
# ./examples/steps/steps.rb:107:in `block in '
# ./lib/turnip/execute.rb:25:in `step'
# ./lib/turnip/rspec.rb:44:in `run_step'
# ./examples/errors.feature:6:in `block (6 levels) in run'
# ./examples/errors.feature:5:in `each'
# ./examples/errors.feature:5:in `block (5 levels) in run'
# ./examples/errors.feature:5:in `raise error'
```

RSpec has been obtained `Failure/Error` string from the first line that matches with filename which the error occurred.

See also:

- [v3.1.0/rspec/core/notifications.rb#L278-L284](https://github.com/rspec/rspec-core/blob/v3.1.0/lib/rspec/core/notifications.rb#L278-L284)
- [v2.14.8/rspec/core/formatters/base_formatter.rb#L207-L213](https://github.com/rspec/rspec-core/blob/v2.14.8/lib/rspec/core/formatters/base_formatter.rb#L207-L213)

In the present case, 4 line are selected. And 4 line is [instance_eval line](https://github.com/jnicklas/turnip/blob/v1.2.2/lib/turnip/rspec.rb#L85-L88).

## Solution

New backtrace `2)`:

```
2) raises errors Step raises error When raise error
Failure/Error: When raise error <= correct !!
RuntimeError:
foobar
# ./examples/errors.feature:5:in `raise error' <= Add new line.
# ./examples/steps/steps.rb:107:in `block in '
# ./lib/turnip/execute.rb:25:in `step'
# ./lib/turnip/rspec.rb:44:in `run_step'
# ./examples/errors.feature:6:in `block (6 levels) in run'
# ./examples/errors.feature:5:in `each'
# ./examples/errors.feature:5:in `block (5 levels) in run'
```

### Question

@jnicklas Do you remember the reason for using `push` instead of `unshift` ? https://github.com/jnicklas/turnip/commit/6fd445733696ab7ec551c0a1079e98794b9422f1
I think another way if there is a reason you.