From c3003f82517b403de16e1e6e0155108170f2ec8e Mon Sep 17 00:00:00 2001 From: mkucharz Date: Sun, 28 Jan 2018 14:26:54 +0100 Subject: [PATCH] feat(cli:socket): verifying socket compatibility --- packages/cli/package.json | 4 ++-- packages/cli/src/commands/socket-submit.js | 6 +++++- packages/cli/src/utils/errors.js | 8 ++++++++ packages/cli/src/utils/session.js | 3 +++ packages/cli/src/utils/sockets/sockets.js | 11 ++++++++++- packages/cli/tests/e2e/registry.test-e2e.js | 21 +++++++++++++++++++-- packages/lib-js-core/src/server.js | 4 ++++ packages/lib-js-core/test/e2e/event.js | 1 + packages/lib-js-core/test/e2e/server.js | 11 +++++++++++ 9 files changed, 63 insertions(+), 6 deletions(-) create mode 100644 packages/lib-js-core/test/e2e/server.js diff --git a/packages/cli/package.json b/packages/cli/package.json index a6122ec3..3e27a1ff 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -110,10 +110,10 @@ "prepush": "npm run lint && npm run test:unit", "prepublish": "npm run build", "build": "npx babel src -d lib/ --copy-files --ignore *.test.js,*.test-e2e.js --source-maps", - "build:watch": "npm run build -w", + "build:watch": "npm run build -- -w", "clean": "rm -rf lib/", "unit": "mocha 'tests/unit/*.test.js' --require babel-register", - "unit:watch": "npm run unit -w", + "unit:watch": "npm run unit -- -w", "test": "npm-run-all --parallel test:unit test:e2e:group:*", "test:e2e:init": "npm run test:e2e-single tests/e2e/init.test-e2e.js", "test:e2e:anonymous": "npm run test:e2e-single tests/e2e/anonymous.test-e2e.js", diff --git a/packages/cli/src/commands/socket-submit.js b/packages/cli/src/commands/socket-submit.js index ec113c37..5c32b135 100644 --- a/packages/cli/src/commands/socket-submit.js +++ b/packages/cli/src/commands/socket-submit.js @@ -1,4 +1,5 @@ import format from 'chalk' +import { CompatibilityError } from '../utils/errors' import { error, echo, echon } from '../utils/print-tools' export default class SocketSubmitCmd { @@ -44,7 +45,10 @@ export default class SocketSubmitCmd { echo(`Type ${publishCommand} to make it available for everyone!`) echo() } catch (err) { - if (err.response && err.response.data) { + if (err instanceof CompatibilityError) { + error(4)(err.message) + echo() + } else if (err.response && err.response.data) { error(4)(err.response.data.message) echo() } else { diff --git a/packages/cli/src/utils/errors.js b/packages/cli/src/utils/errors.js index 46f02b11..aea6e032 100644 --- a/packages/cli/src/utils/errors.js +++ b/packages/cli/src/utils/errors.js @@ -4,6 +4,14 @@ class CompileError { } } +class CompatibilityError { + constructor (socketVersion, envVersion) { + this.message = `Socket major version (${socketVersion}) is not\ + comaptible with this Syncano environment major version (${envVersion}).` + } +} + export { + CompatibilityError, CompileError } diff --git a/packages/cli/src/utils/session.js b/packages/cli/src/utils/session.js index fe25c08c..d6c484d9 100644 --- a/packages/cli/src/utils/session.js +++ b/packages/cli/src/utils/session.js @@ -8,6 +8,7 @@ import walkUp from 'node-walkup' import Promise from 'bluebird' import logger from './debug' +import pjson from '../../package.json' import getSettings from '../settings' import genUniqueName from './unique-instance' import Socket from './sockets' @@ -26,6 +27,8 @@ export class Session { this.userId = null this.walkup = Promise.promisify(walkUp) + this.majorVersion = pjson.version.split('.')[0] + this.HOST = process.env.SYNCANO_HOST || 'api.syncano.io' this.ENDPOINT_HOST = this.HOST === 'api.syncano.io' ? 'syncano.space' : 'syncano.link' } diff --git a/packages/cli/src/utils/sockets/sockets.js b/packages/cli/src/utils/sockets/sockets.js index f5cca078..7131b5e5 100644 --- a/packages/cli/src/utils/sockets/sockets.js +++ b/packages/cli/src/utils/sockets/sockets.js @@ -24,7 +24,7 @@ import Hosting from '../hosting' import Registry from '../registry' import { p, echo } from '../print-tools' import { getTemplate } from '../templates' -import { CompileError } from '../errors' +import { CompileError, CompatibilityError } from '../errors' const { debug } = logger('utils-sockets') @@ -1284,6 +1284,7 @@ class Socket { submit () { debug('submit') + this.isCompatible() const registry = new Registry() return registry.submitSocket(this) } @@ -1369,6 +1370,14 @@ class Socket { return checksums } + isCompatible () { + const socketMajorVersion = this.spec.version.split('.')[0] + if (socketMajorVersion !== session.majorVersion) { + throw new CompatibilityError(socketMajorVersion, session.majorVersion) + } + return true + } + shouldBeUpdated () { debug('shouldBeUpdated') if (this.existLocally && this.existRemotely) { diff --git a/packages/cli/tests/e2e/registry.test-e2e.js b/packages/cli/tests/e2e/registry.test-e2e.js index c9a60677..97cd25a3 100644 --- a/packages/cli/tests/e2e/registry.test-e2e.js +++ b/packages/cli/tests/e2e/registry.test-e2e.js @@ -1,4 +1,5 @@ /* global describe it before after */ +import replace from 'replace' import path from 'path' import { nixt, @@ -41,7 +42,7 @@ describe('[E2E] CLI Registry', function () { .end(done) }) - it('can submit created socket to registry', function (done) { + it('can submit created socket to the registry', function (done) { testNixt() .run(`${cliLocation} submit ${createdSocketName}`) .stdout(/to make it available for everyone/) @@ -50,11 +51,27 @@ describe('[E2E] CLI Registry', function () { it('can bump version of submited socket', function (done) { testNixt() - .run(`${cliLocation} submit ${createdSocketName} -b major`) + .run(`${cliLocation} submit ${createdSocketName} -b minor`) .stdout(/\(1\.0\.0\)\.\.\. Done/) .end(done) }) + it('can submit socket with wrong version to the registry', function (done) { + testNixt() + .before(() => { + replace({ + regex: 'version: 0.0.1', + replacement: 'version: 25.0.1', + paths: [path.join(testsLocation, testInstance, 'syncano', createdSocketName)], + recursive: true, + silent: true + }) + }) + .run(`${cliLocation} submit ${createdSocketName}`) + .stdout(/is not comaptible with this Syncano environment/) + .end(done) + }) + it('can publish created socket', function (done) { testNixt() .run(`${cliLocation} publish ${createdSocketName}`) diff --git a/packages/lib-js-core/src/server.js b/packages/lib-js-core/src/server.js index 14cf90cd..320c862f 100644 --- a/packages/lib-js-core/src/server.js +++ b/packages/lib-js-core/src/server.js @@ -1,3 +1,4 @@ +import pjson from '../package.json' import Data from './data' import Users from './users' import Account from './account' @@ -20,6 +21,9 @@ class Server { const getConfig = className => Object.assign({className}, settings) const config = getConfig() + this.version = pjson.version + this.majorVersion = pjson.version.split('.')[0] + this._class = new Class(config) this.event = new Event(config) this.endpoint = new Endpoint(config) diff --git a/packages/lib-js-core/test/e2e/event.js b/packages/lib-js-core/test/e2e/event.js index 2e517755..55aa38ed 100644 --- a/packages/lib-js-core/test/e2e/event.js +++ b/packages/lib-js-core/test/e2e/event.js @@ -1,3 +1,4 @@ +/* global it describe before after */ import {expect} from 'chai' import Server from '../../src' import {getRandomString, createTestInstance, deleteTestInstance} from '../utils' diff --git a/packages/lib-js-core/test/e2e/server.js b/packages/lib-js-core/test/e2e/server.js new file mode 100644 index 00000000..b93e7262 --- /dev/null +++ b/packages/lib-js-core/test/e2e/server.js @@ -0,0 +1,11 @@ +/* global it describe */ +import {expect} from 'chai' +import pjson from '../../package.json' +import Server from '../../src' + +describe('Server', function () { + it('get major version', async () => { + const majorVersion = new Server().majorVersion + expect(majorVersion).to.be.equal(pjson.version.split('.')[0]) + }) +})