Skip to content

Commit

Permalink
fix issues with absolute paths (#2063)
Browse files Browse the repository at this point in the history
* add failing scenario

* convert to relative path before adding to map

* refactor test

* update CHANGELOG.md
  • Loading branch information
davidjgoss committed Jun 29, 2022
1 parent 1da05aa commit 140fc02
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 56 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
Please see [CONTRIBUTING.md](https://github.com/cucumber/cucumber/blob/master/CONTRIBUTING.md) on how to contribute to Cucumber.

## [Unreleased]
### Fixed
- Fix issues with using absolute paths for features ([#2063](https://github.com/cucumber/cucumber-js/pull/2063))

## [8.3.1] - 2022-06-21
### Fixed
Expand Down
5 changes: 5 additions & 0 deletions features/target_specific_scenarios_by_line.feature
Expand Up @@ -49,3 +49,8 @@ Feature: Target specific scenarios
| args |
| features/a.feature:2:10 |
| features/a.feature:2 features/a.feature:10 |

Scenario: using absolute paths
When I run cucumber-js with `{{{tmpDir}}}/features/a.feature:2`
Then it fails
And it runs the scenario "first scenario"
10 changes: 9 additions & 1 deletion src/pickle_filter.ts
Expand Up @@ -56,20 +56,28 @@ export class PickleLineFilter {

constructor(cwd: string, featurePaths: string[] = []) {
this.featureUriToLinesMapping = this.getFeatureUriToLinesMapping({
cwd,
featurePaths,
})
}

getFeatureUriToLinesMapping({
cwd,
featurePaths,
}: {
cwd: string
featurePaths: string[]
}): Record<string, number[]> {
const mapping: Record<string, number[]> = {}
featurePaths.forEach((featurePath) => {
const match = FEATURE_LINENUM_REGEXP.exec(featurePath)
if (doesHaveValue(match)) {
const uri = path.normalize(match[1])
let uri = match[1]
if (path.isAbsolute(uri)) {
uri = path.relative(cwd, uri)
} else {
uri = path.normalize(uri)
}
const linesExpression = match[2]
if (doesHaveValue(linesExpression)) {
if (doesNotHaveValue(mapping[uri])) {
Expand Down
131 changes: 76 additions & 55 deletions src/pickle_filter_spec.ts
@@ -1,5 +1,6 @@
import { beforeEach, describe, it } from 'mocha'
import { expect } from 'chai'
import path from 'path'
import PickleFilter from './pickle_filter'
import { parse } from '../test/gherkin_helpers'

Expand Down Expand Up @@ -37,67 +38,87 @@ describe('PickleFilter', () => {
})

describe('line filters', () => {
beforeEach(function () {
pickleFilter = new PickleFilter({
cwd,
const variants = [
{
name: 'with relative paths',
featurePaths: ['features/a.feature', 'features/b.feature:2:4'],
names: [],
tagExpression: '',
})
})

describe('pickle in feature without line specified', () => {
it('returns true', async function () {
// Arrange
const {
pickles: [pickle],
gherkinDocument,
} = await parse({
data: ['Feature: a', 'Scenario: b', 'Given a step'].join('\n'),
uri: 'features/a.feature',
},
{
name: 'with absolute paths',
featurePaths: [
path.join(cwd, 'features/a.feature'),
path.join(cwd, 'features/b.feature:2:4'),
],
},
]

variants.forEach(({ name, featurePaths }) => {
describe(name, () => {
beforeEach(function () {
pickleFilter = new PickleFilter({
cwd,
featurePaths,
names: [],
tagExpression: '',
})
})

// Act
const result = pickleFilter.matches({ pickle, gherkinDocument })

// Assert
expect(result).to.eql(true)
})
})

describe('pickle in feature with line specified', () => {
it('returns true if pickle line matches', async function () {
// Arrange
const {
pickles: [pickle],
gherkinDocument,
} = await parse({
data: ['Feature: a', 'Scenario: b', 'Given a step'].join('\n'),
uri: 'features/b.feature',
describe('pickle in feature without line specified', () => {
it('returns true', async function () {
// Arrange
const {
pickles: [pickle],
gherkinDocument,
} = await parse({
data: ['Feature: a', 'Scenario: b', 'Given a step'].join('\n'),
uri: 'features/a.feature',
})

// Act
const result = pickleFilter.matches({ pickle, gherkinDocument })

// Assert
expect(result).to.eql(true)
})
})

// Act
const result = pickleFilter.matches({ pickle, gherkinDocument })

// Assert
expect(result).to.eql(true)
})

it('returns false if pickle line does not match', async function () {
// Arrange
const {
pickles: [pickle],
gherkinDocument,
} = await parse({
data: ['Feature: a', '', 'Scenario: b', 'Given a step'].join('\n'),
uri: 'features/b.feature',
describe('pickle in feature with line specified', () => {
it('returns true if pickle line matches', async function () {
// Arrange
const {
pickles: [pickle],
gherkinDocument,
} = await parse({
data: ['Feature: a', 'Scenario: b', 'Given a step'].join('\n'),
uri: 'features/b.feature',
})

// Act
const result = pickleFilter.matches({ pickle, gherkinDocument })

// Assert
expect(result).to.eql(true)
})

it('returns false if pickle line does not match', async function () {
// Arrange
const {
pickles: [pickle],
gherkinDocument,
} = await parse({
data: ['Feature: a', '', 'Scenario: b', 'Given a step'].join(
'\n'
),
uri: 'features/b.feature',
})

// Act
const result = pickleFilter.matches({ pickle, gherkinDocument })

// Assert
expect(result).to.eql(false)
})
})

// Act
const result = pickleFilter.matches({ pickle, gherkinDocument })

// Assert
expect(result).to.eql(false)
})
})
})
Expand Down

0 comments on commit 140fc02

Please sign in to comment.