1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-09 08:45:55 +02:00
joplin/packages/lib/fsDriver.test.ts

32 lines
1.4 KiB
TypeScript

import FsDriverNode from './fs-driver-node';
import shim from './shim';
import { expectThrow } from './testing/test-utils';
const windowsPartitionLetter = __filename[0];
// On Windows, path.resolve is going to convert a path such as
// /tmp/file.txt to {partition}:\tmp\file.txt
function platformPath(path: string) {
if (shim.isWindows()) {
return `${windowsPartitionLetter}:${path.replace(/\//g, '\\')}`;
} else {
return path;
}
}
describe('fsDriver', () => {
it('should resolveRelativePathWithinDir', async () => {
const fsDriver = new FsDriverNode();
expect(fsDriver.resolveRelativePathWithinDir('/test/temp', './my/file.txt').toLowerCase()).toBe(platformPath('/test/temp/my/file.txt'));
expect(fsDriver.resolveRelativePathWithinDir('/', './test').toLowerCase()).toBe(platformPath('/test'));
expect(fsDriver.resolveRelativePathWithinDir('/test', 'myfile.txt').toLowerCase()).toBe(platformPath('/test/myfile.txt'));
expect(fsDriver.resolveRelativePathWithinDir('/test/temp', './mydir/../test.txt').toLowerCase()).toBe(platformPath('/test/temp/test.txt'));
await expectThrow(() => fsDriver.resolveRelativePathWithinDir('/test/temp', '../myfile.txt'));
await expectThrow(() => fsDriver.resolveRelativePathWithinDir('/test/temp', './mydir/../../test.txt'));
await expectThrow(() => fsDriver.resolveRelativePathWithinDir('/test/temp', '/var/local/no.txt'));
});
});