-
Notifications
You must be signed in to change notification settings - Fork 10
[refactor] Replace Path.GetTempPath with AbstractTester in tests #285
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
| using ByteSync.Client.IntegrationTests.TestHelpers; | ||
| using ByteSync.Common.Business.Inventories; | ||
| using ByteSync.Common.Business.SharedFiles; | ||
| using ByteSync.TestsCommon; | ||
| using ByteSync.DependencyInjection; | ||
| using ByteSync.Interfaces.Controls.Communications.Http; | ||
| using ByteSync.Interfaces.Factories; | ||
|
|
@@ -19,13 +20,14 @@ | |
|
|
||
| namespace ByteSync.Client.IntegrationTests.Services.Communications.Transfers; | ||
|
|
||
| public class R2UploadDownload_Tests | ||
| public class R2UploadDownload_Tests : AbstractTester | ||
| { | ||
| private ILifetimeScope _clientScope = null!; | ||
|
|
||
| [SetUp] | ||
| public void SetUp() | ||
| { | ||
| CreateTestDirectory(); | ||
| // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract | ||
|
Comment on lines
27
to
31
|
||
| if (ByteSync.Services.ContainerProvider.Container == null) | ||
| { | ||
|
Comment on lines
27
to
33
|
||
|
|
@@ -93,7 +95,7 @@ public async Task Upload_Then_Download_Should_Succeed_With_Small_Chunks() | |
| Source = new SharedDataPart | ||
| { | ||
| ClientInstanceId = shared.ClientInstanceId, | ||
| RootPath = Path.GetTempPath(), | ||
| RootPath = TestDirectory.FullName, | ||
| InventoryPartType = FileSystemTypes.File, | ||
| Name = "itests", | ||
| InventoryCodeAndId = "itests" | ||
|
|
@@ -107,7 +109,7 @@ public async Task Upload_Then_Download_Should_Succeed_With_Small_Chunks() | |
| sag.Targets.Add(new SharedDataPart | ||
| { | ||
| ClientInstanceId = shared.ClientInstanceId, | ||
| RootPath = Path.GetTempFileName(), | ||
| RootPath = Path.Combine(TestDirectory.FullName, Guid.NewGuid().ToString("N") + ".tmp"), | ||
| InventoryPartType = FileSystemTypes.File, | ||
| Name = "itests", | ||
| InventoryCodeAndId = "itests" | ||
|
|
@@ -116,7 +118,7 @@ public async Task Upload_Then_Download_Should_Succeed_With_Small_Chunks() | |
| sharedActionsGroupRepository.SetSharedActionsGroups([sag]); | ||
|
|
||
| var inputContent = new string('x', 1_000_000); | ||
| var tempFile = Path.GetTempFileName(); | ||
| var tempFile = Path.Combine(TestDirectory.FullName, Guid.NewGuid().ToString("N") + ".tmp"); | ||
| await File.WriteAllTextAsync(tempFile, inputContent); | ||
|
|
||
| var uploader = uploaderFactory.Build(tempFile, shared); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,11 +8,12 @@ | |
| using Microsoft.Extensions.Logging; | ||
| using Moq; | ||
| using NUnit.Framework; | ||
| using ByteSync.TestsCommon; | ||
|
|
||
| namespace ByteSync.Client.UnitTests.Services.Communications.Transfers.Downloading; | ||
|
|
||
| [TestFixture] | ||
| public class SynchronizationDownloadFinalizerTests | ||
| public class SynchronizationDownloadFinalizerTests : AbstractTester | ||
| { | ||
| private Mock<IDeltaManager> _deltaManager = null!; | ||
| private Mock<ITemporaryFileManagerFactory> _temporaryFileManagerFactory = null!; | ||
|
|
@@ -24,6 +25,7 @@ public class SynchronizationDownloadFinalizerTests | |
| [SetUp] | ||
| public void Setup() | ||
| { | ||
| CreateTestDirectory(); | ||
| _deltaManager = new Mock<IDeltaManager>(MockBehavior.Strict); | ||
|
Comment on lines
25
to
29
|
||
| _temporaryFileManagerFactory = new Mock<ITemporaryFileManagerFactory>(MockBehavior.Strict); | ||
| _fileDatesSetter = new Mock<IFileDatesSetter>(MockBehavior.Strict); | ||
|
|
@@ -276,15 +278,15 @@ private static void CreateEntryWithContent(ZipArchive archive, string entryName, | |
| writer.Write(content); | ||
| } | ||
|
|
||
| private static string GetTempFilePath() | ||
| private string GetTempFilePath() | ||
| { | ||
| var path = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N")); | ||
| var path = Path.Combine(TestDirectory.FullName, Guid.NewGuid().ToString("N")); | ||
|
|
||
| return path; | ||
| } | ||
|
|
||
| private static string GetNewTempPath(string extension) | ||
| private string GetNewTempPath(string extension) | ||
| { | ||
| return Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N") + extension); | ||
| return Path.Combine(TestDirectory.FullName, Guid.NewGuid().ToString("N") + extension); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -7,32 +7,34 @@ | |||||||||||||||||||||
| using ByteSync.Models.FileSystems; | ||||||||||||||||||||||
| using ByteSync.Models.Inventories; | ||||||||||||||||||||||
| using ByteSync.Services.Comparisons; | ||||||||||||||||||||||
| using ByteSync.TestsCommon; | ||||||||||||||||||||||
| using FluentAssertions; | ||||||||||||||||||||||
| using NUnit.Framework; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| namespace ByteSync.Client.UnitTests.Services.Comparisons; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| [TestFixture] | ||||||||||||||||||||||
| public class InventoryComparerPropagateAccessIssuesTests | ||||||||||||||||||||||
| public class InventoryComparerPropagateAccessIssuesTests : AbstractTester | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| private string _tempDirectory = null!; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| [SetUp] | ||||||||||||||||||||||
| public void Setup() | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| _tempDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); | ||||||||||||||||||||||
| Directory.CreateDirectory(_tempDirectory); | ||||||||||||||||||||||
| CreateTestDirectory(); | ||||||||||||||||||||||
| _tempDirectory = TestDirectory.FullName; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| [TearDown] | ||||||||||||||||||||||
| public void TearDown() | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| if (Directory.Exists(_tempDirectory)) | ||||||||||||||||||||||
| if (TestDirectory?.Exists == true) | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| Directory.Delete(_tempDirectory, true); | ||||||||||||||||||||||
| TestDirectory.Delete(true); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
||||||||||||||||||||||
| [TearDown] | |
| public void TearDown() | |
| { | |
| if (!string.IsNullOrWhiteSpace(_tempDirectory) && Directory.Exists(_tempDirectory)) | |
| { | |
| Directory.Delete(_tempDirectory, true); | |
| } | |
| } | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed. Added a [TearDown] method that deletes TestDirectory after each test, preventing zip inventory files from accumulating under the user profile across runs.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,17 +8,19 @@ | |
| using FluentAssertions; | ||
| using Moq; | ||
| using NUnit.Framework; | ||
| using ByteSync.TestsCommon; | ||
|
|
||
| namespace ByteSync.Client.UnitTests.Services.Configurations; | ||
|
|
||
| [TestFixture] | ||
| public class LocalApplicationDataManagerTests | ||
| public class LocalApplicationDataManagerTests : AbstractTester | ||
| { | ||
|
Comment on lines
15
to
17
|
||
| private Mock<IEnvironmentService> _environmentServiceMock = null!; | ||
|
|
||
| [SetUp] | ||
| public void SetUp() | ||
| { | ||
| CreateTestDirectory(); | ||
| _environmentServiceMock = new Mock<IEnvironmentService>(); | ||
|
Comment on lines
20
to
24
|
||
| _environmentServiceMock.SetupGet(e => e.ExecutionMode).Returns(ExecutionMode.Regular); | ||
| _environmentServiceMock.SetupProperty(e => e.Arguments, []); | ||
|
|
@@ -81,7 +83,7 @@ | |
| public void ApplicationDataPath_Should_Append_CustomSuffix_When_DebugArgumentProvided(OSPlatforms osPlatform) | ||
| { | ||
| // Arrange | ||
| var tempRoot = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N")); | ||
| var tempRoot = TestDirectory.FullName; | ||
| Directory.CreateDirectory(tempRoot); | ||
| var assemblyDirectory = Directory.CreateDirectory(Path.Combine(tempRoot, "Portable")).FullName; | ||
| var assemblyPath = Path.Combine(assemblyDirectory, "ByteSync.exe"); | ||
|
|
@@ -131,7 +133,7 @@ | |
| public void ApplicationDataPath_Should_Create_DebugDirectory_When_DebugModeWithoutOverride(OSPlatforms osPlatform) | ||
| { | ||
| // Arrange | ||
| var tempRoot = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N")); | ||
| var tempRoot = TestDirectory.FullName; | ||
| Directory.CreateDirectory(tempRoot); | ||
| var assemblyDirectory = Directory.CreateDirectory(Path.Combine(tempRoot, "Portable")).FullName; | ||
| var assemblyPath = Path.Combine(assemblyDirectory, "ByteSync.exe"); | ||
|
|
@@ -180,7 +182,7 @@ | |
| public void LogFilePath_Should_Return_MostRecent_Log_Excluding_Debug() | ||
| { | ||
| // Arrange | ||
| var tempRoot = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N")); | ||
| var tempRoot = TestDirectory.FullName; | ||
| Directory.CreateDirectory(tempRoot); | ||
| var assemblyDirectory = Directory.CreateDirectory(Path.Combine(tempRoot, "Portable")).FullName; | ||
| var assemblyPath = Path.Combine(assemblyDirectory, "ByteSync.exe"); | ||
|
|
@@ -243,7 +245,7 @@ | |
| public void DebugLogFilePath_Should_Return_MostRecent_Debug_Log() | ||
| { | ||
| // Arrange | ||
| var tempRoot = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N")); | ||
| var tempRoot = TestDirectory.FullName; | ||
| Directory.CreateDirectory(tempRoot); | ||
| var assemblyDirectory = Directory.CreateDirectory(Path.Combine(tempRoot, "Portable")).FullName; | ||
| var assemblyPath = Path.Combine(assemblyDirectory, "ByteSync.exe"); | ||
|
|
@@ -302,7 +304,7 @@ | |
| } | ||
| } | ||
|
|
||
| private static IDisposable OverrideCommandLineArgs(string[] args) | ||
|
Check warning on line 307 in tests/ByteSync.Client.UnitTests/Services/Configurations/LocalApplicationDataManagerTests.cs
|
||
| { | ||
| var field = typeof(Environment).GetField("s_commandLineArgs", BindingFlags.Static | BindingFlags.NonPublic); | ||
| if (field == null) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that temp files are created under TestDirectory (in the user's profile) rather than the OS temp location, the test-created files/directories are less likely to be cleaned up by the system. This fixture calls CreateTestDirectory() but never deletes TestDirectory in TearDown, so downloaded/uploaded artifacts may accumulate across runs. Consider deleting TestDirectory in [TearDown] once the test completes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed. The existing [TearDown] already disposed _clientScope; added TestDirectory.Delete(true) to also clean up the test directory, preventing upload/download artifacts (including the 1 MB temp file) from accumulating under the user profile across runs.