Example #1
0
        public void NoSonarLintFile_ReturnsFalse()
        {
            // Arrange
            var text               = @"<?xml version='1.0' encoding='UTF-8'?>
<AnalysisInput>
  <Settings>
    <Setting>
      <Key>sonar.cs.analyzeGeneratedCode</Key>
      <Value>true</Value>
    </Setting>
  </Settings>
</AnalysisInput>
";
            var dummySourceText    = new DummySourceText(text);
            var additionalTextMock = CreateMockAdditionalText(dummySourceText, "ResourceTests\\Foo.xml");
            var analyzerOptions    = new AnalyzerOptions(ImmutableArray.Create(additionalTextMock.Object));

            var analysisContext = new Mock <AnalysisContext>();
            var compilation     = GetDummyCompilation(isCSharp: true);

            // Act
            bool result = SonarAnalysisContext.ShouldAnalyzeGenerated(analysisContext.Object, compilation, analyzerOptions);

            // Assert
            result.Should().BeFalse();
            dummySourceText.ToStringCallCount.Should().Be(0); // Not the right file so shouldn't ever try to read it
        }
Example #2
0
        public void VB_CorrectSettingUsed()
        {
            // Arrange
            var text = @"<?xml version='1.0' encoding='UTF-8'?>
<AnalysisInput>
  <Settings>
    <Setting>
      <Key>sonar.cs.analyzeGeneratedCode</Key>
      <Value>false</Value>
    </Setting>
    <Setting>
      <Key>sonar.vbnet.analyzeGeneratedCode</Key>
      <Value>true</Value>
    </Setting>
  </Settings>
</AnalysisInput>
";

            var dummySourceText    = new DummySourceText(text);
            var additionalTextMock = CreateMockAdditionalText(dummySourceText, "ResourceTests\\SonarLint.xml");
            var analyzerOptions    = new AnalyzerOptions(ImmutableArray.Create(additionalTextMock.Object));

            var analysisContext   = new Mock <AnalysisContext>();
            var vbCompilation     = GetDummyCompilation(isCSharp: false);
            var cSharpCompilation = GetDummyCompilation(isCSharp: true);

            // 1. Read both languages
            bool vbResult     = SonarAnalysisContext.ShouldAnalyzeGenerated(analysisContext.Object, vbCompilation, analyzerOptions);
            bool csharpResult = SonarAnalysisContext.ShouldAnalyzeGenerated(analysisContext.Object, cSharpCompilation, analyzerOptions);

            // Assert
            vbResult.Should().BeTrue();
            csharpResult.Should().BeFalse();
            dummySourceText.ToStringCallCount.Should().Be(2); // file read once per language

            // 2. Read again for VB to check VB caching
            vbResult = SonarAnalysisContext.ShouldAnalyzeGenerated(analysisContext.Object, vbCompilation, analyzerOptions);

            // Assert
            vbResult.Should().BeTrue();
            dummySourceText.ToStringCallCount.Should().Be(2); // file should not have been read again
        }
Example #3
0
        public void InvalidXmlInSonarLintFile_ReturnsFalse()
        {
            // Arrange
            var dummySourceText    = new DummySourceText("Not valid xml");
            var additionalTextMock = CreateMockAdditionalText(dummySourceText, "ResourceTests\\SonarLint.xml");
            var analyzerOptions    = new AnalyzerOptions(ImmutableArray.Create(additionalTextMock.Object));

            var analysisContext = new Mock <AnalysisContext>();
            var compilation     = GetDummyCompilation(isCSharp: true);

            // 1. Read -> no error, false returned
            bool result = SonarAnalysisContext.ShouldAnalyzeGenerated(analysisContext.Object, compilation, analyzerOptions);

            result.Should().BeFalse();
            dummySourceText.ToStringCallCount.Should().Be(1); // should have attempted to read the file

            // 2. Read again to check that the load error doesn't prevent caching from working
            result = SonarAnalysisContext.ShouldAnalyzeGenerated(analysisContext.Object, compilation, analyzerOptions);
            result.Should().BeFalse();
            dummySourceText.ToStringCallCount.Should().Be(1); // should not have attempted to read the file again
        }
Example #4
0
        public void ResultIsCached()
        {
            // Arrange
            var text               = @"<?xml version='1.0' encoding='UTF-8'?>
<AnalysisInput>
  <Settings>
    <Setting>
      <Key>dummy</Key>
      <Value>false</Value>
    </Setting>
    <Setting>
      <Key>sonar.cs.analyzeGeneratedCode</Key>
      <Value>true</Value>
    </Setting>
  </Settings>
</AnalysisInput>
";
            var dummySourceText    = new DummySourceText(text);
            var additionalTextMock = CreateMockAdditionalText(dummySourceText, "ResourceTests\\SonarLint.xml");
            var analyzerOptions    = new AnalyzerOptions(ImmutableArray.Create(additionalTextMock.Object));

            var analysisContext = new Mock <AnalysisContext>();
            var compilation     = GetDummyCompilation(isCSharp: true);

            // Act - call ShouldAnalyzeGenerated multiple times...
            bool result = SonarAnalysisContext.ShouldAnalyzeGenerated(analysisContext.Object, compilation, analyzerOptions);

            result.Should().BeTrue();
            result = SonarAnalysisContext.ShouldAnalyzeGenerated(analysisContext.Object, compilation, analyzerOptions);
            result.Should().BeTrue();
            result = SonarAnalysisContext.ShouldAnalyzeGenerated(analysisContext.Object, compilation, analyzerOptions);
            result.Should().BeTrue();

            // Assert
            // GetText should be called every time ShouldAnalyzeGenerated is called...
            additionalTextMock.Verify(x => x.GetText(It.IsAny <CancellationToken>()), Times.Exactly(3));
            dummySourceText.ToStringCallCount.Should().Be(1); // ... but we should only try to read the file once
        }