mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-02-24 04:00:02 +01:00
io test helper
This commit is contained in:
@@ -22,9 +22,14 @@
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" />
|
||||
<PackageReference Include="MSTest.TestAdapter" />
|
||||
<PackageReference Include="MSTest.TestFramework" />
|
||||
<PackageReference Include="System.IO.Abstractions" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Properties\" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\FancyZonesEditorCommon\FancyZonesEditorCommon.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,78 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.IO.Abstractions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Microsoft.FancyZonesEditor.UITests.Utils
|
||||
{
|
||||
public class IOTestHelper
|
||||
{
|
||||
private readonly IFileSystem _fileSystem = new FileSystem();
|
||||
|
||||
private string _fileName;
|
||||
|
||||
private string _data = string.Empty;
|
||||
|
||||
public IOTestHelper(string fileName)
|
||||
{
|
||||
_fileName = fileName;
|
||||
|
||||
if (_fileSystem.File.Exists(_fileName))
|
||||
{
|
||||
_data = ReadFile(_fileName);
|
||||
}
|
||||
}
|
||||
|
||||
~IOTestHelper()
|
||||
{
|
||||
RestoreData();
|
||||
}
|
||||
|
||||
public void RestoreData()
|
||||
{
|
||||
if (_data != string.Empty)
|
||||
{
|
||||
WriteData(_data);
|
||||
}
|
||||
else
|
||||
{
|
||||
_fileSystem.File.Delete(_fileName);
|
||||
}
|
||||
}
|
||||
|
||||
public void WriteData(string data)
|
||||
{
|
||||
_fileSystem.File.WriteAllText(_fileName, data);
|
||||
}
|
||||
|
||||
private string ReadFile(string fileName)
|
||||
{
|
||||
var attempts = 0;
|
||||
while (attempts < 10)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (Stream inputStream = _fileSystem.File.Open(fileName, FileMode.Open))
|
||||
using (StreamReader reader = new StreamReader(inputStream))
|
||||
{
|
||||
string data = reader.ReadToEnd();
|
||||
inputStream.Close();
|
||||
return data;
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
Task.Delay(10).Wait();
|
||||
}
|
||||
|
||||
attempts++;
|
||||
}
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user