TestUtil.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright (c) Microsoft. All rights reserved.
  2. // Licensed under the MIT license. See LICENSE file in the project root for full license information.
  3. using System;
  4. using System.Diagnostics;
  5. using System.IO;
  6. namespace OpenXmlPowerTools
  7. {
  8. public static class TestUtil
  9. {
  10. public static readonly DirectoryInfo SourceDir = new DirectoryInfo("../../../../TestFiles/");
  11. private static bool? _deleteTempFiles;
  12. private static DirectoryInfo _tempDir;
  13. public static bool DeleteTempFiles
  14. {
  15. get
  16. {
  17. if (_deleteTempFiles != null) return (bool)_deleteTempFiles;
  18. var donotdelete = new FileInfo("donotdelete.txt");
  19. _deleteTempFiles = !donotdelete.Exists;
  20. return (bool)_deleteTempFiles;
  21. }
  22. }
  23. public static DirectoryInfo TempDir
  24. {
  25. get
  26. {
  27. if (_tempDir != null) return _tempDir;
  28. DateTime now = DateTime.Now;
  29. string tempDirName =
  30. $"Test-{now.Year - 2000:00}-{now.Month:00}-{now.Day:00}-{now.Hour:00}{now.Minute:00}{now.Second:00}";
  31. _tempDir = new DirectoryInfo(Path.Combine(".", tempDirName));
  32. _tempDir.Create();
  33. return _tempDir;
  34. }
  35. }
  36. public static void NotePad(string str)
  37. {
  38. string guidName = Guid.NewGuid().ToString().Replace("-", "") + ".txt";
  39. var fi = new FileInfo(Path.Combine(TempDir.FullName, guidName));
  40. File.WriteAllText(fi.FullName, str);
  41. var notepadExe = new FileInfo(@"C:\Program Files (x86)\Notepad++\notepad++.exe");
  42. if (!notepadExe.Exists)
  43. {
  44. notepadExe = new FileInfo(@"C:\Program Files\Notepad++\notepad++.exe");
  45. }
  46. if (!notepadExe.Exists)
  47. {
  48. notepadExe = new FileInfo(@"C:\Windows\System32\notepad.exe");
  49. }
  50. ExecutableRunner.RunExecutable(notepadExe.FullName, fi.FullName, TempDir.FullName);
  51. }
  52. public static void KDiff3(FileInfo oldFi, FileInfo newFi)
  53. {
  54. var kdiffExe = new FileInfo(@"C:\Program Files (x86)\KDiff3\kdiff3.exe");
  55. ExecutableRunner.RunResults unused =
  56. ExecutableRunner.RunExecutable(kdiffExe.FullName, oldFi.FullName + " " + newFi.FullName, TempDir.FullName);
  57. }
  58. public static void Explorer(DirectoryInfo di)
  59. {
  60. Process.Start(di.FullName);
  61. }
  62. }
  63. }