Skip to content

Commit de60f80

Browse files
[refactor] Replace Path.GetTempPath with AbstractTester in tests (#285)
1 parent f7b0501 commit de60f80

File tree

10 files changed

+167
-211
lines changed

10 files changed

+167
-211
lines changed

tests/ByteSync.Client.IntegrationTests/Services/Communications/Transfers/R2DownloadResume_Tests.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using ByteSync.Common.Business.Actions;
77
using ByteSync.Common.Business.Inventories;
88
using ByteSync.Common.Business.SharedFiles;
9+
using ByteSync.TestsCommon;
910
using ByteSync.DependencyInjection;
1011
using ByteSync.Interfaces.Controls.Communications.Http;
1112
using ByteSync.Interfaces.Factories;
@@ -22,13 +23,14 @@
2223

2324
namespace ByteSync.Client.IntegrationTests.Services.Communications.Transfers;
2425

25-
public class R2DownloadResume_Tests
26+
public class R2DownloadResume_Tests : AbstractTester
2627
{
2728
private ILifetimeScope _clientScope = null!;
2829

2930
[SetUp]
3031
public void SetUp()
3132
{
33+
CreateTestDirectory();
3234
// ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
3335
if (ByteSync.Services.ContainerProvider.Container == null)
3436
{
@@ -58,6 +60,11 @@ public void SetUp()
5860
public void TearDown()
5961
{
6062
_clientScope.Dispose();
63+
64+
if (TestDirectory?.Exists == true)
65+
{
66+
TestDirectory.Delete(true);
67+
}
6168
}
6269

6370
[Test]
@@ -99,7 +106,7 @@ public async Task Download_WithTransientFailure_Should_Retry_And_Succeed()
99106
Source = new SharedDataPart
100107
{
101108
ClientInstanceId = shared.ClientInstanceId,
102-
RootPath = Path.GetTempPath(),
109+
RootPath = TestDirectory.FullName,
103110
InventoryPartType = FileSystemTypes.File,
104111
Name = "itests",
105112
InventoryCodeAndId = "itests"
@@ -113,7 +120,7 @@ public async Task Download_WithTransientFailure_Should_Retry_And_Succeed()
113120
sag.Targets.Add(new SharedDataPart
114121
{
115122
ClientInstanceId = shared.ClientInstanceId,
116-
RootPath = Path.GetTempFileName(),
123+
RootPath = Path.Combine(TestDirectory.FullName, Guid.NewGuid().ToString("N") + ".tmp"),
117124
InventoryPartType = FileSystemTypes.File,
118125
Name = "itests",
119126
InventoryCodeAndId = "itests"
@@ -123,7 +130,7 @@ public async Task Download_WithTransientFailure_Should_Retry_And_Succeed()
123130

124131
// First upload a file so we can download it
125132
var inputContent = new string('z', 1_000_000);
126-
var tempFile = Path.GetTempFileName();
133+
var tempFile = Path.Combine(TestDirectory.FullName, Guid.NewGuid().ToString("N") + ".tmp");
127134
await File.WriteAllTextAsync(tempFile, inputContent);
128135

129136
var uploader = uploaderFactory.Build(tempFile, shared);

tests/ByteSync.Client.IntegrationTests/Services/Communications/Transfers/R2UploadDownload_Tests.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using ByteSync.Client.IntegrationTests.TestHelpers;
55
using ByteSync.Common.Business.Inventories;
66
using ByteSync.Common.Business.SharedFiles;
7+
using ByteSync.TestsCommon;
78
using ByteSync.DependencyInjection;
89
using ByteSync.Interfaces.Controls.Communications.Http;
910
using ByteSync.Interfaces.Factories;
@@ -19,13 +20,14 @@
1920

2021
namespace ByteSync.Client.IntegrationTests.Services.Communications.Transfers;
2122

22-
public class R2UploadDownload_Tests
23+
public class R2UploadDownload_Tests : AbstractTester
2324
{
2425
private ILifetimeScope _clientScope = null!;
2526

2627
[SetUp]
2728
public void SetUp()
2829
{
30+
CreateTestDirectory();
2931
// ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
3032
if (ByteSync.Services.ContainerProvider.Container == null)
3133
{
@@ -93,7 +95,7 @@ public async Task Upload_Then_Download_Should_Succeed_With_Small_Chunks()
9395
Source = new SharedDataPart
9496
{
9597
ClientInstanceId = shared.ClientInstanceId,
96-
RootPath = Path.GetTempPath(),
98+
RootPath = TestDirectory.FullName,
9799
InventoryPartType = FileSystemTypes.File,
98100
Name = "itests",
99101
InventoryCodeAndId = "itests"
@@ -107,7 +109,7 @@ public async Task Upload_Then_Download_Should_Succeed_With_Small_Chunks()
107109
sag.Targets.Add(new SharedDataPart
108110
{
109111
ClientInstanceId = shared.ClientInstanceId,
110-
RootPath = Path.GetTempFileName(),
112+
RootPath = Path.Combine(TestDirectory.FullName, Guid.NewGuid().ToString("N") + ".tmp"),
111113
InventoryPartType = FileSystemTypes.File,
112114
Name = "itests",
113115
InventoryCodeAndId = "itests"
@@ -116,7 +118,7 @@ public async Task Upload_Then_Download_Should_Succeed_With_Small_Chunks()
116118
sharedActionsGroupRepository.SetSharedActionsGroups([sag]);
117119

118120
var inputContent = new string('x', 1_000_000);
119-
var tempFile = Path.GetTempFileName();
121+
var tempFile = Path.Combine(TestDirectory.FullName, Guid.NewGuid().ToString("N") + ".tmp");
120122
await File.WriteAllTextAsync(tempFile, inputContent);
121123

122124
var uploader = uploaderFactory.Build(tempFile, shared);

tests/ByteSync.Client.UnitTests/Services/Communications/Transfers/Downloading/SynchronizationDownloadFinalizerTests.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
using Microsoft.Extensions.Logging;
99
using Moq;
1010
using NUnit.Framework;
11+
using ByteSync.TestsCommon;
1112

1213
namespace ByteSync.Client.UnitTests.Services.Communications.Transfers.Downloading;
1314

1415
[TestFixture]
15-
public class SynchronizationDownloadFinalizerTests
16+
public class SynchronizationDownloadFinalizerTests : AbstractTester
1617
{
1718
private Mock<IDeltaManager> _deltaManager = null!;
1819
private Mock<ITemporaryFileManagerFactory> _temporaryFileManagerFactory = null!;
@@ -24,6 +25,7 @@ public class SynchronizationDownloadFinalizerTests
2425
[SetUp]
2526
public void Setup()
2627
{
28+
CreateTestDirectory();
2729
_deltaManager = new Mock<IDeltaManager>(MockBehavior.Strict);
2830
_temporaryFileManagerFactory = new Mock<ITemporaryFileManagerFactory>(MockBehavior.Strict);
2931
_fileDatesSetter = new Mock<IFileDatesSetter>(MockBehavior.Strict);
@@ -276,15 +278,15 @@ private static void CreateEntryWithContent(ZipArchive archive, string entryName,
276278
writer.Write(content);
277279
}
278280

279-
private static string GetTempFilePath()
281+
private string GetTempFilePath()
280282
{
281-
var path = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"));
283+
var path = Path.Combine(TestDirectory.FullName, Guid.NewGuid().ToString("N"));
282284

283285
return path;
284286
}
285287

286-
private static string GetNewTempPath(string extension)
288+
private string GetNewTempPath(string extension)
287289
{
288-
return Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N") + extension);
290+
return Path.Combine(TestDirectory.FullName, Guid.NewGuid().ToString("N") + extension);
289291
}
290292
}

tests/ByteSync.Client.UnitTests/Services/Comparisons/InventoryComparerIncompletePartsFlatTests.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,29 @@
88
using ByteSync.Models.Inventories;
99
using ByteSync.Services.Comparisons;
1010
using FluentAssertions;
11+
using ByteSync.TestsCommon;
1112
using NUnit.Framework;
1213

1314
namespace ByteSync.Client.UnitTests.Services.Comparisons;
1415

1516
[TestFixture]
16-
public class InventoryComparerIncompletePartsFlatTests
17+
public class InventoryComparerIncompletePartsFlatTests : AbstractTester
1718
{
1819
private string _tempDirectory = null!;
1920

2021
[SetUp]
2122
public void Setup()
2223
{
23-
_tempDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
24-
Directory.CreateDirectory(_tempDirectory);
24+
CreateTestDirectory();
25+
_tempDirectory = TestDirectory.FullName;
2526
}
2627

2728
[TearDown]
2829
public void TearDown()
2930
{
30-
if (Directory.Exists(_tempDirectory))
31+
if (TestDirectory?.Exists == true)
3132
{
32-
Directory.Delete(_tempDirectory, true);
33+
TestDirectory.Delete(true);
3334
}
3435
}
3536

tests/ByteSync.Client.UnitTests/Services/Comparisons/InventoryComparerPropagateAccessIssuesTests.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,34 @@
77
using ByteSync.Models.FileSystems;
88
using ByteSync.Models.Inventories;
99
using ByteSync.Services.Comparisons;
10+
using ByteSync.TestsCommon;
1011
using FluentAssertions;
1112
using NUnit.Framework;
1213

1314
namespace ByteSync.Client.UnitTests.Services.Comparisons;
1415

1516
[TestFixture]
16-
public class InventoryComparerPropagateAccessIssuesTests
17+
public class InventoryComparerPropagateAccessIssuesTests : AbstractTester
1718
{
1819
private string _tempDirectory = null!;
1920

2021
[SetUp]
2122
public void Setup()
2223
{
23-
_tempDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
24-
Directory.CreateDirectory(_tempDirectory);
24+
CreateTestDirectory();
25+
_tempDirectory = TestDirectory.FullName;
2526
}
26-
27+
2728
[TearDown]
2829
public void TearDown()
2930
{
30-
if (Directory.Exists(_tempDirectory))
31+
if (TestDirectory?.Exists == true)
3132
{
32-
Directory.Delete(_tempDirectory, true);
33+
TestDirectory.Delete(true);
3334
}
3435
}
3536

37+
3638
private static string CreateInventoryZipFile(string directory, Inventory inventory)
3739
{
3840
var zipPath = Path.Combine(directory, $"{Guid.NewGuid()}.zip");

tests/ByteSync.Client.UnitTests/Services/Configurations/LocalApplicationDataManagerTests.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,19 @@
88
using FluentAssertions;
99
using Moq;
1010
using NUnit.Framework;
11+
using ByteSync.TestsCommon;
1112

1213
namespace ByteSync.Client.UnitTests.Services.Configurations;
1314

1415
[TestFixture]
15-
public class LocalApplicationDataManagerTests
16+
public class LocalApplicationDataManagerTests : AbstractTester
1617
{
1718
private Mock<IEnvironmentService> _environmentServiceMock = null!;
1819

1920
[SetUp]
2021
public void SetUp()
2122
{
23+
CreateTestDirectory();
2224
_environmentServiceMock = new Mock<IEnvironmentService>();
2325
_environmentServiceMock.SetupGet(e => e.ExecutionMode).Returns(ExecutionMode.Regular);
2426
_environmentServiceMock.SetupProperty(e => e.Arguments, []);
@@ -81,7 +83,7 @@ public void ApplicationDataPath_Should_Be_Logical_When_Not_Store()
8183
public void ApplicationDataPath_Should_Append_CustomSuffix_When_DebugArgumentProvided(OSPlatforms osPlatform)
8284
{
8385
// Arrange
84-
var tempRoot = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"));
86+
var tempRoot = TestDirectory.FullName;
8587
Directory.CreateDirectory(tempRoot);
8688
var assemblyDirectory = Directory.CreateDirectory(Path.Combine(tempRoot, "Portable")).FullName;
8789
var assemblyPath = Path.Combine(assemblyDirectory, "ByteSync.exe");
@@ -131,7 +133,7 @@ public void ApplicationDataPath_Should_Append_CustomSuffix_When_DebugArgumentPro
131133
public void ApplicationDataPath_Should_Create_DebugDirectory_When_DebugModeWithoutOverride(OSPlatforms osPlatform)
132134
{
133135
// Arrange
134-
var tempRoot = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"));
136+
var tempRoot = TestDirectory.FullName;
135137
Directory.CreateDirectory(tempRoot);
136138
var assemblyDirectory = Directory.CreateDirectory(Path.Combine(tempRoot, "Portable")).FullName;
137139
var assemblyPath = Path.Combine(assemblyDirectory, "ByteSync.exe");
@@ -180,7 +182,7 @@ public void ApplicationDataPath_Should_Create_DebugDirectory_When_DebugModeWitho
180182
public void LogFilePath_Should_Return_MostRecent_Log_Excluding_Debug()
181183
{
182184
// Arrange
183-
var tempRoot = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"));
185+
var tempRoot = TestDirectory.FullName;
184186
Directory.CreateDirectory(tempRoot);
185187
var assemblyDirectory = Directory.CreateDirectory(Path.Combine(tempRoot, "Portable")).FullName;
186188
var assemblyPath = Path.Combine(assemblyDirectory, "ByteSync.exe");
@@ -243,7 +245,7 @@ public void LogFilePath_Should_Return_MostRecent_Log_Excluding_Debug()
243245
public void DebugLogFilePath_Should_Return_MostRecent_Debug_Log()
244246
{
245247
// Arrange
246-
var tempRoot = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"));
248+
var tempRoot = TestDirectory.FullName;
247249
Directory.CreateDirectory(tempRoot);
248250
var assemblyDirectory = Directory.CreateDirectory(Path.Combine(tempRoot, "Portable")).FullName;
249251
var assemblyPath = Path.Combine(assemblyDirectory, "ByteSync.exe");

0 commit comments

Comments
 (0)