Normalize path for comparison on Windows (#23404)

Signed-off-by: Tyler Smalley <tyler.smalley@elastic.co>
This commit is contained in:
Tyler Smalley 2018-09-23 21:13:43 -07:00 committed by Tyler Smalley
parent 848de4dbfb
commit b3778f9899
2 changed files with 19 additions and 8 deletions

View file

@ -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}]`
);

View file

@ -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);
});
});