Example #1
0
        void RunInternal(string dir, string fileToRoundtrip, Action <string> testAction)
        {
            if (!Directory.Exists(TestDir))
            {
                Assert.Ignore($"Assembly-roundtrip test ignored: test directory '{TestDir}' needs to be checked out separately." + Environment.NewLine +
                              $"git clone https://github.com/icsharpcode/ILSpy-tests \"{TestDir}\"");
            }
            string inputDir      = Path.Combine(TestDir, dir);
            string decompiledDir = inputDir + "-decompiled";
            string outputDir     = inputDir + "-output";

            if (inputDir.EndsWith("TestCases"))
            {
                // make sure output dir names are unique so that we don't get trouble due to parallel test execution
                decompiledDir += Path.GetFileNameWithoutExtension(fileToRoundtrip);
                outputDir     += Path.GetFileNameWithoutExtension(fileToRoundtrip);
            }
            ClearDirectory(decompiledDir);
            ClearDirectory(outputDir);
            string projectFile = null;

            foreach (string file in Directory.EnumerateFiles(inputDir, "*", SearchOption.AllDirectories))
            {
                if (!file.StartsWith(inputDir + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase))
                {
                    Assert.Fail($"Unexpected file name: {file}");
                }
                string relFile = file.Substring(inputDir.Length + 1);
                Directory.CreateDirectory(Path.Combine(outputDir, Path.GetDirectoryName(relFile)));
                if (relFile.Equals(fileToRoundtrip, StringComparison.OrdinalIgnoreCase))
                {
                    Console.WriteLine($"Decompiling {fileToRoundtrip}...");
                    Stopwatch w = Stopwatch.StartNew();
                    using (var fileStream = new FileStream(file, FileMode.Open, FileAccess.Read)) {
                        PEFile module   = new PEFile(file, fileStream, PEStreamOptions.PrefetchEntireImage);
                        var    resolver = new UniversalAssemblyResolver(file, false, module.Reader.DetectTargetFrameworkId(), PEStreamOptions.PrefetchMetadata);
                        resolver.AddSearchDirectory(inputDir);
                        resolver.RemoveSearchDirectory(".");
                        var decompiler = new TestProjectDecompiler(inputDir);
                        decompiler.AssemblyResolver = resolver;
                        // Let's limit the roundtrip tests to C# 7.3 for now; because 8.0 is still in preview
                        // and the generated project doesn't build as-is.
                        decompiler.Settings = new DecompilerSettings(LanguageVersion.CSharp7_3);
                        // use a fixed GUID so that we can diff the output between different ILSpy runs without spurious changes
                        decompiler.ProjectGuid = Guid.Parse("{127C83E4-4587-4CF9-ADCA-799875F3DFE6}");
                        decompiler.DecompileProject(module, decompiledDir);
                        Console.WriteLine($"Decompiled {fileToRoundtrip} in {w.Elapsed.TotalSeconds:f2}");
                        projectFile = Path.Combine(decompiledDir, module.Name + ".csproj");
                    }
                }
                else
                {
                    File.Copy(file, Path.Combine(outputDir, relFile));
                }
            }
            Assert.IsNotNull(projectFile, $"Could not find {fileToRoundtrip}");

            Compile(projectFile, outputDir);
            testAction(outputDir);
        }
Example #2
0
        public static string FindAssemblyFile(AssemblyDefinition assemblyDefinition, string assemblyFile)
        {
            var assemblyResolver = new UniversalAssemblyResolver(assemblyFile, false,
                                                                 DetectTargetFrameworkId(assemblyDefinition, assemblyFile));

            if (IsReferenceAssembly(assemblyDefinition, assemblyFile))
            {
                assemblyResolver.RemoveSearchDirectory(Path.GetDirectoryName(assemblyFile));
            }
            return(assemblyResolver.FindAssemblyFile(
                       ICSharpCode.Decompiler.Metadata.AssemblyNameReference.Parse(assemblyDefinition.Name.FullName)));
        }
Example #3
0
        void RunTest(string name, string asmPath, string sourcePath)
        {
            using (var fileStream = new FileStream(asmPath, FileMode.Open, FileAccess.Read)) {
                var module   = new PEFile(asmPath, fileStream);
                var resolver = new UniversalAssemblyResolver(asmPath, false, module.Reader.DetectTargetFrameworkId());
                resolver.RemoveSearchDirectory(".");
                resolver.AddSearchDirectory(Path.GetDirectoryName(asmPath));
                var    res        = module.Resources.First();
                Stream bamlStream = LoadBaml(res, name + ".baml");
                Assert.IsNotNull(bamlStream);
                XDocument document = BamlResourceEntryNode.LoadIntoDocument(module, resolver, bamlStream, CancellationToken.None);

                XamlIsEqual(File.ReadAllText(sourcePath), document.ToString());
            }
        }
Example #4
0
        void RunTest(string name, string asmPath, string sourcePath)
        {
            using (var fileStream = new FileStream(asmPath, FileMode.Open, FileAccess.Read))
            {
                var module   = new PEFile(asmPath, fileStream);
                var resolver = new UniversalAssemblyResolver(asmPath, false, module.Reader.DetectTargetFrameworkId());
                resolver.RemoveSearchDirectory(".");
                resolver.AddSearchDirectory(Path.GetDirectoryName(asmPath));
                var    res        = module.Resources.First();
                Stream bamlStream = LoadBaml(res, name + ".baml");
                Assert.IsNotNull(bamlStream);

                BamlDecompilerTypeSystem typeSystem = new BamlDecompilerTypeSystem(module, resolver);
                var decompiler = new XamlDecompiler(typeSystem, new BamlDecompilerSettings());

                XDocument document = decompiler.Decompile(bamlStream).Xaml;

                XamlIsEqual(File.ReadAllText(sourcePath), document.ToString());
            }
        }