Skip to content

Commit

Permalink
Merge pull request #2 from mouafa/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
mouafa committed Oct 26, 2018
2 parents 9387de5 + 7a242e6 commit 5c527b2
Show file tree
Hide file tree
Showing 23 changed files with 906 additions and 106 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ yarn-debug.log*
yarn-error.log*

shots/
__shots__/
ignore/
service/
*.ignore.*
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,9 @@ firn http://example.com/
```js
const firn = require('firn')

firn('http://example.com/')
;(async () => {
const [err, raport] = await firn(urls, config)
if (err) log('😦 Oh No! ', err)
log('📝 raport ', raport)
})()
```
12 changes: 12 additions & 0 deletions __mocks__/browser.mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const page = {
setViewport: jest.fn(),
goto: jest.fn(),
screenshot: jest.fn(() => Buffer.from('mock')),
close: jest.fn()
}

const browser = {
newPage: jest.fn(() => page)
}

module.exports = browser
1 change: 1 addition & 0 deletions __mocks__/make-dir.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = jest.fn()
9 changes: 9 additions & 0 deletions __mocks__/royax.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const royax = jest.fn(x => {
throw new Error(`\n ${x} NOT MOCKED`)
})

royax.mockResult = data => {
royax.mockImplementationOnce(() => data)
}

module.exports = royax
15 changes: 5 additions & 10 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,14 @@ program
.option('-c, --config [path]', 'Remove recursively')
.option('-u, --update', 'Update specefic Shots')
.option('-a, --all', 'Update all Shots')
.action(function(url, config) {
.action(async function(url, config) {
if (!url.startsWith('http')) url = `https://${url}`

console.log('url ', url)
console.log('config ', program.config)
firn(url)
.then(e => {
log('👍 ALL GOOD')
})
.catch(e => {
log('😦 Oh No! ', e)
process.exit(1)
})
// console.log('config ', program.config)
const [err, raport] = await firn(url)
if (err) log('😦 Oh No! ', err)
log('📝 raport ', raport)
})

program.parse(process.argv)
8 changes: 8 additions & 0 deletions jest-setup.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
const browser = require('./__mocks__/browser.mock')
const { getConfig } = require('./src/config')
// const consoleLog = console.log
console.log = jest.fn()

/* start browser mock */

global.browser = browser
global.config = getConfig()
/* end browser mock */
13 changes: 9 additions & 4 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
const puppeteer = require('puppeteer')
const promx = require('promx')
/* local */
const { mergeConfig } = require('./src/config')

const log = require('./src/lib/log')
const { check } = require('./src/lib/check')
const { mergeConfig } = require('./src/config')
const raport = require('./src/lib/raport')

async function main(urls, config) {
mergeConfig(config)
global.config = mergeConfig(config)
await boot()
await check(urls)
const [err] = await promx(check(urls))
await teardown()
return [err, raport.get()]
}

async function boot() {
Expand All @@ -19,7 +23,8 @@ async function boot() {

async function teardown() {
log('👋 firn')
global.browser.close()
await global.browser.close()
global.browser = null
}

module.exports = main
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "firn",
"version": "0.1.0",
"version": "0.2.2",
"description": "visual regression test tool",
"main": "main.js",
"author": "mouafa",
Expand All @@ -15,9 +15,9 @@
"commander": "2.17.1",
"listr": "0.14.1",
"make-dir": "1.3.0",
"pixelmatch": "4.0.2",
"promx": "1.0.1",
"puppeteer": "1.7.0",
"royax": "0.1.1"
"royax": "1.0.0"
},
"devDependencies": {
"coveralls": "3.0.2",
Expand Down
17 changes: 17 additions & 0 deletions src/config/default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const path = require('path')
/* local */

const viewports = require('./viewport')

const resolve = pth => path.resolve(__dirname, '../../', pth)

const config = {
viewports,
screenshotExt: 'png',
tmpShotPath: resolve('.cache'),
legitShotPath: resolve('__shots__/legit'),
fishyShotPath: resolve('__shots__/fishy'),
diffShotPath: resolve('__shots__/diff')
}

module.exports = config
20 changes: 5 additions & 15 deletions src/config/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
const path = require('path')
/* local */
const viewports = require('./viewport')
const defaultConfig = require('./default')

const resolve = pth => path.resolve(__dirname, '../../', pth)
let usedConfig = { ...defaultConfig }

const defaultConfig = {
viewports,
screenshotExt: 'png',
legitShotPath: resolve('shots/legit'),
pendingShotPath: resolve('.cache'),
fishyShotPath: resolve('shots/fishy')
}

let usedConfig = defaultConfig

function mergeConfig(customConfig) {
usedConfig = { ...defaultConfig, ...customConfig }
function mergeConfig(userConfig) {
usedConfig = { ...defaultConfig, ...userConfig }
return usedConfig
}

function getConfig() {
Expand Down
30 changes: 27 additions & 3 deletions src/lib/check.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,46 @@
const Listr = require('listr')
const promx = require('promx')

const Shot = require('./shot')
const Page = require('./page')
const log = require('./log')
const { arrayArg } = require('./util')

const Taskr = require('./taskr')

const listrOption = { concurrent: true, exitOnError: false, renderer: 'default' }
// async function check(urls) {
// urls = arrayArg(urls)
// return Promise.all(urls.map(checkPage))
// }

async function check(urls) {
urls = arrayArg(urls)
return Promise.all(urls.map(checkPage))
const tasks = urls.map(url => {
return {
title: url,
task() {
return checkPage(url)
}
}
})

return new Listr(tasks, listrOption).run()
}

async function checkPage(url) {
const page = new Page(url)
await page.runTasks(verifier)
const tasks = page.getTasks(verifier)
return new Listr(tasks, listrOption)
// await page.runTasks(verifier)
}

async function verifier([url, viewport, suffix]) {
const shot = new Shot(url, viewport, suffix)
const match = await shot.check()
log('match', match, url, suffix)
// console.log('verifier', match)

return match
}

module.exports = {
Expand Down
1 change: 0 additions & 1 deletion src/lib/compare.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const royax = require('royax')

async function compare(img1, img2, writeTo) {
const [err, result] = await royax(img1, img2, writeTo)
// console.log('result', result)
if (err) return [err]
const match = result.match === 1
return [null, match]
Expand Down
17 changes: 12 additions & 5 deletions src/lib/page.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const Listr = require('listr')

const config = require('../config').getConfig()
const viewports = config.viewports

Expand All @@ -15,16 +17,21 @@ class Page {

getTasks(cb) {
return this.set.map(options => {
return function task() {
return cb(options)
return {
title: `${options[2]}`,
task() {
return cb(options)
}
}
})
}

runTasks(cb) {
const tasks = this.getTasks(cb)
const promises = tasks.map(async task => await task())
return Promise.all(promises)
const tasks = new Listr(this.getTasks(cb), { concurrent: true, exitOnError: false })

// const promises = tasks.map(async task => await task())
// return Promise.all(promises)
return tasks.run()
}
}

Expand Down
20 changes: 20 additions & 0 deletions src/lib/raport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const raport = {}

function add({ url, suffix }, data) {
if (!raport[url]) raport[url] = {}
raport[url][suffix] = data
return raport
}

function get() {
return raport
}

function isObject(object) {
return object !== null && typeof object === 'object'
}

module.exports = {
add,
get
}
Loading

0 comments on commit 5c527b2

Please sign in to comment.