FileMutantsSequence points
TestDirectory.cs
198 / 198
36 / 38

TestDirectory.cs

Line: Mutants: Source code                                                                       
   1:        : #region Copyright & licence​
   2:        : 
   3:        : // This file is part of NinjaTurtles.​
   4:        : // ​
   5:        : // NinjaTurtles is free software: you can redistribute it and/or modify​
   6:        : // it under the terms of the GNU Lesser General Public License as​
   7:        : // published by the Free Software Foundation, either version 3 of the​
   8:        : // License, or (at your option) any later version.​
   9:        : // ​
  10:        : // NinjaTurtles is distributed in the hope that it will be useful,​
  11:        : // but WITHOUT ANY WARRANTY; without even the implied warranty of​
  12:        : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the​
  13:        : // GNU Lesser General Public License for more details.​
  14:        : // ​
  15:        : // You should have received a copy of the GNU Lesser General Public​
  16:        : // License along with NinjaTurtles.  If not, see <http://www.gnu.org/licenses/>.​
  17:        : // ​
  18:        : // Copyright (C) 2012 David Musgrove and others.​
  19:        : 
  20:        : #endregion​
  21:        : 
  22:        : using System;​
  23:        : using System.IO;​
  24:        : 
  25:        : using NLog;​
  26:        : 
  27:        : namespace NinjaTurtles​
  28:        : {​
  29:        :     /// <summary>​
  30:        :     /// Represents a temporary directory used to contain a mutated assembly​
  31:        :     /// to be tested. The directory cleans up after itself when its​
  32:        :     /// <see mref="Dispose" /> method is called, unless its​
  33:        :     /// <see pref="DoNotDelete" /> property is set to <b>true</b>.​
  34:        :     /// </summary>​
  35:        :     public class TestDirectory : IDisposable​
  36:        :     {​
  37:        :         #region Logging​
  38:        : 
  39:        :         private static Logger _log = LogManager.GetCurrentClassLogger();​
  40:        : 
  41:        :         #endregion​
  42:        : 
  43:        :         private readonly string _folder;​
  44:        : 
  45:        :         /// <summary>​
  46:        :         /// Initializes a new instance of the <see cref="TestDirectory" />​
  47:        :         /// class.​
  48:        :         /// </summary>​
  49:        :         public TestDirectory()​
  50:        :         {​
  51:     1/1:             _folder = Path.Combine(Path.GetTempPath(),​
  52:     1/1:                                    "NinjaTurtles",​
  53:     1/1:                                    Guid.NewGuid().ToString("N"));​
  54:     1/1:             _log.Debug("Creating folder \"{0}\".", _folder);​
  55:     1/1:             Directory.CreateDirectory(_folder);​
  56:        :         }​
  57:        : 
  58:        :         /// <summary>​
  59:        :         /// Initializes a new instance of the <see cref="TestDirectory" />​
  60:        :         /// class.​
  61:        :         /// </summary>​
  62:        :         /// <param name="sourceFolder">​
  63:        :         /// The name of a folder whose contents should be recursively​
  64:        :         /// copied to the temporary folder.​
  65:        :         /// </param>​
  66:        :         public TestDirectory(string sourceFolder)​
  67:        :             : this()​
  68:        :         {​
  69:     2/2:             _log.Debug("Copying contents from folder \"{0}\".", sourceFolder);​
  70:     3/3:             CopyDirectoryContents(sourceFolder, _folder);​
  71:        :         }​
  72:        : 
  73:        :         /// <summary>​
  74:        :         /// Saves an image of a mutated assembly into the root of the test​
  75:        :         /// directory.​
  76:        :         /// </summary>​
  77:        :         /// <param name="module"></param>​
  78:        :         public void SaveAssembly(Module module)​
  79:        :         {​
  80:     3/3:             string fileName = Path.GetFileName(module.AssemblyLocation);​
  81:     7/7:             string path = Path.Combine(_folder, fileName);​
  82:     5/5:             _log.Debug("Writing assembly \"{0}\" to \"{1}\".", fileName, _folder);​
  83:     3/3:             module.AssemblyDefinition.Write(path);​
  84:        :         }​
  85:        : 
  86:        :         private static void CopyDirectoryContents​
  87:        :             (string directory, string targetDirectory)​
  88:        :         {​
  89:   19/19:             foreach (var file in Directory.GetFiles(directory))​
  90:        :             {​
  91:   11/11:                 string fileName = Path.GetFileName(file);​
  92:     7/7:                 _log.Trace("Copying file \"{0}\".", fileName);​
  93:   17/17:                 string target = Path.Combine(targetDirectory, fileName);​
  94:   13/13:                 File.Copy(file, target);​
  95:     5/5:             }​
  96:   19/19:             foreach (var subDirectory in Directory.GetDirectories(directory))​
  97:        :             {​
  98:   11/11:                 string subDirectoryName = Path.GetFileName(subDirectory);​
  99:     7/7:                 _log.Trace("Creating subdirectory \"{0}\".", subDirectoryName);​
 100:   17/17:                 string target = Path.Combine(targetDirectory, subDirectoryName);​
 101:     7/7:                 Directory.CreateDirectory(target);​
 102:   13/13:                 CopyDirectoryContents(subDirectory, target);​
 103:     5/5:             }​
 104:        :         }​
 105:        : 
 106:        :         /// <summary>​
 107:        :         /// Gets the full path of the test directory.​
 108:        :         /// </summary>​
 109:        :         public string FullName​
 110:        :         {​
 111:     2/2:             get { return _folder; }​
 112:        :         }​
 113:        : 
 114:        :         /// <summary>​
 115:        :         /// Performs application-defined tasks associated with freeing,​
 116:        :         /// releasing, or resetting unmanaged resources.​
 117:        :         /// </summary>​
 118:        :         public void Dispose()​
 119:        :         {​
 120:     5/5:             if (DoNotDelete)​
 121:        :             {​
 122:     1/1:                 return;​
 123:        :             }​
 124:        :             try​
 125:        :             {​
 126:     2/2:                 _log.Debug("Deleting folder \"{0}\".", _folder);​
 127:     2/2:                 Directory.Delete(_folder, true);​
 128:     1/1:             }​
 129:     1/1:             catch (Exception ex)​
 130:        :             {​
 131:     3/3:                 string message = string.Format("Failed to delete folder \"{0}\".", _folder);​
 132:     2/2:                 _log.ErrorException(message, ex);​
 133:     2/2:             }​
 134:        :         }​
 135:        : 
 136:        :         /// <summary>​
 137:        :         /// Gets or sets a flag indicating whether or not the contents of the​
 138:        :         /// test directory should be allowed to remain on disk when the​
 139:        :         /// instance is disposed.​
 140:        :         /// </summary>​
 141:        :         public bool DoNotDelete { get; set; }​
 142:        :     }​
 143:        : }​
 144:        :