/*--------------------------------------------------------------------------------------------- * 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 { return await createTestFile(contents, 'TestProject.sqlproj', folderPath); } export async function createTestDataSources(contents: string, folderPath?: string): Promise { return await createTestFile(contents, constants.dataSourcesFileName, folderPath); } export async function generateTestFolderPath(): Promise { 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 { 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; }