Esempio n. 1
0
        public void TestEmbedTextFileInConsoleExe()
        {
            var file    = Path.Combine(RepositoryLocator.Locate(RepositoryDirectory.TestFiles), "test.txt");
            var command = string.Format("\"/input:{0}\" \"/output:{1}\" {2}>ConsoleTestWithResource.exe.test.txt",
                                        Path.Combine(AssemblyDirectory(), "ConsoleTest.exe"),
                                        Path.Combine(AssemblyDirectory(), "ConsoleTestWithResource.exe"),
                                        file);

            // embed file into exe
            var r = Process.Start(Path.Combine(AssemblyDirectory(), "ResourceEmbedderSample.exe"), command);

            r.WaitForExit(5000).Should().BeTrue();
            r.ExitCode.Should().Be(0);

            // new file should now exist
            File.Exists(Path.Combine(AssemblyDirectory(), "ConsoleTestWithResource.exe")).Should().BeTrue();

            var path = Path.GetTempFileName();

            File.Delete(path);

            // run new file with command line that will extract the resource
            var r2 = Process.Start(Path.Combine(AssemblyDirectory(), "ConsoleTestWithResource.exe"), string.Format("ConsoleTestWithResource.exe.test.txt {0}", path));

            r2.WaitForExit(5000).Should().BeTrue();
            r2.ExitCode.Should().Be(0);

            // since we embedded a text file the file that was extracted should be that same file
            File.ReadAllText(path).Should().Be("Hello world!");
        }
        public void TestEmbeddTextFileInConsoleApplication()
        {
            var fileToEmbed = Path.Combine(RepositoryLocator.Locate(RepositoryDirectory.TestFiles), "test.txt");
            var resources   = new[]
            {
                new ResourceInfo(fileToEmbed, "ConsoleTest.subfolder.test.txt"),
                new ResourceInfo(fileToEmbed, "TotallyDifferentName.test.txt")
            };

            using (var helper = EmbedHelper(Path.Combine(AssemblyDirectory(), "ConsoleTest.exe"), resources))
            {
                var names = helper.Assembly.GetManifestResourceNames();
                names.Should().Contain(new[]
                {
                    "ConsoleTest.subfolder.test.txt",
                    "TotallyDifferentName.test.txt"
                });
                var tempFile2 = Path.GetTempFileName();
                using (var extractedFile = helper.Assembly.GetManifestResourceStream("ConsoleTest.subfolder.test.txt"))
                    using (var fs = new FileStream(tempFile2, FileMode.Truncate))
                    {
                        extractedFile.Should().NotBeNull();
                        extractedFile.CopyTo(fs);
                    }
                File.ReadAllText(tempFile2).Should().Be("Hello world!");
                File.Delete(tempFile2);
            }
        }
Esempio n. 3
0
        public void TestCopyLocalProjectReference()
        {
            // test for https://github.com/MarcStan/Resource.Embedder/issues/5

            /* specifically:
             *
             * if a project reference is set to CopyLocal: false (and then e.g. copied manually via xcopy script or similar) the resource embedder may fail
             * it will only fail if the project containing the reference fullfills this requirement: https://github.com/jbevain/cecil/issues/236
             * 1. either you have a const field with a TypeRef to an enum or
             * 2. you have a custom attribute instantiated with a TypeRef to an enum.
             *
             * The ProjectForcingCecilAssemblyResolve contains a const reference to an enum from ProjectWithEnum which is a project reference that is marked CopyLocal: False
             * This enum now forces cecil to actually look for the ProjectWithEnum assembly. Resource.Embeder v1.1.1 and prior used to wrongly look in only the target directory of ProjectForcingCecilAssemblyResolve
             * Since copy local is false, the ProjectWithEnum reference is not yet found there; crashing Resource.Embedder
             */

            // technically this test never ran with Resource.Embedder v1.1.1 or prior because a clean build always resulted in a crash and no compiled exe
            // we now use this test to assert that the exe is actually compiled which means that reference bug must now be fixed (otherwise we would get a compile error)

            var fi = new DirectoryInfo(AssemblyDirectory());
            // e.g. net46\Debug
            var config = Path.Combine(fi.Parent.Name, fi.Name);

            // locate the exe
            var file = Path.Combine(RepositoryLocator.Locate(RepositoryDirectory.SourceCode), @"testmodules\ProjectForcingCecilAssemblyResolve\bin\" + config + "\\ProjectForcingCecilAssemblyResolve.exe");

            File.Exists(file).Should().BeTrue();

            // execute it and capture the output
            var pi = new ProcessStartInfo(file, "de")
            {
                RedirectStandardOutput = true,
                UseShellExecute        = false
            };
            var r2 = new Process {
                StartInfo = pi
            };
            var lines = new List <string>();

            r2.OutputDataReceived += (sender, args) =>
            {
                if (args.Data != null)
                {
                    lines.Add(args.Data);
                }
            };

            r2.Start();
            r2.BeginOutputReadLine();

            r2.WaitForExit(1000).Should().BeTrue();
            r2.ExitCode.Should().Be(0);

            // verify the output
            lines.Should().HaveCount(2);
            lines[0].Should().Be("Language: German");
            lines[1].Should().Be("Const enum value is: One");
        }
        public void MsBuildBasedEmbeddingWithPortableSymbolsWorksInNetCore()
        {
            // copy elsewhere and ensure localization works
            var copyDir       = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString())).FullName;
            var configuration =
#if DEBUG
                "Debug";
#else
                "Release";
#endif
            // must copy from original dir as .Net Core 2.2. can't be referenced from full framework..
            // not ideal as it doesn't ensure build is up to date..
            // also must copy multiple files for .net core

            var originalDir = $"{RepositoryLocator.Locate(RepositoryDirectory.SourceCode)}\\testmodules\\Symbols\\NetCorePortable\\bin\\{configuration}\\netcoreapp3.1";
            var output      = Path.Combine(copyDir, "NetCorePortable.dll");
            var outputPdb   = Path.Combine(copyDir, "NetCorePortable.pdb");
            var toCopy      = Directory.GetFiles(originalDir, "*.*", SearchOption.AllDirectories);
            foreach (var srcFile in toCopy)
            {
                var target = srcFile.Substring(originalDir.Length + 1);
                if (target.Contains("\\"))
                {
                    Directory.CreateDirectory(Path.Combine(copyDir, target.Substring(0, target.LastIndexOf("\\"))));
                }
                File.Copy(srcFile, Path.Combine(copyDir, target));
            }

            var fakeEngine = NSubstitute.Substitute.For <IBuildEngine>();

            var task = new SatelliteAssemblyEmbedderTask
            {
                ProjectDirectory = ".",
                AssemblyPath     = output,
                TargetPath       = output,
                BuildEngine      = fakeEngine,
                References       = ".",
                DebugSymbols     = true,
                DebugType        = "portable"
            };
            task.Execute().Should().BeTrue();
            File.Exists(output).Should().BeTrue();
            File.Exists(outputPdb).Should().BeTrue();

            var p = Process.Start("dotnet", output);
            p.WaitForExit(3000).Should().BeTrue();
            p.ExitCode.Should().Be(0);
            Directory.Delete(copyDir, true);
        }