Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void GivenSystemCreatedFileName_WhenSaving_TheContentIsAsExpected()
public void GivenSystemSpecialFolderAndCustomName_WhenSaving_TheContentIsAsExpected()
{
//Arrange
var appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
var appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
var tempFile = Path.Combine(appDataPath, "text.txt");

//Act
Expand All @@ -51,4 +51,47 @@ public void GivenSystemSpecialFolderAndCustomName_WhenSaving_TheContentIsAsExpec
//Assert
Assert.Equal("Hello from CodeMaze!", tempFileContent);
}
}

[Fact]
public void GivenTempSubdirectory_WhenSaving_TheContentIsAsExpected()
{
//Arrange
var tempDirectory = Directory.CreateTempSubdirectory("codemaze-");
var tempFile = Path.Combine(tempDirectory.FullName, "text.txt");

//Act
TempFileCreator.CreateTempFile(tempFile);
var tempFileContent = File.ReadAllText(tempFile);

tempDirectory.Delete(recursive: true);

//Assert
Assert.Equal("Hello from CodeMaze!", tempFileContent);
Assert.False(Directory.Exists(tempDirectory.FullName));
}

[Fact]
public void GivenDeleteOnClose_WhenStreamIsDisposed_TheFileIsGone()
{
//Arrange
var tempFile = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
var options = new FileStreamOptions
{
Mode = FileMode.CreateNew,
Access = FileAccess.ReadWrite,
Options = FileOptions.DeleteOnClose
};

//Act
bool existedWhileOpen;
using (var stream = new FileStream(tempFile, options))
{
stream.Write("Hello from CodeMaze!"u8);
existedWhileOpen = File.Exists(tempFile);
}

//Assert
Assert.True(existedWhileOpen);
Assert.False(File.Exists(tempFile));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

Expand All @@ -10,13 +10,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.11.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="4.0.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PackageReference Include="coverlet.collector" Version="10.1.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using WriteFileToTempFolder;

var tempFile = Path.Combine(Path.GetTempPath(),"text.txt");
var tempFile = Path.Combine(Path.GetTempPath(), "text.txt");
Console.WriteLine($"Creating temp file: '{tempFile}'");
TempFileCreator.CreateTempFile(tempFile);

Expand All @@ -22,12 +22,40 @@

Console.WriteLine();

var appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
var appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
tempFile = Path.Combine(appDataPath, "text.txt");
Console.WriteLine($"Creating temp file: '{tempFile}'");
TempFileCreator.CreateTempFile(tempFile);

Console.WriteLine($"Reading temp file contents: '{tempFile}'");
tempFileContent = File.ReadAllText(tempFile);
Console.WriteLine(tempFileContent);
File.Delete(tempFile);
File.Delete(tempFile);

Console.WriteLine();

var tempDirectory = Directory.CreateTempSubdirectory("codemaze-");
tempFile = Path.Combine(tempDirectory.FullName, "text.txt");
Console.WriteLine($"Creating temp file: '{tempFile}'");
TempFileCreator.CreateTempFile(tempFile);

Console.WriteLine(File.ReadAllText(tempFile));
tempDirectory.Delete(recursive: true);

Console.WriteLine();

tempFile = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
var options = new FileStreamOptions
{
Mode = FileMode.CreateNew,
Access = FileAccess.ReadWrite,
Options = FileOptions.DeleteOnClose
};

using (var stream = new FileStream(tempFile, options))
{
stream.Write("Hello from CodeMaze!"u8);
Console.WriteLine($"Temp file '{tempFile}' exists? {File.Exists(tempFile)}");
}

Console.WriteLine($"Temp file '{tempFile}' exists? {File.Exists(tempFile)}");
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Loading