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

Create migrate script to change mocha.opts to [json | js | yml | yaml] #4069

Closed
wants to merge 15 commits into from
2 changes: 2 additions & 0 deletions lib/cli/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const path = require('path');
const {loadOptions, YARGS_PARSER_CONFIG} = require('./options');
const commands = require('./commands');
const ansi = require('ansi-colors');
const migrateOpts = require('../../scripts/migrate-opts-init');
const {repository, homepage, version, gitter} = require('../../package.json');

/**
Expand All @@ -40,6 +41,7 @@ exports.main = (argv = process.argv.slice(2)) => {
.scriptName('mocha')
.command(commands.run)
.command(commands.init)
.command(migrateOpts.init)
.updateStrings({
'Positionals:': 'Positional Arguments',
'Options:': 'Other Options',
Expand Down
77 changes: 43 additions & 34 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions package-scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ module.exports = {
'test.node.exports',
'test.node.unit',
'test.node.integration',
'test.node.migrateOpts',
'test.node.jsapi',
'test.node.requires',
'test.node.reporters',
Expand Down Expand Up @@ -128,6 +129,11 @@ module.exports = {
description: 'Run tests concerning mocha.opts',
hiddenFromHelp: true
},
migrateOpts: {
script: test('migrateOpts', 'test/migrate-opts'),
description: 'Run tests concerning migrate mocha.opts script',
hiddenFromHelp: true
},
jsapi: {
script: 'node test/jsapi',
description: 'Run Node.js Mocha JavaScript API tests',
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,10 @@
"glob": "7.1.3",
"growl": "1.10.5",
"he": "1.2.0",
"js-beautify": "^1.10.2",
"js-yaml": "3.13.1",
"json2yaml": "^1.1.0",
"jsonfile": "^5.0.0",
"log-symbols": "2.2.0",
"minimatch": "3.0.4",
"mkdirp": "0.5.1",
Expand Down
8 changes: 8 additions & 0 deletions scripts/migrate-opts-init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';
/**
* Exports Yargs commands
* @see https://git.io/fpJ0G
* @private
* @module
*/
exports.init = require('./migrate-opts.js');
59 changes: 59 additions & 0 deletions scripts/migrate-opts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'use strict';
/**
* Command module for "migrate" command
* Interface : mocha migrate-opts -file ./test/mocha.opts --json
* Migrate opts
* Read opts file and change to othter file like yaml, yml, js, json
* @see https://mochajs.org/#configuring-mocha-nodejs
* @private
* @module
*/
const path = require('path');
const YAML = require('json2yaml');
const jsonfile = require('jsonfile');
const beautify = require('js-beautify').js;
const fs = require('fs');
const loadMochaOpts = require('../lib/cli/options.js').loadMochaOpts;
const setJsType = content =>
beautify(`(module.exports = ${JSON.stringify(content)})`);
const setYamlType = content => YAML.stringify(content);
const writeFile = {
yaml: (content, _path) =>
fs.writeFileSync(path.join(_path, `.mocharc.yaml`), setYamlType(content)),
yml: (content, _path) =>
fs.writeFileSync(path.join(_path, `.mocharc.yml`), setYamlType(content)),
js: (content, _path) =>
fs.writeFileSync(path.join(_path, `.mocharc.js`), setJsType(content)),
json: (content, _path) =>
jsonfile.writeFileSync(path.join(_path, `.mocharc.json`), content, {
spaces: 1
})
};
const writeConfig = (type, content, _path) => {
if (type === 'yaml') {
writeFile.yaml(content, _path);
} else if (type === 'yml') {
writeFile.yml(content, _path);
} else if (type === 'js') {
writeFile.js(content, _path);
} else {
writeFile.json(content, _path);
}
return content;
};

exports.init = (filepath, type, _path = process.cwd()) => {
const content = loadMochaOpts({opts: filepath});
Copy link

@azu azu Mar 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

content includes alias and yargs's property like _.
We need to filter by mocha's options and normalize it.

For example

--require ts-node-test-register
--reporter spec

it's content will be

{
  _: [],
  require: [ 'ts-node-test-register' ],
  r: [ 'ts-node-test-register' ],
  reporter: 'spec',
  R: 'spec'
}

Copy link

@azu azu Mar 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My patch that filtering by types.

exports.types = {

  const content = loadMochaOpts({opts: filepath});
  const types = require("./run-option-metadata").types;
  // filtering by types that remove alias and yargs property
  const allowPropertyNames = Object.values(types).reduce((names, flagNames) => {
    return names.concat(flagNames);
  }, []);
  const normalizedContent = Object.keys(content)
      .filter(key => allowPropertyNames.includes(key))
      .reduce((obj, key) => {
        obj[key] = content[key];
        return obj;
      }, {});
  writeConfig(type, normalizedContent, _path);

writeConfig(type, content, _path);
};
exports.command = 'migrate-opts';

exports.description = 'Migrate opts file to type that user wanted';

exports.builder = yargs => yargs.option('file').option('type');

exports.handler = argv => {
const filepath = path.join(process.cwd(), argv.file[0]);
const type = argv.type;
this.init(filepath, type);
};
16 changes: 16 additions & 0 deletions test/migrate-opts/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

var Mocha = require('../../');

var mocha = new Mocha({
ui: 'bdd',
globals: ['okGlobalA', 'okGlobalB', 'okGlobalC', 'callback*'],
growl: true,
timeout: 1000
});

require('../setup');

mocha.addFile('test/migrate-opts/migrate-opts.spec.js');

mocha.run(function() {});
35 changes: 35 additions & 0 deletions test/migrate-opts/migrate-opts.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';
var fs = require('fs');
var path = require('path');
var expect = require('chai').expect;
var migrateOpts = require('../../scripts/migrate-opts');
var types = ['json', 'js', 'yml', 'yaml'];

describe('Test migrate-opts.js script', function() {
types.forEach(function(type) {
it('should be worked writeFile ' + type, function() {
var filepath = path.join(process.cwd(), './test/migrate-opts/mocha.opts');
var _path = path.join(process.cwd(), './test/migrate-opts');
migrateOpts.init(filepath, type, _path);
var __path = path.join(
process.cwd(),
'./test/migrate-opts',
'.mocharc.' + type
);
var isFile = fs.existsSync(__path);
expect(isFile).to.equal(true);
if (isFile) fs.unlinkSync(__path);
});
});
after(function() {
types.forEach(function(type) {
var __path = path.join(
process.cwd(),
'./test/migrate-opts',
'.mocharc.' + type
);
var isFile = fs.existsSync(__path);
if (isFile) fs.unlinkSync(__path);
});
});
});
4 changes: 4 additions & 0 deletions test/migrate-opts/mocha.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
--reporter dot
--growl
--extension mjs
--extension js