Improve handling of absolute paths in inso (#2575)

This commit is contained in:
Opender Singh authored and GitHub committed 2020-08-31 21:42:39 +12:00
1 parent 3385a899d8
commit 99c47fc1e7
4 files changed
+47 -2

No files matched your search

@@ -4,6 +4,7 @@ import { writeFileWithCliOptions } from '../write-file';
import mkdirp from 'mkdirp';
import fs from 'fs';
import { InsoError } from '../errors';
import os from 'os';
jest.mock('mkdirp', () => ({
sync: jest.fn().mockResolvedValue(),
@@ -32,6 +33,26 @@ describe('writeFileWithCliOptions', () => {
await expect(promise).resolves.toBe('file.yaml');
});
it('should write to absolute output file', async () => {
const absolutePath = path.join(os.tmpdir(), 'dev', 'file.yaml');
const output = absolutePath;
const contents = 'contents';
const workingDir = undefined;
const promise = writeFileWithCliOptions(output, contents, workingDir);
await expect(promise).resolves.toBe(absolutePath);
});
it('should write to absolute output file and ignore working dir', async () => {
const absolutePath = path.join(os.tmpdir(), 'dev', 'file.yaml');
const output = absolutePath;
const contents = 'contents';
const workingDir = 'working/dir';
const promise = writeFileWithCliOptions(output, contents, workingDir);
await expect(promise).resolves.toBe(absolutePath);
});
it('should write to output file under working dir', async () => {
const output = 'file.yaml';
const contents = 'contents';
@@ -6,6 +6,7 @@ import { writeFileWithCliOptions } from '../../write-file';
import { globalBeforeAll, globalBeforeEach } from '../../../__jest__/before';
import logger from '../../logger';
import { InsoError } from '../../errors';
import os from 'os';
jest.mock('openapi-2-kong');
jest.mock('../../write-file');
@@ -120,6 +121,27 @@ describe('generateConfig()', () => {
expect(logger.__getLogs().log).toEqual([`Configuration generated to "${outputPath}".`]);
});
it('should generate documents using absolute path', async () => {
mock(o2k.generate).mockResolvedValue({ documents: ['a', 'b'] });
const outputPath = 'this-is-the-output-path';
mock(writeFileWithCliOptions).mockResolvedValue(outputPath);
const absolutePath = path.join(os.tmpdir(), 'dev', 'file.yaml');
const result = await generateConfig(absolutePath, {
type: 'kubernetes',
workingDir: 'test/dir',
output: 'output.yaml',
});
expect(result).toBe(true);
// Read from workingDir
expect(o2k.generate).toHaveBeenCalledWith(absolutePath, ConversionTypeMap.kubernetes);
expect(logger.__getLogs().log).toEqual([`Configuration generated to "${outputPath}".`]);
});
it('should throw InsoError if there is an error thrown by openapi-2-kong', async () => {
const error = new Error('err');
mock(o2k.generate).mockRejectedValue(error);
@@ -52,7 +52,9 @@ export async function generateConfig(
result = await o2k.generateFromString(specFromDb.contents, ConversionTypeMap[type]);
} else if (identifier) {
// try load as a file
const fileName = path.join(workingDir || '.', identifier);
const fileName = path.isAbsolute(identifier)
? identifier
: path.join(workingDir || '.', identifier);
logger.trace(`Generating config from file \`${fileName}\``);
result = await o2k.generate(fileName, ConversionTypeMap[type]);
}
+1 -1
View File
@@ -9,7 +9,7 @@ export async function writeFileWithCliOptions(
contents: string,
workingDir?: string,
): Promise<string> {
const outputPath = path.join(workingDir || '.', output);
const outputPath = path.isAbsolute(output) ? output : path.join(workingDir || '.', output);
try {
await mkdirp.sync(path.dirname(outputPath));
await fs.promises.writeFile(outputPath, contents);