public void ActivatePlugins_ThrowErrors_ShouldCatch_ShouldReport()
        {
            // arrange
            var path            = Path.GetFullPath(Path.Combine(TestContext.CurrentContext.TestDirectory, @"UnitTests/PluginHandler/tsqllint-plugin-throws-exception.dll"));
            var assemblyWrapper = new AssemblyWrapper();

            var pluginPaths = new Dictionary <string, string>
            {
                {
                    "my-plugin", path
                }
            };

            var reporter       = Substitute.For <IReporter>();
            var versionWrapper = Substitute.For <IFileversionWrapper>();
            var context        = Substitute.For <IPluginContext>();

            versionWrapper.GetVersion(Arg.Any <Assembly>()).Returns("1.2.3");

            // act
            var pluginHandler = new Infrastructure.Plugins.PluginHandler(reporter, new FileSystem(), assemblyWrapper, versionWrapper);

            pluginHandler.ProcessPaths(pluginPaths);
            pluginHandler.ActivatePlugins(context);

            // assert
            Assert.AreEqual(1, pluginHandler.Plugins.Count);
            reporter.Received().Report(@"There was a problem with plugin: tsqllint_plugin_throws_exception.PluginThatThrows - something bad happened");
        }
Esempio n. 2
0
        public void ActivatePlugins_PluginRuleViolations_ShouldCallReporter()
        {
            // arrange
            var filePath1  = TestHelper.GetTestFilePath(@"c:\pluginDirectory\plugin_one.dll");
            var fileSystem = new MockFileSystem(new Dictionary <string, MockFileData>
            {
                {
                    filePath1, new MockFileData(string.Empty)
                }
            });

            var assemblyWrapper = new TestAssemblyWrapper();

            var pluginPaths = new Dictionary <string, string>
            {
                {
                    "my-plugin", filePath1
                }
            };

            var reporter   = Substitute.For <IReporter>();
            var textReader = ParsingUtility.CreateTextReaderFromString("\tSELECT * FROM FOO");

            var scriptPath = TestHelper.GetTestFilePath(@"c:\scripts\foo.sql");
            var context    = new PluginContext(scriptPath, new List <IRuleException>(), textReader);

            var versionWrapper = Substitute.For <IFileversionWrapper>();

            versionWrapper.GetVersion(Arg.Any <Assembly>()).Returns("1.2.3");

            // act
            var pluginHandler = new Infrastructure.Plugins.PluginHandler(reporter, fileSystem, assemblyWrapper, versionWrapper);

            pluginHandler.ProcessPaths(pluginPaths);

            // assert
            Assert.AreEqual(1, pluginHandler.Plugins.Count);
            Assert.DoesNotThrow(() => pluginHandler.ActivatePlugins(context));

            reporter.Received().ReportViolation(Arg.Is <IRuleViolation>(x =>
                                                                        x.FileName == context.FilePath &&
                                                                        x.RuleName == "prefer-tabs" &&
                                                                        x.Text == "Should use spaces rather than tabs" &&
                                                                        x.Line == 1 &&
                                                                        x.Column == 0 &&
                                                                        x.Severity == RuleViolationSeverity.Warning));
        }
        public void LoadPlugins_ShouldLoadPluginsFilesWithRelativePaths()
        {
            // arrange
            var currentDirectory = Directory.GetParent(Assembly.GetExecutingAssembly().Location);
            var executingLocationParentFolder = currentDirectory.Parent.FullName;

            var filePath1 = Path.Combine(executingLocationParentFolder, "plugin_one.dll");
            var filePath2 = Path.Combine(executingLocationParentFolder, "plugin_two.dll");

            var fileSystem = new MockFileSystem(
                new Dictionary <string, MockFileData>
            {
                {
                    filePath1, new MockFileData(string.Empty)
                },
                {
                    filePath2, new MockFileData(string.Empty)
                }
            },
                currentDirectory.FullName);

            var assemblyWrapper = new TestAssemblyWrapper();

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

            var versionWrapper = Substitute.For <IFileversionWrapper>();

            versionWrapper.GetVersion(Arg.Any <Assembly>()).Returns("1.2.3");

            var pluginPaths = new Dictionary <string, string>
            {
                {
                    "my-second-plugin", @"..\plugin_one.dll"
                }
            };

            // act
            var pluginHandler = new Infrastructure.Plugins.PluginHandler(reporter, fileSystem, assemblyWrapper, versionWrapper);

            pluginHandler.ProcessPaths(pluginPaths);

            // assert
            Assert.AreEqual(1, pluginHandler.Plugins.Count);
        }
        public void LoadPlugins_ThrowErrors_When_Same_Type_Is_Loaded_More_Than_Once()
        {
            // arrange
            const string filePath1  = @"c:\pluginDirectory\plugin_one.dll";
            var          fileSystem = new MockFileSystem(new Dictionary <string, MockFileData>
            {
                {
                    filePath1, new MockFileData(string.Empty)
                }
            });

            var assemblyWrapper = new TestAssemblyWrapper(defaultPlugin: typeof(TestPluginThrowsException));

            var pluginPaths = new Dictionary <string, string>
            {
                {
                    "my-plugin", filePath1
                },
                {
                    "my-plugin-directories", @"c:\pluginDirectory"
                },
                {
                    "my-plugin-invalid-path", @"c:\doesnt-exist"
                }
            };

            var versionWrapper = Substitute.For <IFileversionWrapper>();

            versionWrapper.GetVersion(Arg.Any <Assembly>()).Returns("1.2.3");

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

            // act
            var pluginHandler = new Infrastructure.Plugins.PluginHandler(reporter, fileSystem, assemblyWrapper, versionWrapper);

            pluginHandler.ProcessPaths(pluginPaths);

            // assert
            Assert.AreEqual(1, pluginHandler.Plugins.Count);
            var type = typeof(TestPluginThrowsException);

            reporter.Received().Report($"Loaded plugin: '{type.FullName}', Version: '1.2.3'");
            reporter.Received().Report($"Already loaded plugin with type '{type.FullName}'");
        }
        public void LoadPlugins_ShouldLoadPluginsFromPathAndFile()
        {
            // arrange
            const string filePath1 = @"c:\pluginDirectory\plugin_one.dll";
            const string filePath2 = @"c:\pluginDirectory\plugin_two.dll";
            const string filePath3 = @"c:\pluginDirectory\plugin_three.dll";
            const string filePath4 = @"c:\pluginDirectory\foo.txt";
            const string filePath5 = @"c:\pluginDirectory\subDirectory\bar.txt";
            const string filePath6 = @"c:\pluginDirectory\subDirectory\plugin_four.dll";

            var fileSystem = new MockFileSystem(new Dictionary <string, MockFileData>
            {
                {
                    filePath1, new MockFileData(string.Empty)
                },
                {
                    filePath2, new MockFileData(string.Empty)
                },
                {
                    filePath3, new MockFileData(string.Empty)
                },
                {
                    filePath4, new MockFileData("foo")
                },
                {
                    filePath5, new MockFileData("bar")
                },
                {
                    filePath6, new MockFileData(string.Empty)
                },
            });

            var assemblyWrapper = new TestAssemblyWrapper(new Dictionary <string, int>
            {
                { @"c:\pluginDirectory\plugin_one.dll", 0 },
                { @"c:\pluginDirectory\plugin_two.dll", 1 },
                { @"c:\pluginDirectory\plugin_three.dll", 2 },
                { @"c:\pluginDirectory\subDirectory\plugin_four.dll", 3 }
            });

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

            var versionWrapper = Substitute.For <IFileversionWrapper>();

            versionWrapper.GetVersion(Arg.Any <Assembly>()).Returns("1.2.3");

            var pluginPaths = new Dictionary <string, string>
            {
                {
                    "my-first-plugin", @"c:\pluginDirectory\"
                },
                {
                    "my-second-plugin", filePath6
                }
            };

            // act
            var pluginHandler = new Infrastructure.Plugins.PluginHandler(reporter, fileSystem, assemblyWrapper, versionWrapper);

            pluginHandler.ProcessPaths(pluginPaths);

            // assert
            Assert.AreEqual(4, pluginHandler.Plugins.Count);
        }