Skip to content

Commit 8fe487c

Browse files
fix util test
1 parent 2633598 commit 8fe487c

File tree

5 files changed

+40
-34
lines changed

5 files changed

+40
-34
lines changed

src/cli.ts

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {promisify} from 'util'
1010
import {join, resolve, dirname, basename} from 'path'
1111
import stdin = require('stdin')
1212
import {compile, Options} from './index'
13+
import {pathTransform} from './utils'
1314

1415
// Promisify mkdirp & glob
1516
const mkdirp = (path: string): Promise<_mkdirp.Made> =>
@@ -91,29 +92,6 @@ async function processGlob(argIn: string, argOut: string | undefined, argv: Part
9192
)
9293
}
9394

94-
/*
95-
the following logic determines the out path by comparing the in path to the users specified out path.
96-
For example, if input directory MultiSchema looks like:
97-
MultiSchema/foo/a.json
98-
MultiSchema/bar/fuzz/c.json
99-
MultiSchema/bar/d.json
100-
And the user wants the outputs to be in MultiSchema/Out, then this code will be able to map the inner directories foo, bar, and fuzz into the intended Out directory like so:
101-
MultiSchema/Out/foo/a.json
102-
MultiSchema/Out/bar/fuzz/c.json
103-
MultiSchema/Out/bar/d.json
104-
*/
105-
export function pathTransform(o: string, i: string): string {
106-
const outPathList = o.split('/')
107-
const inPathList = i.split('/')
108-
109-
const intersection = outPathList.filter(x => inPathList.includes(x))
110-
const difference = outPathList
111-
.filter(x => !inPathList.includes(x))
112-
.concat(inPathList.filter(x => !outPathList.includes(x)))
113-
114-
return join(...intersection, ...difference)
115-
}
116-
11795
async function processDir(argIn: string, argOut: string | undefined, argv: Partial<Options>): Promise<void[]> {
11896
const files = getPaths(argIn)
11997

src/utils.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {whiteBright} from 'cli-color'
22
import {deburr, isPlainObject, mapValues, trim, upperFirst} from 'lodash'
3-
import {basename, extname} from 'path'
3+
import {basename, extname, join} from 'path'
44
import {JSONSchema} from './types/JSONSchema'
55

66
// TODO: pull out into a separate package
@@ -221,3 +221,26 @@ export function escapeBlockComment(schema: JSONSchema) {
221221
}
222222
}
223223
}
224+
225+
/*
226+
the following logic determines the out path by comparing the in path to the users specified out path.
227+
For example, if input directory MultiSchema looks like:
228+
MultiSchema/foo/a.json
229+
MultiSchema/bar/fuzz/c.json
230+
MultiSchema/bar/d.json
231+
And the user wants the outputs to be in MultiSchema/Out, then this code will be able to map the inner directories foo, bar, and fuzz into the intended Out directory like so:
232+
MultiSchema/Out/foo/a.json
233+
MultiSchema/Out/bar/fuzz/c.json
234+
MultiSchema/Out/bar/d.json
235+
*/
236+
export function pathTransform(o: string, i: string): string {
237+
const outPathList = o.split('/')
238+
const inPathList = i.split('/')
239+
240+
const intersection = outPathList.filter(x => inPathList.includes(x))
241+
const symmetricDifference = outPathList
242+
.filter(x => !inPathList.includes(x))
243+
.concat(inPathList.filter(x => !outPathList.includes(x)))
244+
245+
return join(...intersection, ...symmetricDifference)
246+
}

test/test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ import {run as runCLITests} from './testCLI'
22
import {run as runCompileFromFileTests} from './testCompileFromFile'
33
import {hasOnly, run as runE2ETests} from './testE2E'
44
import {run as runNormalizerTests} from './testNormalizer'
5+
import {run as runUtilsTests} from './testUtils'
56

67
runE2ETests()
78

89
if (!hasOnly()) {
910
runCompileFromFileTests()
1011
runCLITests()
1112
runNormalizerTests()
13+
runUtilsTests()
1214
}

test/testCLI.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import {serial as test} from 'ava'
22
import {execSync} from 'child_process'
33
import {readFileSync, unlinkSync, readdirSync, rmdirSync, existsSync, lstatSync} from 'fs'
44
import {resolve, join} from 'path'
5-
import {pathTransform} from '../src/cli'
65

76
export function run() {
87
test('pipe in, pipe out', t => {
@@ -114,15 +113,6 @@ export function run() {
114113
})
115114
rmdirSync('./test/resources/MultiSchema2/out', {recursive: true})
116115
})
117-
118-
test('validate pathTransform function', t => {
119-
const inputPaths = ['MultiSchema/foo/a.json', 'MultiSchema/bar/fuzz/c.json', 'MultiSchema/bar/d.json']
120-
const outputPath = 'MultiSchema/Out'
121-
122-
t.is(pathTransform(outputPath, inputPaths[0]), 'MultiSchema/Out/foo/a.json')
123-
t.is(pathTransform(outputPath, inputPaths[1]), 'MultiSchema/Out/bar/d.json')
124-
t.is(pathTransform(outputPath, inputPaths[2]), 'MultiSchema/Out/bar/fuzz/c.json')
125-
})
126116
}
127117

128118
function getPaths(path: string, paths: string[] = []) {

test/testUtils.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import {serial as test} from 'ava'
2+
import {pathTransform} from '../src/utils'
3+
4+
export function run() {
5+
test('validate pathTransform function', t => {
6+
const inputPaths = ['MultiSchema/foo/a.json', 'MultiSchema/bar/fuzz/c.json', 'MultiSchema/bar/d.json']
7+
const outputPath = 'MultiSchema/Out'
8+
9+
t.is(pathTransform(outputPath, inputPaths[0]), 'MultiSchema/Out/foo/a.json')
10+
t.is(pathTransform(outputPath, inputPaths[1]), 'MultiSchema/Out/bar/fuzz/c.json')
11+
t.is(pathTransform(outputPath, inputPaths[2]), 'MultiSchema/Out/bar/d.json')
12+
})
13+
}

0 commit comments

Comments
 (0)