Esempio n. 1
0
        public void GetRelatedAssemblies_LoadsRelatedAssembly()
        {
            // Arrange
            var destination = Path.Combine(AssemblyDirectory, "RelatedAssembly.dll");
            var assembly    = new TestAssembly
            {
                AttributeAssembly = "RelatedAssembly",
            };
            var relatedAssembly = typeof(RelatedAssemblyPartTest).Assembly;

            try
            {
                File.WriteAllBytes(destination, new byte[0]);
                var result = RelatedAssemblyAttribute.GetRelatedAssemblies(assembly, throwOnError: true, file =>
                {
                    Assert.Equal(file, destination);
                    return(relatedAssembly);
                });
                Assert.Equal(new[] { relatedAssembly }, result);
            }
            finally
            {
                File.Delete(destination);
            }
        }
Esempio n. 2
0
        private static IEnumerable <Assembly> GetAsemblyClosure(Assembly assembly)
        {
            yield return(assembly);

            var relatedAssemblies = RelatedAssemblyAttribute.GetRelatedAssemblies(assembly, throwOnError: false)
                                    .OrderBy(assembly => assembly.FullName, StringComparer.Ordinal);

            foreach (var relatedAssembly in relatedAssemblies)
            {
                yield return(relatedAssembly);
            }
        }
Esempio n. 3
0
        public void GetRelatedAssemblies_Noops_ForDynamicAssemblies()
        {
            // Arrange
            var name     = new AssemblyName($"DynamicAssembly-{Guid.NewGuid()}");
            var assembly = AssemblyBuilder.DefineDynamicAssembly(name, AssemblyBuilderAccess.RunAndCollect);

            // Act
            var result = RelatedAssemblyAttribute.GetRelatedAssemblies(assembly, throwOnError: true);

            // Assert
            Assert.Empty(result);
        }
        public void GetRelatedAssemblies_ThrowsIfAssemblyCannotBeFound()
        {
            // Arrange
            var assemblyPath = Path.Combine(AssemblyDirectory, "MyAssembly.dll");
            var assembly     = new TestAssembly
            {
                AttributeAssembly = "DoesNotExist"
            };

            // Act & Assert
            Assert.Throws <FileNotFoundException>(() => RelatedAssemblyAttribute.GetRelatedAssemblies(assembly, throwOnError: true));
        }
Esempio n. 5
0
        public void GetRelatedAssemblies_ThrowsIfRelatedAttributeReferencesSelf()
        {
            // Arrange
            var expected = "RelatedAssemblyAttribute specified on MyAssembly cannot be self referential.";
            var assembly = new TestAssembly {
                AttributeAssembly = "MyAssembly"
            };

            // Act & Assert
            var ex = Assert.Throws <InvalidOperationException>(() => RelatedAssemblyAttribute.GetRelatedAssemblies(assembly, throwOnError: true));

            Assert.Equal(expected, ex.Message);
        }
        public void GetAssemblyLocation_CodeBase_HasPoundCharacterDOSPath()
        {
            var destination = Path.Combine(AssemblyDirectory, "RelatedAssembly.dll");
            var expected    = @"C:\#NIN\dotnetcore\tryx\try1.dll";
            var assembly    = new TestAssembly
            {
                CodeBaseSettable = "file:///C:/#NIN/dotnetcore/tryx/try1.dll",
                LocationSettable = expected,
            };

            // Act
            var actual = RelatedAssemblyAttribute.GetAssemblyLocation(assembly);

            Assert.Equal(expected, actual);
        }
Esempio n. 7
0
        public void GetAssemblyLocation_UsesCodeBase()
        {
            // Arrange
            var destination = Path.Combine(AssemblyDirectory, "RelatedAssembly.dll");
            var codeBase    = "file://x/file/Assembly.dll";
            var expected    = new Uri(codeBase).LocalPath;
            var assembly    = new TestAssembly
            {
                CodeBaseSettable = codeBase,
            };

            // Act
            var actual = RelatedAssemblyAttribute.GetAssemblyLocation(assembly);

            Assert.Equal(expected, actual);
        }
Esempio n. 8
0
        public void GetRelatedAssemblies_ThrowsIfAssemblyCannotBeFound()
        {
            // Arrange
            var expected     = $"Related assembly 'DoesNotExist' specified by assembly 'MyAssembly' could not be found in the directory {AssemblyDirectory}. Related assemblies must be co-located with the specifying assemblies.";
            var assemblyPath = Path.Combine(AssemblyDirectory, "MyAssembly.dll");
            var assembly     = new TestAssembly
            {
                AttributeAssembly = "DoesNotExist"
            };

            // Act & Assert
            var ex = Assert.Throws <FileNotFoundException>(() => RelatedAssemblyAttribute.GetRelatedAssemblies(assembly, throwOnError: true));

            Assert.Equal(expected, ex.Message);
            Assert.Equal(Path.Combine(AssemblyDirectory, "DoesNotExist.dll"), ex.FileName);
        }
Esempio n. 9
0
        public void GetAssemblyLocation_UsesLocation_IfCodeBaseIsNotLocal()
        {
            // Arrange
            var destination = Path.Combine(AssemblyDirectory, "RelatedAssembly.dll");
            var expected    = Path.Combine(AssemblyDirectory, "Some-Dir", "Assembly.dll");
            var assembly    = new TestAssembly
            {
                CodeBaseSettable = "https://www.microsoft.com/test.dll",
                LocationSettable = expected,
            };

            // Act
            var actual = RelatedAssemblyAttribute.GetAssemblyLocation(assembly);

            Assert.Equal(expected, actual);
        }
Esempio n. 10
0
        public void GetRelatedAssemblies_ReadsAssemblyFromLoadContext_IfItAlreadyExists()
        {
            // Arrange
            var expected        = $"Related assembly 'DoesNotExist' specified by assembly 'MyAssembly' could not be found in the directory {AssemblyDirectory}. Related assemblies must be co-located with the specifying assemblies.";
            var assemblyPath    = Path.Combine(AssemblyDirectory, "MyAssembly.dll");
            var relatedAssembly = typeof(RelatedAssemblyPartTest).Assembly;
            var assembly        = new TestAssembly
            {
                AttributeAssembly = "RelatedAssembly"
            };
            var loadContext = new TestableAssemblyLoadContextWrapper
            {
                Assemblies =
                {
                    ["RelatedAssembly"] = relatedAssembly,
                }
            };

            // Act
            var result = RelatedAssemblyAttribute.GetRelatedAssemblies(assembly, throwOnError: true, file => false, loadContext);

            // Assert
            Assert.Equal(new[] { relatedAssembly }, result);
        }
Esempio n. 11
0
 protected virtual IReadOnlyList <Assembly> GetRelatedAssemblies(Assembly assembly)
 {
     // Do not require related assemblies to be available in the default code path.
     return(RelatedAssemblyAttribute.GetRelatedAssemblies(assembly, throwOnError: false));
 }
Esempio n. 12
0
            public override object[] GetCustomAttributes(Type attributeType, bool inherit)
            {
                var attribute = new RelatedAssemblyAttribute(AttributeAssembly);

                return(new[] { attribute });
            }