public void VisitRules_GlobalIgnoreNoFragments_ShouldNotReportErrors()
        {
            // arrange
            var sqlStream = ParsingUtility.GenerateStreamFromString(@"/* tsqllint-disable */
@@Scripts\dfu_setutp_import_cleanup.sql");

            var mockReporter           = Substitute.For <IReporter>();
            var mockRuleVisitorBuilder = Substitute.For <IRuleVisitorBuilder>();

            var visitors = new List <TSqlFragmentVisitor>
            {
                new SetAnsiNullsRule(null)
            };

            mockRuleVisitorBuilder.BuildVisitors(Arg.Any <string>(), Arg.Any <List <IRuleException> >()).Returns(visitors);

            var sqlRuleVisitor = new SqlRuleVisitor(mockRuleVisitorBuilder, new FragmentBuilder(120), mockReporter);

            // act
            sqlRuleVisitor.VisitRules(Path, new List <IRuleException> {
                new GlobalRuleException(0, 99)
            }, sqlStream);

            // assert
            mockReporter.DidNotReceive().ReportViolation(Arg.Any <IRuleViolation>());
        }
Beispiel #2
0
        public static void RunDynamicSQLRulesTest(Type ruleType, string sql, List <RuleViolation> expectedRuleViolations)
        {
            // arrange
            var ruleViolations  = new List <RuleViolation>();
            var mockReporter    = Substitute.For <IReporter>();
            var mockPath        = string.Empty;
            var compareer       = new RuleViolationComparer();
            var fragmentBuilder = new FragmentBuilder();
            var sqlStream       = ParsingUtility.GenerateStreamFromString(sql);

            void ErrorCallback(string ruleName, string ruleText, int startLine, int startColumn)
            {
                ruleViolations.Add(new RuleViolation(ruleName, startLine, startColumn));
            }

            var visitors = new List <TSqlFragmentVisitor>
            {
                GetVisitor(ruleType, ErrorCallback)
            };

            var mockRuleVisitorBuilder = Substitute.For <IRuleVisitorBuilder>();

            mockRuleVisitorBuilder.BuildVisitors(Arg.Any <string>(), Arg.Any <IEnumerable <IRuleException> >()).Returns(visitors);

            var sqlRuleVisitor = new SqlRuleVisitor(mockRuleVisitorBuilder, fragmentBuilder, mockReporter);

            // act
            sqlRuleVisitor.VisitRules(mockPath, new List <IRuleException>(), sqlStream);

            ruleViolations         = ruleViolations.OrderBy(o => o.Line).ThenBy(o => o.Column).ToList();
            expectedRuleViolations = expectedRuleViolations.OrderBy(o => o.Line).ThenBy(o => o.Column).ToList();

            // assert
            CollectionAssert.AreEqual(expectedRuleViolations, ruleViolations, compareer);
        }
Beispiel #3
0
        public void Run()
        {
            configReader.LoadConfig(commandLineOptions.ConfigFile);

            var fragmentBuilder    = new FragmentBuilder(configReader.CompatabilityLevel);
            var ruleVisitorBuilder = new RuleVisitorBuilder(configReader, this.reporter);
            var ruleVisitor        = new SqlRuleVisitor(ruleVisitorBuilder, fragmentBuilder, reporter);

            pluginHandler = new PluginHandler(reporter);
            fileProcessor = new SqlFileProcessor(ruleVisitor, pluginHandler, reporter, new FileSystem());

            pluginHandler.ProcessPaths(configReader.GetPlugins());
            var response = commandLineOptionHandler.Handle(new CommandLineRequestMessage(commandLineOptions));

            if (response.ShouldLint)
            {
                fileProcessor.ProcessList(commandLineOptions.LintPath);
            }

            if (fileProcessor.FileCount > 0)
            {
                reporter.ReportResults(timer.Stop(), fileProcessor.FileCount);
            }

            if (!response.Success)
            {
                Environment.ExitCode = 1;
            }
        }
        public void VisitRules_InvalidSql_ShouldStillLint()
        {
            // arrange
            var mockReporter    = Substitute.For <IReporter>();
            var fragmentBuilder = new FragmentBuilder(120);

            var ruleViolations = new List <RuleViolation>();

            void ErrorCallback(string ruleName, string ruleText, int startLine, int startColumn)
            {
                ruleViolations.Add(new RuleViolation(ruleName, startLine, startColumn));
            }

            var visitors = new List <TSqlFragmentVisitor>
            {
                new KeywordCapitalizationRule(ErrorCallback)
            };

            var mockRuleVisitorBuilder = Substitute.For <IRuleVisitorBuilder>();

            mockRuleVisitorBuilder.BuildVisitors(Arg.Any <string>(), Arg.Any <List <IRuleException> >()).Returns(visitors);
            var sqlStream      = ParsingUtility.GenerateStreamFromString("select");
            var sqlRuleVisitor = new SqlRuleVisitor(mockRuleVisitorBuilder, fragmentBuilder, mockReporter);

            // act
            sqlRuleVisitor.VisitRules(Path, new List <IRuleException>(), sqlStream);

            // assert
            Assert.AreEqual(1, ruleViolations.Count);
        }
        public void VisitRules_NullFragentWithIgnores_ShouldNotReportErrors()
        {
            // arrange
            var path       = System.IO.Path.GetFullPath(System.IO.Path.Combine(TestContext.CurrentContext.TestDirectory, @"UnitTests/Parser/invalid-characters-ignore-rules.sql"));
            var fileStream = File.OpenRead(path);

            var mockReporter = Substitute.For <IReporter>();
            var visitors     = new List <TSqlFragmentVisitor>
            {
                new SetAnsiNullsRule(null)
            };

            var mockRuleVisitorBuilder = Substitute.For <IRuleVisitorBuilder>();

            mockRuleVisitorBuilder.BuildVisitors(Arg.Any <string>(), Arg.Any <List <IRuleException> >()).Returns(visitors);

            var sqlRuleVisitor = new SqlRuleVisitor(mockRuleVisitorBuilder, new FragmentBuilder(120), mockReporter);

            // act
            sqlRuleVisitor.VisitRules(Path, new List <IRuleException> {
                new GlobalRuleException(0, 99)
            }, fileStream);

            // assert
            mockReporter.DidNotReceive().ReportViolation(Arg.Any <IRuleViolation>());
        }
Beispiel #6
0
        public void TestRule(string rule, string testFileName, Type ruleType, List <RuleViolation> expectedRuleViolations)
        {
            // arrange
            var sqlString = TestHelper.GetTestFile(string.Format("Rules\\{0}\\{1}.sql", rule, testFileName));

            var fragmentVisitor = new SqlRuleVisitor(null, null);
            var ruleViolations  = new List <RuleViolation>();

            Action <string, string, int, int> ErrorCallback = delegate(string ruleName, string ruleText, int startLine, int startColumn)
            {
                ruleViolations.Add(new RuleViolation(ruleName, startLine, startColumn));
            };

            var visitor    = (TSqlFragmentVisitor)Activator.CreateInstance(ruleType, ErrorCallback);
            var textReader = TSQLLINT_LIB.Utility.Utility.CreateTextReaderFromString(sqlString);

            // act
            fragmentVisitor.VisitRule(textReader, visitor);

            ruleViolations         = ruleViolations.OrderBy(o => o.Line).ToList();
            expectedRuleViolations = expectedRuleViolations.OrderBy(o => o.Line).ToList();

            // assert
            Assert.AreEqual(expectedRuleViolations.Count, ruleViolations.Count);

            var compareer = new RuleViolationCompare();

            CollectionAssert.AreEqual(expectedRuleViolations, ruleViolations, compareer);
        }
Beispiel #7
0
 public LintingHandler(CommandLineOptions commandLineOptions, IReporter reporter)
 {
     CommandLineOptions = commandLineOptions;
     ConfigReader       = new ConfigReader(CommandLineOptions.ConfigFile);
     RuleVisitor        = new SqlRuleVisitor(ConfigReader, reporter);
     Parser             = new SqlFileProcessor(RuleVisitor, reporter);
 }
Beispiel #8
0
        public void VisitRules_InvalidSql_ShouldReportError()
        {
            // arrange
            var mockReporter           = Substitute.For <IReporter>();
            var fragmentBuilder        = new FragmentBuilder();
            var mockRuleVisitorBuilder = Substitute.For <IRuleVisitorBuilder>();
            var sqlStream      = ParsingUtility.GenerateStreamFromString("SELECT");
            var sqlRuleVisitor = new SqlRuleVisitor(mockRuleVisitorBuilder, fragmentBuilder, mockReporter);

            // act
            sqlRuleVisitor.VisitRules(Path, sqlStream);

            // assert
            mockReporter.Received().ReportViolation(Arg.Is <RuleViolation>(x => x.FileName == Path && x.Text == "TSQL not syntactically correct"));
        }
Beispiel #9
0
        public Application(string[] args, IReporter reporter)
        {
            timer = new ConsoleTimer();
            timer.Start();

            this.reporter            = reporter;
            commandLineOptions       = new CommandLineOptions.CommandLineOptions(args);
            configReader             = new ConfigReader(reporter);
            commandLineOptionHandler = new CommandLineOptionHandler(commandLineOptions, new ConfigFileGenerator(), configReader, reporter);
            var          fragmentBuilder    = new FragmentBuilder();
            var          ruleVisitorBuilder = new RuleVisitorBuilder(configReader, this.reporter);
            IRuleVisitor ruleVisitor        = new SqlRuleVisitor(ruleVisitorBuilder, fragmentBuilder, reporter);

            pluginHandler = new PluginHandler(reporter);
            fileProcessor = new SqlFileProcessor(ruleVisitor, pluginHandler, reporter, new FileSystem());
        }
Beispiel #10
0
        public void Run()
        {
            configReader.LoadConfig(commandLineOptions.ConfigFile);

            var          fragmentBuilder    = new FragmentBuilder(configReader.CompatabilityLevel);
            var          ruleVisitorBuilder = new RuleVisitorBuilder(configReader, this.reporter);
            IRuleVisitor ruleVisitor        = new SqlRuleVisitor(ruleVisitorBuilder, fragmentBuilder, reporter);

            pluginHandler = new PluginHandler(reporter);
            fileProcessor = new SqlFileProcessor(ruleVisitor, pluginHandler, reporter, new FileSystem());

            pluginHandler.ProcessPaths(configReader.GetPlugins());
            commandLineOptionHandler.HandleCommandLineOptions(commandLineOptions);
            fileProcessor.ProcessList(commandLineOptions.LintPath);

            if (fileProcessor.FileCount > 0)
            {
                reporter.ReportResults(timer.Stop(), fileProcessor.FileCount);
            }
        }
Beispiel #11
0
        public void VisitRules_ValidSql_ShouldVisitRules()
        {
            // arrange
            var sqlStream = ParsingUtility.GenerateStreamFromString("SELECT 1");

            var mockReporter = Substitute.For <IReporter>();
            var mockFragment = Substitute.For <TSqlFragment>();

            var mockFragmentBuilder       = Substitute.For <IFragmentBuilder>();
            IList <ParseError> mockErrors = new List <ParseError>();

            mockFragmentBuilder.GetFragment(Arg.Any <TextReader>(), out var errors).Returns(x =>
            {
                x[1] = mockErrors;
                return(mockFragment);
            });

            var mockRuleExceptionFinder = Substitute.For <IRuleExceptionFinder>();

            mockRuleExceptionFinder.GetIgnoredRuleList(Arg.Any <Stream>()).Returns(new List <IRuleException>());

            var visitors = new List <TSqlFragmentVisitor>
            {
                new SemicolonTerminationRule(null)
            };

            var mockRuleVisitorBuilder = Substitute.For <IRuleVisitorBuilder>();

            mockRuleVisitorBuilder.BuildVisitors(Arg.Any <string>(), Arg.Any <List <IRuleException> >()).Returns(visitors);

            var sqlRuleVisitor = new SqlRuleVisitor(mockRuleVisitorBuilder, mockRuleExceptionFinder, mockFragmentBuilder, mockReporter);

            // act
            sqlRuleVisitor.VisitRules(Path, sqlStream);

            // assert
            mockFragment.Received().Accept(Arg.Any <TSqlFragmentVisitor>());
        }