Skip to content

Commit 9e4e826

Browse files
Copilotbaronfel
andcommitted
Refactor tests to reduce duplication by extracting helper methods
Co-authored-by: baronfel <573979+baronfel@users.noreply.github.com>
1 parent d248e2e commit 9e4e826

File tree

2 files changed

+24
-48
lines changed

2 files changed

+24
-48
lines changed

src/Tasks/Microsoft.NET.Build.Tasks.UnitTests/GivenAGenerateDepsFile.cs

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,26 @@ public GivenAGenerateDepsFile()
2525
[Fact]
2626
public void ItDoesNotOverwriteFileWithSameContent()
2727
{
28-
var task = new TestableGenerateDepsFile
29-
{
30-
BuildEngine = new MockNeverCacheBuildEngine4(),
31-
ProjectPath = "TestProject.csproj",
32-
DepsFilePath = _depsFilePath,
33-
TargetFramework = "net8.0",
34-
AssemblyName = "TestProject",
35-
AssemblyExtension = ".dll",
36-
AssemblyVersion = "1.0.0.0",
37-
IncludeMainProject = true,
38-
CompileReferences = new MockTaskItem[0],
39-
ResolvedNuGetFiles = new MockTaskItem[0],
40-
ResolvedRuntimeTargetsFiles = new MockTaskItem[0],
41-
RuntimeGraphPath = ""
42-
};
43-
4428
// Execute task first time
29+
var task = CreateTestTask();
4530
task.PublicExecuteCore();
4631
var firstWriteTime = File.GetLastWriteTimeUtc(_depsFilePath);
4732

4833
// Wait a bit to ensure timestamp would change if file is rewritten
4934
Thread.Sleep(100);
5035

5136
// Execute task again with same configuration
52-
var task2 = new TestableGenerateDepsFile
37+
var task2 = CreateTestTask();
38+
task2.PublicExecuteCore();
39+
var secondWriteTime = File.GetLastWriteTimeUtc(_depsFilePath);
40+
41+
// File should not have been rewritten when content is the same
42+
secondWriteTime.Should().Be(firstWriteTime, "file should not be rewritten when content is unchanged");
43+
}
44+
45+
private TestableGenerateDepsFile CreateTestTask()
46+
{
47+
return new TestableGenerateDepsFile
5348
{
5449
BuildEngine = new MockNeverCacheBuildEngine4(),
5550
ProjectPath = "TestProject.csproj",
@@ -64,12 +59,6 @@ public void ItDoesNotOverwriteFileWithSameContent()
6459
ResolvedRuntimeTargetsFiles = new MockTaskItem[0],
6560
RuntimeGraphPath = ""
6661
};
67-
68-
task2.PublicExecuteCore();
69-
var secondWriteTime = File.GetLastWriteTimeUtc(_depsFilePath);
70-
71-
// File should not have been rewritten when content is the same
72-
secondWriteTime.Should().Be(firstWriteTime, "file should not be rewritten when content is unchanged");
7362
}
7463

7564
private class TestableGenerateDepsFile : GenerateDepsFile

src/Tasks/Microsoft.NET.Build.Tasks.UnitTests/GivenAGenerateRuntimeConfigurationFiles.cs

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -221,33 +221,26 @@ public void GivenTargetMonikerItGeneratesShortName()
221221
[Fact]
222222
public void ItDoesNotOverwriteFileWithSameContent()
223223
{
224-
var task = new TestableGenerateRuntimeConfigurationFiles
225-
{
226-
BuildEngine = new MockNeverCacheBuildEngine4(),
227-
TargetFrameworkMoniker = $".NETCoreApp,Version=v{ToolsetInfo.CurrentTargetFrameworkVersion}",
228-
RuntimeConfigPath = _runtimeConfigPath,
229-
RuntimeFrameworks = new[]
230-
{
231-
new MockTaskItem(
232-
"Microsoft.NETCore.App",
233-
new Dictionary<string, string>
234-
{
235-
{"FrameworkName", "Microsoft.NETCore.App"}, {"Version", $"{ToolsetInfo.CurrentTargetFrameworkVersion}.0"}
236-
}
237-
)
238-
},
239-
RollForward = "LatestMinor"
240-
};
241-
242224
// Execute task first time
225+
var task = CreateBasicTestTask();
243226
task.PublicExecuteCore();
244227
var firstWriteTime = File.GetLastWriteTimeUtc(_runtimeConfigPath);
245228

246229
// Wait a bit to ensure timestamp would change if file is rewritten
247230
Thread.Sleep(100);
248231

249232
// Execute task again with same configuration
250-
var task2 = new TestableGenerateRuntimeConfigurationFiles
233+
var task2 = CreateBasicTestTask();
234+
task2.PublicExecuteCore();
235+
var secondWriteTime = File.GetLastWriteTimeUtc(_runtimeConfigPath);
236+
237+
// File should not have been rewritten when content is the same
238+
secondWriteTime.Should().Be(firstWriteTime, "file should not be rewritten when content is unchanged");
239+
}
240+
241+
private TestableGenerateRuntimeConfigurationFiles CreateBasicTestTask()
242+
{
243+
return new TestableGenerateRuntimeConfigurationFiles
251244
{
252245
BuildEngine = new MockNeverCacheBuildEngine4(),
253246
TargetFrameworkMoniker = $".NETCoreApp,Version=v{ToolsetInfo.CurrentTargetFrameworkVersion}",
@@ -264,12 +257,6 @@ public void ItDoesNotOverwriteFileWithSameContent()
264257
},
265258
RollForward = "LatestMinor"
266259
};
267-
268-
task2.PublicExecuteCore();
269-
var secondWriteTime = File.GetLastWriteTimeUtc(_runtimeConfigPath);
270-
271-
// File should not have been rewritten when content is the same
272-
secondWriteTime.Should().Be(firstWriteTime, "file should not be rewritten when content is unchanged");
273260
}
274261

275262
private class TestableGenerateRuntimeConfigurationFiles : GenerateRuntimeConfigurationFiles

0 commit comments

Comments
 (0)