Esempio n. 1
0
        public void RoslynConfig_MissingRuleset()
        {
            // Arrange
            string            rootDir  = CreateTestFolders();
            TestLogger        logger   = new TestLogger();
            TeamBuildSettings settings = CreateSettings(rootDir);

            MockSonarQubeServer mockServer = CreateValidServer("valid.project", null, "valid.profile");
            QualityProfile      csProfile  = mockServer.Data.FindProfile("valid.profile", RoslynAnalyzerProvider.CSharpLanguage);

            csProfile.SetExport(RoslynAnalyzerProvider.RoslynCSharpFormatName, @"<?xml version=""1.0"" encoding=""utf-8""?>
<RoslynExportProfile Version=""1.0="">
  <Configuration>
    <!-- Missing ruleset -->
    <AdditionalFiles>
      <AdditionalFile FileName=""SonarLint.xml"" >
      </AdditionalFile>
    </AdditionalFiles>
  </Configuration>
  <Deployment />
</RoslynExportProfile>");

            RoslynAnalyzerProvider testSubject = CreateTestSubject(logger);

            // Act
            AnalyzerSettings actualSettings = testSubject.SetupAnalyzers(mockServer, settings, "valid.project", null);

            // Assert
            AssertAnalyzerSetupNotPerformed(actualSettings, rootDir);

            logger.AssertErrorsLogged(0);
            logger.AssertWarningsLogged(0);
        }
Esempio n. 2
0
        public QualityProfile FindProfile(string name, string language)
        {
            // Multiple profiles can have the same name; look for a profile where the language matches the rule language
            QualityProfile profile = this.qualityProfiles.SingleOrDefault(qp => string.Equals(qp.Name, name) && string.Equals(qp.Language, language));

            return(profile);
        }
Esempio n. 3
0
        public void RoslynConfig_NoAnalyzerAssemblies_Succeeds()
        {
            // Arrange
            string            rootDir  = CreateTestFolders();
            TestLogger        logger   = new TestLogger();
            TeamBuildSettings settings = CreateSettings(rootDir);

            MockSonarQubeServer mockServer = CreateValidServer("valid.project", null, "valid.profile");
            QualityProfile      csProfile  = mockServer.Data.FindProfile("valid.profile", RoslynAnalyzerProvider.CSharpLanguage);

            csProfile.SetExport(RoslynAnalyzerProvider.RoslynCSharpFormatName, @"<?xml version=""1.0"" encoding=""utf-8""?>
<RoslynExportProfile Version=""1.0="">
  <Configuration>
    <RuleSet />
    <AdditionalFiles />
  </Configuration>
  <Deployment>
    <Plugins /> <!-- empty -->
  </Deployment>
</RoslynExportProfile>");

            RoslynAnalyzerProvider testSubject = CreateTestSubject(logger);

            // Act
            AnalyzerSettings actualSettings = testSubject.SetupAnalyzers(mockServer, settings, "valid.project", null);

            // Assert
            CheckSettingsInvariants(actualSettings);

            CheckExpectedAssemblies(actualSettings /* none */);

            logger.AssertErrorsLogged(0);
            logger.AssertWarningsLogged(0);
        }
Esempio n. 4
0
        public void AddRuleToProfile(string ruleId, string profileName)
        {
            // We're assuming rule ids are unique across repositories
            Rule rule = this.repos.SelectMany(repo => repo.Rules.Where(r => string.Equals(ruleId, r.Key))).Single();

            QualityProfile profile = this.FindProfile(profileName, rule.Language);

            profile.AddRule(rule);
        }
 public QualityProfile AddQualityProfile(string name, string language)
 {
     QualityProfile profile = this.FindProfile(name, language);
     Assert.IsNull(profile, "A quality profile already exists. Name: {0}, language: {1}", name, language);
         
     profile = new QualityProfile(name, language);
     this.qualityProfiles.Add(profile);
     return profile;
 }
Esempio n. 6
0
        public QualityProfile AddQualityProfile(string name, string language)
        {
            QualityProfile profile = this.FindProfile(name, language);

            Assert.IsNull(profile, "A quality profile already exists. Name: {0}, language: {1}", name, language);

            profile = new QualityProfile(name, language);
            this.qualityProfiles.Add(profile);
            return(profile);
        }
        public QualityProfile AddQualityProfile(string id, string language, string organization)
        {
            QualityProfile profile = this.FindProfile(id);

            Assert.IsNull(profile, "A quality profile already exists. Id: {0}, language: {1}", id, language);

            profile = new QualityProfile(id, language, organization);
            this.qualityProfiles.Add(profile);
            return(profile);
        }
Esempio n. 8
0
        bool ISonarQubeServer.TryGetQualityProfile(string projectKey, string language, out string qualityProfile)
        {
            this.LogMethodCalled();

            Assert.IsFalse(string.IsNullOrEmpty(projectKey), "Project key is required");
            Assert.IsFalse(string.IsNullOrEmpty(language), "Language is required");

            QualityProfile profile = this.Data.QualityProfiles.FirstOrDefault(qp => string.Equals(qp.Language, language) && qp.Projects.Contains(projectKey));

            qualityProfile = profile == null ? null : profile.Name;
            return(profile != null);
        }
Esempio n. 9
0
        IList <string> ISonarQubeServer.GetInactiveRules(string qprofile, string language)
        {
            this.LogMethodCalled();
            Assert.IsFalse(string.IsNullOrEmpty(qprofile), "Quality profile is required");
            QualityProfile profile = this.Data.QualityProfiles.FirstOrDefault(qp => string.Equals(qp.Id, qprofile));

            if (profile == null)
            {
                return(null);
            }

            return(profile.InactiveRules);
        }
Esempio n. 10
0
        bool ISonarQubeServer.TryGetProfileExport(string qualityProfile, string language, string format, out string content)
        {
            this.LogMethodCalled();

            Assert.IsFalse(string.IsNullOrEmpty(qualityProfile), "Quality profile is required");
            Assert.IsFalse(string.IsNullOrEmpty(language), "Language is required");
            Assert.IsFalse(string.IsNullOrEmpty(format), "Format is required");

            QualityProfile profile = this.Data.FindProfile(qualityProfile, language);

            content = profile != null?profile.GetExport(format) : null;

            return(content != null);
        }
Esempio n. 11
0
        IEnumerable <string> ISonarQubeServer.GetActiveRuleKeys(string qualityProfile, string language, string repository)
        {
            this.LogMethodCalled();

            Assert.IsFalse(string.IsNullOrEmpty(qualityProfile), "Quality profile is required");
            Assert.IsFalse(string.IsNullOrEmpty(language), "Language is required");
            Assert.IsFalse(string.IsNullOrEmpty(repository), "Repository is required");

            QualityProfile profile = this.Data.QualityProfiles.FirstOrDefault(qp => string.Equals(qp.Name, qualityProfile) && string.Equals(qp.Language, language));

            if (profile == null)
            {
                return(null);
            }

            return(profile.ActiveRules.Where(r => string.Equals(r.Repository.Key, repository)).Select(r => r.Key));
        }
Esempio n. 12
0
        bool ISonarQubeServer.TryGetQualityProfile(string projectKey, string projectBranch, string language, out string qualityProfile)
        {
            this.LogMethodCalled();

            Assert.IsFalse(string.IsNullOrEmpty(projectKey), "Project key is required");
            Assert.IsFalse(string.IsNullOrEmpty(language), "Language is required");

            string projectId = projectKey;

            if (!String.IsNullOrWhiteSpace(projectBranch))
            {
                projectId = projectKey + ":" + projectBranch;
            }

            QualityProfile profile = this.Data.QualityProfiles.FirstOrDefault(qp => string.Equals(qp.Language, language) && qp.Projects.Contains(projectId));

            qualityProfile = profile == null ? null : profile.Id;
            return(profile != null);
        }
Esempio n. 13
0
        public void RoslynConfig_DuplicateAdditionalFileName_DuplicateFileIgnored()
        {
            // Arrange
            string            rootDir  = CreateTestFolders();
            TestLogger        logger   = new TestLogger();
            TeamBuildSettings settings = CreateSettings(rootDir);

            string expectedFileContent   = "expected";
            string unexpectedFileContent = "not expected: file should already exist with the expected content";

            MockSonarQubeServer mockServer = CreateValidServer("valid.project", null, "valid.profile");
            QualityProfile      csProfile  = mockServer.Data.FindProfile("valid.profile", RoslynAnalyzerProvider.CSharpLanguage);

            csProfile.SetExport(RoslynAnalyzerProvider.RoslynCSharpFormatName, @"<?xml version=""1.0"" encoding=""utf-8""?>
<RoslynExportProfile Version=""1.0="">
  <Configuration>
    <RuleSet />
    <AdditionalFiles>
      <AdditionalFile FileName=""foo.txt"" >" + GetBase64EncodedString(expectedFileContent) + @"</AdditionalFile>
      <AdditionalFile FileName=""foo.txt"" >" + GetBase64EncodedString(unexpectedFileContent) + @"</AdditionalFile>
      <AdditionalFile FileName=""file2.txt""></AdditionalFile>
    </AdditionalFiles>
  </Configuration>
  <Deployment />
</RoslynExportProfile>");

            RoslynAnalyzerProvider testSubject = CreateTestSubject(logger);

            // Act
            AnalyzerSettings actualSettings = testSubject.SetupAnalyzers(mockServer, settings, "valid.project", null);

            // Assert
            CheckSettingsInvariants(actualSettings);
            CheckRuleset(actualSettings, rootDir);
            CheckExpectedAdditionalFileExists("foo.txt", expectedFileContent, actualSettings);
            CheckExpectedAdditionalFileExists("file2.txt", string.Empty, actualSettings);

            logger.AssertErrorsLogged(0);
            logger.AssertWarningsLogged(0);
        }
Esempio n. 14
0
        public void RoslynConfig_ProfileExportIsUnavailable_FailsGracefully()
        {
            // Arrange
            string            rootDir  = CreateTestFolders();
            TestLogger        logger   = new TestLogger();
            TeamBuildSettings settings = CreateSettings(rootDir);

            // Create a server that doesn't export the expected format (simulates
            // calling an older plugin version)
            MockSonarQubeServer mockServer = CreateValidServer("valid.project", null, "valid.profile");
            QualityProfile      csProfile  = mockServer.Data.FindProfile("valid.profile", RoslynAnalyzerProvider.CSharpLanguage);

            csProfile.SetExport(RoslynAnalyzerProvider.RoslynCSharpFormatName, null);

            RoslynAnalyzerProvider testSubject = CreateTestSubject(logger);

            // Act
            AnalyzerSettings actualSettings = testSubject.SetupAnalyzers(mockServer, settings, "valid.project", null);

            // Assert
            AssertAnalyzerSetupNotPerformed(actualSettings, rootDir);

            logger.AssertErrorsLogged(0);
        }
 public QualityProfile AddQualityProfile(string name, string language)
 {
     QualityProfile profile = new QualityProfile(name, language);
     this.qualityProfiles.Add(profile);
     return profile;
 }
        public QualityProfile FindProfile(string id)
        {
            QualityProfile profile = this.qualityProfiles.SingleOrDefault(qp => string.Equals(qp.Id, id));

            return(profile);
        }
        public void AddInactiveRuleToProfile(string qProfile, string ruleKey)
        {
            QualityProfile profile = this.FindProfile(qProfile);

            profile.InactiveRules.Add(ruleKey);
        }
        public void AddActiveRuleToProfile(string qProfile, ActiveRule rule)
        {
            QualityProfile profile = this.FindProfile(qProfile);

            profile.ActiveRules.Add(rule);
        }