Skip to content
This repository has been archived by the owner on Oct 25, 2023. It is now read-only.

Commit

Permalink
Add a helper to verify that two paths are pointing to the same destin…
Browse files Browse the repository at this point in the history
…ation (file or folder) (#97)
  • Loading branch information
Mykola Mokhnach committed Dec 5, 2018
1 parent 87385fe commit 5b04913
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
26 changes: 25 additions & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import B from 'bluebird';
import _ from 'lodash';
import os from 'os';
import path from 'path';
import fs from './fs';

const W3C_WEB_ELEMENT_IDENTIFIER = 'element-6066-11e4-a52e-4f735466cecf';

Expand Down Expand Up @@ -194,8 +195,31 @@ function isSubPath (originalPath, root, forcePosix = null) {
return normalizedPath.startsWith(normalizedRoot);
}

/**
* Checks whether the given paths are pointing to the same file system
* destination.
*
* @param {string} path1 - Absolute or relative path to a file/folder
* @param {string} path2 - Absolute or relative path to a file/folder
* @param {...string} pathN - Zero or more absolute or relative paths to files/folders
* @returns {boolean} true if all paths are pointing to the same file system item
*/
async function isSameDestination (path1, path2, ...pathN) {
const allPaths = [path1, path2, ...pathN];
if (!await B.reduce(allPaths, async (a, b) => a && await fs.exists(b), true)) {
return false;
}

const areAllItemsEqual = (arr) => !!arr.reduce((a, b) => a === b ? a : NaN);
if (areAllItemsEqual(allPaths)) {
return true;
}
return areAllItemsEqual(await B.map(allPaths, async (x) => (await fs.stat(x)).ino));
}

export {
hasValue, escapeSpace, escapeSpecialChars, localIp, cancellableDelay,
multiResolve, safeJsonParse, unwrapElement, filterObject,
toReadableSizeString, isSubPath, W3C_WEB_ELEMENT_IDENTIFIER
toReadableSizeString, isSubPath, W3C_WEB_ELEMENT_IDENTIFIER,
isSameDestination,
};
30 changes: 29 additions & 1 deletion test/util-specs.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@

import { util } from '..';
import { util, fs, tempDir } from '..';
import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import B from 'bluebird';
import sinon from 'sinon';
import os from 'os';
import path from 'path';

const {W3C_WEB_ELEMENT_IDENTIFIER} = util;

Expand Down Expand Up @@ -334,4 +335,31 @@ describe('util', function () {
should.throw(() => util.isSubPath('some/..', '/root'), /absolute/);
});
});

describe('isSameDestination', function () {
let path1;
let path2;
let tmpDir;
before(async function () {
tmpDir = await tempDir.openDir();
path1 = path.resolve(tmpDir, 'path1.txt');
path2 = path.resolve(tmpDir, 'path2.txt');
for (const p of [path1, path2]) {
await fs.writeFile(p, p, 'utf8');
}
});
after(async function () {
await fs.rimraf(tmpDir);
});
it('should match paths to the same file/folder', async function () {
(await util.isSameDestination(path1, path.resolve(tmpDir, '..', path.basename(tmpDir), path.basename(path1))))
.should.be.true;
});
it('should not match paths if they point to non existing items', async function () {
(await util.isSameDestination(path1, 'blabla')).should.be.false;
});
it('should not match paths to different files', async function () {
(await util.isSameDestination(path1, path2)).should.be.false;
});
});
});

0 comments on commit 5b04913

Please sign in to comment.