PageRenderTime 26ms CodeModel.GetById 10ms app.highlight 3ms RepoModel.GetById 11ms app.codeStats 0ms

/src/Squirrel.Tests/TestHelpers/IntegrationTestHelper.cs

https://github.com/solrevdev/Squirrel.Windows
C# | 100 lines | 78 code | 15 blank | 7 comment | 2 complexity | d197fcb7c7f4c7e204838c3e3a61f483 MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.Diagnostics;
  4using System.IO;
  5using System.Linq;
  6using System.Reactive;
  7using System.Reactive.Disposables;
  8using System.Reactive.Linq;
  9using System.Threading;
 10using Ionic.Zip;
 11using Squirrel.Core;
 12using ReactiveUIMicro;
 13using Squirrel.Tests.WiXUi;
 14
 15namespace Squirrel.Tests.TestHelpers
 16{
 17    public static class IntegrationTestHelper
 18    {
 19        public static string GetPath(params string[] paths)
 20        {
 21            var ret = GetIntegrationTestRootDirectory();
 22            return (new FileInfo(paths.Aggregate (ret, Path.Combine))).FullName;
 23        }
 24
 25        public static string GetIntegrationTestRootDirectory()
 26        {
 27            // XXX: This is an evil hack, but it's okay for a unit test
 28            // We can't use Assembly.Location because unit test runners love
 29            // to move stuff to temp directories
 30            var st = new StackFrame(true);
 31            var di = new DirectoryInfo(Path.Combine(Path.GetDirectoryName(st.GetFileName()), ".."));
 32
 33            return di.FullName;
 34        }
 35
 36        public static bool SkipTestOnXPAndVista()
 37        {
 38            int osVersion = Environment.OSVersion.Version.Major*100 + Environment.OSVersion.Version.Minor;
 39            return (osVersion < 601);
 40        }
 41
 42        public static void RunBlockAsSTA(Action block)
 43        {
 44            Exception ex = null;
 45            var t = new Thread(() => {
 46                try {
 47                    block();
 48                } catch (Exception e) {
 49                    ex = e;
 50                }
 51            });
 52
 53            t.SetApartmentState(ApartmentState.STA);
 54            t.Start();
 55            t.Join();
 56
 57            if (ex != null) {
 58                // NB: If we don't do this, the test silently passes
 59                throw new Exception("", ex);
 60            }
 61        }
 62
 63        static object gate = 42;
 64        public static IDisposable WithFakeInstallDirectory(string packageFileName, out string path)
 65        {
 66            var ret = Utility.WithTempDirectory(out path);
 67
 68            File.Copy(GetPath("fixtures", packageFileName), Path.Combine(path, packageFileName));
 69            var rp = ReleaseEntry.GenerateFromFile(Path.Combine(path, packageFileName));
 70            ReleaseEntry.WriteReleaseFile(new[] { rp }, Path.Combine(path, "RELEASES"));
 71
 72            // NB: This is a temporary hack. The reason we serialize the tests
 73            // like this, is to make sure that we don't have two tests registering
 74            // their Service Locators with RxApp.
 75            Monitor.Enter(gate);
 76            return new CompositeDisposable(ret, Disposable.Create(() => Monitor.Exit(gate)));
 77        }
 78
 79        public static IDisposable WithFakeInstallDirectory(out string path)
 80        {
 81            return WithFakeInstallDirectory("SampleUpdatingApp.1.1.0.0.nupkg", out path);
 82        }
 83
 84        public static IDisposable WithFakeAlreadyInstalledApp(out string path)
 85        {
 86            return WithFakeAlreadyInstalledApp("InstalledSampleUpdatingApp-1.1.0.0.zip", out path);
 87        }
 88
 89        public static IDisposable WithFakeAlreadyInstalledApp(string zipFile, out string path)
 90        {
 91            var ret = Utility.WithTempDirectory(out path);
 92
 93            var zf = new ZipFile(GetPath("fixtures", zipFile));
 94            zf.ExtractAll(path);
 95
 96            Monitor.Enter(gate);
 97            return new CompositeDisposable(ret, Disposable.Create(() => Monitor.Exit(gate)));
 98        }
 99    }
100}