public void LatestServer_APICompatibility()
        {
            // Arrange the service used to interact with SQ
            var s          = new SonarQubeServiceWrapper(this.serviceProvider);
            var connection = new ConnectionInformation(new Uri("https://sonarqube.com"));

            // Step 1: Connect anonymously
            ProjectInformation[] projects = null;

            RetryAction(() => s.TryGetProjects(connection, CancellationToken.None, out projects),
                        "Get projects from SonarQube server");
            projects.Should().NotBeEmpty("No projects were returned");

            // Step 2: Get quality profile for the first project
            var            project = projects.FirstOrDefault();
            QualityProfile profile = null;

            RetryAction(() => s.TryGetQualityProfile(connection, project, Language.CSharp, CancellationToken.None, out profile),
                        "Get quality profile from SonarQube server");
            profile.Should().NotBeNull("No quality profile was returned");

            // Step 3: Get quality profile export for the quality profile
            RoslynExportProfile export = null;

            RetryAction(() => s.TryGetExportProfile(connection, profile, Language.CSharp, CancellationToken.None, out export),
                        "Get quality profile export from SonarQube server");
            export.Should().NotBeNull("No quality profile export was returned");

            // Errors are logged to output window pane and we don't expect any
            this.outputWindowPane.AssertOutputStrings(0);
        }
 private static void AssertExpectedAdditionalFileExists(string fileName, RoslynExportProfile profile)
 {
     Assert.IsNotNull(profile.Configuration.AdditionalFiles);
     AdditionalFile[] matches = profile.Configuration.AdditionalFiles.Where(f => string.Equals(fileName, f.FileName, System.StringComparison.OrdinalIgnoreCase)).ToArray();
     Assert.AreNotEqual(0, matches.Length, "Expected additional file was not found. File name: {0}", fileName);
     Assert.AreEqual(1, matches.Length, "Expecting only one matching file. File name: {0}", fileName);
     Assert.IsNotNull(matches[0].Content, "File content should not be null. File name: {0}", fileName);
 }
        public void RoslynProfile_LoadRealExample_Succeeds()
        {
            RoslynExportProfile profile = LoadAndCheckXml(SampleExportXml.RoslynExportedValidSonarLintXml);

            AssertExpectedAdditionalFileExists(SampleExportXml.RoslynExportedAdditionalFileName, profile);
            AssertExpectedPackageExists(SampleExportXml.RoslynExportedPackageId, SampleExportXml.RoslynExportedPackageVersion, profile);
            AssertExpectedPluginExists(SampleExportXml.RoslynExportedPluginKey, SampleExportXml.RoslynExportedPackageVersion, SampleExportXml.RoslynStaticResourceName, profile);
        }
Example #4
0
        private QualityProfile ConfigureProfileExport(RoslynExportProfile export, Language language)
        {
            var profile = new QualityProfile {
                Language = SonarQubeServiceWrapper.GetServerLanguageKey(language)
            };

            this.sonarQubeService.ReturnProfile[language] = profile;
            this.sonarQubeService.ReturnExport[profile]   = export;

            return(profile);
        }
        public void BindingWorkflow_DownloadQualityProfile_WithNoActiveRules_Fails()
        {
            // Arrange
            const string QualityProfileName   = "SQQualityProfileName";
            const string SonarQubeProjectName = "SQProjectName";
            var          projectInfo          = new ProjectInformation {
                Key = "key", Name = SonarQubeProjectName
            };
            BindingWorkflow testSubject = this.CreateTestSubject(projectInfo);
            ConfigurableProgressController controller = new ConfigurableProgressController();
            var notifications = new ConfigurableProgressStepExecutionEvents();

            RuleSet ruleSet = TestRuleSetHelper.CreateTestRuleSetWithRuleIds(new[] { "Key1", "Key2" });

            foreach (var rule in ruleSet.Rules)
            {
                rule.Action = RuleAction.None;
            }
            var expectedRuleSet = new RuleSet(ruleSet)
            {
                NonLocalizedDisplayName = string.Format(Strings.SonarQubeRuleSetNameFormat, SonarQubeProjectName, QualityProfileName),
                NonLocalizedDescription = "\r\nhttp://connected/profiles/show?key="
            };
            var nugetPackages   = new[] { new PackageName("myPackageId", new SemanticVersion("1.0.0")) };
            var additionalFiles = new[] { new AdditionalFile {
                                              FileName = "abc.xml", Content = new byte[] { 1, 2, 3 }
                                          } };
            RoslynExportProfile export = RoslynExportProfileHelper.CreateExport(ruleSet, nugetPackages, additionalFiles);

            var            language = Language.VBNET;
            QualityProfile profile  = this.ConfigureProfileExport(export, language);

            profile.Name = QualityProfileName;

            // Act
            testSubject.DownloadQualityProfile(controller, CancellationToken.None, notifications, new[] { language });

            // Assert
            testSubject.Rulesets.Should().NotContainKey(Language.VBNET, "Not expecting any rules for this language");
            testSubject.Rulesets.Should().NotContainKey(language, "Not expecting any rules");
            controller.NumberOfAbortRequests.Should().Be(1);

            notifications.AssertProgressMessages(Strings.DownloadingQualityProfileProgressMessage);

            this.outputWindowPane.AssertOutputStrings(1);
            var expectedOutput = string.Format(Strings.SubTextPaddingFormat,
                                               string.Format(Strings.NoSonarAnalyzerActiveRulesForQualityProfile, QualityProfileName, language.Name));

            this.outputWindowPane.AssertOutputStrings(expectedOutput);
        }
        private static RoslynExportProfile LoadAndCheckXml(string xml)
        {
            RoslynExportProfile profile = null;

            using (StringReader reader = new StringReader(xml))
            {
                profile = RoslynExportProfile.Load(reader);
            }

            Assert.IsNotNull(profile);
            Assert.IsNotNull(profile.Configuration);
            Assert.IsNotNull(profile.Configuration.RuleSet);

            return(profile);
        }
