private void InstallTargetsFileAndAssert(string expectedContent, bool expectCopy) { var localLogger = new TestLogger(); var installer = new TargetsInstaller(localLogger); using (new AssertIgnoreScope()) { installer.InstallLoaderTargets(this.WorkingDirectory); } var msBuildPathSettings = new MsBuildPathSettings(); foreach (var destinationDir in msBuildPathSettings.GetImportBeforePaths()) { var path = Path.Combine(destinationDir, FileConstants.ImportBeforeTargetsName); File.Exists(path).Should().BeTrue(".targets file not found at: " + path); File.ReadAllText(path).Should().Be(expectedContent, ".targets does not have expected content at " + path); localLogger.DebugMessages.Any(m => m.Contains(destinationDir)).Should().BeTrue(); } var targetsPath = Path.Combine(this.WorkingDirectory, "bin", "targets", FileConstants.IntegrationTargetsName); File.Exists(targetsPath).Should().BeTrue(".targets file not found at: " + targetsPath); if (expectCopy) { localLogger.DebugMessages.Should().HaveCount(msBuildPathSettings.GetImportBeforePaths().Count() + 1, "All destinations should have been covered"); } }
private static void CleanupMsbuildDirectories() { // SONARMSBRU-149: we used to deploy the targets file to the 4.0 directory but this // is no longer supported. To be on the safe side we'll clean up the old location too. IList <string> cleanUpDirs = new MsBuildPathSettings().GetImportBeforePaths().ToList(); var appData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); cleanUpDirs.Add(Path.Combine(appData, "Microsoft", "MSBuild", "4.0", "Microsoft.Common.targets", "ImportBefore")); foreach (var destinationDir in cleanUpDirs) { var path = Path.Combine(destinationDir, FileConstants.ImportBeforeTargetsName); if (File.Exists(path)) { File.Delete(path); } } }
public void InstallTargetsFile_Overwrite() { // In case the dummy targets file somehow does not get deleted (e.g. when debugging), make sure its content valid XML var sourceTargetsContent1 = @"<Project ToolsVersion=""4.0"" xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"" />"; var sourceTargetsContent2 = @"<Project ToolsVersion=""12.0"" xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"" />"; CreateDummySourceTargetsFile(sourceTargetsContent1); var msBuildPathSettings = new MsBuildPathSettings(); InstallTargetsFileAndAssert(sourceTargetsContent1, expectCopy: true); Assert.AreEqual(6, msBuildPathSettings.GetImportBeforePaths().Count(), "Expecting six destination directories"); var path = Path.Combine(msBuildPathSettings.GetImportBeforePaths().First(), FileConstants.ImportBeforeTargetsName); File.Delete(path); CreateDummySourceTargetsFile(sourceTargetsContent2); InstallTargetsFileAndAssert(sourceTargetsContent2, expectCopy: true); InstallTargetsFileAndAssert(sourceTargetsContent2, expectCopy: true); }