From 69e8b7fbc738a4a09fdbe096c2aa89ffa147a58e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Bazyd=C5=82o?= Date: Mon, 4 Jun 2018 23:10:19 +0200 Subject: [PATCH] Fix oc-cli breaking on Windows when modifying files --- src/cli/domain/watch.js | 6 +++++- test/unit/cli-domain-watch.js | 37 +++++++++++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/cli/domain/watch.js b/src/cli/domain/watch.js index 6196e3591..31ca4fe41 100644 --- a/src/cli/domain/watch.js +++ b/src/cli/domain/watch.js @@ -19,7 +19,7 @@ module.exports = function(dirs, baseDir, changed) { (fileName, currentStat, previousStat) => { if (!!currentStat || !!previousStat) { const componentDir = dirs.find(dir => - Boolean(fileName.match(dir + path.sep)) + Boolean(fileName.match(escapeRegularExpression(dir + path.sep))) ); changed(null, fileName, componentDir); } @@ -29,3 +29,7 @@ module.exports = function(dirs, baseDir, changed) { changed(err); } }; + +function escapeRegularExpression(text) { + return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&'); +} diff --git a/test/unit/cli-domain-watch.js b/test/unit/cli-domain-watch.js index d14d0c8b7..9534f7de5 100644 --- a/test/unit/cli-domain-watch.js +++ b/test/unit/cli-domain-watch.js @@ -5,7 +5,7 @@ const injectr = require('injectr'); const sinon = require('sinon'); describe('cli : domain : watch', () => { - const execute = (fileChanged, cb) => { + const execute = (fileChanged, separator, cb) => { class Stats { constructor(settings) { this.stuff = settings.stuff; @@ -15,7 +15,7 @@ describe('cli : domain : watch', () => { const watch = injectr('../../src/cli/domain/watch.js', { path: { resolve: x => x, - sep: '/' + sep: separator }, watch: { watchTree: sinon @@ -30,6 +30,7 @@ describe('cli : domain : watch', () => { watch( [ + 'C:\\Windows-like\\path\\to\\yet-another-component', '/path/to/my-component', '/path/to/my-component2', '/path/to/some-other-component' @@ -44,6 +45,7 @@ describe('cli : domain : watch', () => { before(done => { execute( '/path/to/my-component/server.js', + '/', (error, fileName, componentDir) => { result = { error, fileName, componentDir }; done(); @@ -69,6 +71,7 @@ describe('cli : domain : watch', () => { before(done => { execute( '/path/to/my-component2/server.js', + '/', (error, fileName, componentDir) => { result = { error, fileName, componentDir }; done(); @@ -88,4 +91,34 @@ describe('cli : domain : watch', () => { expect(result.componentDir).to.equal('/path/to/my-component2'); }); }); + + describe('when a file from a component on Windows-like path changes', () => { + let result; + before(done => { + execute( + 'C:\\Windows-like\\path\\to\\yet-another-component\\server.js', + '\\', + (error, fileName, componentDir) => { + result = { error, fileName, componentDir }; + done(); + } + ); + }); + + it('should return no error', () => { + expect(result.error).to.be.null; + }); + + it('should return the fileName', () => { + expect(result.fileName).to.equal( + 'C:\\Windows-like\\path\\to\\yet-another-component\\server.js' + ); + }); + + it('should return the component folder', () => { + expect(result.componentDir).to.equal( + 'C:\\Windows-like\\path\\to\\yet-another-component' + ); + }); + }); });