Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Commit

Permalink
feat: add tests for extract
Browse files Browse the repository at this point in the history
  • Loading branch information
fox1t committed Mar 11, 2020
1 parent 1494f8c commit bf15c94
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 23 deletions.
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"lint": "eslint \"src/**/*.{js,jsx,ts,tsx}\"",
"lint:fix": "eslint \"src/**/*.{js,jsx,ts,tsx}\"",
"test": "cross-env TS_NODE_PROJECT=./test/tsconfig.json tap test/**/*.ts",
"test:ci": "cross-env TS_NODE_PROJECT=./test/tsconfig.json tap --coverage-report=text-lcov test/**/*.ts",
"test:ci": "cross-env TS_NODE_PROJECT=./test/tsconfig.json tap --coverage-report=text-lcov test/**/*.ts | coveralls",
"lint-staged": "lint-staged",
"commitlint": "commitlint",
"semantic-release": "semantic-release"
Expand Down Expand Up @@ -49,6 +49,7 @@
"@types/mkdirp": "~1.0.0",
"@types/node": "~13.9.0",
"@types/require-from-string": "~1.2.0",
"@types/rimraf": "~2.0.3",
"@types/sade": "~1.6.0",
"@typescript-eslint/eslint-plugin": "~2.23.0",
"@typescript-eslint/parser": "~2.23.0",
Expand All @@ -65,10 +66,10 @@
"husky": "~4.2.0",
"lint-staged": "~10.0.7",
"prettier": "~1.19.1",
"rimraf": "~3.0.0",
"rimraf": "~3.0.2",
"semantic-release": "~17.0.4",
"tap": "~14.10.5",
"tap-mocha-reporter": "~5.0.0",
"semantic-release": "~17.0.4"
"tap-mocha-reporter": "~5.0.0"
},
"config": {
"commitizen": {
Expand Down
53 changes: 36 additions & 17 deletions src/ddoc/extract/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,23 @@ const DesignDocumentSchema = Type.Object({

type DesignDocument = Static<typeof DesignDocumentSchema>

export default async function(src: string, opts: { destination: string; format: string }) {
export default async function(
src: string,
opts: { destination: string; format: string; noVerbose?: boolean },
) {
const methods = new Map<string, string>()
try {
if (!FORMATS.includes(opts.format)) {
throw Error(`Wrong destination format provided ${opts.format}, must be one of ${FORMATS}`)
}
const srcPath = path.resolve(src)
const dbBackup = (await readFIle(srcPath)).toString()
console.log(`> ${chalk.bgBlueBright(chalk.black(' ddoc extract src '))} ${chalk.cyan(srcPath)}`)

if (!opts.noVerbose) {
console.log(
`> ${chalk.bgBlueBright(chalk.black(' ddoc extract src '))} ${chalk.cyan(srcPath)}`,
)
}

const main = JSON.parse(dbBackup.replace(/\]\n\[/g, ',')) as any[]
const designDocuments = main.filter(doc => doc._id.startsWith('_design/')) as DesignDocument[]
Expand Down Expand Up @@ -78,13 +86,17 @@ export default async function(src: string, opts: { destination: string; format:
const filename = `${path.basename(src, path.extname(src))}-designs.${opts.format}`
const dest = path.resolve(path.join(opts.destination, filename))

console.log(`> ${chalk.bgBlueBright(chalk.black(' ddoc extract designs found '))}`)
console.table(stats)
if (!opts.noVerbose) {
console.log(`> ${chalk.bgBlueBright(chalk.black(' ddoc extract designs found '))}`)
console.table(stats)
}
await mkdirp(path.dirname(dest))

console.log(
`> ${chalk.bgGreenBright(chalk.black(' ddoc extract destination '))} ${chalk.cyan(dest)}`,
)
if (!opts.noVerbose) {
console.log(
`> ${chalk.bgGreenBright(chalk.black(' ddoc extract destination '))} ${chalk.cyan(dest)}`,
)
}

await writeFile(dest, JSON.stringify(designDocuments, null, 1))
} else {
Expand Down Expand Up @@ -153,11 +165,13 @@ export default async function(src: string, opts: { destination: string; format:
}
}

console.log(
`> ${chalk.bgBlueBright(
chalk.black(' ddoc extract designs creating destination folder '),
)}`,
)
if (!opts.noVerbose) {
console.log(
`> ${chalk.bgBlueBright(
chalk.black(' ddoc extract designs creating destination folder '),
)}`,
)
}

const destFolder = path.resolve(
path.join(opts.destination, `${path.basename(src, path.extname(src))}-designs`),
Expand All @@ -169,9 +183,12 @@ export default async function(src: string, opts: { destination: string; format:
const filename = `${doc._id?.split('_design/')[1]}-${doc._rev}.${
opts.format === 'js' ? 'js' : 'ts'
}`
console.log(
`> ${chalk.bgBlueBright(chalk.black(` ddoc extract designs creating ${filename} `))}`,
)

if (!opts.noVerbose) {
console.log(
`> ${chalk.bgBlueBright(chalk.black(` ddoc extract designs creating ${filename} `))}`,
)
}
const dest = path.resolve(path.join(destFolder, filename))

let docString =
Expand All @@ -185,8 +202,10 @@ export default async function(src: string, opts: { destination: string; format:
await writeFile(dest, docString)
}

console.log(`> ${chalk.bgGreenBright(chalk.black(' ddoc extract designs done '))}`)
console.table(stats)
if (!opts.noVerbose) {
console.log(`> ${chalk.bgGreenBright(chalk.black(' ddoc extract designs done '))}`)
console.table(stats)
}
}
} catch (err) {
console.error(chalk.bgRed(chalk.white(` ${err.message} `)))
Expand Down
21 changes: 21 additions & 0 deletions test/dummy-data/main.txt

Large diffs are not rendered by default.

39 changes: 37 additions & 2 deletions test/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,40 @@
import { test } from 'tap'
import { join } from 'path'
import mkdirp from 'mkdirp'
import rimraf from 'rimraf'

test('default root route', async (t: any) => {
t.deepEqual('placeholder', 'placeholder')
import extract from '../src/ddoc/extract/extract'

const dummyData = join(__dirname, './dummy-data')

test('Should extract design documents from a backup', async (t: any) => {
const destinationFolder = join(dummyData, 'output')
await mkdirp(destinationFolder)

t.test('to json', async () => {
await extract(join(dummyData, 'main.txt'), {
destination: destinationFolder,
format: 'json',
noVerbose: true,
})
})

t.test('to JavaScript', async () => {
await extract(join(dummyData, 'main.txt'), {
destination: destinationFolder,
format: 'js',
noVerbose: true,
})
})

t.test('to TypeScript', async () => {
await extract(join(dummyData, 'main.txt'), {
destination: destinationFolder,
format: 'ts',
noVerbose: true,
})
})

t.tearDown(() => rimraf.sync(destinationFolder))
t.end()
})

0 comments on commit bf15c94

Please sign in to comment.