Beispiel #1
0
 protected Type GetContractType()
 {
     return(TestableTypeBuilder.New()
            .WithName("IMyTestType")
            .WithNamespace("Test.Type")
            .Build());
 }
        public void ProvideActivationContext_Works()
        {
            var assemblyShim    = this.mockRepository.Create <IAssemblyShim>();
            var contract        = GetContract();
            var pluginAttribute = GetPluginAttributeForContract(contract);

            var typeName     = "MyTestType";
            var testableType = TestableTypeBuilder.New()
                               .WithCustomAttributes(pluginAttribute)
                               .WithName(typeName)
                               .WithNamespace("Test.Type")
                               .Build();

            // NO factory method
            // NO activated method
            // NO bootstrapper
            // NO services

            assemblyShim.Setup(a => a.Types).Returns(new[] { testableType });

            var sut    = new DefaultPluginActivationContextProvider();
            var result = sut.ProvideActivationContext(testableType, assemblyShim.Object);

            Assert.IsNotNull(result);
            Assert.IsNull(result.PluginActivatedMethod);
            Assert.IsNull(result.PluginFactoryMethod);
            Assert.AreEqual(0, result.PluginServices.Count());
            Assert.AreEqual(typeName, result.PluginType.Name);
        }
        public async Task LoadPluginAssembly_Guard_Works()
        {
            var testContext        = SetupAssemblyLoadContext();
            var loadContext        = testContext.Sut();
            var fileSystemUtility  = testContext.GetMock <IFileSystemUtilities>();
            var pluginAssemblyPath = "/var/home/MyPluginAssembly.dll";
            var pluginDependencyContextProvider = testContext.GetMock <IPluginDependencyContextProvider>();
            var pluginDependencyContext         = testContext.GetMock <IPluginDependencyContext>();

            var contract = TestableTypeBuilder.New()
                           .WithName("IMyTestType")
                           .WithNamespace("Test.Type")
                           .Build();

            var assembly       = this.GetType().Assembly;
            var assemblyStream = File.OpenRead(assembly.Location);

            fileSystemUtility.Setup(f => f.EnsureFileExists(pluginAssemblyPath)).Returns(pluginAssemblyPath);
            fileSystemUtility.Setup(f => f.ReadFileFromDisk(pluginAssemblyPath)).ReturnsAsync(assemblyStream);

            var pluginLoadContext = new PluginLoadContext(pluginAssemblyPath, contract, "netcoreapp3.1");

            pluginDependencyContextProvider.Setup(p => p.FromPluginLoadContext(pluginLoadContext)).ReturnsAsync(pluginDependencyContext.Object);

            var priseAssembly = await loadContext.LoadPluginAssembly(pluginLoadContext);

            await Assert.ThrowsExceptionAsync <AssemblyLoadingException>(() => loadContext.LoadPluginAssembly(pluginLoadContext));
        }
        public async Task Scan_Succeeds()
        {
            var startingPath        = "/home/maarten";
            var metadataLoadContext = this.mockRepository.Create <IMetadataLoadContext>();
            var assemblyShim        = this.mockRepository.Create <IAssemblyShim>();
            var directoryTraverser  = this.mockRepository.Create <IDirectoryTraverser>();

            directoryTraverser.Setup(d => d.TraverseDirectories(startingPath)).Returns(new[] { "pathy/mcpathface" });
            directoryTraverser.Setup(d => d.TraverseFiles(It.IsAny <string>(), It.IsAny <IEnumerable <string> >())).Returns(new[] { "filey.mcfile.face" });

            var contract = TestableTypeBuilder.New()
                           .WithName("IMyTestType")
                           .WithNamespace("Test.Type")
                           .Build();

            var pluginAttributeTypedValue = new CustomAttributeTypedArgument(contract);
            var pluginAttribute           = new TestableAttribute
            {
                _AttributeType  = typeof(Prise.Plugin.PluginAttribute),
                _NamedArguments = new[] { new CustomAttributeNamedArgument(new TestableMemberInfo {
                        _Name = "PluginType"
                    }, pluginAttributeTypedValue) }
            };

            var testableType = TestableTypeBuilder.New()
                               .WithCustomAttributes(pluginAttribute)
                               .WithName("MyTestType")
                               .WithNamespace("Test.Type")
                               .Build();

            assemblyShim.Setup(a => a.Types).Returns(new[] { testableType });

            metadataLoadContext.Setup(c => c.LoadFromAssemblyName(It.IsAny <string>())).Returns(assemblyShim.Object);

            var scanner = new DefaultAssemblyScanner(
                (s) => metadataLoadContext.Object,
                () => directoryTraverser.Object
                );
            var types = await scanner.Scan(new AssemblyScannerOptions
            {
                StartingPath = startingPath,
                PluginType   = contract
            });

            var result = types.FirstOrDefault();

            Assert.IsNotNull(result);
            Assert.AreEqual("MyTestType", result.PluginType.Name);
            Assert.AreEqual("Test.Type", result.PluginType.Namespace);
        }
        public void Selecting_Plugin_Types_Works()
        {
            var assemblyShim = this.mockRepository.Create <IAssemblyShim>();
            var contract     = TestableTypeBuilder.New()
                               .WithName("IMyTestType")
                               .WithNamespace("Test.Type")
                               .Build();

            var pluginAttributeTypedValue = new CustomAttributeTypedArgument(contract);
            var pluginAttribute           = new TestableAttribute
            {
                _AttributeType  = typeof(Prise.Plugin.PluginAttribute),
                _NamedArguments = new[] { new CustomAttributeNamedArgument(new TestableMemberInfo {
                        _Name = "PluginType"
                    }, pluginAttributeTypedValue) }
            };

            var testableType = TestableTypeBuilder.New()
                               .WithCustomAttributes(pluginAttribute)
                               .WithName("MyTestType")
                               .WithNamespace("Test.Type")
                               .Build();

            var testableType2 = TestableTypeBuilder.New()
                                .WithCustomAttributes(pluginAttribute)
                                .WithName("MyTestType2")
                                .WithNamespace("Test.Type")
                                .Build();

            assemblyShim.Setup(a => a.Types).Returns(new[] { testableType2, testableType, typeof(ITestPlugin) });

            var selector     = new DefaultPluginTypeSelector();
            var pluginsTypes = selector.SelectPluginTypes(contract, assemblyShim.Object);

            Assert.AreEqual(2, pluginsTypes.Count());
            Assert.AreEqual("MyTestType", pluginsTypes.First().Name);
            Assert.AreEqual("MyTestType2", pluginsTypes.ElementAt(1).Name);
        }
        public void ProvideActivationContext_WithBootstrapper_Works()
        {
            var assemblyShim    = this.mockRepository.Create <IAssemblyShim>();
            var contract        = GetContract();
            var pluginAttribute = GetPluginAttributeForContract(contract);

            var serviceTypeName = "IMyService1";
            var serviceType     = TestableTypeBuilder.New()
                                  .WithName(serviceTypeName)
                                  .WithNamespace("Test.Type")
                                  .Build();

            var proxyTypeName = "BootstrapperService";
            var proxyType     = TestableTypeBuilder.New()
                                .WithName(proxyTypeName)
                                .WithNamespace("Test.Type")
                                .Build();

            var bootstrapperServiceFieldName = "bootstrapperService";
            var bootstrapperServiceField     = TestableFieldBuilder.New()
                                               .WithName(bootstrapperServiceFieldName)
                                               .WithAttribute(TestableAttributeBuilder.New()
                                                              .WithAttributeType(typeof(Prise.Plugin.BootstrapperServiceAttribute))
                                                              .WithNamedAgrument("ServiceType", serviceType)
                                                              .WithNamedAgrument("ProxyType", proxyType)
                                                              .Build())
                                               .Build();

            var typeName     = "MyTestType";
            var testableType = TestableTypeBuilder.New()
                               .WithCustomAttributes(pluginAttribute)
                               .WithName(typeName)
                               .WithNamespace("Test.Type")
                               .Build();

            var bootstrapperAttribute = TestableAttributeBuilder.New()
                                        .WithAttributeType(typeof(Prise.Plugin.PluginBootstrapperAttribute))
                                        .WithNamedAgrument("PluginType", testableType)
                                        .Build();

            var bootstrapperName = "MyTestTypeBootstrapper";
            var bootstrapperType = TestableTypeBuilder.New()
                                   .WithCustomAttributes(bootstrapperAttribute)
                                   .WithName(bootstrapperName)
                                   .WithNamespace("Test.Type")
                                   .WithFields(new[] { bootstrapperServiceField })
                                   .Build();

            assemblyShim.Setup(a => a.Types).Returns(new[] { testableType, bootstrapperType });

            var sut    = new DefaultPluginActivationContextProvider();
            var result = sut.ProvideActivationContext(testableType, assemblyShim.Object);

            Assert.IsNotNull(result);
            Assert.IsNull(result.PluginActivatedMethod);
            Assert.IsNull(result.PluginFactoryMethod);
            Assert.AreEqual(0, result.PluginServices.Count());
            Assert.AreEqual(bootstrapperServiceFieldName, result.BootstrapperServices.ElementAt(0).FieldName);
            Assert.AreEqual(proxyTypeName, result.BootstrapperServices.ElementAt(0).ProxyType.Name);
            Assert.AreEqual(serviceTypeName, result.BootstrapperServices.ElementAt(0).ServiceType.Name);
            Assert.AreEqual(typeName, result.PluginType.Name);
            Assert.AreEqual(bootstrapperName, result.PluginBootstrapperType.Name);
        }
        public void ProvideActivationContext_WithPluginServices_Works()
        {
            var assemblyShim    = this.mockRepository.Create <IAssemblyShim>();
            var contract        = GetContract();
            var pluginAttribute = GetPluginAttributeForContract(contract);

            // Factory method
            var factoryMethodName = "This_Is_The_Factory_Method";
            var factoryMethod     = TestableMethodInfoBuilder.New()
                                    .WithName(factoryMethodName)
                                    .WithAttribute(TestableAttributeBuilder.New()
                                                   .WithAttributeType(typeof(Prise.Plugin.PluginFactoryAttribute)).Build())
                                    .Build();

            // Activated method
            var activatedMethodName = "This_Is_The_Activation_Method";
            var activatedMethod     = TestableMethodInfoBuilder.New()
                                      .WithName(activatedMethodName)
                                      .WithAttribute(TestableAttributeBuilder.New()
                                                     .WithAttributeType(typeof(Prise.Plugin.PluginActivatedAttribute)).Build())
                                      .Build();


            var serviceTypeName1 = "IMyService1";
            var serviceType1     = TestableTypeBuilder.New()
                                   .WithName(serviceTypeName1)
                                   .WithNamespace("Test.Type")
                                   .Build();

            var pluginService1 = "pluginService";
            var serviceField1  = TestableFieldBuilder.New()
                                 .WithName(pluginService1)
                                 .WithAttribute(TestableAttributeBuilder.New()
                                                .WithAttributeType(typeof(Prise.Plugin.PluginServiceAttribute))
                                                .WithNamedAgrument("ServiceType", serviceType1)
                                                .WithNamedAgrument("ProvidedBy", ProvidedBy.Plugin)
                                                .Build())
                                 .Build();

            var proxyTypeName = "IProxyType";
            var proxyType     = TestableTypeBuilder.New()
                                .WithName(proxyTypeName)
                                .WithNamespace("Test.Type")
                                .Build();

            var pluginService2 = "hostService";
            var serviceField2  = TestableFieldBuilder.New()
                                 .WithName(pluginService2)
                                 .WithAttribute(TestableAttributeBuilder.New()
                                                .WithAttributeType(typeof(Prise.Plugin.PluginServiceAttribute))
                                                .WithNamedAgrument("ServiceType", serviceType1)
                                                .WithNamedAgrument("ProvidedBy", ProvidedBy.Host)
                                                .WithNamedAgrument("ProxyType", proxyType)
                                                .Build())
                                 .Build();

            var typeName     = "MyTestType";
            var testableType = TestableTypeBuilder.New()
                               .WithCustomAttributes(pluginAttribute)
                               .WithName(typeName)
                               .WithNamespace("Test.Type")
                               .WithMethods(new[] { factoryMethod, activatedMethod })
                               .WithFields(new[] { serviceField1, serviceField2 })
                               .Build();

            // NO bootstrapper

            assemblyShim.Setup(a => a.Types).Returns(new[] { testableType });

            var sut    = new DefaultPluginActivationContextProvider();
            var result = sut.ProvideActivationContext(testableType, assemblyShim.Object);

            Assert.IsNotNull(result);
            Assert.IsNotNull(result.PluginActivatedMethod);
            Assert.AreEqual(activatedMethodName, result.PluginActivatedMethod.Name);
            Assert.IsNotNull(result.PluginFactoryMethod);
            Assert.AreEqual(factoryMethodName, result.PluginFactoryMethod.Name);
            Assert.AreEqual(2, result.PluginServices.Count());
            Assert.AreEqual(pluginService1, result.PluginServices.ElementAt(0).FieldName);
            Assert.AreEqual(pluginService2, result.PluginServices.ElementAt(1).FieldName);
            Assert.AreEqual(ProvidedBy.Plugin, result.PluginServices.ElementAt(0).ProvidedBy);
            Assert.AreEqual(ProvidedBy.Host, result.PluginServices.ElementAt(1).ProvidedBy);
            Assert.IsNull(result.PluginServices.ElementAt(0).ProxyType);
            Assert.IsNotNull(result.PluginServices.ElementAt(1).ProxyType);
            Assert.AreEqual(proxyTypeName, result.PluginServices.ElementAt(1).ProxyType.Name);
            Assert.AreEqual(serviceTypeName1, result.PluginServices.ElementAt(0).ServiceType.Name);
            Assert.AreEqual(serviceTypeName1, result.PluginServices.ElementAt(1).ServiceType.Name);
            Assert.AreEqual(typeName, result.PluginType.Name);
        }
