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