Пример #1
0
        public void ProcessPath_SingleFile_ShouldProcessFile()
        {
            // arrange
            const string filePath = "c:\\dbscripts\\myfile.sql";

            var ruleVisitor   = Substitute.For <IRuleVisitor>();
            var reporter      = Substitute.For <IReporter>();
            var fileSystem    = Substitute.For <IFileSystem>();
            var fileBase      = Substitute.For <FileBase>();
            var pluginHandler = Substitute.For <IPluginHandler>();
            var processor     = new SqlFileProcessor(ruleVisitor, pluginHandler, reporter, fileSystem);

            fileBase.Exists(filePath).Returns(true);
            fileBase.OpenRead(filePath).Returns(ParsingUtility.GenerateStreamFromString("Some Sql To Parse"));
            fileSystem.File.Returns(fileBase);

            // act
            processor.ProcessPath("\" " + filePath + " \""); // Also testing removal of quotes and leading/trailing spaces

            // assert
            fileBase.Received().Exists(filePath);
            fileBase.Received().OpenRead(filePath);
            ruleVisitor.Received().VisitRules(filePath, Arg.Any <Stream>());
            Assert.AreEqual(1, processor.FileCount);
        }
Пример #2
0
        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>());
        }
Пример #3
0
        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);
        }
Пример #4
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);
        }
        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>());
        }
Пример #6
0
        public async Task <object> Lint(string fileContents)
        {
            var sqlStream = ParsingUtility.GenerateStreamFromString("SELECT id FROM tbl_1");

            return(new
            {
                errors = new string[]
                {
                    "File length " + fileContents.Length
                }
            });
        }
        public void CreateReader_WithoutPlaceholders_DoesNotChangeSql()
        {
            // arrange
            var mockEnvironmentWrapper = Substitute.For <IEnvironmentWrapper>();
            var inputSql = "select 1;";

            // act
            var outputSql = new SqlStreamReaderBuilder(mockEnvironmentWrapper)
                            .CreateReader(ParsingUtility.GenerateStreamFromString(inputSql))
                            .ReadToEnd();

            // assert
            Assert.AreEqual(inputSql, outputSql);
        }
Пример #8
0
        public void Test(string description, string sql, int expectedOverrideCount)
        {
            var fragmentBuilder = new FragmentBuilder(Constants.DefaultCompatabilityLevel);
            var overrideFider   = new OverrideFinder();

            var sqlStream = ParsingUtility.GenerateStreamFromString(sql);
            var overrides = overrideFider.GetOverrideList(sqlStream);

            var fragment = fragmentBuilder.GetFragment(new StreamReader(sqlStream), out var errors, overrides);

            Assert.AreEqual(expectedOverrideCount, overrides.Count());
            Assert.IsEmpty(errors);
            Assert.IsNotNull(fragment);
        }
        public void TestOverride(
            string message,
            string testString,
            List <IExtendedRuleException> expectedruleExceptionResult,
            List <IOverride> expectedOverrideResult)
        {
            var overrideResult = OverrideFinder.GetOverrideList(ParsingUtility.GenerateStreamFromString(testString));

            CollectionAssert.AreEqual(overrideResult, expectedOverrideResult, OverrideComparer, message);

            var ruleExceptionResult = RuleExceptionFinder.GetIgnoredRuleList(ParsingUtility.GenerateStreamFromString(testString));

            CollectionAssert.AreEqual(expectedruleExceptionResult, ruleExceptionResult, RuleExceptionComparer, message);
        }
Пример #10
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"));
        }
        public void CreateReader_WithPlaceholdersButNoEnvironmentVariables_DoesNotChangeSql()
        {
            // arrange
            var mockEnvironmentWrapper = Substitute.For <IEnvironmentWrapper>();

            mockEnvironmentWrapper.GetEnvironmentVariable("bar").ReturnsNull();
            var inputSql = "select 1 where foo = $(bar);";

            // act
            var outputSql = new SqlStreamReaderBuilder(mockEnvironmentWrapper)
                            .CreateReader(ParsingUtility.GenerateStreamFromString(inputSql))
                            .ReadToEnd();

            // assert
            Assert.AreEqual(inputSql, outputSql);
        }
        public void CreateReader_WithPlaceholdersAndSomeEnvironmentVariables_ChangesSql()
        {
            // arrange
            var mockEnvironmentWrapper = Substitute.For <IEnvironmentWrapper>();

            mockEnvironmentWrapper.GetEnvironmentVariable("foo").Returns("foo");
            mockEnvironmentWrapper.GetEnvironmentVariable("bar").ReturnsNull();
            var inputSql = "select 1 where foo = '$(foo)' and bar = '$(bar)';";

            // act
            var outputSql = new SqlStreamReaderBuilder(mockEnvironmentWrapper)
                            .CreateReader(ParsingUtility.GenerateStreamFromString(inputSql))
                            .ReadToEnd();

            // assert
            Assert.AreEqual("select 1 where foo = 'foo' and bar = '$(bar)';", outputSql);
        }
        public void ShouldIgnore(string description, string testString)
        {
            var stream = ParsingUtility.GenerateStreamFromString(testString);

            void DynamicCallback(string dynamicSQL, int dynamicStartLine, int dynamicStartColumn)
            {
                Assert.Fail("should not perform callback");
            }

            var visitor         = new DynamicSQLParser(DynamicCallback);
            var fragmentBuilder = new FragmentBuilder();
            var textReader      = new StreamReader(stream);
            var sqlFragment     = fragmentBuilder.GetFragment(textReader, out var errors);

            sqlFragment.Accept(visitor);

            CollectionAssert.IsEmpty(errors, "parsing errors were generated");
        }
        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));
        }
Пример #15
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>());
        }
        public void ShouldParse(string description, string executeStatement, string innerSql, bool expectCallback = true)
        {
            var stream = ParsingUtility.GenerateStreamFromString(executeStatement);

            var receivedCallback = false;

            void DynamicCallback(string dynamicSQL, int dynamicStartLine, int dynamicStartColumn)
            {
                Assert.AreEqual(innerSql, dynamicSQL);
                receivedCallback = true;
            }

            var visitor         = new DynamicSQLParser(DynamicCallback);
            var fragmentBuilder = new FragmentBuilder();
            var textReader      = new StreamReader(stream);

            var sqlFragment = fragmentBuilder.GetFragment(textReader, out var errors);

            CollectionAssert.IsEmpty(errors, "parsing errors were generated");

            sqlFragment.Accept(visitor);

            Assert.AreEqual(receivedCallback, expectCallback);
        }
Пример #17
0
        public void TestOverride(string message, string testString, List <IOverride> expectedResult)
        {
            var overrides = testOverrideFinder.GetOverrideList(ParsingUtility.GenerateStreamFromString(testString));

            CollectionAssert.AreEqual(overrides, expectedResult, overrideComparer, message);
        }