From b3778f98994e383ded0d8fbf81068669b7ed2af6 Mon Sep 17 00:00:00 2001 From: Tyler Smalley Date: Sun, 23 Sep 2018 21:13:43 -0700 Subject: [PATCH] Normalize path for comparison on Windows (#23404) Signed-off-by: Tyler Smalley --- .../ui_export_types/style_sheet_paths.js | 4 ++-- .../ui_export_types/style_sheet_paths.test.js | 23 ++++++++++++++----- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/ui/ui_exports/ui_export_types/style_sheet_paths.js b/src/ui/ui_exports/ui_export_types/style_sheet_paths.js index 569c80379fb7f1..81644a494288a1 100644 --- a/src/ui/ui_exports/ui_export_types/style_sheet_paths.js +++ b/src/ui/ui_exports/ui_export_types/style_sheet_paths.js @@ -25,7 +25,7 @@ const OK_EXTNAMES = ['.css', '.scss']; function normalize(localPath, type, pluginSpec) { const pluginId = pluginSpec.getId(); - const publicDir = pluginSpec.getPublicDir(); + const publicDir = path.normalize(pluginSpec.getPublicDir()); const extname = path.extname(localPath); if (!OK_EXTNAMES.includes(extname)) { @@ -40,7 +40,7 @@ function normalize(localPath, type, pluginSpec) { ); } - if (!localPath.startsWith(publicDir)) { + if (!path.normalize(localPath).startsWith(publicDir)) { throw new Error( `[plugin:${pluginId}] uiExports.styleSheetPaths must be child of publicDir [${publicDir}]` ); diff --git a/src/ui/ui_exports/ui_export_types/style_sheet_paths.test.js b/src/ui/ui_exports/ui_export_types/style_sheet_paths.test.js index 7fa7fec412b445..6616ec3731cbda 100644 --- a/src/ui/ui_exports/ui_export_types/style_sheet_paths.test.js +++ b/src/ui/ui_exports/ui_export_types/style_sheet_paths.test.js @@ -17,22 +17,25 @@ * under the License. */ +import { resolve } from 'path'; +import { tmpdir } from 'os'; import { styleSheetPaths } from './style_sheet_paths'; describe('uiExports.styleSheetPaths', () => { + const dir = tmpdir(); const pluginSpec = { - getId: () => 'test', - getPublicDir: () => '/kibana/public' + getId: jest.fn(() => 'test'), + getPublicDir: jest.fn(() => resolve(dir, 'kibana/public')) }; it('does not support relative paths', () => { expect(() => styleSheetPaths([], 'public/bar.css', 'styleSheetPaths', pluginSpec)) - .toThrowError('[plugin:test] uiExports.styleSheetPaths must be an absolute path, got "public/bar.css"'); + .toThrowError(/\[plugin:test\] uiExports.styleSheetPaths must be an absolute path/); }); it('path must be child of public path', () => { expect(() => styleSheetPaths([], '/another/public/bar.css', 'styleSheetPaths', pluginSpec)) - .toThrowError('[plugin:test] uiExports.styleSheetPaths must be child of publicDir [/kibana/public]'); + .toThrowError(/\[plugin:test\] uiExports.styleSheetPaths must be child of publicDir/); }); it('only supports css or scss extensions', () => { @@ -41,7 +44,7 @@ describe('uiExports.styleSheetPaths', () => { }); it('provides publicPath for scss extensions', () => { - const localPath = '/kibana/public/bar.scss'; + const localPath = resolve(dir, 'kibana/public/bar.scss'); const uiExports = styleSheetPaths([], localPath, 'styleSheetPaths', pluginSpec); expect(uiExports.styleSheetPaths).toHaveLength(1); @@ -50,11 +53,19 @@ describe('uiExports.styleSheetPaths', () => { }); it('provides publicPath for css extensions', () => { - const localPath = '/kibana/public/bar.css'; + const localPath = resolve(dir, 'kibana/public/bar.scss'); const uiExports = styleSheetPaths([], localPath, 'styleSheetPaths', pluginSpec); expect(uiExports.styleSheetPaths).toHaveLength(1); expect(uiExports.styleSheetPaths[0].localPath).toEqual(localPath); expect(uiExports.styleSheetPaths[0].publicPath).toEqual('plugins/test/bar.css'); }); + + it('should normalize mixed slashes', () => { + const localPath = resolve(dir, 'kibana/public\\bar.scss'); + const uiExports = styleSheetPaths([], localPath, 'styleSheetPaths', pluginSpec); + + expect(uiExports.styleSheetPaths).toHaveLength(1); + expect(uiExports.styleSheetPaths[0].localPath).toEqual(localPath); + }); }); \ No newline at end of file