private void InstallTargetsFileAndAssert(string expectedContent, bool expectCopy) { var installer = new TargetsInstaller(); var logger = new TestLogger(); installer.InstallLoaderTargets(logger, WorkingDirectory); foreach (var destinationDir in FileConstants.ImportBeforeDestinationDirectoryPaths) { var path = Path.Combine(destinationDir, FileConstants.ImportBeforeTargetsName); Assert.IsTrue(File.Exists(path), ".targets file not found at: " + path); Assert.AreEqual( expectedContent, File.ReadAllText(path), ".targets does not have expected content at " + path); Assert.IsTrue(logger.DebugMessages.Any(m => m.Contains(destinationDir))); } var targetsPath = Path.Combine(WorkingDirectory, "bin", "targets", FileConstants.IntegrationTargetsName); Assert.IsTrue(File.Exists(targetsPath), ".targets file not found at: " + targetsPath); if (expectCopy) { Assert.AreEqual( FileConstants.ImportBeforeDestinationDirectoryPaths.Count + 1, logger.DebugMessages.Count, "All destinations should have been covered"); } }
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 InstallTargetsFileAndAssert(string expectedContent, bool expectCopy) { TargetsInstaller installer = new TargetsInstaller(); TestLogger logger = new TestLogger(); installer.InstallLoaderTargets(logger); foreach (string destinationDir in TargetsInstaller.DestinationDirectories) { string path = Path.Combine(destinationDir, TargetsInstaller.LoaderTargetsName); Assert.IsTrue(File.Exists(path), ".targets file not found at: " + path); Assert.AreEqual( expectedContent, File.ReadAllText(path), ".targets does not have expected content at " + path); Assert.IsTrue(logger.DebugMessages.Any(m => m.Contains(destinationDir))); } if (expectCopy) { Assert.AreEqual( TargetsInstaller.DestinationDirectories.Count, logger.DebugMessages.Count, "All destinations should have been covered"); } }
private void InstallLoaderTargets_InternalCopyTargetsFile(string sourceContent, bool destinationExists, string destinationContent = null) { // Arrange var targetsInstaller = new TargetsInstaller(logger, msBuildPathSettingsMock.Object, fileWrapperMock.Object, directoryWrapperMock.Object); var sourcePathRegex = "bin\\\\(?:debug|release)\\\\targets\\\\SonarQube.Integration.ImportBefore.targets"; msBuildPathSettingsMock .Setup(x => x.GetImportBeforePaths()) .Returns(new[] { "c:\\global paths" }); fileWrapperMock .Setup(x => x.ReadAllText(It.IsRegex(sourcePathRegex, RegexOptions.IgnoreCase))) .Returns(sourceContent); fileWrapperMock .Setup(x => x.ReadAllText("c:\\global paths\\SonarQube.Integration.ImportBefore.targets")) .Returns(destinationContent); fileWrapperMock .Setup(x => x.Exists("c:\\global paths\\SonarQube.Integration.ImportBefore.targets")) .Returns(destinationExists); // Act using (new AssertIgnoreScope()) { targetsInstaller.InstallLoaderTargets("c:\\project"); } // Assert var sameContent = sourceContent.Equals(destinationContent); if (!destinationExists || !sameContent) { var shouldOverwrite = destinationExists && !sameContent; fileWrapperMock .Verify(x => x.Copy(It.IsRegex(sourcePathRegex, RegexOptions.IgnoreCase), "c:\\global paths\\SonarQube.Integration.ImportBefore.targets", shouldOverwrite), Times.Once); } else { fileWrapperMock.Verify( x => x.Copy(It.IsRegex(sourcePathRegex, RegexOptions.IgnoreCase), "c:\\global paths\\SonarQube.Integration.ImportBefore.targets", It.IsAny <bool>()), Times.Never); } }
public void InstallLoaderTargets_Running_Under_LocalSystem_Account() { // Arrange var targetsInstaller = new TargetsInstaller(logger, msBuildPathSettingsMock.Object, fileWrapperMock.Object, directoryWrapperMock.Object); msBuildPathSettingsMock.Setup(x => x.GetImportBeforePaths()).Returns(new[] { "c:\\windows\\system32\\appdata" }); // Act using (new AssertIgnoreScope()) { targetsInstaller.InstallLoaderTargets("c:\\project"); } // Assert Assert.IsTrue(logger.Warnings.Any(m => m.StartsWith("Running the Scanner for MSBuild under Local System or Network Service account is not supported."))); }
public void InstallLoaderTargets_GlobalTargets_NotExist() { // Arrange var targetsInstaller = new TargetsInstaller(logger, msBuildPathSettingsMock.Object, fileWrapperMock.Object, directoryWrapperMock.Object); msBuildPathSettingsMock.Setup(x => x.GetGlobalTargetsPaths()).Returns(new[] { "global" }); fileWrapperMock.Setup(x => x.Exists("global\\SonarQube.Integration.ImportBefore.targets")).Returns(false); // Act using (new AssertIgnoreScope()) { targetsInstaller.InstallLoaderTargets("c:\\project"); } // Assert Assert.IsFalse(logger.Warnings.Any(m => m.StartsWith("This version of the SonarScanner for MSBuild automatically deploys"))); }
private void InstallLoaderTargets_InternalCopyTargetFileToProject(string sourceContent, bool destinationExists, string destinationContent = null) { // Arrange var targetsInstaller = new TargetsInstaller(this.logger, this.msBuildPathSettingsMock.Object, this.fileWrapperMock.Object, this.directoryWrapperMock.Object); var sourcePathRegex = "bin\\\\(?:debug|release)\\\\targets\\\\SonarQube.Integration.targets"; this.fileWrapperMock .Setup(x => x.ReadAllText(It.IsRegex(sourcePathRegex, RegexOptions.IgnoreCase))) .Returns(sourceContent); this.fileWrapperMock .Setup(x => x.ReadAllText("c:\\project\\bin\\targets\\SonarQube.Integration.targets")) .Returns(destinationContent); this.fileWrapperMock .Setup(x => x.Exists("c:\\project\\bin\\targets\\SonarQube.Integration.targets")) .Returns(destinationExists); // Act using (new AssertIgnoreScope()) { targetsInstaller.InstallLoaderTargets("c:\\project"); } // Assert var sameContent = sourceContent.Equals(destinationContent); if (!destinationExists || !sameContent) { // Copy is executed once, overwriting existing files this.fileWrapperMock.Verify( x => x.Copy(It.IsRegex(sourcePathRegex, RegexOptions.IgnoreCase), "c:\\project\\bin\\targets\\SonarQube.Integration.targets", true), Times.Once); } else { // Copy is not executed this.fileWrapperMock.Verify( x => x.Copy(It.IsRegex(sourcePathRegex, RegexOptions.IgnoreCase), "c:\\project\\bin\\targets\\SonarQube.Integration.targets", It.IsAny <bool>()), Times.Never); } }
public void InstallLoaderTargets_Running_Under_LocalSystem_Account() { // Arrange var targetsInstaller = new TargetsInstaller(this.logger, this.msBuildPathSettingsMock.Object, this.fileWrapperMock.Object, this.directoryWrapperMock.Object); this.msBuildPathSettingsMock.Setup(x => x.GetImportBeforePaths()).Returns(new[] { "c:\\windows\\system32\\appdata" }); // Act Action act = () => { using (new AssertIgnoreScope()) { targetsInstaller.InstallLoaderTargets("c:\\project"); } }; // Assert act.Should().NotThrow(); }
public void InstallLoaderTargets_Running_Under_LocalSystem_Account() { // Arrange var targetsInstaller = new TargetsInstaller(logger, msBuildPathSettingsMock.Object, fileWrapperMock.Object, directoryWrapperMock.Object); msBuildPathSettingsMock.Setup(x => x.GetImportBeforePaths()).Returns(new[] { "c:\\windows\\system32\\appdata" }); // Act Action act = () => { using (new AssertIgnoreScope()) { targetsInstaller.InstallLoaderTargets("c:\\project"); } }; // Assert act.Should().ThrowExactly <AnalysisException>().And.Message.Should() .Be("Running the Scanner for MSBuild under Local System or Network Service account is not supported. Please, use a local or domain user account instead."); }
private void InstallLoaderTargets_InternalCopyTargetsFile(string sourceContent, bool destinationExists, string destinationContent = null) { // Arrange var targetsInstaller = new TargetsInstaller(this.logger, this.msBuildPathSettingsMock.Object, this.fileWrapperMock.Object, this.directoryWrapperMock.Object); var sourcePath = Path.Combine(TestContext.DeploymentDirectory, "Targets", "SonarQube.Integration.ImportBefore.targets"); this.msBuildPathSettingsMock .Setup(x => x.GetImportBeforePaths()) .Returns(new[] { "c:\\global paths" }); this.fileWrapperMock .Setup(x => x.ReadAllText(sourcePath)) .Returns(sourceContent); this.fileWrapperMock .Setup(x => x.ReadAllText("c:\\global paths\\SonarQube.Integration.ImportBefore.targets")) .Returns(destinationContent); this.fileWrapperMock .Setup(x => x.Exists("c:\\global paths\\SonarQube.Integration.ImportBefore.targets")) .Returns(destinationExists); // Act using (new AssertIgnoreScope()) { targetsInstaller.InstallLoaderTargets("c:\\project"); } // Assert var sameContent = sourceContent.Equals(destinationContent); if (!destinationExists || !sameContent) { // Copy is executed once, overwriting existing files this.fileWrapperMock.Verify(x => x.Copy(sourcePath, "c:\\global paths\\SonarQube.Integration.ImportBefore.targets", true), Times.Once); } else { // Copy is not executed this.fileWrapperMock.Verify(x => x.Copy(sourcePath, "c:\\global paths\\SonarQube.Integration.ImportBefore.targets", It.IsAny <bool>()), Times.Never); } }
public void InstallLoaderTargets_ExceptionsOnCopyAreSuppressed() { // Arrange var targetsInstaller = new TargetsInstaller(this.logger, this.msBuildPathSettingsMock.Object, this.fileWrapperMock.Object, this.directoryWrapperMock.Object); bool exceptionThrown = false; var sourcePathRegex = "bin\\\\(?:debug|release)\\\\targets\\\\SonarQube.Integration.targets"; this.fileWrapperMock .Setup(x => x.ReadAllText(It.IsRegex(sourcePathRegex, RegexOptions.IgnoreCase))) .Returns("sourceContent"); this.fileWrapperMock .Setup(x => x.Exists("c:\\project\\bin\\targets\\SonarQube.Integration.targets")) .Returns(false); this.fileWrapperMock .Setup(x => x.Copy(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <bool>())) .Callback(() => { exceptionThrown = true; throw new InvalidOperationException("This exception should be caught and suppressed by the product code"); }); // Act using (new AssertIgnoreScope()) { targetsInstaller.InstallLoaderTargets("c:\\project"); } // Assert exceptionThrown.Should().BeTrue(); this.logger.AssertSingleWarningExists("This exception should be caught and suppressed by the product code"); }