Skip to content
This repository has been archived by the owner on Dec 27, 2024. It is now read-only.

Commit

Permalink
refactor: only one file and ass more test
Browse files Browse the repository at this point in the history
  • Loading branch information
joolfe committed Apr 28, 2021
1 parent 543dcc3 commit 1c440da
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 115 deletions.
44 changes: 42 additions & 2 deletions bin/cli.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,48 @@
#!/usr/bin/env node

const p2o = require('./p2o')
const { program } = require('commander')
const { version } = require('../package.json')
const postmanToOpenApi = require('../lib')
const { promises: { readFile } } = require('fs')

p2o.parseAsync()
// TODO
// - handle error in a homogeneous way, put messages more clear or where is happening the error maybe?
// - check the input field or should be test inside library?
// - Errors should be more descriptive i think, for exmaple i dont know where the collection fail or the output fail?
// - check the coverage

const additionalHelp = `
Example calls:
$ p2o ./path/to/PostmantoCollection.json -f ./path/to/result.yml -o ./path/to/options.json
For more info about how to use it visit our documentation in <https://joolfe.github.io/postman-to-openapi/>
`

program
.version(version, '-v, --vers', 'Output the current version of the library.')
.name('p2o')
.usage('<collection> [options]')
.addHelpText('after', additionalHelp)
.arguments('<collection>')
.description('Transform a postman collection to OpenAPI specification yml.', {
collection: 'Relative path to the Postman collection json file'
})
.option('-f, --file <file>', 'Relative path to the file where result will be saved. If empty result will be returned by cli.')
.option('-o, --options <options>', 'Relative path to json file that contain the optional parameters for the transformation.')
.action(async (collection, { file, options }, command) => {
try {
const parsedOptions = options ? JSON.parse(await readFile(options)) : {}
const result = await postmanToOpenApi(collection, file, parsedOptions)
console.info(result)
} catch (err) {
// TODO normalize errors here
throw new Error(err)
}
})

program
.parseAsync()
.catch(err => {
process.exitCode = 1
console.error(err.message)
Expand Down
97 changes: 0 additions & 97 deletions bin/p2o.js

This file was deleted.

70 changes: 54 additions & 16 deletions test/cli.spec.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
'use strict'

const { describe, it } = require('mocha')
const { readFileSync } = require('fs')
const { equal, rejects } = require('assert').strict
const { describe, it, afterEach } = require('mocha')
const { readFileSync, existsSync, unlinkSync } = require('fs')
const { equal, rejects, ok } = require('assert').strict
const execa = require('execa')

const cliPath = './bin/cli.js'

const COLLECTION_BASIC = './test/resources/input/v21/PostmantoOpenAPI.json'
const COLLECTION_SIMPLE = './test/resources/input/v21/SimplePost.json'
const OPTIONS_INFO = './test/resources/input/options.json'
const INVALID_OPTIONS_INFO = './test/resources/input/invalidOptions.txt'
const EXPECTED_INFO_OPTS = readFileSync('./test/resources/output/InfoOpts.yml', 'utf8')
const EXPECTED_BASIC = readFileSync('./test/resources/output/Basic.yml', 'utf8')
const OUTPUT_PATH = './openAPIRes.yml'
const HELP_OUTPUT = readFileSync('./test/resources/console/help.txt', 'utf8')

const { version } = require('../package.json')

describe('Cli specs', function () {
afterEach('remove file', function () {
if (existsSync(OUTPUT_PATH)) {
unlinkSync(OUTPUT_PATH)
}
})

describe.only('Cli specs', function () {
it('should transform correctly a basic collection (HP)', async function () {
const { stdout } = await execa('node', [cliPath, COLLECTION_BASIC])
equal(stdout, EXPECTED_BASIC)
Expand All @@ -32,23 +45,48 @@ describe.only('Cli specs', function () {
})
})

it.only('should print an error when result file cannot be created', async function () {
it('should print an error when result file cannot be created', async function () {
await rejects(execa('node', [cliPath, COLLECTION_BASIC, '-f', './no_exist/result.yml']), {
name: 'Error',
stderr: "Error: ENOENT: no such file or directory, open './none/file.json'",
stderr: "Error: ENOENT: no such file or directory, open './no_exist/result.yml'",
exitCode: 1
})
})

it('should transform and write into a file the output (HP)')
it('should transform and write into a file the output if file already exist (HP)')
it('should transform and write into a file the output (HP)', async function () {
const { stdout } = await execa('node', [cliPath, COLLECTION_BASIC, '-f', OUTPUT_PATH])
ok(existsSync(OUTPUT_PATH))
equal(stdout, EXPECTED_BASIC)
})

it('should print correctly help command')
it('should print correctly the version of the cli')
it('should fail if no postman collection is informed')
it('should transform correctly a basic collection when using option file (HP)', async function () {
const { stdout } = await execa('node', [cliPath, COLLECTION_SIMPLE, '-o', OPTIONS_INFO])
equal(stdout, EXPECTED_INFO_OPTS)
})

it('should accept options file as input')
it('should fail if options file doesn\'t exist')
it('should accept options string as input')
it('should fail if options string is malformed')
it('should print an error when options file doesn\'t exist', async function () {
await rejects(execa('node', [cliPath, COLLECTION_SIMPLE, '-o', './no_exist/options.json']), {
name: 'Error',
stderr: "Error: ENOENT: no such file or directory, open './no_exist/options.json'",
exitCode: 1
})
})

it('should print an error when options file is not a valid json', async function () {
await rejects(execa('node', [cliPath, COLLECTION_SIMPLE, '-o', INVALID_OPTIONS_INFO]), {
name: 'Error',
stderr: 'SyntaxError: Unexpected token i in JSON at position 6',
exitCode: 1
})
})

it('should print correctly the version of the cli', async function () {
const { stdout } = await execa('node', [cliPath, '-v'])
equal(stdout, version)
})

it('should print correctly help command', async function () {
const { stdout } = await execa('node', [cliPath, '-h'])
equal(stdout, HELP_OUTPUT)
})
})
20 changes: 20 additions & 0 deletions test/resources/console/help.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Usage: p2o <collection> [options]

Transform a postman collection to OpenAPI specification yml.

Arguments:
collection Relative path to the Postman collection json file

Options:
-v, --vers Output the current version of the library.
-f, --file <file> Relative path to the file where result will be
saved. If empty result will be returned by cli.
-o, --options <options> Relative path to json file that contain the optional
parameters for the transformation.
-h, --help display help for command


Example calls:
$ p2o ./path/to/PostmantoCollection.json -f ./path/to/result.yml -o ./path/to/options.json

For more info about how to use it visit our documentation in <https://joolfe.github.io/postman-to-openapi/>
8 changes: 8 additions & 0 deletions test/resources/input/invalidOptions.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
info": {
"title": "Options title",
"version: "6.0.7-beta",
"description": "Description from options",
"termsOfService": "http://tos.myweb.com"
}
}
8 changes: 8 additions & 0 deletions test/resources/input/options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"info": {
"title": "Options title",
"version": "6.0.7-beta",
"description": "Description from options",
"termsOfService": "http://tos.myweb.com"
}
}

0 comments on commit 1c440da

Please sign in to comment.