Remove build and folder includes when converting to SDK-style project (#18889)

* remove Build and Folder Includes

* add tests

* cleanup

* rollback if there was an error during the conversion
This commit is contained in:
Kim Santiago
2022-04-01 18:04:18 -07:00
committed by GitHub
parent a795e64bed
commit d6dab3dd18
7 changed files with 296 additions and 35 deletions

View File

@@ -1657,16 +1657,149 @@ describe('Project: legacy to SDK-style updates', function (): void {
});
it('Should update legacy style project to SDK-style', async function (): Promise<void> {
const projFilePath = await testUtils.createTestSqlProjFile(baselines.openProjectFileBaseline);
const projFilePath = await testUtils.createTestSqlProjFile(baselines.newProjectFileBaseline);
const list: Uri[] = [];
await testUtils.createDummyFileStructure(true, list, path.dirname(projFilePath));
const project = await Project.openProject(projFilePath);
await project.addToProject(list);
const beforeFileCount = project.files.filter(f => f.type === EntryType.File).length;
const beforeFolderCount = project.files.filter(f => f.type === EntryType.Folder).length;
should(beforeFolderCount).equal(2, 'There should be 2 folders in the project');
should(beforeFileCount).equal(11, 'There should be 11 files in the project');
should(project.importedTargets.length).equal(3, 'SSDT and ADS imports should be in the project');
should(project.isSdkStyleProject).equal(false);
let projFileText = (await fs.readFile(projFilePath)).toString();
should(projFileText.includes('<Build Include=')).equal(true, 'sqlproj should have Build Includes before converting');
should(projFileText.includes('<Folder Include=')).equal(true, 'sqlproj should have Folder Includes before converting');
await project.convertProjectToSdkStyle();
should(await exists(projFilePath + '_backup')).equal(true, 'Backup file should have been generated before the project was updated');
should(project.importedTargets.length).equal(0, 'SSDT and ADS imports should have been removed');
should(project.isSdkStyleProject).equal(true);
projFileText = (await fs.readFile(projFilePath)).toString();
should(projFileText.includes('<Build Include=')).equal(false, 'All Build Includes should have been removed');
should(projFileText.includes('<Folder Include=')).equal(false, 'All Folder Includes should have been removed');
should(project.files.filter(f => f.type === EntryType.File).length).equal(beforeFileCount, 'Same number of files should be included after Build Includes are removed');
should(project.files.filter(f => f.type === EntryType.Folder).length).equal(beforeFolderCount, 'Same number of folders should be included after Folder Includes are removed');
});
it('Should not fail if legacy style project does not have Properties folder in sqlproj', async function (): Promise<void> {
const projFilePath = await testUtils.createTestSqlProjFile(baselines.newProjectFileNoPropertiesFolderBaseline);
const list: Uri[] = [];
await testUtils.createDummyFileStructure(true, list, path.dirname(projFilePath));
const project = await Project.openProject(projFilePath);
await project.addToProject(list);
const beforeFolderCount = project.files.filter(f => f.type === EntryType.Folder).length;
await project.convertProjectToSdkStyle();
should(project.isSdkStyleProject).equal(true);
const projFileText = (await fs.readFile(projFilePath)).toString();
should(projFileText.includes('<Folder Include=')).equal(false, 'All Folder Includes should have been removed');
should(project.files.filter(f => f.type === EntryType.Folder).length).equal(beforeFolderCount, 'Same number of folders should be included after Folder Includes are removed');
});
it('Should exclude sql files that were not in previously included in legacy style sqlproj', async function (): Promise<void> {
const projFilePath = await testUtils.createTestSqlProjFile(baselines.newProjectFileBaseline);
const list: Uri[] = [];
await testUtils.createDummyFileStructure(true, list, path.dirname(projFilePath));
const project = await Project.openProject(projFilePath);
// don't add file1.sql, folder1\file1.sql and folder2\file1.sql
await project.addToProject(list.filter(f => !f.fsPath.includes('file1.sql')));
const beforeFileCount = project.files.filter(f => f.type === EntryType.File).length;
const beforeFolderCount = project.files.filter(f => f.type === EntryType.Folder).length;
should(beforeFileCount).equal(8, 'There should be 8 files in the project before converting');
await project.convertProjectToSdkStyle();
should(project.isSdkStyleProject).equal(true);
const projFileText = (await fs.readFile(projFilePath)).toString();
should(projFileText.includes('<Build Include=')).equal(false, 'All Build Includes should have been removed');
should(projFileText.match(/<Build Remove=\W+(?:\w+\W+){0,2}?file1.sql/g)?.length).equal(3, 'There should be 3 Build Removes for the 3 file1.sql files');
should(projFileText.includes('<Folder Include=')).equal(false, 'All Folder Includes should have been removed');
should(project.files.filter(f => f.type === EntryType.File).length).equal(beforeFileCount, 'Same number of files should be included after Build Includes are removed');
should(project.files.filter(f => f.type === EntryType.Folder).length).equal(beforeFolderCount, 'Same number of folders should be included after Folder Includes are removed');
});
it('Should keep Build Includes for files outside of project folder', async function (): Promise<void> {
const testFolderPath = await testUtils.generateTestFolderPath();
const mainProjectPath = path.join(testFolderPath, 'project');
const otherFolderPath = path.join(testFolderPath, 'other');
projFilePath = await testUtils.createTestSqlProjFile(baselines.newProjectFileBaseline, mainProjectPath);
let list: Uri[] = [];
await testUtils.createDummyFileStructure(true, list, path.dirname(projFilePath));
// create files outside of project folder that are included in the project file
await fs.mkdir(otherFolderPath);
const otherFiles = await testUtils.createOtherDummyFiles(otherFolderPath);
list = list.concat(otherFiles);
const project = await Project.openProject(projFilePath);
// add all the files, except the pre and post deploy scripts
await project.addToProject(list.filter(f => !f.fsPath.includes('Script.')));
const beforeFileCount = project.files.filter(f => f.type === EntryType.File).length;
const beforeFolderCount = project.files.filter(f => f.type === EntryType.Folder).length;
should(beforeFileCount).equal(19, 'There should be 19 files in the project');
await project.convertProjectToSdkStyle();
should(project.isSdkStyleProject).equal(true);
const projFileText = (await fs.readFile(projFilePath)).toString();
should(projFileText.match(/<Build Include=/g)?.length).equal(8, 'There should be Build includes for the 8 .sql files outside of the project folder');
should(projFileText.includes('<Folder Include=')).equal(false, 'All Folder Includes should have been removed');
should(project.files.filter(f => f.type === EntryType.File).length).equal(beforeFileCount, 'Same number of files should be included after Build Includes are removed');
should(project.files.filter(f => f.type === EntryType.Folder).length).equal(beforeFolderCount, 'Same number of folders should be included after Folder Includes are removed');
});
it('Should list previously included empty folders in sqlproj after converting to SDK-style', async function (): Promise<void> {
const projFilePath = await testUtils.createTestSqlProjFile(baselines.newProjectFileBaseline);
const list: Uri[] = [];
const folderPath = path.dirname(projFilePath);
await testUtils.createDummyFileStructure(true, list, folderPath);
const project = await Project.openProject(projFilePath);
await project.addToProject(list);
await project.addFolderItem('folder3');
await project.addFolderItem('folder3\\nestedFolder');
await project.addFolderItem('folder4');
const beforeFolderCount = project.files.filter(f => f.type === EntryType.Folder).length;
should(beforeFolderCount).equal(5);
await project.convertProjectToSdkStyle();
should(project.isSdkStyleProject).equal(true);
const projFileText = (await fs.readFile(projFilePath)).toString();
should(projFileText.includes('<Folder Include="folder3\\" />')).equal(false, 'There should not be a folder include for folder3\\nestedFolder because it gets included by the nestedFolder entry');
should(projFileText.includes('<Folder Include="folder3\\nestedFolder\\" />')).equal(true, 'There should be a folder include for folder3\\nestedFolder');
should(projFileText.includes('<Folder Include="folder4\\" />')).equal(true, 'There should be a folder include for folder4');
should(project.files.filter(f => f.type === EntryType.Folder).length).equal(beforeFolderCount, 'Same number of folders should be included after Folder Includes are removed');
});
it('Should rollback changes if there was an error during conversion to SDK-style', async function (): Promise<void> {
const folderPath = await testUtils.generateTestFolderPath();
const sqlProjPath = await testUtils.createTestSqlProjFile(baselines.newProjectFileBaseline, folderPath);
const project = await Project.openProject(Uri.file(sqlProjPath).fsPath);
should(project.isSdkStyleProject).equal(false);
// add an empty folder so that addFolderItem() will get called during the conversion. Empty folders aren't included by glob, so they need to be added to the sqlproj
// to show up in the project tree
await project.addFolderItem('folder1');
sinon.stub(Project.prototype, 'addFolderItem').throwsException('error');
const result = await project.convertProjectToSdkStyle();
should(result).equal(false);
should(project.isSdkStyleProject).equal(false);
should(project.importedTargets.length).equal(3, 'SSDT and ADS imports should still be there');
});
it('Should not update project and no backup file should be created when project is already SDK-style', async function (): Promise<void> {