Skip to content

Commit 12dbf85

Browse files
committed
Add tests for BuildDate
1 parent 393f135 commit 12dbf85

File tree

7 files changed

+125
-8
lines changed

7 files changed

+125
-8
lines changed

Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@
2121
<PackageVersion Include="DotNet.ReproducibleBuilds" Version="1.2.25" />
2222
<PackageVersion Include="DotNet.ReproducibleBuilds.Isolated" Version="1.2.25" />
2323
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
24+
<PackageVersion Include="System.Reflection.MetadataLoadContext" Version="8.0.1" />
2425
</ItemGroup>
2526
</Project>

tests/IntegrationTests.fs

Lines changed: 71 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
module Tests.IntegrationTests
22

33
open System.IO
4+
open System.IO.Compression
5+
open System.Reflection
6+
open System.Runtime.InteropServices
47
open System.Threading.Tasks
58
open Ionide.KeepAChangelog.Tasks.Test
69
open Microsoft.VisualStudio.TestTools.UnitTesting
@@ -23,7 +26,7 @@ module Utils =
2326
Directory.CreateDirectory packageCache |> ignore
2427

2528
// Read improves the error logging when the command fails
26-
let! (_, _) =
29+
let! _, _ =
2730
Command.ReadAsync(
2831
"dotnet",
2932
CmdLine.empty
@@ -54,6 +57,32 @@ module Utils =
5457
let packAndGetPackageProperties projectName =
5558
packAndGetPackagePropertiesWithExtraArg projectName None
5659

60+
let getAssemblyInfoFromNupkg (projectName: string) version =
61+
let projectName = Path.GetFileNameWithoutExtension projectName
62+
63+
let packageFile =
64+
Path.Combine(VirtualWorkspace.fixtures.bin.Release.``.``, $"{projectName}.{version}.nupkg")
65+
66+
File.Exists(packageFile).Should().BeTrue() |> ignore
67+
68+
use zip = ZipFile.OpenRead(packageFile)
69+
use zipStream = zip.Entries |> Seq.find (_.Name.EndsWith(".dll")) |> _.Open()
70+
use assemblyStream = new MemoryStream()
71+
zipStream.CopyTo assemblyStream
72+
73+
let runtimeAssemblies =
74+
Directory.GetFiles(RuntimeEnvironment.GetRuntimeDirectory(), "*.dll")
75+
76+
use mlc = new MetadataLoadContext(PathAssemblyResolver runtimeAssemblies)
77+
let assembly = mlc.LoadFromStream assemblyStream
78+
79+
assembly.CustomAttributes
80+
|> Seq.tryPick (fun attr ->
81+
match attr.ConstructorArguments |> Seq.map _.Value.ToString() |> Seq.toArray with
82+
| [| "BuildDate"; date |] -> Some date
83+
| _ -> None
84+
)
85+
5786
[<TestClass>]
5887
type IntegrationTests() =
5988
[<TestInitialize>]
@@ -97,8 +126,6 @@ type IntegrationTests() =
97126
task {
98127
let projectName = "WorksForAbsolutePathWithKeepAChangelog.fsproj"
99128

100-
// this.AddPackageReference projectName
101-
102129
let! struct (stdout, _) = Utils.packAndGetPackageProperties projectName
103130

104131
stdout
@@ -114,6 +141,9 @@ type IntegrationTests() =
114141
"""
115142
)
116143
|> ignore
144+
145+
let buildDate = Utils.getAssemblyInfoFromNupkg projectName "0.1.0"
146+
buildDate.Should().BeSome().WhoseValue.Should().Be("2022-01-13") |> ignore
117147
}
118148

119149
[<TestMethod>]
@@ -125,6 +155,9 @@ type IntegrationTests() =
125155

126156
let! struct (stdout, _) = Utils.packAndGetPackageProperties projectName
127157

158+
let buildDate = Utils.getAssemblyInfoFromNupkg projectName "0.1.0"
159+
buildDate.Should().BeSome().WhoseValue.Should().Be("2022-01-13") |> ignore
160+
128161
stdout
129162
.Should()
130163
.BeLineEndingEquivalent(
@@ -210,19 +243,46 @@ type IntegrationTests() =
210243
"""
211244
)
212245
|> ignore
246+
247+
let buildDate = Utils.getAssemblyInfoFromNupkg projectName "0.1.1-alpha"
248+
buildDate.Should().BeNone() |> ignore
213249
}
214250

