public void TestRandomString() { for (int i = 1; i < 16; i++) { var s = RandomString.Next(i); Assert.AreEqual(i * 2, s.Length); } }
public void IntermediateDirectoryCanBeSpecified() { var intermediateDirectory = RandomString.Next(16); using (var t = new TempDirectory(intermediateDirectory)) { var expectedParentOfTempDirectory = Path.Combine(Environment.GetEnvironmentVariable("TEMP"), intermediateDirectory); var actualParentOfTempDirectory = new DirectoryInfo(t.FullPath).Parent.FullName; Assert.AreEqual(expectedParentOfTempDirectory, actualParentOfTempDirectory); } }
/// <summary> /// Creates a temporary directory under the user's temp directory (i.e. the TEMP /// environment variable). /// /// Optionally the temporary directory may be created in an intermediate subdirectory /// of the user's TEMP directory. /// /// The temporary directory (but not the intermediate directory) will be automatically /// deleted when the TempDirectory object is disposed. /// </summary> public TempDirectory(string subdirectory = "") { var userTempDir = Environment.GetEnvironmentVariable("TEMP"); if (userTempDir == null) { throw new Exception("TEMP environment variable is not set"); } Name = DateTime.UtcNow.ToString("yyyyMMdd-HHmmss-") + RandomString.Next(); FullPath = Path.Combine(userTempDir, subdirectory, Name); Directory.CreateDirectory(FullPath); }
public void ThrowsOnInvalidArgument() { Assert.Throws <ArgumentOutOfRangeException>(() => RandomString.Next(0)); Assert.Throws <ArgumentOutOfRangeException>(() => RandomString.Next(-1)); Assert.Throws <ArgumentOutOfRangeException>(() => RandomString.Next(17)); }