Skip to content

Commit fa743c6

Browse files
committed
fix: Allow saving snapshots to sub-directories if slash is a part of the name
1 parent 559ce7e commit fa743c6

File tree

4 files changed

+19
-17
lines changed

4 files changed

+19
-17
lines changed

Gruntfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ module.exports = function (grunt) {
128128
doctype: ''
129129
},
130130
url: 'http://localhost:8881/test/pages/no-doctype.html',
131-
file: 'no-doctype'
131+
file: 'directory/no-doctype'
132132
},
133133
{
134134
options: {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
"grunt-standard": "^3.1.0",
5252
"nsp": "^3.2.1",
5353
"phantomjs-prebuilt": "^2.1.16",
54-
"semantic-release": "^15.4.0",
54+
"semantic-release": "^15.4.1",
5555
"travis-deploy-once": "^5.0.0"
5656
},
5757
"peerDependencies": {

tasks/html-dom-snapshot.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
'use strict'
1111

12-
const fs = require('fs')
12+
const {writeFile} = require('fs')
1313
const pad = require('pad-left')
14-
const path = require('path')
14+
const {dirname, isAbsolute, join} = require('path')
1515
const mkdirp = require('mkdirp')
1616
const nodeCleanup = require('node-cleanup')
1717
const instructions = [
@@ -138,9 +138,9 @@ module.exports = grunt => {
138138
.reduce((scenarios, scenario) =>
139139
scenarios.concat(grunt.file.expand(scenario)), [])
140140
.reduce((scenarios, scenario) => {
141-
grunt.verbose.writeln('Load scenario "' + scenario + '".')
142-
if (!path.isAbsolute(scenario)) {
143-
scenario = path.join(currentDirectory, scenario)
141+
grunt.log.ok('Load scenario "' + scenario + '".')
142+
if (!isAbsolute(scenario)) {
143+
scenario = join(currentDirectory, scenario)
144144
}
145145
return scenarios.concat(require(scenario))
146146
}, commands || [])
@@ -263,15 +263,16 @@ module.exports = grunt => {
263263
if (file) {
264264
let fileName = file.toLowerCase()
265265
fileName = fileName.endsWith('.html') ||
266-
fileName.endsWith('.htm') ? file : file + '.html'
266+
fileName.endsWith('.htm') ? file : file + '.html'
267267
if (fileNumbering) {
268268
fileName = numberFileName(fileName)
269269
}
270-
fileName = path.join(snapshots, fileName)
270+
fileName = join(snapshots, fileName)
271271
grunt.log.ok('Write snapshot to "' + fileName + '".')
272-
return ensureDirectory(snapshots)
272+
const directory = dirname(fileName)
273+
return ensureDirectory(directory)
273274
.then(() => new Promise((resolve, reject) =>
274-
fs.writeFile(fileName, commandOptions.doctype + html,
275+
writeFile(fileName, commandOptions.doctype + html,
275276
error => {
276277
if (error) {
277278
reject(error)
@@ -294,11 +295,12 @@ module.exports = grunt => {
294295
if (fileNumbering) {
295296
fileName = numberFileName(fileName)
296297
}
297-
fileName = path.join(screenshots, fileName + '.png')
298+
fileName = join(screenshots, fileName + '.png')
298299
grunt.log.ok('Write screenshot to "' + fileName + '".')
299-
return ensureDirectory(screenshots)
300+
const directory = dirname(fileName)
301+
return ensureDirectory(directory)
300302
.then(() => new Promise((resolve, reject) =>
301-
fs.writeFile(fileName, Buffer.from(png.value, 'base64'),
303+
writeFile(fileName, Buffer.from(png.value, 'base64'),
302304
error => {
303305
if (error) {
304306
reject(error)

test/test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
const fs = require('fs')
44

5-
function readPages (name) {
5+
function readPages (name, name2) {
66
return {
77
expected: readPage('pages/' + name),
8-
actual: readPage('snapshots/' + name)
8+
actual: readPage('snapshots/' + (name2 || name))
99
}
1010
}
1111

@@ -68,7 +68,7 @@ exports['html-dom-snapshot'] = {
6868
},
6969

7070
'no-doctype': function (test) {
71-
const pages = readPages('no-doctype.html')
71+
const pages = readPages('no-doctype.html', 'directory/no-doctype.html')
7272
test.equal(pages.expected, pages.actual, 'no-doctype.html')
7373
test.done()
7474
},

0 commit comments

Comments
 (0)