fix: Accept absolute paths in CliRunner

This commit is contained in:
Joachim Van Herwegen 2021-01-19 10:22:21 +01:00
parent 36761e8124
commit cf6270d161
4 changed files with 42 additions and 7 deletions

View File

@ -5,7 +5,7 @@ import type { IComponentsManagerBuilderOptions, LogLevel } from 'componentsjs';
import { ComponentsManager } from 'componentsjs'; import { ComponentsManager } from 'componentsjs';
import yargs from 'yargs'; import yargs from 'yargs';
import { getLoggerFor } from '../logging/LogUtil'; import { getLoggerFor } from '../logging/LogUtil';
import { joinFilePath, ensureTrailingSlash } from '../util/PathUtil'; import { joinFilePath, ensureTrailingSlash, absoluteFilePath } from '../util/PathUtil';
import type { Initializer } from './Initializer'; import type { Initializer } from './Initializer';
export class CliRunner { export class CliRunner {
@ -71,7 +71,7 @@ export class CliRunner {
*/ */
protected resolveFilePath(cwdPath?: string | null, modulePath = ''): string { protected resolveFilePath(cwdPath?: string | null, modulePath = ''): string {
return typeof cwdPath === 'string' ? return typeof cwdPath === 'string' ?
joinFilePath(process.cwd(), cwdPath) : absoluteFilePath(cwdPath) :
joinFilePath(__dirname, '../../', modulePath); joinFilePath(__dirname, '../../', modulePath);
} }

View File

@ -1,4 +1,4 @@
import { posix } from 'path'; import { posix, win32 } from 'path';
import type { ResourceIdentifier } from '../ldp/representation/ResourceIdentifier'; import type { ResourceIdentifier } from '../ldp/representation/ResourceIdentifier';
/** /**
@ -13,7 +13,7 @@ function windowsToPosixPath(path: string): string {
} }
/** /**
* Resolves relative segments in the path/ * Resolves relative segments in the path.
* *
* @param path - Path to check (POSIX or Windows). * @param path - Path to check (POSIX or Windows).
* *
@ -35,6 +35,26 @@ export function joinFilePath(basePath: string, ...paths: string[]): string {
return posix.join(windowsToPosixPath(basePath), ...paths); return posix.join(windowsToPosixPath(basePath), ...paths);
} }
/**
* Resolves a path to its absolute form.
* Absolute inputs will not be changed (except changing Windows to POSIX).
* Relative inputs will be interpreted relative to process.cwd().
*
* @param path - Path to check (POSIX or Windows).
*
* @returns The potentially changed path (POSIX).
*/
export function absoluteFilePath(path: string): string {
if (posix.isAbsolute(path)) {
return path;
}
if (win32.isAbsolute(path)) {
return windowsToPosixPath(path);
}
return joinFilePath(process.cwd(), path);
}
/** /**
* Makes sure the input path has exactly 1 slash at the end. * Makes sure the input path has exactly 1 slash at the end.
* Multiple slashes will get merged into one. * Multiple slashes will get merged into one.

View File

@ -104,7 +104,7 @@ describe('CliRunner', (): void => {
'urn:solid-server:default:variable:loggingLevel': 'debug', 'urn:solid-server:default:variable:loggingLevel': 'debug',
'urn:solid-server:default:variable:podTemplateFolder': '/var/cwd/templates', 'urn:solid-server:default:variable:podTemplateFolder': '/var/cwd/templates',
'urn:solid-server:default:variable:port': 4000, 'urn:solid-server:default:variable:port': 4000,
'urn:solid-server:default:variable:rootFilePath': '/var/cwd/root', 'urn:solid-server:default:variable:rootFilePath': '/root',
'urn:solid-server:default:variable:sparqlEndpoint': 'http://localhost:5000/sparql', 'urn:solid-server:default:variable:sparqlEndpoint': 'http://localhost:5000/sparql',
}, },
}, },

View File

@ -1,4 +1,5 @@
import { import {
absoluteFilePath,
decodeUriPathComponents, decodeUriPathComponents,
encodeUriPathComponents, encodeUriPathComponents,
ensureTrailingSlash, ensureTrailingSlash,
@ -8,7 +9,7 @@ import {
} from '../../../src/util/PathUtil'; } from '../../../src/util/PathUtil';
describe('PathUtil', (): void => { describe('PathUtil', (): void => {
describe('normalizeFilePath', (): void => { describe('#normalizeFilePath', (): void => {
it('normalizes POSIX paths.', async(): Promise<void> => { it('normalizes POSIX paths.', async(): Promise<void> => {
expect(normalizeFilePath('/foo/bar/../baz')).toEqual('/foo/baz'); expect(normalizeFilePath('/foo/bar/../baz')).toEqual('/foo/baz');
}); });
@ -18,7 +19,7 @@ describe('PathUtil', (): void => {
}); });
}); });
describe('joinFilePath', (): void => { describe('#joinFilePath', (): void => {
it('joins POSIX paths.', async(): Promise<void> => { it('joins POSIX paths.', async(): Promise<void> => {
expect(joinFilePath('/foo/bar/', '..', '/baz')).toEqual('/foo/baz'); expect(joinFilePath('/foo/bar/', '..', '/baz')).toEqual('/foo/baz');
}); });
@ -28,6 +29,20 @@ describe('PathUtil', (): void => {
}); });
}); });
describe('#absoluteFilePath', (): void => {
it('does not change absolute posix paths.', async(): Promise<void> => {
expect(absoluteFilePath('/foo/bar/')).toEqual('/foo/bar/');
});
it('converts absolute win32 paths to posix paths.', async(): Promise<void> => {
expect(absoluteFilePath('C:\\foo\\bar')).toEqual('C:/foo/bar');
});
it('makes relative paths absolute.', async(): Promise<void> => {
expect(absoluteFilePath('foo/bar/')).toEqual(joinFilePath(process.cwd(), 'foo/bar/'));
});
});
describe('#ensureTrailingSlash', (): void => { describe('#ensureTrailingSlash', (): void => {
it('makes sure there is always exactly 1 slash.', async(): Promise<void> => { it('makes sure there is always exactly 1 slash.', async(): Promise<void> => {
expect(ensureTrailingSlash('http://test.com')).toEqual('http://test.com/'); expect(ensureTrailingSlash('http://test.com')).toEqual('http://test.com/');