Exemplo n.º 1
0
        public static string RunMigration(string supportDll, CecilMigrationResult expectedResult, string logPath = null)
        {
            var migratedDll = Utils.GetTempFilename() + ".dll";

            var migrator = new CecilMigrator();

            if (!string.IsNullOrWhiteSpace(logPath))
            {
                migrator.MessageLogged += (sender, e) =>
                {
                    var log = string.Empty;
                    if (e.IsError)
                    {
                        log += "ERROR: ";
                    }
                    if (e.IsVerbose)
                    {
                        log += "VERBOSE: ";
                    }
                    log += e.Message;
                    File.AppendAllLines(logPath, new [] { log });
                };
            }

            var result = migrator.Migrate(supportDll, migratedDll);

            Assert.Equal(expectedResult, result);

            return(migratedDll);
        }
        public void CanRunMigrationOnNonPortablePdbs(string assembly, CecilMigrationResult expectedResult)
        {
            Assert.False(IsPortablePdb(Path.ChangeExtension(assembly, "pdb")));

            var output = RunMigration(assembly, expectedResult);

            Assert.True(IsPortablePdb(Path.ChangeExtension(output, "pdb")), output);
        }
Exemplo n.º 3
0
        public static string RunMigration(string supportDll, CecilMigrationResult expectedResult)
        {
            var migratedDll = Utils.GetTempFilename() + ".dll";

            var migrator = new CecilMigrator();
            var result   = migrator.Migrate(supportDll, migratedDll);

            Assert.Equal(expectedResult, result);

            return(migratedDll);
        }
        public void CanRunMigrationOnCorruptedPdbs(string assembly, string symbols, CecilMigrationResult expectedResult)
        {
            var dll = Utils.GetTempFilename(Path.GetFileName(assembly));
            var pdb = Path.ChangeExtension(dll, "pdb");

            File.Copy(assembly, dll, true);
            File.Copy(symbols, pdb, true);

            var migratedDll = RunMigration(dll, expectedResult);

            using (var def = AssemblyDefinition.ReadAssembly(migratedDll))
            {
                var types = def.MainModule.GetTypeReferences();
                AssertNoSupportTypes(types);
            }
            Assert.True(IsPortablePdb(Path.ChangeExtension(migratedDll, "pdb")), migratedDll);
        }
        public void AssembliesHaveTheSameReferencesAfterMigration(string supportDll, string androidXDll, CecilMigrationResult expectedResult)
        {
            var migratedDll = RunMigration(supportDll, expectedResult);

            using (var migrated = AssemblyDefinition.ReadAssembly(migratedDll))
                using (var androidx = AssemblyDefinition.ReadAssembly(androidXDll))
                {
                    CecilAssert.Equal(
                        androidx.MainModule.AssemblyReferences.OrderBy(a => a.Name),
                        migrated.MainModule.AssemblyReferences.OrderBy(a => a.Name));
                }
        }
        public void AssembliesHaveTheSameTypesAfterMigration(string supportDll, string androidXDll, bool ignoreResourceType, CecilMigrationResult expectedResult)
        {
            var migratedDll = RunMigration(supportDll, expectedResult);

            using (var migrated = AssemblyDefinition.ReadAssembly(migratedDll))
                using (var androidx = AssemblyDefinition.ReadAssembly(androidXDll))
                {
                    CecilAssert.Equal(androidx.GetPublicTypes(ignoreResourceType), migrated.GetPublicTypes(ignoreResourceType));
                }
        }
        public void EmbeddedJavaArtifactsAreMigrated(string supportDll, string layoutFile, CecilMigrationResult expectedResult)
        {
            var migratedDll = RunMigration(supportDll, expectedResult);

            using (var migrated = AssemblyDefinition.ReadAssembly(migratedDll))
            {
                var aarResource = migrated.MainModule.Resources.FirstOrDefault(r => r.Name == "__AndroidLibraryProjects__.zip") as EmbeddedResource;
                using (var aarStream = aarResource.GetResourceStream())
                {
                    var migratedLayout = ReadAarEntry(aarStream, layoutFile);

                    Assert.Equal(
                        "androidx.appcompat.widget.AppCompatButton",
                        XDocument.Load(migratedLayout).Root.Elements().FirstOrDefault().Name.LocalName);
                }
            }
        }
        public void AllTypesHaveTheSameMembers(string supportDll, string androidXDll, bool ignoreResourceType, CecilMigrationResult expectedResult)
        {
            var migratedDll = RunMigration(supportDll, expectedResult);

            using (var migrated = AssemblyDefinition.ReadAssembly(migratedDll))
                using (var androidx = AssemblyDefinition.ReadAssembly(androidXDll))
                {
                    var mTypes = migrated.GetPublicTypes(ignoreResourceType).OrderBy(t => t.FullName).ToArray();
                    var xTypes = androidx.GetPublicTypes(ignoreResourceType).OrderBy(t => t.FullName).ToArray();

                    for (var i = 0; i < xTypes.Length; i++)
                    {
                        CecilAssert.Equal(xTypes[i], mTypes[i]);
                    }
                }
        }
