public void CreateTemporaryFolder_CreateOnly() { TemporaryFolderManager manager = new TemporaryFolderManager(); var folder = manager.CreateTemporaryFolder(); Assert.StartsWith(manager.TemporaryFolderPath, folder.FolderPath); }
public void GetDefault() { TemporaryFolderManager manager = TemporaryFolderManager.Default; Assert.True(Path.IsPathRooted(manager.TemporaryFolderPath)); Assert.StartsWith(Path.GetTempPath(), manager.TemporaryFolderPath); }
public void GetDefault_Twice() { TemporaryFolderManager manager1 = TemporaryFolderManager.Default; TemporaryFolderManager manager2 = TemporaryFolderManager.Default; Assert.Same(manager1, manager2); }
public void Create_Default() { TemporaryFolderManager manager = new TemporaryFolderManager(); Assert.True(Path.IsPathRooted(manager.TemporaryFolderPath)); Assert.StartsWith(Path.GetTempPath(), manager.TemporaryFolderPath); }
private static string CreateTempManagerAndFolder(string temporaryFolderBaseName) { TemporaryFolderManager manager = new TemporaryFolderManager(temporaryFolderBaseName); var folder = manager.CreateTemporaryFolder(); return(folder.FolderPath); }
public void MultipleManagers() { // create a temporary folder and drop a file into it... TemporaryFolderManager manager1 = new TemporaryFolderManager(); var folder1 = manager1.CreateTemporaryFolder(); string filePath1 = Path.Combine(folder1.FolderPath, "file1.txt"); File.WriteAllText(filePath1, "Test"); // create another temporary folder manager TemporaryFolderManager manager2 = new TemporaryFolderManager(); var folder2 = manager2.CreateTemporaryFolder(); var filePath2 = Path.Combine(folder2.FolderPath, "file2.txt"); File.WriteAllText(filePath2, "Test"); // both managers should still have their files as both managers are alive // (manager 2 must not remove directories still managed by manager 1) Assert.True(File.Exists(filePath1)); Assert.True(File.Exists(filePath2)); // keep managers alive, otherwise the finalizer might clean up and remove directories GC.KeepAlive(manager1); GC.KeepAlive(manager2); }
public void Create_SpecificFolder() { string tempFolder = Path.Combine(Environment.CurrentDirectory, "TEMP"); TemporaryFolderManager manager = new TemporaryFolderManager(tempFolder); Assert.True(Path.IsPathRooted(manager.TemporaryFolderPath)); Assert.StartsWith(manager.TemporaryFolderPath, tempFolder); }
/// <summary> /// Initializes a new instance of the <see cref="TemporaryFolder"/> class. /// </summary> /// <param name="temporaryFolderManager">The temporary folder manager creating the folder.</param> /// <param name="basePath">Path of the temporary base folder.</param> internal TemporaryFolder(TemporaryFolderManager temporaryFolderManager, string basePath) { mTemporaryFolderManager = temporaryFolderManager; string processSpecificDirectoryName = $"[TMPDIR] {Guid.NewGuid():D}"; FolderPath = Path.Combine(basePath, processSpecificDirectoryName); mLockFilePath = Path.Combine(basePath, processSpecificDirectoryName + ".lock"); mLockFile = new FileStream(mLockFilePath, FileMode.Create, FileAccess.Write, FileShare.None); Directory.CreateDirectory(FolderPath); }
public void CreateTemporaryFolder_CreateAndAddContent() { TemporaryFolderManager manager = new TemporaryFolderManager(); var folder = manager.CreateTemporaryFolder(); Assert.StartsWith(manager.TemporaryFolderPath, folder.FolderPath); // write a file to the temporary folder string filePath = Path.Combine(folder.FolderPath, "file.txt"); File.WriteAllText(filePath, "Test"); // add a directory to the temporary folder string directoryPath = Path.Combine(folder.FolderPath, "Test"); Directory.CreateDirectory(directoryPath); // dispose temporary folder Assert.True(Directory.Exists(folder.FolderPath)); folder.Dispose(); Assert.False(Directory.Exists(folder.FolderPath)); }
public void CleanupAfterRestart() { const string temporaryFolderBaseName = "Temporary Folders"; // create a temporary folder and drop a file into it... string dirPath1 = CreateTempManagerAndFolder(temporaryFolderBaseName); string filePath1 = Path.Combine(dirPath1, "file1.txt"); File.WriteAllText(filePath1, "Test"); // create another temporary folder manager string dirPath2 = CreateTempManagerAndFolder(temporaryFolderBaseName); var filePath2 = Path.Combine(dirPath2, "file2.txt"); File.WriteAllText(filePath2, "Test"); // kick both managers out of memory (without disposing, would otherwise clean up properly) GC.Collect(2, GCCollectionMode.Forced, true); GC.WaitForPendingFinalizers(); // the files, the folders and the lock files should still be there... Assert.True(File.Exists(filePath1)); Assert.True(File.Exists(filePath2)); Assert.True(Directory.Exists(dirPath1)); Assert.True(Directory.Exists(dirPath2)); Assert.True(File.Exists(dirPath1 + ".lock")); Assert.True(File.Exists(dirPath2 + ".lock")); // starting a new folder manager instance should clean up orphaned directories and lock files var unused = new TemporaryFolderManager(temporaryFolderBaseName); Assert.False(Directory.Exists(dirPath1)); Assert.False(Directory.Exists(dirPath2)); Assert.False(File.Exists(dirPath1 + ".lock")); Assert.False(File.Exists(dirPath2 + ".lock")); }