Files
azuredatastudio/extensions/sql-database-projects/src/test/testUtils.ts
Benjin Dubishar 80901c9a7b Swapping vscode calls for ApiWrapper for testability (#10267)
* swapping vscode calls for apiwrapper for testability

* Adding mainController tests

* Adding unit tests for input validation

* Adding project controller tests, reorganizing error handling

* Removing commented-out code
2020-05-06 14:16:27 -07:00

53 lines
1.9 KiB
TypeScript

/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as path from 'path';
import * as os from 'os';
import * as constants from '../common/constants';
import { promises as fs } from 'fs';
import should = require('should');
import { AssertionError } from 'assert';
export async function shouldThrowSpecificError(block: Function, expectedMessage: string, details?: string) {
let succeeded = false;
try {
await block();
succeeded = true;
}
catch (err) {
should(err.message).equal(expectedMessage);
}
if (succeeded) {
throw new AssertionError({ message: `Operation succeeded, but expected failure with exception: "${expectedMessage}".${details ? ' ' + details : ''}` });
}
}
export async function createTestSqlProj(contents: string, folderPath?: string): Promise<string> {
return await createTestFile(contents, 'TestProject.sqlproj', folderPath);
}
export async function createTestDataSources(contents: string, folderPath?: string): Promise<string> {
return await createTestFile(contents, constants.dataSourcesFileName, folderPath);
}
export async function generateTestFolderPath(): Promise<string> {
const folderPath = path.join(os.tmpdir(), `TestProject_${new Date().getTime()}`);
await fs.mkdir(folderPath, { recursive: true });
return folderPath;
}
async function createTestFile(contents: string, fileName: string, folderPath?: string): Promise<string> {
folderPath = folderPath ?? await generateTestFolderPath();
const filePath = path.join(folderPath, fileName);
await fs.mkdir(path.dirname(filePath), { recursive: true });
await fs.writeFile(filePath, contents);
return filePath;
}