コード例 #1
0
        public void RetargetableFlagSet(bool strictMode)
        {
            string syntax = @"
using System.Reflection;

[assembly: AssemblyFlags(AssemblyNameFlags.Retargetable)]
";

            // Emitting the assembly to a physical location to workaround:
            // https://github.com/dotnet/roslyn/issues/54836
            string leftAssembly  = SymbolFactory.EmitAssemblyFromSyntax(syntax, publicKey: _publicKey);
            string rightAssembly = SymbolFactory.EmitAssemblyFromSyntax(syntax);

            IAssemblySymbol leftSymbol  = new AssemblySymbolLoader().LoadAssembly(leftAssembly);
            IAssemblySymbol rightSymbol = new AssemblySymbolLoader().LoadAssembly(rightAssembly);

            Assert.True(leftSymbol.Identity.IsRetargetable);
            Assert.True(rightSymbol.Identity.IsRetargetable);
            Assert.False(rightSymbol.Identity.HasPublicKey);
            Assert.Equal(_publicKey, leftSymbol.Identity.PublicKey);

            ApiComparer differ = new();

            differ.StrictMode = strictMode;

            Assert.Empty(differ.GetDifferences(leftSymbol, rightSymbol));
        }
コード例 #2
0
        public override bool Execute()
        {
            if ((LeftPaths == null || LeftPaths.Length == 0) && (LeftSourcesPath == null || LeftSourcesPath.Length == 0))
            {
                Log.LogError($"'{nameof(LeftPaths)}' or '{nameof(LeftSourcesPath)}' must contain at least one element to run ApiCompatibility.");
                return(false);
            }

            if (RightDirectories == null || RightDirectories.Length == 0)
            {
                Log.LogError($"'{nameof(RightDirectories)}' must contain at least one directory to search for the right binaries.");
                return(false);
            }

            if (string.IsNullOrEmpty(LeftName))
            {
                LeftName = "contract";
            }

            if (string.IsNullOrEmpty(RightName))
            {
                RightName = "implementation";
            }

            IEnumerable <IAssemblySymbol> leftSymbols;
            HashSet <string> rightDependsOnDirs = new HashSet <string>();

            if (ShouldResolveAssemblyReferences && RightDependsOn != null)
            {
                foreach (string dependency in RightDependsOn)
                {
                    if (Directory.Exists(dependency))
                    {
                        rightDependsOnDirs.Add(dependency);
                    }
                    else if (File.Exists(dependency))
                    {
                        rightDependsOnDirs.Add(Path.GetDirectoryName(dependency));
                    }
                }
            }

            AssemblySymbolLoader leftLoader;

            if (LeftPaths != null)
            {
                leftLoader = new AssemblySymbolLoader(resolveAssemblyReferences: ShouldResolveAssemblyReferences);
                leftLoader.AddReferenceSearchDirectories(rightDependsOnDirs);
                leftSymbols = leftLoader.LoadAssemblies(LeftPaths);
            }
            else
            {
                leftLoader  = new AssemblySymbolLoader(AssemblyName);
                leftSymbols = new[]
                {
                    leftLoader.LoadAssemblyFromSourceFiles(LeftSourcesPath, RightDependsOn),
                };
                ValidateMatchingAssemblyIdentities = false; // in memory assembly might not match identities
            }

            AssemblySymbolLoader rightLoader = new(resolveAssemblyReferences : ShouldResolveAssemblyReferences);

            rightLoader.AddReferenceSearchDirectories(RightDependsOn ?? Array.Empty <string>());
            IEnumerable <IAssemblySymbol> rightSymbols = rightLoader.LoadMatchingAssemblies(leftSymbols, RightDirectories, validateMatchingIdentity: ValidateMatchingAssemblyIdentities);

            ApiDiffer differ = new(includeInternalSymbols : IncludeInternals);

            differ.NoWarn = NoWarn;

            IEnumerable <CompatDifference> differences = differ.GetDifferences(leftSymbols, rightSymbols);

            foreach (CompatDifference difference in differences)
            {
                Log.LogError(difference.ToString());
            }

            return(!Log.HasLoggedErrors);
        }