public void PreProc_NonEmptySonarRunnerProperties() { // Checks the ruleset generator is called with the expected arguments // Arrange string testDir = TestUtils.CreateTestSpecificFolder(this.TestContext); string propertiesFile = CreatePropertiesFile(testDir, "my url", "my user name", "my password"); MockPropertiesFetcher mockPropertiesFetcher = new MockPropertiesFetcher(); MockRulesetGenerator mockRulesetGenerator = new MockRulesetGenerator(); TestLogger logger = new TestLogger(); string expectedConfigFileName; using (PreprocessTestUtils.CreateValidLegacyTeamBuildScope("tfs uri", "build uri", testDir)) { TeamBuildSettings settings = TeamBuildSettings.GetSettingsFromEnvironment(new ConsoleLogger()); Assert.IsNotNull(settings, "Test setup error: TFS environment variables have not been set correctly"); expectedConfigFileName = settings.AnalysisConfigFilePath; TeamBuildPreProcessor preProcessor = new TeamBuildPreProcessor(mockPropertiesFetcher, mockRulesetGenerator); // Act preProcessor.Execute(logger, "key", "name", "ver", propertiesFile); } // Assert mockPropertiesFetcher.AssertFetchPropertiesCalled(); mockPropertiesFetcher.CheckFetcherArguments("my url", "key"); mockRulesetGenerator.AssertGenerateCalled(); mockRulesetGenerator.CheckGeneratorArguments("my url", "key"); logger.AssertErrorsLogged(0); logger.AssertWarningsLogged(0); }
public void PreProc_NoPlugin() { // Arrange string workingDir = TestUtils.CreateTestSpecificFolder(this.TestContext); TestLogger logger = new TestLogger(); // Configure the server MockSonarQubeServer mockServer = new MockSonarQubeServer(); ServerDataModel data = mockServer.Data; data.ServerProperties.Add("server.key", "server value 1"); data.Languages.Add("invalid_plugin"); MockRoslynAnalyzerProvider mockAnalyzerProvider = new MockRoslynAnalyzerProvider { SettingsToReturn = new AnalyzerSettings { RuleSetFilePath = "c:\\xxx.ruleset" } }; Mock <ITargetsInstaller> mockTargetsInstaller = new Mock <ITargetsInstaller>(); MockObjectFactory mockFactory = new MockObjectFactory(mockServer, mockTargetsInstaller.Object, mockAnalyzerProvider); TeamBuildSettings settings; using (PreprocessTestUtils.CreateValidNonTeamBuildScope()) using (new WorkingDirectoryScope(workingDir)) { settings = TeamBuildSettings.GetSettingsFromEnvironment(new TestLogger()); Assert.IsNotNull(settings, "Test setup error: TFS environment variables have not been set correctly"); Assert.AreEqual(BuildEnvironment.NotTeamBuild, settings.BuildEnvironment, "Test setup error: build environment was not set correctly"); TeamBuildPreProcessor preProcessor = new TeamBuildPreProcessor(mockFactory, logger); // Act bool success = preProcessor.Execute(CreateValidArgs("key", "name", "1.0")); Assert.IsTrue(success, "Expecting the pre-processing to complete successfully"); } // Assert AssertDirectoriesCreated(settings); mockTargetsInstaller.Verify(x => x.InstallLoaderTargets(logger, workingDir), Times.Once()); mockServer.AssertMethodCalled("GetProperties", 1); mockServer.AssertMethodCalled("GetAllLanguages", 1); mockServer.AssertMethodCalled("TryGetQualityProfile", 0); // No valid plugin mockServer.AssertMethodCalled("GetActiveRules", 0); // No valid plugin mockServer.AssertMethodCalled("GetInactiveRules", 0); // No valid plugin AssertAnalysisConfig(settings.AnalysisConfigFilePath, 0, logger); // only contains SonarQubeAnalysisConfig (no rulesets or aditional files) AssertDirectoryContains(settings.SonarConfigDirectory, Path.GetFileName(settings.AnalysisConfigFilePath)); }
public void PreProc_InvalidArgs() { // Arrange MockSonarQubeServer mockServer = new MockSonarQubeServer(); TeamBuildPreProcessor preprocessor = new TeamBuildPreProcessor( new TestLogger(), new MockSonarQubeServerFactory(mockServer), new MockTargetsInstaller(), new MockRoslynAnalyzerProvider()); // Act and assert AssertException.Expects<ArgumentNullException>(() => preprocessor.Execute(null)); }
public void PreProc_InvalidArgs() { // Arrange MockSonarQubeServer mockServer = new MockSonarQubeServer(); TeamBuildPreProcessor preprocessor = new TeamBuildPreProcessor( new MockObjectFactory(mockServer, new Mock <ITargetsInstaller>().Object, new MockRoslynAnalyzerProvider()), new TestLogger()); // Act and assert AssertException.Expects <ArgumentNullException>(() => preprocessor.Execute(null)); }
public void PreProc_InvalidArgs() { // Arrange var mockServer = new MockSonarQubeServer(); var preprocessor = new TeamBuildPreProcessor( new MockObjectFactory(mockServer, new Mock <ITargetsInstaller>().Object, new MockRoslynAnalyzerProvider()), new TestLogger()); // Act and assert Action act = () => preprocessor.Execute(null); act.Should().ThrowExactly <ArgumentNullException>(); }
public void PreProc_InvalidArgs() { // Arrange TestLogger validLogger = new TestLogger(); string[] validArgs = new string[] { "/k:key", "/n:name", "/v:1.0" }; MockSonarQubeServer mockServer = new MockSonarQubeServer(); MockTargetsInstaller mockTargetsInstaller = new MockTargetsInstaller(); TeamBuildPreProcessor preprocessor = new TeamBuildPreProcessor(new MockSonarQubeServerFactory(mockServer), mockTargetsInstaller); // Act and assert AssertException.Expects <ArgumentNullException>(() => preprocessor.Execute(null, validLogger)); AssertException.Expects <ArgumentNullException>(() => preprocessor.Execute(validArgs, null)); }
public void PreProc_HandleAnalysisException() { // Checks end-to-end behavior when AnalysisException is thrown inside FetchArgumentsAndRulesets var workingDir = TestUtils.CreateTestSpecificFolderWithSubPaths(TestContext); var logger = new TestLogger(); var mockServer = new ThrowingSonarQubeServer(); var mockFactory = new MockObjectFactory(mockServer, new Mock <ITargetsInstaller>().Object, null); using (new WorkingDirectoryScope(workingDir)) { var preProcessor = new TeamBuildPreProcessor(mockFactory, logger); var success = preProcessor.Execute(CreateValidArgs("key", "name", "1.0", "InvalidOrganization")).Result; // Should not throw success.Should().BeFalse("Expecting the pre-processing to fail"); mockServer.AnalysisExceptionThrown.Should().BeTrue(); } }
public void PreProc_InvalidArgs() { // Arrange TestLogger validLogger = new TestLogger(); string[] validArgs = new string[] { "/k:key", "/n:name", "/v:1.0" }; MockPropertiesFetcher mockPropertiesFetcher = new MockPropertiesFetcher(); MockRulesetGenerator mockRulesetGenerator = new MockRulesetGenerator(); MockTargetsInstaller mockTargetsInstaller = new MockTargetsInstaller(); TeamBuildPreProcessor preprocessor = new TeamBuildPreProcessor(mockPropertiesFetcher, mockRulesetGenerator, mockTargetsInstaller); // Act and assert AssertException.Expects <ArgumentNullException>(() => preprocessor.Execute(null, validLogger)); AssertException.Expects <ArgumentNullException>(() => preprocessor.Execute(validArgs, null)); }
public void PreProc_EmptySonarRunnerProperties() { // Checks the pre-processor creates a valid config file // Arrange string testDir = TestUtils.CreateTestSpecificFolder(this.TestContext); string propertiesFile = CreateEmptyPropertiesFile(testDir); MockPropertiesFetcher mockPropertiesFetcher = new MockPropertiesFetcher(); MockRulesetGenerator mockRulesetGenerator = new MockRulesetGenerator(); TestLogger logger = new TestLogger(); string expectedConfigFileName; using (PreprocessTestUtils.CreateValidLegacyTeamBuildScope("tfs uri", "http://builduri", testDir)) { TeamBuildSettings settings = TeamBuildSettings.GetSettingsFromEnvironment(new ConsoleLogger()); Assert.IsNotNull(settings, "Test setup error: TFS environment variables have not been set correctly"); expectedConfigFileName = settings.AnalysisConfigFilePath; TeamBuildPreProcessor preProcessor = new TeamBuildPreProcessor(mockPropertiesFetcher, mockRulesetGenerator); // Act preProcessor.Execute(logger, "key", "name", "ver", propertiesFile); } // Assert Assert.IsTrue(File.Exists(expectedConfigFileName), "Config file does not exist: {0}", expectedConfigFileName); AnalysisConfig config = AnalysisConfig.Load(expectedConfigFileName); Assert.IsTrue(Directory.Exists(config.SonarOutputDir), "Output directory was not created: {0}", config.SonarOutputDir); Assert.IsTrue(Directory.Exists(config.SonarConfigDir), "Config directory was not created: {0}", config.SonarConfigDir); Assert.AreEqual("key", config.SonarProjectKey); Assert.AreEqual("name", config.SonarProjectName); Assert.AreEqual("ver", config.SonarProjectVersion); Assert.AreEqual("http://builduri", config.GetBuildUri()); Assert.AreEqual("tfs uri", config.GetTfsUri()); Assert.AreEqual(propertiesFile, config.SonarRunnerPropertiesPath); mockPropertiesFetcher.AssertFetchPropertiesCalled(); mockPropertiesFetcher.CheckFetcherArguments("http://localhost:9000", "key"); mockRulesetGenerator.AssertGenerateCalled(); mockRulesetGenerator.CheckGeneratorArguments("http://localhost:9000", "key"); }
public void PreProc_EndToEnd_SuccessCase() { // Checks end-to-end happy path for the pre-processor i.e. // * arguments are parsed // * targets are installed // * server properties are fetched // * rulesets are generated // * config file is created // Arrange string workingDir = TestUtils.CreateTestSpecificFolder(this.TestContext); TestLogger logger = new TestLogger(); // Configure the server MockSonarQubeServer mockServer = new MockSonarQubeServer(); ServerDataModel data = mockServer.Data; data.ServerProperties.Add("server.key", "server value 1"); data.InstalledPlugins.Add("csharp"); data.InstalledPlugins.Add("vbnet"); data.AddRepository("fxcop", "cs") .AddRule("cs.rule1", "cs.rule1.internal") .AddRule("cs.rule2", "cs.rule2.internal"); data.AddRepository("fxcop-vbnet", "vbnet") .AddRule("vb.rule1", "vb.rule1.internal") .AddRule("vb.rule2", "vb.rule2.internal"); data.AddQualityProfile("test.profile", "cs") .AddProject("key"); data.AddRuleToProfile("cs.rule1", "test.profile"); data.AddQualityProfile("test.profile", "vbnet") .AddProject("key"); data.AddRuleToProfile("vb.rule2", "test.profile"); MockRoslynAnalyzerProvider mockAnalyzerProvider = new MockRoslynAnalyzerProvider(); mockAnalyzerProvider.SettingsToReturn = new AnalyzerSettings(); mockAnalyzerProvider.SettingsToReturn.RuleSetFilePath = "c:\\xxx.ruleset"; MockTargetsInstaller mockTargetsInstaller = new MockTargetsInstaller(); MockObjectFactory mockFactory = new MockObjectFactory(mockServer, mockTargetsInstaller, mockAnalyzerProvider); string[] validArgs = new string[] { "/k:key", "/n:name", "/v:1.0", "/d:cmd.line1=cmdline.value.1", "/d:sonar.host.url=http://host", "/d:sonar.log.level=INFO|DEBUG" }; TeamBuildSettings settings; using (PreprocessTestUtils.CreateValidNonTeamBuildScope()) using (new WorkingDirectoryScope(workingDir)) { settings = TeamBuildSettings.GetSettingsFromEnvironment(new TestLogger()); Assert.IsNotNull(settings, "Test setup error: TFS environment variables have not been set correctly"); Assert.AreEqual(BuildEnvironment.NotTeamBuild, settings.BuildEnvironment, "Test setup error: build environment was not set correctly"); TeamBuildPreProcessor preProcessor = new TeamBuildPreProcessor(mockFactory, logger); // Act bool success = preProcessor.Execute(validArgs); Assert.IsTrue(success, "Expecting the pre-processing to complete successfully"); } // Assert AssertDirectoryExists(settings.AnalysisBaseDirectory); AssertDirectoryExists(settings.SonarConfigDirectory); AssertDirectoryExists(settings.SonarOutputDirectory); // The bootstrapper is responsible for creating the bin directory mockTargetsInstaller.AssertsTargetsCopied(); mockServer.AssertMethodCalled("GetProperties", 1); mockServer.AssertMethodCalled("GetInternalKeys", 2); // C# and VB logger.AssertErrorsLogged(0); logger.AssertWarningsLogged(0); logger.AssertVerbosity(LoggerVerbosity.Debug); AssertConfigFileExists(settings.AnalysisConfigFilePath); AnalysisConfig actualConfig = AnalysisConfig.Load(settings.AnalysisConfigFilePath); Assert.AreEqual("key", actualConfig.SonarProjectKey, "Unexpected project key"); Assert.AreEqual("name", actualConfig.SonarProjectName, "Unexpected project name"); Assert.AreEqual("1.0", actualConfig.SonarProjectVersion, "Unexpected project version"); Assert.IsNotNull(actualConfig.AnalyzerSettings, "Analyzer settings should not be null"); Assert.AreEqual("c:\\xxx.ruleset", actualConfig.AnalyzerSettings.RuleSetFilePath, "Unexpected ruleset path"); AssertExpectedLocalSetting(SonarProperties.HostUrl, "http://host", actualConfig); AssertExpectedLocalSetting("cmd.line1", "cmdline.value.1", actualConfig); AssertExpectedServerSetting("server.key", "server value 1", actualConfig); string fxCopFilePath = AssertFileExists(settings.SonarConfigDirectory, TeamBuildPreProcessor.FxCopCSharpRuleset); PreProcessAsserts.AssertRuleSetContainsRules(fxCopFilePath, "cs.rule1"); fxCopFilePath = AssertFileExists(settings.SonarConfigDirectory, TeamBuildPreProcessor.FxCopVBNetRuleset); PreProcessAsserts.AssertRuleSetContainsRules(fxCopFilePath, "vb.rule2"); }
public void PreProc_EndToEnd_SuccessCase() { // Checks end-to-end happy path for the pre-processor i.e. // * arguments are parsed // * targets are installed // * build wrapper installer is called // * server properties are fetched // * rulesets are generated // * config file is created // Arrange string workingDir = TestUtils.CreateTestSpecificFolder(this.TestContext); TestLogger logger = new TestLogger(); // Configure the server MockSonarQubeServer mockServer = new MockSonarQubeServer(); ServerDataModel data = mockServer.Data; data.ServerProperties.Add("server.key", "server value 1"); data.InstalledPlugins.Add("csharp"); data.InstalledPlugins.Add("vbnet"); data.AddRepository("fxcop", "cs") .AddRule("cs.rule1", "cs.rule1.internal") .AddRule("cs.rule2", "cs.rule2.internal"); data.AddRepository("fxcop-vbnet", "vbnet") .AddRule("vb.rule1", "vb.rule1.internal") .AddRule("vb.rule2", "vb.rule2.internal"); data.AddQualityProfile("test.profile", "cs") .AddProject("key"); data.AddRuleToProfile("cs.rule1", "test.profile"); data.AddQualityProfile("test.profile", "vbnet") .AddProject("key"); data.AddRuleToProfile("vb.rule2", "test.profile"); MockRoslynAnalyzerProvider mockAnalyzerProvider = new MockRoslynAnalyzerProvider(); mockAnalyzerProvider.SettingsToReturn = new AnalyzerSettings(); mockAnalyzerProvider.SettingsToReturn.RuleSetFilePath = "c:\\xxx.ruleset"; MockTargetsInstaller mockTargetsInstaller = new MockTargetsInstaller(); MockBuildWrapperInstaller mockBuildWrapperInstaller = new MockBuildWrapperInstaller(); MockObjectFactory mockFactory = new MockObjectFactory(mockServer, mockTargetsInstaller, mockAnalyzerProvider, mockBuildWrapperInstaller); string[] validArgs = new string[] { "/k:key", "/n:name", "/v:1.0", "/d:cmd.line1=cmdline.value.1", "/d:sonar.host.url=http://host", "/d:sonar.log.level=INFO|DEBUG"}; TeamBuildSettings settings; using (PreprocessTestUtils.CreateValidNonTeamBuildScope()) using (new WorkingDirectoryScope(workingDir)) { settings = TeamBuildSettings.GetSettingsFromEnvironment(new TestLogger()); Assert.IsNotNull(settings, "Test setup error: TFS environment variables have not been set correctly"); Assert.AreEqual(BuildEnvironment.NotTeamBuild, settings.BuildEnvironment, "Test setup error: build environment was not set correctly"); TeamBuildPreProcessor preProcessor = new TeamBuildPreProcessor(mockFactory, logger); // Act bool success = preProcessor.Execute(validArgs); Assert.IsTrue(success, "Expecting the pre-processing to complete successfully"); } // Assert AssertDirectoryExists(settings.AnalysisBaseDirectory); AssertDirectoryExists(settings.SonarConfigDirectory); AssertDirectoryExists(settings.SonarOutputDirectory); // The bootstrapper is responsible for creating the bin directory mockTargetsInstaller.AssertsTargetsCopied(); mockServer.AssertMethodCalled("GetProperties", 1); mockServer.AssertMethodCalled("GetInternalKeys", 2); // C# and VB mockBuildWrapperInstaller.AssertExpectedCallCount(1); logger.AssertErrorsLogged(0); logger.AssertWarningsLogged(0); logger.AssertVerbosity(LoggerVerbosity.Debug); AssertConfigFileExists(settings.AnalysisConfigFilePath); AnalysisConfig actualConfig = AnalysisConfig.Load(settings.AnalysisConfigFilePath); Assert.AreEqual("key", actualConfig.SonarProjectKey, "Unexpected project key"); Assert.AreEqual("name", actualConfig.SonarProjectName, "Unexpected project name"); Assert.AreEqual("1.0", actualConfig.SonarProjectVersion, "Unexpected project version"); Assert.IsNotNull(actualConfig.AnalyzerSettings, "Analyzer settings should not be null"); Assert.AreEqual("c:\\xxx.ruleset", actualConfig.AnalyzerSettings.RuleSetFilePath, "Unexpected ruleset path"); AssertExpectedLocalSetting(SonarProperties.HostUrl, "http://host", actualConfig); AssertExpectedLocalSetting("cmd.line1", "cmdline.value.1", actualConfig); AssertExpectedServerSetting("server.key", "server value 1", actualConfig); string fxCopFilePath = AssertFileExists(settings.SonarConfigDirectory, TeamBuildPreProcessor.FxCopCSharpRuleset); PreProcessAsserts.AssertRuleSetContainsRules(fxCopFilePath, "cs.rule1"); fxCopFilePath = AssertFileExists(settings.SonarConfigDirectory, TeamBuildPreProcessor.FxCopVBNetRuleset); PreProcessAsserts.AssertRuleSetContainsRules(fxCopFilePath, "vb.rule2"); }
public void PreProc_EndToEnd_ShouldWarnOrNot_SonarQubeDeprecatedVersion(string sqVersion, bool shouldWarn) { // Arrange var workingDir = TestUtils.CreateTestSpecificFolderWithSubPaths(TestContext); var logger = new TestLogger(); // Configure the server var mockServer = new MockSonarQubeServer(); var data = mockServer.Data; data.ServerProperties.Add("server.key", "server value 1"); data.SonarQubeVersion = new Version(sqVersion); data.Languages.Add("cs"); data.Languages.Add("vbnet"); data.Languages.Add("another_plugin"); data.AddQualityProfile("qp1", "cs", "organization") .AddProject("key") .AddRule(new SonarRule("csharpsquid", "cs.rule3")); data.AddQualityProfile("qp2", "vbnet", "organization") .AddProject("key") .AddRule(new SonarRule("vbnet", "vb.rule3")); var mockAnalyzerProvider = new MockRoslynAnalyzerProvider { SettingsToReturn = new AnalyzerSettings { RulesetPath = "c:\\xxx.ruleset" } }; var mockTargetsInstaller = new Mock <ITargetsInstaller>(); var mockFactory = new MockObjectFactory(mockServer, mockTargetsInstaller.Object, mockAnalyzerProvider); TeamBuildSettings settings; using (PreprocessTestUtils.CreateValidNonTeamBuildScope()) using (new WorkingDirectoryScope(workingDir)) { settings = TeamBuildSettings.GetSettingsFromEnvironment(new TestLogger()); settings.Should().NotBeNull("Test setup error: TFS environment variables have not been set correctly"); settings.BuildEnvironment.Should().Be(BuildEnvironment.NotTeamBuild, "Test setup error: build environment was not set correctly"); var preProcessor = new TeamBuildPreProcessor(mockFactory, logger); // Act var success = preProcessor.Execute(CreateValidArgs("key", "name", "1.0", "organization")).Result; success.Should().BeTrue("Expecting the pre-processing to complete successfully"); } mockTargetsInstaller.Verify(x => x.InstallLoaderTargets(workingDir), Times.Once()); if (shouldWarn) { mockServer.AssertWarningWritten("version is below supported"); } else { mockServer.AssertNoWarningWritten(); } }
// Regression test for https://github.com/SonarSource/sonar-scanner-msbuild/issues/699 public void PreProc_EndToEnd_Success_LocalSettingsAreUsedInSonarLintXML() { // Checks that local settings are used when creating the SonarLint.xml file, // overriding // Arrange var workingDir = TestUtils.CreateTestSpecificFolderWithSubPaths(TestContext); var logger = new TestLogger(); // Configure the server var mockServer = new MockSonarQubeServer(); var data = mockServer.Data; data.Languages.Add("cs"); data.AddQualityProfile("qp1", "cs", null) .AddProject("key") .AddRule(new SonarRule("csharpsquid", "cs.rule3")); // Server-side settings data.ServerProperties.Add("server.key", "server value 1"); data.ServerProperties.Add("shared.key1", "server shared value 1"); data.ServerProperties.Add("shared.CASING", "server upper case value"); // Local settings that should override matching server settings var args = new List <string>(CreateValidArgs("key", "name", "1.0")); args.Add("/d:local.key=local value 1"); args.Add("/d:shared.key1=local shared value 1 - should override server value"); args.Add("/d:shared.casing=local lower case value"); var mockAnalyzerProvider = new MockRoslynAnalyzerProvider { SettingsToReturn = new AnalyzerSettings { RuleSetFilePath = "c:\\xxx.ruleset" } }; var mockTargetsInstaller = new Mock <ITargetsInstaller>(); var mockFactory = new MockObjectFactory(mockServer, mockTargetsInstaller.Object, mockAnalyzerProvider); TeamBuildSettings settings; using (PreprocessTestUtils.CreateValidNonTeamBuildScope()) using (new WorkingDirectoryScope(workingDir)) { settings = TeamBuildSettings.GetSettingsFromEnvironment(new TestLogger()); settings.Should().NotBeNull("Test setup error: TFS environment variables have not been set correctly"); settings.BuildEnvironment.Should().Be(BuildEnvironment.NotTeamBuild, "Test setup error: build environment was not set correctly"); var preProcessor = new TeamBuildPreProcessor(mockFactory, logger); // Act var success = preProcessor.Execute(args.ToArray()).Result; success.Should().BeTrue("Expecting the pre-processing to complete successfully"); } // Assert // Check the settings used when creating the SonarLint file - local and server settings should be merged mockAnalyzerProvider.SuppliedSonarProperties.Should().NotBeNull(); mockAnalyzerProvider.SuppliedSonarProperties.AssertExpectedPropertyValue("server.key", "server value 1"); mockAnalyzerProvider.SuppliedSonarProperties.AssertExpectedPropertyValue("local.key", "local value 1"); mockAnalyzerProvider.SuppliedSonarProperties.AssertExpectedPropertyValue("shared.key1", "local shared value 1 - should override server value"); // Keys are case-sensitive so differently cased values should be preserved mockAnalyzerProvider.SuppliedSonarProperties.AssertExpectedPropertyValue("shared.CASING", "server upper case value"); mockAnalyzerProvider.SuppliedSonarProperties.AssertExpectedPropertyValue("shared.casing", "local lower case value"); // Check the settings used when creating the config file - settings should be separate var actualConfig = AssertAnalysisConfig(settings.AnalysisConfigFilePath, 1, logger); AssertExpectedLocalSetting("local.key", "local value 1", actualConfig); AssertExpectedLocalSetting("shared.key1", "local shared value 1 - should override server value", actualConfig); AssertExpectedLocalSetting("shared.casing", "local lower case value", actualConfig); AssertExpectedServerSetting("server.key", "server value 1", actualConfig); AssertExpectedServerSetting("shared.key1", "server shared value 1", actualConfig); AssertExpectedServerSetting("shared.CASING", "server upper case value", actualConfig); }
public void PreProc_NoProject() { // Arrange var workingDir = TestUtils.CreateTestSpecificFolderWithSubPaths(TestContext); var logger = new TestLogger(); // Configure the server var mockServer = new MockSonarQubeServer(); var data = mockServer.Data; data.ServerProperties.Add("server.key", "server value 1"); data.Languages.Add("cs"); data.Languages.Add("vbnet"); data.Languages.Add("another_plugin"); data.AddQualityProfile("qp1", "cs", null) .AddProject("invalid") .AddRule(new SonarRule("fxcop", "cs.rule1")) .AddRule(new SonarRule("fxcop", "cs.rule2")); data.AddQualityProfile("qp2", "vbnet", null) .AddProject("invalid") .AddRule(new SonarRule("fxcop-vbnet", "vb.rule1")) .AddRule(new SonarRule("fxcop-vbnet", "vb.rule2")); var mockAnalyzerProvider = new MockRoslynAnalyzerProvider { SettingsToReturn = new AnalyzerSettings { RuleSetFilePath = "c:\\xxx.ruleset" } }; var mockTargetsInstaller = new Mock <ITargetsInstaller>(); var mockFactory = new MockObjectFactory(mockServer, mockTargetsInstaller.Object, mockAnalyzerProvider); TeamBuildSettings settings; using (PreprocessTestUtils.CreateValidNonTeamBuildScope()) using (new WorkingDirectoryScope(workingDir)) { settings = TeamBuildSettings.GetSettingsFromEnvironment(new TestLogger()); settings.Should().NotBeNull("Test setup error: TFS environment variables have not been set correctly"); settings.BuildEnvironment.Should().Be(BuildEnvironment.NotTeamBuild, "Test setup error: build environment was not set correctly"); var preProcessor = new TeamBuildPreProcessor(mockFactory, logger); // Act var success = preProcessor.Execute(CreateValidArgs("key", "name", "1.0", null)).Result; success.Should().BeTrue("Expecting the pre-processing to complete successfully"); } // Assert AssertDirectoriesCreated(settings); mockTargetsInstaller.Verify(x => x.InstallLoaderTargets(workingDir), Times.Once()); mockServer.AssertMethodCalled("GetProperties", 1); mockServer.AssertMethodCalled("GetAllLanguages", 1); mockServer.AssertMethodCalled("TryGetQualityProfile", 2); // C# and VBNet mockServer.AssertMethodCalled("GetActiveRules", 0); // no quality profile assigned to project mockServer.AssertMethodCalled("GetInactiveRules", 0); AssertAnalysisConfig(settings.AnalysisConfigFilePath, 0, logger); // only contains SonarQubeAnalysisConfig (no rulesets or additional files) AssertDirectoryContains(settings.SonarConfigDirectory, Path.GetFileName(settings.AnalysisConfigFilePath)); }
public void PreProc_EndToEnd_SuccessCase() { // Checks end-to-end happy path for the pre-processor i.e. // * arguments are parsed // * targets are installed // * server properties are fetched // * rulesets are generated // * config file is created // Arrange string workingDir = TestUtils.CreateTestSpecificFolder(this.TestContext); MockRulesetGenerator mockRulesetGenerator = new MockRulesetGenerator(); TestLogger logger = new TestLogger(); MockPropertiesFetcher mockPropertiesFetcher = new MockPropertiesFetcher(); mockPropertiesFetcher.PropertiesToReturn = new Dictionary<string, string>(); MockTargetsInstaller mockTargetsInstaller = new MockTargetsInstaller(); // The set of server properties to return mockPropertiesFetcher.PropertiesToReturn.Add("server.key", "server value 1"); string[] validArgs = new string[] { "/k:key", "/n:name", "/v:1.0", "/d:cmd.line1=cmdline.value.1", "/d:sonar.host.url=http://host", "/d:sonar.log.level=INFO|DEBUG"}; TeamBuildSettings settings; using (PreprocessTestUtils.CreateValidNonTeamBuildScope()) using (new WorkingDirectoryScope(workingDir)) { settings = TeamBuildSettings.GetSettingsFromEnvironment(new TestLogger()); Assert.IsNotNull(settings, "Test setup error: TFS environment variables have not been set correctly"); Assert.AreEqual(BuildEnvironment.NotTeamBuild, settings.BuildEnvironment, "Test setup error: build environment was not set correctly"); TeamBuildPreProcessor preProcessor = new TeamBuildPreProcessor(mockPropertiesFetcher, mockRulesetGenerator, mockTargetsInstaller); // Act bool success = preProcessor.Execute(validArgs, logger); Assert.IsTrue(success, "Expecting the pre-processing to complete successfully"); } // Assert AssertDirectoryExists(settings.AnalysisBaseDirectory); AssertDirectoryExists(settings.SonarConfigDirectory); AssertDirectoryExists(settings.SonarOutputDirectory); // The bootstrapper is responsible for creating the bin directory mockTargetsInstaller.AssertsTargetsCopied(); mockPropertiesFetcher.AssertFetchPropertiesCalled(); mockRulesetGenerator.AssertGenerateCalled(2); // C# and VB logger.AssertErrorsLogged(0); logger.AssertWarningsLogged(0); logger.AssertVerbosity(LoggerVerbosity.Debug); AssertConfigFileExists(settings.AnalysisConfigFilePath); AnalysisConfig actualConfig = AnalysisConfig.Load(settings.AnalysisConfigFilePath); Assert.AreEqual("key", actualConfig.SonarProjectKey, "Unexpected project key"); Assert.AreEqual("name", actualConfig.SonarProjectName, "Unexpected project name"); Assert.AreEqual("1.0", actualConfig.SonarProjectVersion, "Unexpected project version"); AssertExpectedLocalSetting(SonarProperties.HostUrl, "http://host", actualConfig); AssertExpectedLocalSetting("cmd.line1", "cmdline.value.1", actualConfig); AssertExpectedServerSetting("server.key", "server value 1", actualConfig); }
public void PreProc_InvalidArgs() { // Arrange TestLogger validLogger = new TestLogger(); string[] validArgs = new string[] { "/k:key", "/n:name", "/v:1.0" }; MockPropertiesFetcher mockPropertiesFetcher = new MockPropertiesFetcher(); MockRulesetGenerator mockRulesetGenerator = new MockRulesetGenerator(); MockTargetsInstaller mockTargetsInstaller = new MockTargetsInstaller(); TeamBuildPreProcessor preprocessor = new TeamBuildPreProcessor(mockPropertiesFetcher, mockRulesetGenerator, mockTargetsInstaller); // Act and assert AssertException.Expects<ArgumentNullException>(() => preprocessor.Execute(null, validLogger)); AssertException.Expects<ArgumentNullException>(() => preprocessor.Execute(validArgs, null)); }
public void PreProc_EndToEnd_SuccessCase() { // Checks end-to-end happy path for the pre-processor i.e. // * arguments are parsed // * targets are installed // * server properties are fetched // * rulesets are generated // * config file is created // Arrange string workingDir = TestUtils.CreateTestSpecificFolder(this.TestContext); MockRulesetGenerator mockRulesetGenerator = new MockRulesetGenerator(); TestLogger logger = new TestLogger(); MockPropertiesFetcher mockPropertiesFetcher = new MockPropertiesFetcher(); mockPropertiesFetcher.PropertiesToReturn = new Dictionary <string, string>(); MockTargetsInstaller mockTargetsInstaller = new MockTargetsInstaller(); // The set of server properties to return mockPropertiesFetcher.PropertiesToReturn.Add("server.key", "server value 1"); string[] validArgs = new string[] { "/k:key", "/n:name", "/v:1.0", "/d:cmd.line1=cmdline.value.1", "/d:sonar.host.url=http://host", "/d:sonar.log.level=INFO|DEBUG" }; TeamBuildSettings settings; using (PreprocessTestUtils.CreateValidNonTeamBuildScope()) using (new WorkingDirectoryScope(workingDir)) { settings = TeamBuildSettings.GetSettingsFromEnvironment(new TestLogger()); Assert.IsNotNull(settings, "Test setup error: TFS environment variables have not been set correctly"); Assert.AreEqual(BuildEnvironment.NotTeamBuild, settings.BuildEnvironment, "Test setup error: build environment was not set correctly"); TeamBuildPreProcessor preProcessor = new TeamBuildPreProcessor(mockPropertiesFetcher, mockRulesetGenerator, mockTargetsInstaller); // Act bool success = preProcessor.Execute(validArgs, logger); Assert.IsTrue(success, "Expecting the pre-processing to complete successfully"); } // Assert AssertDirectoryExists(settings.AnalysisBaseDirectory); AssertDirectoryExists(settings.SonarConfigDirectory); AssertDirectoryExists(settings.SonarOutputDirectory); // The bootstrapper is responsible for creating the bin directory mockTargetsInstaller.AssertsTargetsCopied(); mockPropertiesFetcher.AssertFetchPropertiesCalled(); mockRulesetGenerator.AssertGenerateCalled(2); // C# and VB logger.AssertErrorsLogged(0); logger.AssertWarningsLogged(0); logger.AssertVerbosity(LoggerVerbosity.Debug); AssertConfigFileExists(settings.AnalysisConfigFilePath); AnalysisConfig actualConfig = AnalysisConfig.Load(settings.AnalysisConfigFilePath); Assert.AreEqual("key", actualConfig.SonarProjectKey, "Unexpected project key"); Assert.AreEqual("name", actualConfig.SonarProjectName, "Unexpected project name"); Assert.AreEqual("1.0", actualConfig.SonarProjectVersion, "Unexpected project version"); AssertExpectedLocalSetting(SonarProperties.HostUrl, "http://host", actualConfig); AssertExpectedLocalSetting("cmd.line1", "cmdline.value.1", actualConfig); AssertExpectedServerSetting("server.key", "server value 1", actualConfig); }
public void PreProc_InvalidArgs() { // Arrange TestLogger validLogger = new TestLogger(); string[] validArgs = new string[] { "/k:key", "/n:name", "/v:1.0" }; MockSonarQubeServer mockServer = new MockSonarQubeServer(); MockTargetsInstaller mockTargetsInstaller = new MockTargetsInstaller(); TeamBuildPreProcessor preprocessor = new TeamBuildPreProcessor(new MockSonarQubeServerFactory(mockServer), mockTargetsInstaller); // Act and assert AssertException.Expects<ArgumentNullException>(() => preprocessor.Execute(null, validLogger)); AssertException.Expects<ArgumentNullException>(() => preprocessor.Execute(validArgs, null)); }
public void PreProc_EndToEnd_SuccessCase() { // Checks end-to-end happy path for the pre-processor i.e. // * arguments are parsed // * targets are installed // * server properties are fetched // * rulesets are generated // * config file is created // Arrange string workingDir = TestUtils.CreateTestSpecificFolder(this.TestContext); TestLogger logger = new TestLogger(); // Configure the server MockSonarQubeServer mockServer = new MockSonarQubeServer(); ServerDataModel data = mockServer.Data; data.ServerProperties.Add("server.key", "server value 1"); data.Languages.Add("cs"); data.Languages.Add("vbnet"); data.Languages.Add("another_plugin"); data.AddQualityProfile("qp1", "cs", null) .AddProject("key") .AddRule(new ActiveRule("csharpsquid", "cs.rule3")); data.AddQualityProfile("qp2", "vbnet", null) .AddProject("key") .AddRule(new ActiveRule("vbnet", "vb.rule3")); MockRoslynAnalyzerProvider mockAnalyzerProvider = new MockRoslynAnalyzerProvider { SettingsToReturn = new AnalyzerSettings { RuleSetFilePath = "c:\\xxx.ruleset" } }; Mock <ITargetsInstaller> mockTargetsInstaller = new Mock <ITargetsInstaller>(); MockObjectFactory mockFactory = new MockObjectFactory(mockServer, mockTargetsInstaller.Object, mockAnalyzerProvider); TeamBuildSettings settings; using (PreprocessTestUtils.CreateValidNonTeamBuildScope()) using (new WorkingDirectoryScope(workingDir)) { settings = TeamBuildSettings.GetSettingsFromEnvironment(new TestLogger()); Assert.IsNotNull(settings, "Test setup error: TFS environment variables have not been set correctly"); Assert.AreEqual(BuildEnvironment.NotTeamBuild, settings.BuildEnvironment, "Test setup error: build environment was not set correctly"); TeamBuildPreProcessor preProcessor = new TeamBuildPreProcessor(mockFactory, logger); // Act bool success = preProcessor.Execute(CreateValidArgs("key", "name", "1.0")); Assert.IsTrue(success, "Expecting the pre-processing to complete successfully"); } // Assert AssertDirectoriesCreated(settings); mockTargetsInstaller.Verify(x => x.InstallLoaderTargets(logger, workingDir), Times.Once()); mockServer.AssertMethodCalled("GetProperties", 1); mockServer.AssertMethodCalled("GetAllLanguages", 1); mockServer.AssertMethodCalled("TryGetQualityProfile", 2); // C# and VBNet mockServer.AssertMethodCalled("GetActiveRules", 2); // C# and VBNet mockServer.AssertMethodCalled("GetInactiveRules", 2); // C# and VBNet AssertAnalysisConfig(settings.AnalysisConfigFilePath, 2, logger); }
public void PreProc_NoProject() { // Arrange string workingDir = TestUtils.CreateTestSpecificFolder(this.TestContext); TestLogger logger = new TestLogger(); // Configure the server MockSonarQubeServer mockServer = new MockSonarQubeServer(); ServerDataModel data = mockServer.Data; data.ServerProperties.Add("server.key", "server value 1"); data.InstalledPlugins.Add("csharp"); data.InstalledPlugins.Add("vbnet"); data.InstalledPlugins.Add("another_plugin"); data.AddQualityProfile("qp1", "cs") .AddProject("invalid") .AddRule(new ActiveRule("fxcop", "cs.rule1")) .AddRule(new ActiveRule("fxcop", "cs.rule2")); data.AddQualityProfile("qp2", "vbnet") .AddProject("invalid") .AddRule(new ActiveRule("fxcop-vbnet", "vb.rule1")) .AddRule(new ActiveRule("fxcop-vbnet", "vb.rule2")); MockRoslynAnalyzerProvider mockAnalyzerProvider = new MockRoslynAnalyzerProvider(); mockAnalyzerProvider.SettingsToReturn = new AnalyzerSettings(); mockAnalyzerProvider.SettingsToReturn.RuleSetFilePath = "c:\\xxx.ruleset"; MockTargetsInstaller mockTargetsInstaller = new MockTargetsInstaller(); MockObjectFactory mockFactory = new MockObjectFactory(mockServer, mockTargetsInstaller, mockAnalyzerProvider, new RulesetGenerator()); TeamBuildSettings settings; using (PreprocessTestUtils.CreateValidNonTeamBuildScope()) using (new WorkingDirectoryScope(workingDir)) { settings = TeamBuildSettings.GetSettingsFromEnvironment(new TestLogger()); Assert.IsNotNull(settings, "Test setup error: TFS environment variables have not been set correctly"); Assert.AreEqual(BuildEnvironment.NotTeamBuild, settings.BuildEnvironment, "Test setup error: build environment was not set correctly"); TeamBuildPreProcessor preProcessor = new TeamBuildPreProcessor(mockFactory, logger); // Act bool success = preProcessor.Execute(CreateValidArgs("key", "name", "1.0")); Assert.IsTrue(success, "Expecting the pre-processing to complete successfully"); } // Assert AssertDirectoriesCreated(settings); mockTargetsInstaller.AssertsTargetsCopied(); mockServer.AssertMethodCalled("GetProperties", 1); mockServer.AssertMethodCalled("GetInstalledPlugins", 1); mockServer.AssertMethodCalled("TryGetQualityProfile", 2); // C# and VBNet mockServer.AssertMethodCalled("GetActiveRules", 0); // no quality profile assigned to project mockServer.AssertMethodCalled("GetInactiveRules", 0); AssertAnalysisConfig(settings.AnalysisConfigFilePath, 0, logger); // only contains SonarQubeAnalysisConfig (no rulesets or aditional files) AssertDirectoryContains(settings.SonarConfigDirectory, Path.GetFileName(settings.AnalysisConfigFilePath)); }