public async Task GetRules_AbortIfCancellationRequested()
        {
            // Arrange
            var testLogger = new TestLogger();

            CancellationTokenSource cts = new CancellationTokenSource();

            var serviceMock = new Mock <ISonarQubeService>();

            serviceMock.Setup(x => x.GetRulesAsync(It.IsAny <bool>(), It.IsAny <string>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(() =>
            {
                cts.Cancel();
                return(new List <SonarQubeRule>());
            });

            var testSubject = new CFamilyBindingConfigProvider(serviceMock.Object, testLogger);

            // Act
            var result = await testSubject.GetConfigurationAsync(CreateQp(), null, Language.Cpp, cts.Token);

            // Assert
            result.Should().BeNull();
            testLogger.AssertPartialOutputStringExists(CoreStrings.SonarQubeRequestTimeoutOrCancelled);
        }
        public async Task GetRules_NoData_EmptyResultReturned()
        {
            // Arrange
            var testLogger  = new TestLogger();
            var serviceMock = new Mock <ISonarQubeService>();

            serviceMock.Setup(x => x.GetRulesAsync(It.IsAny <bool>(), It.IsAny <string>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(() => new List <SonarQubeRule>());

            var testSubject = new CFamilyBindingConfigProvider(serviceMock.Object, testLogger);

            // Act
            var result = await testSubject.GetConfigurationAsync(CreateQp(), null, Language.Cpp, CancellationToken.None);

            // Assert
            result.Should().NotBeNull();
            result.Should().BeOfType <CFamilyBindingConfigFile>();

            var cfamilyConfigFile = (CFamilyBindingConfigFile)result;

            cfamilyConfigFile.RuleSettings.Should().NotBeNull();
            cfamilyConfigFile.RuleSettings.Rules.Should().NotBeNull();
            cfamilyConfigFile.RuleSettings.Rules.Count.Should().Be(0);

            testLogger.AssertNoOutputMessages();
        }
Пример #3
0
        public async Task GetRules_Success()
        {
            // Arrange
            var testLogger = new TestLogger();
            var rules      = new List <SonarQubeRule>
            {
                CreateRule("key1", "repo1", true, SonarQubeIssueSeverity.Major),
                CreateRule("key2", "repo2", false, SonarQubeIssueSeverity.Info,
                           new Dictionary <string, string>
                {
                    { "p1", "v1" },
                    { "p2", "v2" }
                })
            };

            var serviceMock = new Mock <ISonarQubeService>();

            serviceMock.Setup(x => x.GetRulesAsync(true, It.IsAny <string>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(() => rules);

            var bindingConfiguration = new BindingConfiguration(new BoundSonarQubeProject {
                ProjectKey = "test"
            }, SonarLintMode.Connected, "c:\\");

            var testSubject = new CFamilyBindingConfigProvider(serviceMock.Object, testLogger);

            // Act
            var result = await testSubject.GetConfigurationAsync(CreateQp(), Language.Cpp, bindingConfiguration, CancellationToken.None);

            // Assert
            result.Should().NotBeNull();
            result.Should().BeOfType <CFamilyBindingConfig>();

            var cfamilyConfigFile = (CFamilyBindingConfig)result;

            cfamilyConfigFile.RuleSettings.Should().NotBeNull();

            var slvsRules = cfamilyConfigFile.RuleSettings.Rules;

            slvsRules.Should().NotBeNull();
            slvsRules.Keys.Should().BeEquivalentTo("repo1:key1", "repo2:key2");
            slvsRules["repo1:key1"].Level.Should().Be(RuleLevel.On);
            slvsRules["repo2:key2"].Level.Should().Be(RuleLevel.Off);

            slvsRules["repo1:key1"].Severity.Should().Be(IssueSeverity.Major);
            slvsRules["repo2:key2"].Severity.Should().Be(IssueSeverity.Info);

            slvsRules["repo1:key1"].Parameters.Should().BeNull();

            var rule2Params = slvsRules["repo2:key2"].Parameters;

            rule2Params.Should().NotBeNull();
            rule2Params.Keys.Should().BeEquivalentTo("p1", "p2");
            rule2Params["p1"].Should().Be("v1");
            rule2Params["p2"].Should().Be("v2");

            testLogger.AssertNoOutputMessages();
        }
        public void GetRules_UnsupportedLanguage_Throws()
        {
            // Arrange
            var testLogger = new TestLogger();

            CancellationTokenSource cts = new CancellationTokenSource();
            var serviceMock             = new Mock <ISonarQubeService>();

            var testSubject = new CFamilyBindingConfigProvider(serviceMock.Object, testLogger);

            // Act
            Action act = () => testSubject.GetConfigurationAsync(CreateQp(), null, Language.VBNET, cts.Token).Wait();

            // Assert
            act.Should().ThrowExactly <AggregateException>().And.InnerException.Should().BeOfType <ArgumentOutOfRangeException>();
        }
        public async Task GetRules_NonCriticalException_IsHandledAndNullResultReturned()
        {
            // Arrange
            var testLogger = new TestLogger();

            var serviceMock = new Mock <ISonarQubeService>();

            serviceMock.Setup(x => x.GetRulesAsync(It.IsAny <bool>(), It.IsAny <string>(), CancellationToken.None))
            .ThrowsAsync(new InvalidOperationException("invalid op"));

            var testSubject = new CFamilyBindingConfigProvider(serviceMock.Object, testLogger);

            // Act
            var result = await testSubject.GetConfigurationAsync(CreateQp(), null, Language.Cpp, CancellationToken.None);

            // Assert
            result.Should().BeNull();
            testLogger.AssertPartialOutputStringExists("invalid op");
        }