Skip to content

Commit

Permalink
wdio-reporter: create outputDir directory if does not exist (#3510)
Browse files Browse the repository at this point in the history
## Proposed changes

[//]: # (Describe the big picture of your changes here to communicate to the maintainers why we should accept this pull request. If it fixes a bug or resolves a feature request, be sure to link to that issue.)

This change adds a check in wdio-reporter that will create the output directory if it's defined but doesn't exist on the system. This addresses #3389

## Types of changes

[//]: # (What types of changes does your code introduce to WebdriverIO?)
[//]: # (_Put an `x` in the boxes that apply_)

- [ ] Bugfix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)

## Checklist

[//]: # (_Put an `x` in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code._)

- [x] I have read the [CONTRIBUTING](https://github.com/webdriverio/webdriverio/blob/master/CONTRIBUTING.md) doc
- [x] I have added tests that prove my fix is effective or that my feature works
- [ ] I have added necessary documentation (if appropriate)

## Further comments

[//]: # (If this is a relatively large or complex change, kick off the discussion by explaining why you chose the solution you did and what alternatives you considered, etc...)

I don't think this really needs a documentation change, but can add something if preferred.

### Reviewers: @webdriverio/technical-committee
  • Loading branch information
klamping authored and christian-bromann committed Feb 6, 2019
1 parent 5b67644 commit 290e453
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
10 changes: 8 additions & 2 deletions packages/wdio-allure-reporter/src/index.js
Expand Up @@ -6,10 +6,16 @@ import {events, stepStatuses, testStatuses} from './constants'

class AllureReporter extends WDIOReporter {
constructor(options) {
super(options)
const outputDir = options.outputDir || 'allure-results'

super({
...options,
outputDir
})
this.config = {}
this.allure = new Allure()
this.allure.setOptions({targetDir: options.outputDir || 'allure-results'})

this.allure.setOptions({targetDir: outputDir})
this.registerListeners()
}

Expand Down
3 changes: 3 additions & 0 deletions packages/wdio-reporter/package.json
Expand Up @@ -34,5 +34,8 @@
},
"publishConfig": {
"access": "public"
},
"dependencies": {
"fs-extra": "^7.0.1"
}
}
6 changes: 6 additions & 0 deletions packages/wdio-reporter/src/index.js
@@ -1,4 +1,5 @@
import fs from 'fs'
import fse from 'fs-extra'
import { format } from 'util'
import EventEmitter from 'events'

Expand Down Expand Up @@ -31,6 +32,11 @@ export default class WDIOReporter extends EventEmitter {
failures: 0
}

// ensure the report directory exists
if (this.options.outputDir) {
fse.ensureDirSync(this.options.outputDir)
}

let currentTest

const rootSuite = new SuiteStats({
Expand Down
28 changes: 28 additions & 0 deletions packages/wdio-reporter/tests/reporter.test.js
@@ -1,5 +1,7 @@
import fs from 'fs'
import fse from 'fs-extra'
import tmp from 'tmp'
import path from 'path'

import WDIOReporter from '../src'

Expand Down Expand Up @@ -71,4 +73,30 @@ describe('WDIOReporter', () => {
reporter.write('foobar')
expect(options.writeStream.write).toBeCalledWith('foobar')
})

it('should create directory if outputDir given and not existing', () => {
const outputDir = path.join(__dirname, `./tempDir-${Date.now()}`, 'multiLevel')

// ensure directory isn't somehow there to begin with
expect(fs.existsSync(outputDir)).toBe(false)

const options = { outputDir }
new WDIOReporter(options)

const dirExists = fs.existsSync(outputDir)

// remove directory before assertion so it doesn't pollute the test directory,
// even if the assertion fails
if (dirExists){
fse.removeSync(outputDir)
}

expect(dirExists).toBe(true)
})

it('should handle invalid directory for outputDir', () => {
expect(() => {
new WDIOReporter({ outputDir: true })
}).toThrow()
})
})

0 comments on commit 290e453

Please sign in to comment.