Example #7
0
        public void BindingWorkflow_DownloadQualityProfile_Success()
        {
            // Setup
            const string QualityProfileName   = "SQQualityProfileName";
            const string SonarQubeProjectName = "SQProjectName";
            var          projectInfo          = new ProjectInformation {
                Key = "key", Name = SonarQubeProjectName
            };
            BindingWorkflow testSubject = this.CreateTestSubject(projectInfo);
            ConfigurableProgressController controller = new ConfigurableProgressController();
            var notifications = new ConfigurableProgressStepExecutionEvents();

            RuleSet ruleSet         = TestRuleSetHelper.CreateTestRuleSetWithRuleIds(new[] { "Key1", "Key2" });
            var     expectedRuleSet = new RuleSet(ruleSet)
            {
                NonLocalizedDisplayName = string.Format(Strings.SonarQubeRuleSetNameFormat, SonarQubeProjectName, QualityProfileName),
                NonLocalizedDescription = "\r\nhttp://connected/profiles/show?key="
            };
            var nugetPackages   = new[] { new PackageName("myPackageId", new SemanticVersion("1.0.0")) };
            var additionalFiles = new[] { new AdditionalFile {
                                              FileName = "abc.xml", Content = new byte[] { 1, 2, 3 }
                                          } };
            RoslynExportProfile export = RoslynExportProfileHelper.CreateExport(ruleSet, nugetPackages, additionalFiles);

            var            language = Language.VBNET;
            QualityProfile profile  = this.ConfigureProfileExport(export, language);

            profile.Name = QualityProfileName;

            // Act
            testSubject.DownloadQualityProfile(controller, CancellationToken.None, notifications, new[] { language });

            // Verify
            RuleSetAssert.AreEqual(expectedRuleSet, testSubject.Rulesets[language], "Unexpected rule set");
            Assert.AreSame(profile, testSubject.QualityProfiles[language]);
            VerifyNuGetPackgesDownloaded(nugetPackages, testSubject, language);
            controller.AssertNumberOfAbortRequests(0);
            notifications.AssertProgress(
                0.0,
                1.0);
            notifications.AssertProgressMessages(Strings.DownloadingQualityProfileProgressMessage, string.Empty);

            this.outputWindowPane.AssertOutputStrings(1);
            var expectedOutput = string.Format(Strings.SubTextPaddingFormat,
                                               string.Format(Strings.QualityProfileDownloadSuccessfulMessageFormat, QualityProfileName, string.Empty, language.Name));

            this.outputWindowPane.AssertOutputStrings(expectedOutput);
        }
 private static void ServiceProfileExport(IOwinContext context, RoslynExportProfile export, bool simulateFault = false)
 {
     if (simulateFault)
     {
         SimulateServerFault(context);
     }
     else
     {
         var serializer = new XmlSerializer(typeof(RoslynExportProfile));
         using (var stream = new MemoryStream())
         {
             serializer.Serialize(stream, export);
             context.Response.Write(stream.ToArray());
         }
     }
 }
        public void RoslynProfile_LoadValidProfile_Succeeds()
        {
            string validXml = @"<?xml version=""1.0"" encoding=""utf-8""?>
<RoslynExportProfile Version=""1.0"">
  <Configuration>
    <RuleSet Name=""Rules for SonarQube"" Description=""This rule set was automatically generated from SonarQube."" ToolsVersion=""14.0"">
      <Rules>
        <Rule Id=""Foo""/>
      </Rules>
    </RuleSet>

    <AdditionalFiles>
      <AdditionalFile FileName=""SonarLint.xml"" >PHRlc3Q+PHN1Yi8+PC90ZXN0</AdditionalFile>
      <AdditionalFile FileName=""MyAnalyzer.xml"" >PEZvby8+</AdditionalFile>
    </AdditionalFiles>
  </Configuration>

  <Deployment>
    <Plugins>
      <Plugin Key=""csharp"" Version=""4.5-SNAPSHOT"" StaticResourceName=""SonarLint.zip"" />
      <Plugin Key=""roslyn.wintellect.analyzers"" Version=""1.0.5.0"" StaticResourceName=""Wintellect.Analyzers.1.0.zip"" />
    </Plugins>

    <NuGetPackages>
      <NuGetPackage Id=""SonarLint"" Version=""1.3.0""/>
      <NuGetPackage Id=""My.Analyzers"" Version=""1.0.5.0-rc1""/>
    </NuGetPackages>
  </Deployment>
</RoslynExportProfile>";

            RoslynExportProfile profile = LoadAndCheckXml(validXml);

            AssertExpectedAdditionalFileExists("SonarLint.xml", profile);
            AssertExpectedAdditionalFileExists("MyAnalyzer.xml", profile);

            AssertExpectedPackageExists("SonarLint", "1.3.0", profile);
            AssertExpectedPackageExists("My.Analyzers", "1.0.5.0-rc1", profile);

            AssertExpectedPluginExists("csharp", "4.5-SNAPSHOT", "SonarLint.zip", profile);
            AssertExpectedPluginExists("roslyn.wintellect.analyzers", "1.0.5.0", "Wintellect.Analyzers.1.0.zip", profile);
        }
