node/test/parallel/test-process-chdir.js
Vse Mozhet Byt 76340e3f10 test: fix RegExp nits
* Remove needless RegExp flag

  In fixed case, `/g` flag is needless in the boolean context.

* Remove needless RegExp capturing

  Use non-capturing grouping or remove capturing completely when:

  * capturing is useless per se, e.g. in test() check;
  * captured groups are not used afterward at all;
  * some of the later captured groups are not used afterward.

* Use test, not match/exec in boolean context

  match() and exec() return a complicated object,
  unneeded in a boolean context.

* Do not needlessly repeat RegExp creation

  This commit takes RegExp creation out of cycles and other repetitions.

  As long as the RegExp does not use /g flag and match indices,
  we are safe here.

  In tests, this fix hardly gives a significant performance gain,
  but it increases clarity and maintainability,
  reassuring some RegExps to be identical.

  RegExp in functions are not taken out of their functions:
  while these functions are called many times
  and their RegExps are recreated with each call,
  the performance gain in test cases
  does not seem to be worth decreasing function self-dependency.

PR-URL: https://github.com/nodejs/node/pull/13770
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
2017-06-21 03:40:27 +03:00

41 lines
1.3 KiB
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const fs = require('fs');
const path = require('path');
process.chdir('..');
assert.notStrictEqual(process.cwd(), __dirname);
process.chdir(__dirname);
assert.strictEqual(process.cwd(), __dirname);
let dirName;
if (process.versions.icu) {
// ICU is available, use characters that could possibly be decomposed
dirName = 'weird \uc3a4\uc3ab\uc3af characters \u00e1\u00e2\u00e3';
} else {
// ICU is unavailable, use characters that can't be decomposed
dirName = 'weird \ud83d\udc04 characters \ud83d\udc05';
}
const dir = path.resolve(common.tmpDir, dirName);
// Make sure that the tmp directory is clean
common.refreshTmpDir();
fs.mkdirSync(dir);
process.chdir(dir);
assert.strictEqual(process.cwd().normalize(), dir.normalize());
process.chdir('..');
assert.strictEqual(process.cwd().normalize(),
path.resolve(common.tmpDir).normalize());
const errMessage = /^TypeError: Bad argument\.$/;
assert.throws(function() { process.chdir({}); },
errMessage, 'Bad argument.');
assert.throws(function() { process.chdir(); },
errMessage, 'Bad argument.');
assert.throws(function() { process.chdir('x', 'y'); },
errMessage, 'Bad argument.');