Fixing bug where templates may get mapped before loaded from their files (#10111)

* Fixing potential bad order of template loading and map construction

* Fixing bug where templates get mapped before loaded from file

* Parallelizing loading templates from file
This commit is contained in:
Benjin Dubishar
2020-04-22 17:01:19 -07:00
committed by GitHub
parent 8311c3985d
commit 52f33cc587
7 changed files with 134 additions and 64 deletions

View File

@@ -0,0 +1,39 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as should from 'should';
import * as path from 'path';
import * as templates from '../templates/templates';
import { shouldThrowSpecificError } from './testUtils';
describe('Templates: loading templates from disk', function (): void {
beforeEach(() => {
templates.reset();
});
it('Should throw error when attempting to use templates before loaded from file', async function (): Promise<void> {
shouldThrowSpecificError(() => templates.projectScriptTypeMap(), 'Templates must be loaded from file before attempting to use.');
shouldThrowSpecificError(() => templates.projectScriptTypes(), 'Templates must be loaded from file before attempting to use.');
});
it('Should load all templates from files', async function (): Promise<void> {
await templates.loadTemplates(path.join(__dirname, '..', '..', 'resources', 'templates'));
// check expected counts
const numScriptObjectTypes = 4;
should(templates.projectScriptTypes().length).equal(numScriptObjectTypes);
should(Object.keys(templates.projectScriptTypes()).length).equal(numScriptObjectTypes);
// check everything has a value
should(templates.newSqlProjectTemplate).not.equal(undefined);
for (const obj of templates.projectScriptTypes()) {
should(obj.templateScript).not.equal(undefined);
}
});
});

View File

@@ -8,6 +8,23 @@ 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 function shouldThrowSpecificError(block: Function, expectedMessage: string) {
let succeeded = false;
try {
block();
succeeded = true;
}
catch (err) {
should(err.message).equal(expectedMessage);
}
if (succeeded) {
throw new AssertionError({ message: 'Operation succeeded, but expected failure with exception: "' + expectedMessage + '"' });
}
}
export async function createTestSqlProj(contents: string, folderPath?: string): Promise<string> {
return await createTestFile(contents, 'TestProject.sqlproj', folderPath);