Beispiel #8
0
        public async Task Compressed_NugetPackage_Must_Be_UnCompressed()
        {
            var package              = "Prise.Plugin.Package.1.0.0.nupkg";
            var startingPath         = "/home/maarten";
            var nugetUtilities       = this.mockRepository.Create <INugetPackageUtilities>();
            var metadataLoadContext  = this.mockRepository.Create <IMetadataLoadContext>();
            var assemblyShim         = this.mockRepository.Create <IAssemblyShim>();
            var directoryTraverser   = this.mockRepository.Create <IDirectoryTraverser>();
            var actualNugetUtilities = new DefaultNugetPackageUtilities();

            nugetUtilities.Setup(n => n.FindAllNugetPackagesFiles(startingPath)).Returns(new[] { package });
            // Call actual implementation here
            nugetUtilities.Setup(n => n.GetVersionFromPackageFile(It.IsAny <string>())).Returns <string>((s) => actualNugetUtilities.GetVersionFromPackageFile(s));
            // Call actual implementation here
            nugetUtilities.Setup(n => n.GetPackageName(It.IsAny <string>())).Returns <string>((s) => actualNugetUtilities.GetPackageName(s));
            nugetUtilities.Setup(n => n.HasAlreadyBeenExtracted(It.IsAny <string>())).Returns(false);
            nugetUtilities.Setup(n => n.UnCompressNugetPackage(It.IsAny <string>(), It.IsAny <string>())).Verifiable();

            directoryTraverser.Setup(d => d.TraverseDirectories($"{startingPath}/_extracted")).Returns(new[] { "pathy/mcpathface" });
            directoryTraverser.Setup(d => d.TraverseFiles(It.IsAny <string>(), It.IsAny <IEnumerable <string> >())).Returns(new[] { "filey.mcfile.face" });

            var contract = TestableTypeBuilder.New()
                           .WithName("IMyTestType")
                           .WithNamespace("Test.Type")
                           .Build();

            var pluginAttributeTypedValue = new CustomAttributeTypedArgument(contract);
            var pluginAttribute           = new TestableAttribute
            {
                _AttributeType  = typeof(Prise.Plugin.PluginAttribute),
                _NamedArguments = new[] { new CustomAttributeNamedArgument(new TestableMemberInfo {
                        _Name = "PluginType"
                    }, pluginAttributeTypedValue) }
            };

            var testableType = TestableTypeBuilder.New()
                               .WithCustomAttributes(pluginAttribute)
                               .WithName("MyTestType")
                               .WithNamespace("Test.Type")
                               .Build();

            assemblyShim.Setup(a => a.Types).Returns(new[] { testableType });

            metadataLoadContext.Setup(c => c.LoadFromAssemblyName(It.IsAny <string>())).Returns(assemblyShim.Object);

            var scanner = new DefaultNugetPackageAssemblyScanner(
                (s) => metadataLoadContext.Object,
                () => directoryTraverser.Object,
                () => nugetUtilities.Object
                );
            var types = await scanner.Scan(new AssemblyScannerOptions
            {
                StartingPath = startingPath,
                PluginType   = contract
            });

            var result = types.FirstOrDefault();

            Assert.IsNotNull(result);
            Assert.AreEqual("MyTestType", result.PluginType.Name);
            Assert.AreEqual("Test.Type", result.PluginType.Namespace);
        }