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

@@ -9,6 +9,7 @@ import { promises as fs } from 'fs';
// Project baselines
export let newProjectFileBaseline: string;
export let newProjectFileWithScriptBaseline: string;
export let newProjectFileNoPropertiesFolderBaseline: string;
export let openProjectFileBaseline: string;
export let openDataSourcesBaseline: string;
export let SSDTProjectFileBaseline: string;
@@ -41,6 +42,7 @@ const baselineFolderPath = __dirname;
export async function loadBaselines() {
newProjectFileBaseline = await loadBaseline(baselineFolderPath, 'newSqlProjectBaseline.xml');
newProjectFileWithScriptBaseline = await loadBaseline(baselineFolderPath, 'newSqlProjectWithScriptBaseline.xml');
newProjectFileNoPropertiesFolderBaseline = await loadBaseline(baselineFolderPath, 'newSqlProjectNoPropertiesFolderBaseline.xml');
openProjectFileBaseline = await loadBaseline(baselineFolderPath, 'openSqlProjectBaseline.xml');
openDataSourcesBaseline = await loadBaseline(baselineFolderPath, 'openDataSourcesBaseline.json');
SSDTProjectFileBaseline = await loadBaseline(baselineFolderPath, 'SSDTProjectBaseline.xml');

View File

@@ -0,0 +1,64 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<Name>TestProjectName</Name>
<SchemaVersion>2.0</SchemaVersion>
<ProjectVersion>4.1</ProjectVersion>
<ProjectGuid>{BA5EBA11-C0DE-5EA7-ACED-BABB1E70A575}</ProjectGuid>
<DSP>Microsoft.Data.Tools.Schema.Sql.Sql150DatabaseSchemaProvider</DSP>
<OutputType>Database</OutputType>
<RootPath>
</RootPath>
<RootNamespace>TestProjectName</RootNamespace>
<AssemblyName>TestProjectName</AssemblyName>
<ModelCollation>1033, CI</ModelCollation>
<DefaultFileStructure>BySchemaAndSchemaType</DefaultFileStructure>
<DeployToDatabase>True</DeployToDatabase>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<TargetLanguage>CS</TargetLanguage>
<AppDesignerFolder>Properties</AppDesignerFolder>
<SqlServerVerification>False</SqlServerVerification>
<IncludeCompositeObjects>True</IncludeCompositeObjects>
<TargetDatabaseSet>True</TargetDatabaseSet>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<OutputPath>bin\Release\</OutputPath>
<BuildScriptName>$(MSBuildProjectName).sql</BuildScriptName>
<TreatWarningsAsErrors>False</TreatWarningsAsErrors>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<DefineDebug>false</DefineDebug>
<DefineTrace>true</DefineTrace>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<OutputPath>bin\Debug\</OutputPath>
<BuildScriptName>$(MSBuildProjectName).sql</BuildScriptName>
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<DefineDebug>true</DefineDebug>
<DefineTrace>true</DefineTrace>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">11.0</VisualStudioVersion>
<!-- Default to the v11.0 targets path if the targets file for the current VS version is not found -->
<SSDTExists Condition="Exists('$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\SSDT\Microsoft.Data.Tools.Schema.SqlTasks.targets')">True</SSDTExists>
<VisualStudioVersion Condition="'$(SSDTExists)' == ''">11.0</VisualStudioVersion>
</PropertyGroup>
<Import Condition="'$(NetCoreBuild)' == 'true'" Project="$(NETCoreTargetsPath)\Microsoft.Data.Tools.Schema.SqlTasks.targets"/>
<Import Condition="'$(NetCoreBuild)' != 'true' AND '$(SQLDBExtensionsRefPath)' != ''" Project="$(SQLDBExtensionsRefPath)\Microsoft.Data.Tools.Schema.SqlTasks.targets" />
<Import Condition="'$(NetCoreBuild)' != 'true' AND '$(SQLDBExtensionsRefPath)' == ''" Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\SSDT\Microsoft.Data.Tools.Schema.SqlTasks.targets" />
<ItemGroup>
<PackageReference Condition="'$(NetCoreBuild)' == 'true'" Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0" PrivateAssets="All" />
</ItemGroup>
<Target Name="BeforeBuild">
<Delete Files="$(BaseIntermediateOutputPath)\project.assets.json" />
</Target>
</Project>

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> {

View File

@@ -203,9 +203,11 @@ export async function createListOfFiles(filePath?: string): Promise<Uri[]> {
* - Script.PostDeployment2.sql
*
*/
export async function createOtherDummyFiles(testFolderPath: string): Promise<string> {
export async function createOtherDummyFiles(testFolderPath: string): Promise<Uri[]> {
const filesList: Uri[] = [];
let filePath = path.join(testFolderPath, 'file1.sql');
await fs.writeFile(filePath, '');
filesList.push(Uri.file(filePath));
for (let dirCount = 1; dirCount <= 2; dirCount++) {
let dirName = path.join(testFolderPath, `folder${dirCount}`);
@@ -214,23 +216,31 @@ export async function createOtherDummyFiles(testFolderPath: string): Promise<str
for (let fileCount = 1; fileCount <= 2; fileCount++) {
let fileName = path.join(dirName, `file${fileCount}.sql`);
await fs.writeFile(fileName, '');
filesList.push(Uri.file(fileName));
}
}
const test1 = path.join(testFolderPath, 'folder1', 'test1.sql');
await fs.writeFile(test1, '');
filesList.push(Uri.file(test1));
const test2 = path.join(testFolderPath, 'folder1', 'test2.sql');
await fs.writeFile(test2, '');
filesList.push(Uri.file(test2));
const testLongerName = path.join(testFolderPath, 'folder1', 'testLongerName.sql');
await fs.writeFile(testLongerName, '');
filesList.push(Uri.file(testLongerName));
const preDeploymentScript = path.join(testFolderPath, 'folder2', 'Script.PreDeployment1.sql');
await fs.writeFile(preDeploymentScript, '');
filesList.push(Uri.file(preDeploymentScript));
const postDeploymentScript1 = path.join(testFolderPath, 'folder2', 'Script.PostDeployment1.sql');
await fs.writeFile(postDeploymentScript1, '');
filesList.push(Uri.file(preDeploymentScript));
const postDeploymentScript2 = path.join(testFolderPath, 'folder2', 'Script.PostDeployment2.sql');
await fs.writeFile(postDeploymentScript2, '');
return testFolderPath;
filesList.push(Uri.file(postDeploymentScript2));
return filesList;
}