|
| 1 | +require('colors') |
| 2 | +import fs from 'fs' |
| 3 | +import { resolve } from 'path' |
| 4 | + |
| 5 | +import expect from 'expect' |
| 6 | +const diff = require('diff') |
| 7 | + |
| 8 | +import ApiFlow from '../../../src/api-flow' |
| 9 | + |
| 10 | +const compare = (actual, expected) => { |
| 11 | + const delta = diff.diffJson(actual, expected) |
| 12 | + if ( |
| 13 | + delta.length === 1 && |
| 14 | + typeof delta[0].removed === 'undefined' && |
| 15 | + typeof delta[0].added === 'undefined' |
| 16 | + ) { |
| 17 | + return true |
| 18 | + } |
| 19 | + |
| 20 | + /* eslint-disable no-console */ |
| 21 | + console.log('\x1b[42m' + |
| 22 | + (new Array(6)).join('-------------\n') + '\x1b[0m') |
| 23 | + delta.forEach(part => { |
| 24 | + let color = 'grey' |
| 25 | + if (part.added) { |
| 26 | + color = 'green' |
| 27 | + } |
| 28 | + else if (part.removed) { |
| 29 | + color = 'red' |
| 30 | + } |
| 31 | + process.stderr.write(part.value[color]) |
| 32 | + }) |
| 33 | + /* eslint-enable no-console */ |
| 34 | + |
| 35 | + return false |
| 36 | +} |
| 37 | + |
| 38 | +const fixDiff = (actual, index) => { |
| 39 | + if (process.env.FIX === 'postman-collection-v2.0--internal-v1.0') { |
| 40 | + /* eslint-disable no-console */ |
| 41 | + console.log('updating spec') |
| 42 | + /* eslint-enable no-console */ |
| 43 | + fs.writeFileSync(resolve(__dirname, './test-case-' + index + '/output.json'), actual) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +describe('postman collection v2 -> internal', () => { |
| 48 | + for (let index = 0; index < 1; index += 1) { |
| 49 | + it('should match expected output for test case #' + index, (done) => { |
| 50 | + const output = fs.readFileSync( |
| 51 | + resolve(__dirname, './test-case-' + index + '/output.json'), |
| 52 | + 'utf-8' |
| 53 | + ).toString() |
| 54 | + // const item = { content: input } |
| 55 | + /* eslint-disable no-console */ |
| 56 | + try { |
| 57 | + const options = ApiFlow.setup({ |
| 58 | + options: { |
| 59 | + source: { format: 'postman-collection', version: 'v2.0' }, |
| 60 | + target: { format: 'internal', version: 'v1.0' } |
| 61 | + } |
| 62 | + }) |
| 63 | + |
| 64 | + ApiFlow |
| 65 | + .transform({ |
| 66 | + options, |
| 67 | + uri: 'file://' + resolve(__dirname, './test-case-' + index + '/input.json') |
| 68 | + }) |
| 69 | + .then(serialized => { |
| 70 | + const success = compare(serialized, output) |
| 71 | + if (!success) { |
| 72 | + done(new Error('found differences')) |
| 73 | + return fixDiff(serialized, index) |
| 74 | + } |
| 75 | + done() |
| 76 | + }, e => { |
| 77 | + console.error('got err:\n', e.stack) |
| 78 | + done() |
| 79 | + }) |
| 80 | + .catch() |
| 81 | + } |
| 82 | + catch (e) { |
| 83 | + console.error(e.stack) |
| 84 | + expect(true).toEqual(false) |
| 85 | + done() |
| 86 | + } |
| 87 | + /* eslint-enable no-console */ |
| 88 | + }) |
| 89 | + } |
| 90 | +}) |
0 commit comments