Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for listing responses bug #110

Merged
merged 4 commits into from
Jan 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions packages/cli/src/commands/socket-list-responses.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ const socketListResponses = (session) => ({
),
createNewOne: `Type ${format.cyan('syncano-cli create <name>')} to create new one.`,
installNewOne: `Type ${format.cyan('syncano-cli add <name>')} to install new one from registry.`,
params: `${format.white('params')}:`,
params: `${format.white('input')}:`,
responses: `${format.white('output')}:`,
socket: (socket) => {
let description = ''
if (socket.existRemotely) {
Expand Down Expand Up @@ -54,17 +55,20 @@ const socketListResponses = (session) => ({
example: `${format.dim('example')}: ${format.cyan.dim(metadata.parameters[param].example)}`
}),
response: (example) => {
const { exit_code, description } = example || ''
const exitCode = example.exit_code || '200'
const mimetype = example.mimetype || 'application/json'
const { description } = example || ''

let exampleLines = ''
if (example && example.example) {
exampleLines = example.example.split('\n').map((line) => p(12)(line)).join('\n')
}

return {
mimetype: `${format.white('response')}: ${metadata.response.mimetype}`,
description: example && `${format.dim('description')}: ${printCode(exit_code, description)}`,
exitCode: example && `${format.dim('exit code')}: ${printCode(exit_code)}`,
example: exampleLines && `${format.dim('example')}:\n${exampleLines}`
mimetype: `${format.dim('mimetype')}: ${mimetype}`,
description: example && `${format.dim('description')}: ${printCode(exitCode, description)}`,
exitCode: example && `${format.dim('exit code')}: ${printCode(exitCode)}`,
example: exampleLines && `${format.dim('example')}:\n${p(2)(exampleLines)}`
}
}
}
Expand Down
26 changes: 15 additions & 11 deletions packages/cli/src/commands/socket-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,28 @@ class SocketListCmd {
echo()

if (endpoint.metadata.parameters && this.fullPrint) {
echo(12)(this.responses.params)
echo(8)(this.responses.params)
echo()
Object.keys(endpoint.metadata.parameters).forEach((param) => {
echo(12)(endpointResponses.parameter(param).name)
echo(12)(endpointResponses.parameter(param).description)
echo(12)(endpointResponses.parameter(param).example)
echo(10)(endpointResponses.parameter(param).name)
echo(10)(endpointResponses.parameter(param).description)
echo(10)(endpointResponses.parameter(param).example)
echo()
})
}

if (endpoint.metadata.response && this.fullPrint) {
echo(12)(endpointResponses.response().mimetype)
const responses = endpoint.metadata.response

if (responses && this.fullPrint) {
echo(8)(this.responses.responses)
echo()
if (endpoint.metadata.response.examples) {
endpoint.metadata.response.examples.forEach((example) => {
echo(12)(endpointResponses.response(example).description)
echo(12)(endpointResponses.response(example).exitCode)
echo(12)(endpointResponses.response(example).example)
if (responses) {
Object.keys(responses).forEach(exampleKey => {
const example = responses[exampleKey]
echo(10)(endpointResponses.response(example).description)
echo(10)(endpointResponses.response(example).mimetype)
echo(10)(endpointResponses.response(example).exitCode)
echo(10)(endpointResponses.response(example).example)
echo()
})
}
Expand Down
8 changes: 8 additions & 0 deletions packages/cli/tests/e2e/socket.test-e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ describe('[E2E] CLI Socket', function () {
.end(done)
})

it('can list single installed socket with docs', function (done) {
testNixt()
.run(`${cliLocation} list hello`)
.stdout(/input:/)
.stdout(/output:/)
.end(done)
})

it('can\'t list non existing Socket', function (done) {
testNixt()
.run(`${cliLocation} list ${getRandomString()}`)
Expand Down
50 changes: 1 addition & 49 deletions packages/cli/tests/unit/commands-socket-list.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* global describe it beforeEach afterEach before */
import _ from 'lodash'
import sinon from 'sinon'
import sinonTestFactory from 'sinon-test'
import { expect } from 'chai'
Expand All @@ -9,7 +8,7 @@ import { getRandomString } from '@syncano/test-tools'

import { SocketList } from '../../src/commands'
import context from '../../src/utils/context'
import printTools, { p, printCode } from '../../src/utils/print-tools'
import printTools from '../../src/utils/print-tools'

sinon.test = sinonTestFactory(sinon)

Expand Down Expand Up @@ -258,52 +257,5 @@ describe('[commands] List Sockets', function () {
`${format.dim('example')}: ${format.cyan.dim(endpoint.metadata.parameters[param].example)}`)
})
})

describe('should print endpoint response with', function () {
let example = null

before(function () {
endpoint.metadata.response = {
mimetype: 'text/plain',
examples: [{
example: getRandomString('createSocket_printEndpoint_response_example'),
exit_code: _.random(100, 600),
description: getRandomString('createSocket_printEndpoint_response_description')
}]
}
example = endpoint.metadata.response.examples[0]
socketList.fullPrint = true
})

beforeEach(function () {
socketList.printEndpoint(endpoint, true)
})

it('mimetype', function () {
sinon.assert.calledWith(interEcho,
`${format.white('response')}: ${endpoint.metadata.response.mimetype}`)
})

it('description', function () {
const { exit_code, description } = endpoint.metadata.response.examples[0]

sinon.assert.calledWith(interEcho,
example && `${format.dim('description')}: ${printCode(exit_code, description)}`)
})

it('exit code', function () {
const { exit_code } = endpoint.metadata.response.examples[0]

sinon.assert.calledWith(interEcho,
example && `${format.dim('exit code')}: ${printCode(exit_code)}`)
})

it('example', function () {
const exampleLines = example.example.split('\n').map((line) => p(12)(line)).join('\n')

sinon.assert.calledWith(interEcho,
exampleLines && `${format.dim('example')}:\n${exampleLines}`)
})
})
})
})