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 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 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 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()); }
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); } }
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)); }