Ejemplo n.º 1
0
        public void GetRuleSeverity()
        {
            var configFilePath = @"c:\.tsqllintrc";

            var jsonConfig = @"{
                'rules': {
                    'select-star': 'warning',
                    'semicolon-termination': 'error'
                }
            }";

            var fileSystem = new MockFileSystem(new Dictionary <string, MockFileData>()
            {
                {
                    configFilePath, new MockFileData(jsonConfig)
                }
            });

            var reporter           = Substitute.For <IReporter>();
            var environmentWrapper = Substitute.For <IEnvironmentWrapper>();

            var configReader = new ConfigReader(reporter, fileSystem, environmentWrapper);

            configReader.LoadConfig(configFilePath);
            var ruleVisitorBuilder = new RuleVisitorBuilder(configReader, null);

            var ignoredRuleList    = new List <IExtendedRuleException>();
            var activeRuleVisitors = ruleVisitorBuilder.BuildVisitors("foo.sql", ignoredRuleList);

            Assert.AreEqual(2, activeRuleVisitors.Count);
        }
        public void RuleVisitorIgnoresRule()
        {
            // arrange
            var mockReporter = Substitute.For <IReporter>();

            mockReporter.ReportViolation(Arg.Any <IRuleViolation>());

            var mockConfigReader = Substitute.For <IConfigReader>();

            mockConfigReader.GetRuleSeverity("select-star").Returns(RuleViolationSeverity.Error);

            var ignoredRuleList = new List <IRuleException>
            {
                new RuleException(typeof(SelectStarRule), 1, 10)
            };

            var pathString         = "DoesntExist.sql";
            var RuleVisitorBuilder = new RuleVisitorBuilder(mockConfigReader, mockReporter);
            var ActiveRuleVisitors = RuleVisitorBuilder.BuildVisitors(pathString, ignoredRuleList);
            var testFileStream     = ParsingUtility.GenerateStreamFromString("SELECT * FROM FOO;");
            var textReader         = new StreamReader(testFileStream);
            var sqlFragment        = _fragmentBuilder.GetFragment(textReader, out _);

            // act
            foreach (var sqlFragmentVisitor in ActiveRuleVisitors)
            {
                sqlFragment.Accept(sqlFragmentVisitor);
                testFileStream.Seek(0, SeekOrigin.Begin);
            }

            // assert
            Assert.AreEqual(1, ActiveRuleVisitors.Count);
            Assert.IsTrue(ActiveRuleVisitors[0].GetType().Name == typeof(SelectStarRule).Name);
            mockReporter.DidNotReceive().ReportViolation(Arg.Any <IRuleViolation>());
        }
        public void RuleVisitorEnforcesOneRuleIgnoresAnother()
        {
            // arrange
            var mockReporter = Substitute.For <IReporter>();

            mockReporter.ReportViolation(Arg.Any <IRuleViolation>());

            var mockConfigReader = Substitute.For <IConfigReader>();

            mockConfigReader.GetRuleSeverity("select-star").Returns(RuleViolationSeverity.Error);
            mockConfigReader.GetRuleSeverity("semicolon-termination").Returns(RuleViolationSeverity.Error);

            var ignoredRuleList = new List <IRuleException>
            {
                new RuleException(typeof(SelectStarRule), 1, 10)
            };

            var pathString         = "DoesntExist.sql";
            var RuleVisitorBuilder = new RuleVisitorBuilder(mockConfigReader, mockReporter);
            var ActiveRuleVisitors = RuleVisitorBuilder.BuildVisitors(pathString, ignoredRuleList);
            var testFileStream     = ParsingUtility.GenerateStreamFromString("SELECT * FROM FOO");
            var textReader         = new StreamReader(testFileStream);
            var sqlFragment        = _fragmentBuilder.GetFragment(textReader, out _);

            // act
            foreach (var sqlFragmentVisitor in ActiveRuleVisitors)
            {
                sqlFragment.Accept(sqlFragmentVisitor);
                testFileStream.Seek(0, SeekOrigin.Begin);
            }

            // assert
            Assert.AreEqual(2, ActiveRuleVisitors.Count);
            mockReporter.Received().ReportViolation(Arg.Is <IRuleViolation>(x =>
                                                                            x.FileName == pathString &&
                                                                            x.RuleName == "semicolon-termination" &&
                                                                            x.Line == 1 &&
                                                                            x.Column == 18 &&
                                                                            x.Severity == RuleViolationSeverity.Error));
        }