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

Commit bf15c94

Browse files
committed
feat: add tests for extract
1 parent 1494f8c commit bf15c94

File tree

4 files changed

+99
-23
lines changed

4 files changed

+99
-23
lines changed

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"lint": "eslint \"src/**/*.{js,jsx,ts,tsx}\"",
1919
"lint:fix": "eslint \"src/**/*.{js,jsx,ts,tsx}\"",
2020
"test": "cross-env TS_NODE_PROJECT=./test/tsconfig.json tap test/**/*.ts",
21-
"test:ci": "cross-env TS_NODE_PROJECT=./test/tsconfig.json tap --coverage-report=text-lcov test/**/*.ts",
21+
"test:ci": "cross-env TS_NODE_PROJECT=./test/tsconfig.json tap --coverage-report=text-lcov test/**/*.ts | coveralls",
2222
"lint-staged": "lint-staged",
2323
"commitlint": "commitlint",
2424
"semantic-release": "semantic-release"
@@ -49,6 +49,7 @@
4949
"@types/mkdirp": "~1.0.0",
5050
"@types/node": "~13.9.0",
5151
"@types/require-from-string": "~1.2.0",
52+
"@types/rimraf": "~2.0.3",
5253
"@types/sade": "~1.6.0",
5354
"@typescript-eslint/eslint-plugin": "~2.23.0",
5455
"@typescript-eslint/parser": "~2.23.0",
@@ -65,10 +66,10 @@
6566
"husky": "~4.2.0",
6667
"lint-staged": "~10.0.7",
6768
"prettier": "~1.19.1",
68-
"rimraf": "~3.0.0",
69+
"rimraf": "~3.0.2",
70+
"semantic-release": "~17.0.4",
6971
"tap": "~14.10.5",
70-
"tap-mocha-reporter": "~5.0.0",
71-
"semantic-release": "~17.0.4"
72+
"tap-mocha-reporter": "~5.0.0"
7273
},
7374
"config": {
7475
"commitizen": {

src/ddoc/extract/extract.ts

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,23 @@ const DesignDocumentSchema = Type.Object({
4141

4242
type DesignDocument = Static<typeof DesignDocumentSchema>
4343

44-
export default async function(src: string, opts: { destination: string; format: string }) {
44+
export default async function(
45+
src: string,
46+
opts: { destination: string; format: string; noVerbose?: boolean },
47+
) {
4548
const methods = new Map<string, string>()
4649
try {
4750
if (!FORMATS.includes(opts.format)) {
4851
throw Error(`Wrong destination format provided ${opts.format}, must be one of ${FORMATS}`)
4952
}
5053
const srcPath = path.resolve(src)
5154
const dbBackup = (await readFIle(srcPath)).toString()
52-
console.log(`> ${chalk.bgBlueBright(chalk.black(' ddoc extract src '))} ${chalk.cyan(srcPath)}`)
55+
56+
if (!opts.noVerbose) {
57+
console.log(
58+
`> ${chalk.bgBlueBright(chalk.black(' ddoc extract src '))} ${chalk.cyan(srcPath)}`,
59+
)
60+
}
5361

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

81-
console.log(`> ${chalk.bgBlueBright(chalk.black(' ddoc extract designs found '))}`)
82-
console.table(stats)
89+
if (!opts.noVerbose) {
90+
console.log(`> ${chalk.bgBlueBright(chalk.black(' ddoc extract designs found '))}`)
91+
console.table(stats)
92+
}
8393
await mkdirp(path.dirname(dest))
8494

85-
console.log(
86-
`> ${chalk.bgGreenBright(chalk.black(' ddoc extract destination '))} ${chalk.cyan(dest)}`,
87-
)
95+
if (!opts.noVerbose) {
96+
console.log(
97+
`> ${chalk.bgGreenBright(chalk.black(' ddoc extract destination '))} ${chalk.cyan(dest)}`,
98+
)
99+
}
88100

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

156-
console.log(
157-
`> ${chalk.bgBlueBright(
158-
chalk.black(' ddoc extract designs creating destination folder '),
159-
)}`,
160-
)
168+
if (!opts.noVerbose) {
169+
console.log(
170+
`> ${chalk.bgBlueBright(
171+
chalk.black(' ddoc extract designs creating destination folder '),
172+
)}`,
173+
)
174+
}
161175

162176
const destFolder = path.resolve(
163177
path.join(opts.destination, `${path.basename(src, path.extname(src))}-designs`),
@@ -169,9 +183,12 @@ export default async function(src: string, opts: { destination: string; format:
169183
const filename = `${doc._id?.split('_design/')[1]}-${doc._rev}.${
170184
opts.format === 'js' ? 'js' : 'ts'
171185
}`
172-
console.log(
173-
`> ${chalk.bgBlueBright(chalk.black(` ddoc extract designs creating ${filename} `))}`,
174-
)
186+
187+
if (!opts.noVerbose) {
188+
console.log(
189+
`> ${chalk.bgBlueBright(chalk.black(` ddoc extract designs creating ${filename} `))}`,
190+
)
191+
}
175192
const dest = path.resolve(path.join(destFolder, filename))
176193

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

188-
console.log(`> ${chalk.bgGreenBright(chalk.black(' ddoc extract designs done '))}`)
189-
console.table(stats)
205+
if (!opts.noVerbose) {
206+
console.log(`> ${chalk.bgGreenBright(chalk.black(' ddoc extract designs done '))}`)
207+
console.table(stats)
208+
}
190209
}
191210
} catch (err) {
192211
console.error(chalk.bgRed(chalk.white(` ${err.message} `)))

test/dummy-data/main.txt

Lines changed: 21 additions & 0 deletions
Large diffs are not rendered by default.

test/index.ts

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,40 @@
11
import { test } from 'tap'
2+
import { join } from 'path'
3+
import mkdirp from 'mkdirp'
4+
import rimraf from 'rimraf'
25

3-
test('default root route', async (t: any) => {
4-
t.deepEqual('placeholder', 'placeholder')
6+
import extract from '../src/ddoc/extract/extract'
7+
8+
const dummyData = join(__dirname, './dummy-data')
9+
10+
test('Should extract design documents from a backup', async (t: any) => {
11+
const destinationFolder = join(dummyData, 'output')
12+
await mkdirp(destinationFolder)
13+
14+
t.test('to json', async () => {
15+
await extract(join(dummyData, 'main.txt'), {
16+
destination: destinationFolder,
17+
format: 'json',
18+
noVerbose: true,
19+
})
20+
})
21+
22+
t.test('to JavaScript', async () => {
23+
await extract(join(dummyData, 'main.txt'), {
24+
destination: destinationFolder,
25+
format: 'js',
26+
noVerbose: true,
27+
})
28+
})
29+
30+
t.test('to TypeScript', async () => {
31+
await extract(join(dummyData, 'main.txt'), {
32+
destination: destinationFolder,
33+
format: 'ts',
34+
noVerbose: true,
35+
})
36+
})
37+
38+
t.tearDown(() => rimraf.sync(destinationFolder))
39+
t.end()
540
})

0 commit comments

Comments
 (0)