Skip to content

Commit

Permalink
UI: Add ember steps:list command for listing available steps (#5255)
Browse files Browse the repository at this point in the history
* ui: Add ember steps:list command for listing available steps

1. Adds `command` addon to house the new command
2. Start to organize out the steps themselves, bring a bit more order to
things ready to dedupe and cleanup
  • Loading branch information
johncowen authored and John Cowen committed Feb 21, 2019
1 parent 2f72484 commit bc02450
Show file tree
Hide file tree
Showing 18 changed files with 776 additions and 520 deletions.
3 changes: 3 additions & 0 deletions ui-v2/GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ lint: deps
format: deps
yarn run format:js

steps:
yarn run steps:list

node_modules: yarn.lock package.json
yarn install

Expand Down
19 changes: 19 additions & 0 deletions ui-v2/lib/commands/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* eslint no-console: "off" */
/* eslint-env node */
'use strict';
module.exports = {
name: 'commands',
includedCommands: function() {
return {
'steps:list': {
name: 'steps:list',
run: function(config, args) {
require('./lib/list.js')(`${process.cwd()}/tests/steps.js`);
},
},
};
},
isDevelopingAddon() {
return true;
},
};
67 changes: 67 additions & 0 deletions ui-v2/lib/commands/lib/list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/* eslint no-console: "off" */
/* eslint-env node */
'use strict';
const babel = require('@babel/core');
const read = require('fs').readFileSync;
const path = require('path');
const vm = require('vm');
const color = require('chalk');

const out = function(prefix, step, desc) {
if (!Array.isArray(step)) {
step = [step];
}
step.forEach(function(item) {
const str =
prefix +
item.replace('\n', ' | ').replace(/\$\w+/g, function(match) {
return color.cyan(match);
});
console.log(color.green(str));
});
};
const library = {
given: function(step, cb, desc) {
out('Given ', step, desc);
return this;
},
desc: function(desc) {
console.log(color.yellow(`- ${desc.trim()}`));
},
section: function() {
console.log(color.yellow(`##`));
},
then: function(step, cb, desc) {
out('Then ', step, desc);
return this;
},
when: function(step, cb, desc) {
out('When ', step, desc);
return this;
},
};
const exec = function(filename) {
const js = read(filename);
const code = babel.transform(js.toString(), {
filename: filename,
presets: [require('babel-preset-env')],
}).code;
const exports = {};
vm.runInNewContext(
code,
{
exports: exports,
require: function(str) {
return exec(path.resolve(`${process.cwd()}/tests`, `${str}.js`)).default;
},
},
{
filename: filename,
}
);
return exports;
};

module.exports = function(filename) {
exec(filename).default(function() {}, library, {}, {}, {}, function() {});
};
6 changes: 6 additions & 0 deletions ui-v2/lib/commands/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "commands",
"keywords": [
"ember-addon"
]
}
8 changes: 6 additions & 2 deletions ui-v2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"test-parallel": "EMBER_EXAM_PARALLEL=true ember exam --split=4 --parallel",
"test:view": "ember test --server --test-port=${EMBER_TEST_PORT:-7357}",
"test:coverage": "COVERAGE=true ember test --test-port=${EMBER_TEST_PORT:-7357}",
"test:view:coverage": "COVERAGE=true ember test --server --test-port=${EMBER_TEST_PORT:-7357}"
"test:view:coverage": "COVERAGE=true ember test --server --test-port=${EMBER_TEST_PORT:-7357}",
"steps:list": "node ./lib/commands/bin/list.js"
},
"husky": {
"hooks": {
Expand All @@ -38,11 +39,13 @@
]
},
"devDependencies": {
"@babel/core": "^7.2.2",
"@hashicorp/consul-api-double": "^2.0.1",
"@hashicorp/ember-cli-api-double": "^1.3.0",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"base64-js": "^1.3.0",
"broccoli-asset-rev": "^2.4.5",
"chalk": "^2.4.2",
"dart-sass": "^1.14.1",
"ember-ajax": "^3.0.0",
"ember-browserify": "^1.2.2",
Expand Down Expand Up @@ -104,7 +107,8 @@
"ember-addon": {
"paths": [
"lib/startup",
"lib/block-slots"
"lib/block-slots",
"lib/commands"
]
}
}
83 changes: 82 additions & 1 deletion ui-v2/tests/acceptance/steps/steps.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,83 @@
/* eslint no-console: "off" */
import Inflector from 'ember-inflector';
import utils from '@ember/test-helpers';
import getDictionary from '@hashicorp/ember-cli-api-double/dictionary';

import yadda from 'consul-ui/tests/helpers/yadda';
import pages from 'consul-ui/tests/pages';
import api from 'consul-ui/tests/helpers/api';

import steps from 'consul-ui/tests/steps';
export default steps;

const pluralize = function(str) {
return Inflector.inflector.pluralize(str);
};
export default function(assert) {
const library = yadda.localisation.English.library(
getDictionary(function(model, cb) {
switch (model) {
case 'datacenter':
case 'datacenters':
case 'dcs':
model = 'dc';
break;
case 'services':
model = 'service';
break;
case 'nodes':
model = 'node';
break;
case 'kvs':
model = 'kv';
break;
case 'acls':
model = 'acl';
break;
case 'sessions':
model = 'session';
break;
case 'intentions':
model = 'intention';
break;
}
cb(null, model);
}, yadda)
);
const create = function(number, name, value) {
// don't return a promise here as
// I don't need it to wait
api.server.createList(name, number, value);
};
const respondWith = function(url, data) {
api.server.respondWith(url.split('?')[0], data);
};
const setCookie = function(key, value) {
api.server.setCookie(key, value);
};
const getLastNthRequest = function(arr) {
return function(n, method) {
let requests = arr.slice(0).reverse();
if (method) {
requests = requests.filter(function(item) {
return item.method === method;
});
}
if (n == null) {
return requests;
}
return requests[n];
};
};
return steps(assert, library, pages, {
pluralize: pluralize,
triggerKeyEvent: utils.triggerKeyEvent,
currentURL: utils.currentURL,
click: utils.click,
fillIn: utils.fillIn,
find: utils.find,
lastNthRequest: getLastNthRequest(api.server.history),
respondWith: respondWith,
create: create,
set: setCookie,
});
}
Loading

0 comments on commit bc02450

Please sign in to comment.