Exemplo n.º 9
0
        public void InstructionsInMethodsAreMappedCorrectly(string supportDll, string androidxDll, CecilMigrationResult expectedResult)
        {
            var mappedDll = RunMigration(supportDll, expectedResult);

            using (var support = AssemblyDefinition.ReadAssembly(supportDll))
                using (var mapped = AssemblyDefinition.ReadAssembly(mappedDll))
                    using (var androidx = AssemblyDefinition.ReadAssembly(androidxDll))
                    {
                        var supportTypes  = support.MainModule.GetTypes().ToArray();
                        var mappedTypes   = mapped.MainModule.GetTypes().ToArray();
                        var androidxTypes = androidx.MainModule.GetTypes().ToArray();

                        for (int i = 0; i < supportTypes.Length; i++)
                        {
                            var sType = supportTypes[i];
                            var mType = mappedTypes[i];
                            var xType = androidxTypes[i];

                            for (int j = 0; j < sType.Methods.Count; j++)
                            {
                                var sMethod = sType.Methods[j];
                                var mMethod = mType.Methods[j];
                                var xMethod = xType.Methods[j];

                                if (sMethod.HasBody)
                                {
                                    for (int k = 0; k < sMethod.Body.Instructions.Count; k++)
                                    {
                                        var sInstruction = sMethod.Body.Instructions[k];
                                        var mInstruction = mMethod.Body.Instructions[k];
                                        var xInstruction = xMethod.Body.Instructions[k];

                                        if (sInstruction.OpCode == OpCodes.Ldstr &&
                                            sInstruction.Operand is string sOperand &&
                                            (sOperand.Contains("android/support") || sOperand.Contains("android/arch")))
                                        {
                                            var mOperand = mInstruction.Operand as string;
                                            var xOperand = xInstruction.Operand as string;

                                            Assert.Equal(xOperand, mOperand);
                                        }
                                    }
                                }
                            }
                        }
                    }
        }
Exemplo n.º 10
0
        public void RegisterAttributesOnMethodsAreMappedCorrectly(string supportDll, string androidxDll, CecilMigrationResult expectedResult)
        {
            var mappedDll = RunMigration(supportDll, expectedResult);

            using (var support = AssemblyDefinition.ReadAssembly(supportDll))
                using (var mapped = AssemblyDefinition.ReadAssembly(mappedDll))
                    using (var androidx = AssemblyDefinition.ReadAssembly(androidxDll))
                    {
                        var supportTypes  = support.MainModule.GetTypes().ToArray();
                        var mappedTypes   = mapped.MainModule.GetTypes().ToArray();
                        var androidxTypes = androidx.MainModule.GetTypes().ToArray();

                        for (int i = 0; i < supportTypes.Length; i++)
                        {
                            var sType = supportTypes[i];
                            var mType = mappedTypes[i];
                            var xType = androidxTypes[i];

                            // make sure the types have the same register attributes before and after the migration
                            Assert.Equal(
                                sType.GetRegisterAttribute().GetArguments(),
                                mType.GetRegisterAttribute().GetArguments());
                            Assert.Equal(
                                sType.GetRegisterAttribute().GetArguments(),
                                xType.GetRegisterAttribute().GetArguments());

                            for (int j = 0; j < sType.Methods.Count; j++)
                            {
                                var mMethod = mType.Methods[j];
                                var xMethod = xType.Methods[j];

                                // make sure all the member register attributes have been migrated
                                // skip the last parameter as that is the generated handler name
                                Assert.Equal(
                                    xMethod.GetRegisterAttribute().GetArguments().Take(2),
                                    mMethod.GetRegisterAttribute().GetArguments().Take(2));
                            }
                        }
                    }
        }
Exemplo n.º 11
0
        public void MigrationDoesNotThrow(string assembly, CecilMigrationResult expectedResult)
        {
            var mappedDll = RunMigration(assembly, expectedResult);

            Assert.True(File.Exists(mappedDll));
        }