Skip to content

Commit 638d13d

Browse files
committed
[refactor] Replace Path.GetTempPath with AbstractTester in tests
1 parent f7b0501 commit 638d13d

7 files changed

Lines changed: 35 additions & 33 deletions

File tree

src/ByteSync.Client/ByteSync.Client.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@
109109
<Generator>ResXFileCodeGenerator</Generator>
110110
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
111111
</EmbeddedResource>
112-
<None Remove="local.settings.json"/>
113112
<EmbeddedResource Include="local.settings.json"/>
114113
<EmbeddedResource Include="Services\Inventories\noise-files.json"/>
115114
</ItemGroup>

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

Lines changed: 6 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
{
@@ -99,7 +101,7 @@ public async Task Download_WithTransientFailure_Should_Retry_And_Succeed()
99101
Source = new SharedDataPart
100102
{
101103
ClientInstanceId = shared.ClientInstanceId,
102-
RootPath = Path.GetTempPath(),
104+
RootPath = TestDirectory.FullName,
103105
InventoryPartType = FileSystemTypes.File,
104106
Name = "itests",
105107
InventoryCodeAndId = "itests"
@@ -113,7 +115,7 @@ public async Task Download_WithTransientFailure_Should_Retry_And_Succeed()
113115
sag.Targets.Add(new SharedDataPart
114116
{
115117
ClientInstanceId = shared.ClientInstanceId,
116-
RootPath = Path.GetTempFileName(),
118+
RootPath = Path.Combine(TestDirectory.FullName, Guid.NewGuid().ToString("N") + ".tmp"),
117119
InventoryPartType = FileSystemTypes.File,
118120
Name = "itests",
119121
InventoryCodeAndId = "itests"
@@ -123,7 +125,7 @@ public async Task Download_WithTransientFailure_Should_Retry_And_Succeed()
123125

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

129131
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/InventoryComparerPropagateAccessIssuesTests.cs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,25 @@
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);
25-
}
26-
27-
[TearDown]
28-
public void TearDown()
29-
{
30-
if (Directory.Exists(_tempDirectory))
31-
{
32-
Directory.Delete(_tempDirectory, true);
33-
}
24+
CreateTestDirectory();
25+
_tempDirectory = TestDirectory.FullName;
3426
}
3527

28+
3629
private static string CreateInventoryZipFile(string directory, Inventory inventory)
3730
{
3831
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");

tests/ByteSync.Client.UnitTests/ViewModels/Sessions/DataNodes/DataNodeSourcesViewModelTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
using FluentAssertions;
1313
using Moq;
1414
using NUnit.Framework;
15+
using ByteSync.TestsCommon;
1516

1617
namespace ByteSync.Client.UnitTests.ViewModels.Sessions.DataNodes;
1718

1819
[TestFixture]
19-
public class DataNodeSourcesViewModelTests
20+
public class DataNodeSourcesViewModelTests : AbstractTester
2021
{
2122
private Mock<ISessionService> _sessionServiceMock = null!;
2223
private Mock<IDataSourceService> _dataSourceServiceMock = null!;
@@ -29,6 +30,7 @@ public class DataNodeSourcesViewModelTests
2930
[SetUp]
3031
public void SetUp()
3132
{
33+
CreateTestDirectory();
3234
_dataNode = new DataNode { Id = "DN_1" };
3335
_sessionServiceMock = new Mock<ISessionService>();
3436
_dataSourceServiceMock = new Mock<IDataSourceService>();
@@ -58,7 +60,7 @@ public void Constructor_WithAllDependencies_ShouldCreateInstance()
5860
public async Task AddDirectoryCommand_WhenUserSelectsDirectory_ShouldCallCreateAndTryAddDataSource()
5961
{
6062
// Arrange
61-
var selectedPath = Path.GetTempPath().TrimEnd(Path.DirectorySeparatorChar);
63+
var selectedPath = TestDirectory.FullName.TrimEnd(Path.DirectorySeparatorChar);
6264
_fileDialogServiceMock
6365
.Setup(f => f.ShowOpenFolderDialogAsync(It.IsAny<string>()))
6466
.ReturnsAsync(selectedPath);

0 commit comments

Comments
 (0)