Esempio n. 1
0
        public void ThrowsIfSubDependencyNotInAnyAssemblyLoader()
        {
            var subDependency = CreateAssembly("");
            var name          = "a";
            var version       = new Version(1, 2, 3);

            CreateAssembly(
                "",
                name,
                version,
                subDependency)
            .VerifyDiagnostics()
            .VerifyEmit(testEmittedAssembly: (original, assembly, assemblyBytes) =>
            {
                var assemblyLoadContext = new AssemblyLoadContext(null, true);
                var assemblyLoader      = new MockAssemblyLoader(
                    new AssemblyLoadResult(assembly, assemblyBytes));
                var dependencyLoader = new DependencyLoader(
                    ImmutableArray.Create <IAssemblyLoader>(assemblyLoader),
                    new DependencyAttributeReader(),
                    _assemblyFactory,
                    GetLogger <DependencyLoader>());
                Assert.Throws <FlcException>(() => dependencyLoader.LoadDependenciesAsync(
                                                 new ProjectInfo(
                                                     "p",
                                                     new flc.ProjectSystem.Version(0, 0),
                                                     ImmutableArray.Create(""),
                                                     references: ImmutableArray.Create(new Reference(
                                                                                           Reference.ReferenceType.Assembly,
                                                                                           name,
                                                                                           version.ToString()))),
                                                 assemblyLoadContext,
                                                 Array.Empty <IAssembly>()).Result);
            });
        }
Esempio n. 2
0
 public void CanLoadDependeciesWithSubdependencies()
 {
     CreateAssembly(
         "",
         "sub",
         new Version(1, 2, 3))
     .VerifyDiagnostics()
     .VerifyEmit(testEmittedAssembly: (_, subDependency, subDependencyBytes) =>
     {
         var name    = "a";
         var version = new Version(1, 2, 3);
         CreateAssembly(
             "",
             name,
             version)
         .VerifyDiagnostics()
         .VerifyEmit(testEmittedAssembly: (original, assembly, assemblyBytes) =>
         {
             var assemblyLoadContext = new AssemblyLoadContext(null, true);
             var assemblyLoader      = new MockAssemblyLoader(
                 new AssemblyLoadResult(subDependency, subDependencyBytes),
                 new AssemblyLoadResult(assembly, assemblyBytes));
             var dependencyLoader = new DependencyLoader(
                 ImmutableArray.Create <IAssemblyLoader>(assemblyLoader),
                 new DependencyAttributeReader(),
                 _assemblyFactory,
                 GetLogger <DependencyLoader>());
             var dependencies = dependencyLoader.LoadDependenciesAsync(
                 new ProjectInfo(
                     "p",
                     new flc.ProjectSystem.Version(0, 0),
                     ImmutableArray.Create(""),
                     references: ImmutableArray.Create(new Reference(
                                                           Reference.ReferenceType.Assembly,
                                                           name,
                                                           version.ToString()))),
                 assemblyLoadContext,
                 Array.Empty <IAssembly>()).Result;
             var dependency = dependencies.Single();
             Assert.Equal(original.Name, dependency.Name);
             Assert.Equal(original.Version, dependency.Version);
         });
     });
 }
Esempio n. 3
0
        public MetadataAssembly(Assembly assembly, ImmutableArray <byte> assemblyBytes, ImmutableArray <IAssembly> dependencies)
        {
            _diagnostics   = new DiagnosticBag(this);
            _assembly      = assembly;
            _assemblyBytes = assemblyBytes;
            if (assembly.FullName is null)
            {
                _diagnostics.Add(new Diagnostic(
                                     new Location(),
                                     ErrorCode.InvalidMetadataAssembly,
                                     ImmutableArray.Create <object?>("Metadata Assembly has no name")));
            }

            var assemblyNameAttributes = assembly.GetAttributes <AssemblyNameAttribute>();

            if (assemblyNameAttributes.Length != 1)
            {
                _diagnostics.Add(new Diagnostic(
                                     new Location(),
                                     ErrorCode.InvalidMetadataAssembly,
                                     ImmutableArray.Create <object?>($"Metadata Assembly must have exactly one {nameof(AssemblyNameAttribute)}")));
                Name = new QualifiedName("");
            }
            else
            {
                Name = QualifiedName.Parse(assemblyNameAttributes[0].Name);
            }

            var assemblyFileVersionAttributes = assembly.GetAttributes <AssemblyFileVersionAttribute>();

            if (assemblyFileVersionAttributes.Length != 1)
            {
                _diagnostics.Add(new Diagnostic(
                                     new Location(),
                                     ErrorCode.InvalidMetadataAssembly,
                                     ImmutableArray.Create <object?>($"Metadata Assembly must have exactly one {nameof(AssemblyFileVersionAttribute)}")));
                Version = new Version(0, 0, 0);
            }
            else
            {
                var versionAttribute = assemblyFileVersionAttributes[0];
                if (!Version.TryParse(versionAttribute.Version, out var version))
                {
                    _diagnostics.Add(new Diagnostic(
                                         new Location(),
                                         ErrorCode.InvalidMetadataAssembly,
                                         ImmutableArray.Create <object?>($"Version `{version}` is invalid")));
                    Version = new Version(0, 0, 0);
                }
                else
                {
                    Version = version;
                }
            }

            _referencedAssemblies = new Lazy <ImmutableArray <IAssembly> >(
                () => ((IAssembly)this).CalculateReferencedAssemblies(dependencies, _diagnostics).ToImmutableArray());
            _referencedAssembliesAndSelf = new Lazy <ImmutableArray <IAssembly> >(
                () => ReferencedAssemblies.Add(this));

            _methodsByName    = new Lazy <IReadOnlyDictionary <QualifiedName, IMethod> >(GenerateMethods);
            _methods          = new Lazy <ImmutableArray <IMethod> >(() => _methodsByName.Value.Values.ToImmutableArray());
            _interfacesByName = new Lazy <IReadOnlyDictionary <QualifiedName, IInterface> >(GenerateInterfaces);
            _interfaces       = new Lazy <ImmutableArray <IInterface> >(() => _interfacesByName.Value.Values.ToImmutableArray());
            _allDiagnostics   = new Lazy <ImmutableArray <Diagnostic> >(() =>
            {
                _diagnostics.EnsureAllDiagnosticsCollectedForSymbol();
                return(_diagnostics.ToImmutableArray());
            });
        }