11module Tests.IntegrationTests
22
33open System.IO
4+ open System.IO .Compression
5+ open System.Reflection
6+ open System.Runtime .InteropServices
47open System.Threading .Tasks
58open Ionide.KeepAChangelog .Tasks .Test
69open 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>]
5887type 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 }
0 commit comments