215251
[<TestMethod>]
216252
member this.``ignores a pre-release version if changelog has unreleased section but disabled``() : Task =
217253
task {
218-
let projectName = "WorksForUnreleased.fsproj"
254+
let projectName = "WorksForUnreleasedWhenIgnored.fsproj"
255+
256+
do! this.AddPackageReference projectName
257+
258+
let! struct (stdout, _) = Utils.packAndGetPackageProperties projectName
259+
260+
let buildDate = Utils.getAssemblyInfoFromNupkg projectName "0.1.0"
261+
buildDate.Should().BeSome().WhoseValue.Should().Be("2022-01-13") |> ignore
262+
263+
stdout
264+
.Should()
265+
.BeLineEndingEquivalent(
266+
"""{
267+
"Properties": {
268+
"Version": "0.1.0",
269+
"PackageVersion": "0.1.0",
270+
"PackageReleaseNotes": "### Added\n\n- Created the package\n\n### Changed\n\n- Changed something in the package\n- Updated the target framework"
271+
}
272+
}
273+
"""
274+
)
275+
|> ignore
276+
}
277+
278+
[<TestMethod>]
279+
member this.``doesn't write the build date if disabled``() : Task =
280+
task {
281+
let projectName = "IgnoresBuildDateIfConfigured.fsproj"
219282

220283
do! this.AddPackageReference projectName
221284

222-
let! struct (stdout, _) =
223-
Utils.packAndGetPackagePropertiesWithExtraArg
224-
projectName
225-
(Some "-p:GenerateVersionForUnreleasedChanges=false")
285+
let! struct (stdout, _) = Utils.packAndGetPackageProperties projectName
226286

227287
stdout
228288
.Should()
@@ -237,4 +297,7 @@ type IntegrationTests() =
237297
"""
238298
)
239299
|> ignore
300+
301+
let buildDate = Utils.getAssemblyInfoFromNupkg projectName "0.1.0"
302+
buildDate.Should().BeNone() |> ignore
240303
}

tests/Ionide.KeepAChangelog.Tasks.Test.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<PackageReference Include="MSTest.TestFramework" />
2626
<PackageReference Include="Semver" />
2727
<PackageReference Include="SimpleExec" />
28+
<PackageReference Include="System.Reflection.MetadataLoadContext" />
2829
</ItemGroup>
2930

3031
<ItemGroup>

tests/Workspace.fs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,8 @@ type VirtualWorkspace =
1010
"""
1111
test-nupkgs/
1212
test-package-cache/
13+
fixtures/
14+
bin/
15+
Release/
1316
"""
1417
>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project Sdk="Microsoft.NET.Sdk">
3+
4+
<PropertyGroup>
5+
<TargetFramework>net6.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<PropertyGroup>
9+
<ChangelogFile>CHANGELOG_unreleased.md</ChangelogFile>
10+
<GenerateAssemblyBuildDateAttribute>false</GenerateAssemblyBuildDateAttribute>
11+
<GenerateVersionForUnreleasedChanges>false</GenerateVersionForUnreleasedChanges>
12+
</PropertyGroup>
13+
14+
<ItemGroup>
15+
<PackageReference Include="DotNet.ReproducibleBuilds.Isolated" Version="1.2.25" />
16+
<PackageReference Include="Ionide.KeepAChangelog.Tasks" Version="[0.0.1-test]">
17+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
18+
<PrivateAssets>all</PrivateAssets>
19+
</PackageReference>
20+
</ItemGroup>
21+
22+
</Project>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project Sdk="Microsoft.NET.Sdk">
3+
4+
<PropertyGroup>
5+
<TargetFramework>net6.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<PropertyGroup>
9+
<ChangelogFile>CHANGELOG_unreleased.md</ChangelogFile>
10+
<GenerateVersionForUnreleasedChanges>false</GenerateVersionForUnreleasedChanges>
11+
</PropertyGroup>
12+
13+
<ItemGroup>
14+
<PackageReference Include="DotNet.ReproducibleBuilds.Isolated" Version="1.2.25" />
15+
<PackageReference Include="Ionide.KeepAChangelog.Tasks" Version="[0.0.1-test]">
16+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
17+
<PrivateAssets>all</PrivateAssets>
18+
</PackageReference>
19+
</ItemGroup>
20+
21+
</Project>

tests/packages.lock.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@
111111
"resolved": "12.0.0",
112112
"contentHash": "ptxlWtxC8vM6Y6e3h9ZTxBBkOWnWrm/Sa1HT+2i1xcXY3Hx2hmKDZP5RShPf8Xr9D+ivlrXNy57ktzyH8kyt+Q=="
113113
},
114+
"System.Reflection.MetadataLoadContext": {
115+
"type": "Direct",
116+
"requested": "[8.0.1, )",
117+
"resolved": "8.0.1",
118+
"contentHash": "c/hiLzoMeYWnoTsdeRY2o0Vbadfedt0b4ZawQ+qDr1BIoYBQo3ASei0Pyiz00n9pHBlfWXiUVy90tOWBEgJ8/Q=="
119+
},
114120
"Castle.Core": {
115121
"type": "Transitive",
116122
"resolved": "5.1.1",

0 commit comments

Comments
 (0)