Example #10
0
        public static RoslynExportProfile CreateExport(RuleSet ruleSet, IEnumerable<PackageName> packages, IEnumerable<AdditionalFile> additionalFiles)
        {
            string xml = TestRuleSetHelper.RuleSetToXml(ruleSet);
            var ruleSetXmlDoc = new XmlDocument();
            ruleSetXmlDoc.LoadXml(xml);

            var export = new RoslynExportProfile
            {
                Configuration = new Configuration
                {
                    RuleSet = ruleSetXmlDoc.DocumentElement,
                    AdditionalFiles = additionalFiles.ToList()
                },
                Deployment = new Deployment
                {
                    NuGetPackages = packages.Select(x => new NuGetPackageInfo { Id = x.Id, Version = x.Version.ToNormalizedString() }).ToList()
                }
            };

            return export;
        }
Example #11
0
        bool ISonarQubeServiceWrapper.TryGetExportProfile(ConnectionInformation serverConnection, QualityProfile profile, Language language, CancellationToken token, out RoslynExportProfile export)
        {
            this.AssertExpectedConnection(serverConnection);

            Assert.IsNotNull(profile, "QualityProfile is expected");

            this.GetExportAction?.Invoke();

            export = null;
            this.ReturnExport.TryGetValue(profile, out export);

            QualityProfile profile2;

            this.ReturnProfile.TryGetValue(language, out profile2);
            Assert.AreSame(profile2, profile, "Unexpected profile for language");

            return(export != null);
        }
 private static void AssertExpectedPluginExists(string pluginKey, string version, string staticResourceName, RoslynExportProfile profile)
 {
     Assert.IsNotNull(profile.Deployment);
     Plugin[] matches = profile.Deployment.Plugins.Where(
         p => string.Equals(pluginKey, p.Key, System.StringComparison.Ordinal) &&
         string.Equals(version, p.Version, System.StringComparison.Ordinal) &&
         string.Equals(staticResourceName, p.StaticResourceName, System.StringComparison.Ordinal)).ToArray();
     Assert.AreNotEqual(0, matches.Length, "Expected plugin entry was not found. Plugin: {0}, Version: {1}, Resource: {2}", pluginKey, version, staticResourceName);
     Assert.AreEqual(1, matches.Length, "Expecting only one matching plugin. Package: {0}, Version: {1}, Resource: {2}", pluginKey, version, staticResourceName);
 }
 private static void AssertExpectedPackageExists(string packageId, string version, RoslynExportProfile profile)
 {
     Assert.IsNotNull(profile.Deployment);
     NuGetPackageInfo[] matches = profile.Deployment.NuGetPackages.Where(
         p => string.Equals(packageId, p.Id, System.StringComparison.Ordinal) &&
         string.Equals(version, p.Version, System.StringComparison.Ordinal)).ToArray();
     Assert.AreNotEqual(0, matches.Length, "Expected package was not found. Package: {0}, Version: {1}", packageId, version);
     Assert.AreEqual(1, matches.Length, "Expecting only one matching package. Package: {0}, Version: {1}", packageId, version);
 }
 public static void AssertAreEqual(RoslynExportProfile expected, RoslynExportProfile actual)
 {
     actual.Version.Should().Be(expected.Version, "Unexpected export version");
     AssertConfigSectionEqual(expected.Configuration, actual.Configuration);
     AssertDeploymentSectionEqual(expected.Deployment, actual.Deployment);
 }