Skip to content

Commit

Permalink
Normalize path for comparison on Windows (#23404)
Browse files Browse the repository at this point in the history
Signed-off-by: Tyler Smalley <[email protected]>
  • Loading branch information
tylersmalley authored and Tyler Smalley committed Sep 24, 2018
1 parent 848de4d commit b3778f9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/ui/ui_exports/ui_export_types/style_sheet_paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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}]`
);
Expand Down
23 changes: 17 additions & 6 deletions src/ui/ui_exports/ui_export_types/style_sheet_paths.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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);
Expand All @@ -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);
});
});

0 comments on commit b3778f9

Please sign in to comment.