Skip to content

Instantly share code, notes, and snippets.

@slav
Created August 1, 2012 02:45
Show Gist options
  • Save slav/3223123 to your computer and use it in GitHub Desktop.
Save slav/3223123 to your computer and use it in GitHub Desktop.
Test files helper to manage loading files with test data or generating temp test files
using System;
using System.IO;
using System.Reflection;
namespace Tests.Helpers
{
/// <summary>
/// Assists working with test files.
/// </summary>
/// <remarks>By default this class assumes your test files will be located
/// in project folder "TestFiles". It's convenient.</remarks>
public static class FilesHelper
{
private const string DefaultTestFilesPath = @"..\..\TestFiles";
private const string DefaultTempFilesPath = "TempFiles";
private static string _assemblyPath;
static FilesHelper()
{
TestFilesPath = DefaultTestFilesPath;
InitWithAssembly( Assembly.GetCallingAssembly() );
}
/// <summary>
/// Initializes helper with assembly.
/// </summary>
/// <param name="assembly">The assembly.</param>
/// <remarks>Test files are looked for relative to this assembly.</remarks>
public static void InitWithAssembly ( Assembly assembly )
{
var uriPath = new Uri( assembly.GetName( false ).CodeBase );
_assemblyPath = Path.GetDirectoryName( uriPath.LocalPath );
}
/// <summary>
/// Gets the assembly path.
/// </summary>
/// <value>The assembly path.</value>
public static string AssemblyPath
{
get
{
return _assemblyPath;
}
}
/// <summary>
/// Gets the test files path.
/// </summary>
/// <value>The test files path.</value>
public static string TestFilesFullPath
{
get
{
return GetDirectory( Path.Combine( AssemblyPath, TestFilesPath ));
}
}
/// <summary>
/// Gets or sets the relative test files path.
/// </summary>
/// <value>The test files path.</value>
/// <remarks>Defaults to "..\..\TestFiles\"</remarks>
public static string TestFilesPath { get; set; }
/// <summary>
/// Gets or sets the test files group folder.
/// </summary>
/// <value>The test files group folder.</value>
/// <remarks>If specified, folder name is inserted into test file path, right before file name.
/// <para>This is useful to group test files for different tests. Set this property before in tests set up.</para>
/// <para>Remember, property is static, so it will carry over to other tests. Set it to <c>string.Empty</c> to reset resolution back to parent tests folder.</para>
/// <para>This property has no effect on temp files folder.</para>
/// <example>Example of how code this would influence test file resolution.
/// <code>
/// FilesHelper.GroupFolder = string.Empty; // default
/// var path = GetTestFilePath( "test.xml" ); // returns "TestFiles\test.xml"
///
/// FilesHelper.GroupFolder = "t2";
/// var path = GetTestFilePath( "test.xml" ); // returns "TestFiles\t2\test.xml"
/// </code></example></remarks>
public static string GroupFolder{ get; set; }
/// <summary>
/// Gets the test file path.
/// </summary>
/// <param name="fileName">Name of the file.</param>
/// <returns>Path to the specified test file.</returns>
/// <remarks>Current <see cref="GroupFolder"/> is used to figure out full file path.</remarks>
public static string GetTestFilePath( string fileName )
{
var groupPath = Path.Combine( GroupFolder, fileName );
return Path.Combine( TestFilesFullPath, groupPath );
}
/// <summary>
/// Gets the test file path without group.
/// </summary>
/// <param name="fileName">Name of the file.</param>
/// <returns>Path to the specified file.</returns>
public static string GetTestFilePathWithoutGroup( string fileName )
{
return Path.Combine( TestFilesFullPath, fileName );
}
/// <summary>
/// Gets the temp test file path.
/// </summary>
/// <returns>Path to a random test file.</returns>
/// <remarks>Random file name is generated in temp folder and returned.</remarks>
public static string GetTempTestFilePath()
{
return GetTempTestFilePath( Path.GetRandomFileName() );
}
/// <summary>
/// Gets the temp test file path.
/// </summary>
/// <param name="fileName">Name of the file.</param>
/// <returns>Path to the specified temp file.</returns>
public static string GetTempTestFilePath( string fileName )
{
return Path.Combine( TempFolderPath, fileName );
}
/// <summary>
/// Gets the temp folder path where temp files are stored.
/// </summary>
/// <value>The temp folder path.</value>
public static string TempFolderPath
{
get
{
return GetDirectory( Path.Combine( TestFilesFullPath, DefaultTempFilesPath ) );
}
}
/// <summary>
/// Deletes all temp folder.
/// </summary>
/// <remarks>Use this with care, since all files will be deleted.</remarks>
public static void DeleteTempFiles()
{
if( Directory.Exists( TempFolderPath ) )
{
Directory.Delete( TempFolderPath, true );
}
}
/// <summary>
/// Gets the full directory.
/// </summary>
/// <param name="path">The path.</param>
/// <returns>Absolute directory path.</returns>
/// <remarks>Creates directory if it doesn't exist.</remarks>
private static string GetDirectory( string path )
{
var dir = new DirectoryInfo( path );
if( !dir.Exists )
dir.Create();
return dir.FullName;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment