Skip to content

Commit

Permalink
test: refactor tests (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Jan 16, 2020
1 parent 4a47946 commit 369760c
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 71 deletions.
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,28 @@
"lint:package-json": "yarn run format:package-json --check",
"lint:prettier": "prettier \"**/*.{css,html,js,json,less,md,scss,ts,vue,yaml,yml}\" --check",
"release": "run-s lint test format dist",
"test": "node test.js",
"test": "ava",
"update": "node -r esm scripts/update.js"
},
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
},
"ava": {
"require": [
"esm"
],
"verbose": true
},
"devDependencies": {
"@commitlint/cli": "8.3.5",
"@fisker/commitlint-config": "1.1.4",
"@fisker/eslint-config": "1.6.0",
"@fisker/husky-config": "1.1.5",
"@fisker/lint-staged-config": "1.0.8",
"@fisker/prettier-config": "1.0.24",
"ava": "2.4.0",
"cheerio": "1.0.0-rc.3",
"cz-conventional-changelog": "3.0.2",
"eslint": "6.8.0",
Expand Down
53 changes: 53 additions & 0 deletions scripts/fetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import got from 'got'
import cheerio from 'cheerio'

const GIT_DOCUMENTATION_URL = 'https://git-scm.com/docs/githooks'
const cheerioObjectToArray = elements =>
Array.from(elements).map(element => cheerio(element))

async function fetchData() {
const {body: html} = await got(GIT_DOCUMENTATION_URL)
const sections = cheerioObjectToArray(cheerio.load(html)('.sect1')).reduce(
(sections, section) => {
const anchor = section.find('a.anchor')
const parent = anchor.parent()
const id = parent.attr('id').replace(/^_/, '')

if (anchor.attr('href') !== `#_${id}`) {
return sections
}

const body = section.find('.sectionbody')
sections[id] = cheerio(body)
return sections
},
{}
)

const hooks = cheerioObjectToArray(sections.hooks.find('.sect2'))
.map(section => {
const anchor = section.find('a.anchor').eq(0)
const parent = anchor.parent()
const id = parent.attr('id')

if (anchor.attr('href') !== `#${id}`) {
return null
}

const hook = parent.text().trim()

return {
hook,
description: cheerioObjectToArray(section.find('.paragraph'))
.map(paragraph => paragraph.text())
.join('\n')
.replace(/\n+/g, '\n')
.trim(),
}
})
.filter(Boolean)

return hooks.map(({hook}) => hook)
}

export default fetchData
57 changes: 3 additions & 54 deletions scripts/update.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,11 @@
import got from 'got'
import cheerio from 'cheerio'
import writePrettierFile from 'write-prettier-file'
import path from 'path'
import fetch from './fetch'

const JSON_FILE = path.join(__dirname, '../index.json')
const GIT_DOCUMENTATION_URL = 'https://git-scm.com/docs/githooks'
const cheerioObjectToArray = elements =>
Array.from(elements).map(element => cheerio(element))

;(async () => {
const {body: html} = await got(GIT_DOCUMENTATION_URL)
const data = await fetch()

const sections = cheerioObjectToArray(cheerio.load(html)('.sect1')).reduce(
(sections, section) => {
const anchor = section.find('a.anchor')
const parent = anchor.parent()
const id = parent.attr('id').replace(/^_/, '')

if (anchor.attr('href') !== `#_${id}`) {
return sections
}

const body = section.find('.sectionbody')
sections[id] = cheerio(body)
return sections
},
{}
)

const hooks = cheerioObjectToArray(sections.hooks.find('.sect2'))
.map(section => {
const anchor = section.find('a.anchor').eq(0)
const parent = anchor.parent()
const id = parent.attr('id')

if (anchor.attr('href') !== `#${id}`) {
return null
}

const hook = parent.text().trim()

return {
hook,
description: cheerioObjectToArray(section.find('.paragraph'))
.map(paragraph => paragraph.text())
.join('\n')
.replace(/\n+/g, '\n')
.trim(),
}
})
.filter(Boolean)

writePrettierFile(
JSON_FILE,
JSON.stringify(
hooks.map(({hook}) => hook),
null,
2
)
)
writePrettierFile(JSON_FILE, JSON.stringify(data, null, 2))
})()
46 changes: 30 additions & 16 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
const assert = require('assert')
const fs = require('fs')
const hooks = require('.')
import test from 'ava'
import fs from 'fs'
import hooks from './index.json'
import fetch from './scripts/fetch'

assert.strict.ok(Array.isArray(hooks), 'Git hooks should be an array.')
assert.strict.ok(hooks.length > 0, 'Git hooks should not be empty.')
assert.strict.ok(
hooks.includes('commit-msg'),
`Git hooks should has \`commit-msg\``
)
test('main', t => {
t.true(Array.isArray(hooks), 'Git hooks should be an array.')
t.true(hooks.length > 0, 'Git hooks should not be empty.')
t.true(hooks.includes('commit-msg'), `Git hooks should has \`commit-msg\`.`)
})

const hooksInRepository = [
...new Set(fs.readdirSync('.git/hooks').filter(file => !file.includes('.'))),
]
test('.git/hooks', t => {
const hooksInRepository = [
...new Set(
fs.readdirSync('.git/hooks').filter(file => !file.includes('.'))
),
]
const set = new Set(hooks)

const set = new Set(hooks)
for (const hook of hooksInRepository) {
assert.strict.ok(set.has(hook), `Git hooks should has ${hook}`)
}
for (const hook of hooksInRepository) {
t.true(set.has(hook), `Git hooks should has ${hook}.`)
}
})

test('git-scm.com', async t => {
const data = await fetch()

t.deepEqual(
hooks,
data,
`Git hooks should has be same as data from git-scm.com .`
)
})

0 comments on commit 369760c

Please sign in to comment.