Skip to content

Commit f6dd104

Browse files
authored
Merge pull request #32 from tboby/improve-tests
Improve tests, correctly trigger warnings, and fix output markdown format
2 parents 1e09aa1 + 105cd6c commit f6dd104

25 files changed

+558
-215
lines changed

Directory.Packages.props

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,21 @@
55
<CentralPackagePackageVersionOverride>false</CentralPackagePackageVersionOverride>
66
</PropertyGroup>
77
<ItemGroup>
8+
<PackageVersion Include="BlackFox.CommandLine" Version="1.0.0" />
89
<PackageVersion Include="EasyBuild.FileSystemProvider" Version="0.3.0" />
9-
<PackageVersion Include="Fixie.TestAdapter" Version="3.4.0" />
10+
<PackageVersion Include="Faqt" Version="4.2.1" />
1011
<PackageVersion Include="FSharp.Core" Version="7.0.300" />
1112
<PackageVersion Include="FsToolkit.ErrorHandling" Version="4.17.0" />
1213
<PackageVersion Include="KeepAChangelogParser" Version="1.2.4" />
1314
<!-- 17.8.* aligns with .NET 8 SDK -->
1415
<PackageVersion Include="Microsoft.Build.Utilities.Core" Version="17.8.3" />
1516
<PackageVersion Include="Moq" Version="4.20.72" />
17+
<PackageVersion Include="MSTest.TestAdapter" Version="3.6.1" />
18+
<PackageVersion Include="MSTest.TestFramework" Version="3.6.1" />
1619
<PackageVersion Include="Semver" Version="2.3.0" />
17-
<PackageVersion Include="Shouldly" Version="4.2.1" />
1820
<PackageVersion Include="SimpleExec" Version="12.0.0" />
1921
<PackageVersion Include="DotNet.ReproducibleBuilds" Version="1.2.25" />
2022
<PackageVersion Include="DotNet.ReproducibleBuilds.Isolated" Version="1.2.25" />
23+
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
2124
</ItemGroup>
22-
</Project>
25+
</Project>

Ionide.KeepAChangelog.sln

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 16
44
VisualStudioVersion = 16.0.30114.105
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{31D4F8AF-532B-4DDF-BBA3-FD9B4C8FDA73}"
7-
EndProject
8-
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Ionide.KeepAChangelog.Tasks", "src\Ionide.KeepAChangelog.Tasks.fsproj", "{6CCAEBD2-9EE9-4540-AECF-02F0221B9C87}"
9-
EndProject
106
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Ionide.KeepAChangelog.Tasks.Test", "tests\Ionide.KeepAChangelog.Tasks.Test.fsproj", "{6456526B-A0F6-4998-A57A-772A055DF8AD}"
117
EndProject
8+
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Ionide.KeepAChangelog.Tasks", "src\Ionide.KeepAChangelog.Tasks.fsproj", "{E2704404-A047-4BAE-8AF9-58DAAFF02952}"
9+
EndProject
1210
Global
1311
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1412
Debug|Any CPU = Debug|Any CPU
@@ -18,16 +16,15 @@ Global
1816
HideSolutionNode = FALSE
1917
EndGlobalSection
2018
GlobalSection(ProjectConfigurationPlatforms) = postSolution
21-
{6CCAEBD2-9EE9-4540-AECF-02F0221B9C87}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
22-
{6CCAEBD2-9EE9-4540-AECF-02F0221B9C87}.Debug|Any CPU.Build.0 = Debug|Any CPU
23-
{6CCAEBD2-9EE9-4540-AECF-02F0221B9C87}.Release|Any CPU.ActiveCfg = Release|Any CPU
24-
{6CCAEBD2-9EE9-4540-AECF-02F0221B9C87}.Release|Any CPU.Build.0 = Release|Any CPU
2519
{6456526B-A0F6-4998-A57A-772A055DF8AD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
2620
{6456526B-A0F6-4998-A57A-772A055DF8AD}.Debug|Any CPU.Build.0 = Debug|Any CPU
2721
{6456526B-A0F6-4998-A57A-772A055DF8AD}.Release|Any CPU.ActiveCfg = Release|Any CPU
2822
{6456526B-A0F6-4998-A57A-772A055DF8AD}.Release|Any CPU.Build.0 = Release|Any CPU
23+
{E2704404-A047-4BAE-8AF9-58DAAFF02952}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
24+
{E2704404-A047-4BAE-8AF9-58DAAFF02952}.Debug|Any CPU.Build.0 = Debug|Any CPU
25+
{E2704404-A047-4BAE-8AF9-58DAAFF02952}.Release|Any CPU.ActiveCfg = Release|Any CPU
26+
{E2704404-A047-4BAE-8AF9-58DAAFF02952}.Release|Any CPU.Build.0 = Release|Any CPU
2927
EndGlobalSection
3028
GlobalSection(NestedProjects) = preSolution
31-
{6CCAEBD2-9EE9-4540-AECF-02F0221B9C87} = {31D4F8AF-532B-4DDF-BBA3-FD9B4C8FDA73}
3229
EndGlobalSection
3330
EndGlobal

src/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
</PropertyGroup>
1010

1111
<Import Project="../Directory.Build.props" />
12-
</Project>
12+
</Project>

src/Library.fs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ type ChangelogExtensions =
4747
static member Unwrapped(sections: ChangelogSectionCollection) =
4848
sections
4949
|> Seq.choose (fun section ->
50-
match SemVersion.TryParse section.MarkdownVersion with
50+
match SemVersion.TryParse(section.MarkdownVersion, SemVersionStyles.Any) with
5151
| false, _ -> None
5252
| true, version ->
5353
Some
@@ -63,11 +63,14 @@ type ChangelogExtensions =
6363
|> Seq.fold
6464
(fun (builder: StringBuilder) subsection ->
6565
let state = builder.AppendLine $"### {subsection.Type}"
66+
|> (fun x -> x.AppendLine "")
6667

6768
subsection.ItemCollection
68-
|> Seq.fold (fun (builder: StringBuilder) line -> builder.AppendLine line.MarkdownText) state)
69+
|> Seq.fold (fun (builder: StringBuilder) line -> builder.AppendLine $"- {line.MarkdownText}") state
70+
|> (fun x -> x.AppendLine ""))
6971
builder
7072
|> _.ToString()
73+
|> _.Trim()
7174

7275
type ParseChangeLogs() =
7376
inherit Task()

tests/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
test-package-cache
2+
test-nupkgs

tests/IntegrationTests.fs

Lines changed: 172 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,180 @@
11
module Tests.IntegrationTests
22

3-
open Tests.Setup
4-
open Moq
5-
open Microsoft.Build.Framework
6-
open Ionide.KeepAChangelog.Tasks
7-
open Shouldly
3+
open System.IO
4+
open System.Runtime.CompilerServices
5+
open System.Threading.Tasks
6+
open Microsoft.VisualStudio.TestTools.UnitTesting
7+
open BlackFox.CommandLine
8+
open Faqt
9+
open SimpleExec
10+
open Workspace
811

9-
// [<Test>]
10-
// let ``works for 'ci' type with default config`` () =
11-
// let buildEngine = Mock<IBuildEngine>()
12-
// let errors = ResizeArray<BuildErrorEventArgs>()
1312

14-
// buildEngine
15-
// .Setup(fun x -> x.LogErrorEvent(It.IsAny<BuildErrorEventArgs>()))
16-
// .Callback(fun args -> errors.Add(args))
17-
// |> ignore
13+
module Utils =
14+
let packAndGetPackageProperties projectName =
15+
let packageCache = VirtualWorkspace.``test-package-cache``.``.``
16+
if Directory.Exists packageCache then
17+
Directory.Delete(packageCache, true)
18+
Directory.CreateDirectory packageCache |> ignore
19+
Command.Run(
20+
"dotnet",
21+
CmdLine.empty
22+
|> CmdLine.appendPrefix "restore" projectName
23+
|> CmdLine.appendPrefix "--packages" VirtualWorkspace.``test-package-cache``.``.``
24+
|> CmdLine.toString,
25+
workingDirectory = Workspace.fixtures.``.``
26+
)
27+
Command.ReadAsync(
28+
"dotnet",
29+
CmdLine.empty
30+
|> CmdLine.appendPrefix "pack" projectName
31+
|> CmdLine.appendPrefix "-c" "Release"
32+
|> CmdLine.append "--no-restore"
33+
|> CmdLine.appendRaw "--getProperty:Version"
34+
|> CmdLine.appendRaw "--getProperty:PackageVersion"
35+
|> CmdLine.appendRaw "--getProperty:PackageReleaseNotes"
36+
|> CmdLine.toString,
37+
workingDirectory = Workspace.fixtures.``.``
38+
)
1839

19-
// let item = Mock<ITaskItem>()
20-
// item.Setup(fun x -> x.GetMetadata("MaximeTest")).Returns("test") |> ignore
40+
type StringHelper =
41+
[<Extension>]
42+
static member ReplaceEscapedNewLines (s: string) =
43+
s.ReplaceLineEndings().Replace("\\r\\n","\\n")
2144

22-
// let myTask = ParseChangelog(ChangelogFile = "MyChangelog.md")
23-
// myTask.BuildEngine <- buildEngine.Object
45+
[<TestClass>]
46+
type IntegrationTests() =
2447

25-
// let success = myTask.Execute()
48+
member val testPackageVersion = null with get, set
2649

27-
// success.ShouldBeTrue()
50+
member this.AddPackageReference(projectName: string) =
51+
let suffix = projectName.Replace(".fsproj", "")
52+
53+
this.testPackageVersion <- $"0.0.1-test-{suffix}"
54+
55+
56+
// Create a package to be used in the tests
57+
// I didn't find a way to test the MSBuild tasks execution using MSBuild only
58+
// So each fsproj, will use a package reference to the package created here
59+
Command.Run(
60+
"dotnet",
61+
CmdLine.empty
62+
|> CmdLine.appendPrefix "pack" "src"
63+
|> CmdLine.appendPrefix "-c" "Release"
64+
|> CmdLine.appendPrefix "-o" VirtualWorkspace.``test-nupkgs``.``.``
65+
|> CmdLine.appendRaw $"-p:PackageVersion=%s{this.testPackageVersion}"
66+
|> CmdLine.toString,
67+
workingDirectory = Workspace.``..``.``.``
68+
)
69+
70+
Command.Run(
71+
"dotnet",
72+
CmdLine.empty
73+
|> CmdLine.appendPrefix "add" projectName
74+
|> CmdLine.appendPrefix "package" "Ionide.KeepAChangelog.Tasks"
75+
// |> CmdLine.appendPrefix "--source" VirtualWorkspace.``test-nupkgs``.``.``
76+
|> CmdLine.appendPrefix "--version" $"[{this.testPackageVersion}]"
77+
|> CmdLine.toString,
78+
workingDirectory = Workspace.fixtures.``.``
79+
)
80+
81+
82+
[<TestMethod>]
83+
member this.``works for absolute path with keep a changelog``() : Task =
84+
task {
85+
let projectName = "WorksForAbsolutePathWithKeepAChangelog.fsproj"
86+
87+
this.AddPackageReference projectName
88+
89+
let! struct (stdout, _) = Utils.packAndGetPackageProperties projectName
90+
91+
stdout
92+
.ReplaceEscapedNewLines()
93+
.Should()
94+
.Be(
95+
"""{
96+
"Properties": {
97+
"Version": "0.1.0",
98+
"PackageVersion": "0.1.0",
99+
"PackageReleaseNotes": "### Added\n\n- Created the package\n- Added a second line\n\n### Changed\n\n- Updated the package"
100+
}
101+
}
102+
"""
103+
)
104+
|> ignore
105+
}
106+
107+
[<TestMethod>]
108+
member this.``works for relative path with keep a changelog``() : Task =
109+
task {
110+
let projectName = "WorksForRelativePathWithKeepAChangelog.fsproj"
111+
112+
this.AddPackageReference projectName
113+
114+
let! struct (stdout, _) = Utils.packAndGetPackageProperties projectName
115+
116+
stdout
117+
.ReplaceEscapedNewLines()
118+
.Should()
119+
.Be(
120+
"""{
121+
"Properties": {
122+
"Version": "0.1.0",
123+
"PackageVersion": "0.1.0",
124+
"PackageReleaseNotes": "### Added\n\n- Created the package\n- Added a second line\n\n### Changed\n\n- Updated the package"
125+
}
126+
}
127+
"""
128+
)
129+
|> ignore
130+
}
131+
132+
[<TestMethod>]
133+
member this.``fails if no changelog is specified``() : Task =
134+
task {
135+
let projectName = "FailIfChangelogNotSpecified.fsproj"
136+
137+
this.AddPackageReference projectName
138+
139+
let! struct (stdout, _) = Utils.packAndGetPackageProperties projectName
140+
141+
stdout
142+
.ReplaceEscapedNewLines()
143+
.Should()
144+
.Be(
145+
"""{
146+
"Properties": {
147+
"Version": "1.0.0",
148+
"PackageVersion": "1.0.0",
149+
"PackageReleaseNotes": ""
150+
}
151+
}
152+
"""
153+
)
154+
|> ignore
155+
}
156+
157+
[<TestMethod>]
158+
member this.``fails if changelog specified doesn't exist``() : Task =
159+
task {
160+
let projectName = "FailIfChangelogDoesNotExist.fsproj"
161+
162+
this.AddPackageReference projectName
163+
164+
let! struct (stdout, _) = Utils.packAndGetPackageProperties projectName
165+
166+
stdout
167+
.ReplaceEscapedNewLines()
168+
.Should()
169+
.Be(
170+
"""{
171+
"Properties": {
172+
"Version": "1.0.0",
173+
"PackageVersion": "1.0.0",
174+
"PackageReleaseNotes": ""
175+
}
176+
}
177+
"""
178+
)
179+
|> ignore
180+
}

tests/Ionide.KeepAChangelog.Tasks.Test.fsproj

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,31 @@
77

88
<ItemGroup>
99
<Compile Include="Workspace.fs" />
10-
<Compile Include="Setup.fs" />
1110
<Compile Include="UnitTests.fs" />
1211
<Compile Include="IntegrationTests.fs" />
1312
</ItemGroup>
1413

1514
<ItemGroup>
15+
<PackageReference Include="BlackFox.CommandLine" />
1616
<PackageReference Include="EasyBuild.FileSystemProvider" />
17-
<PackageReference Include="Fixie.TestAdapter" />
17+
<PackageReference Include="Faqt" />
1818
<PackageReference Include="FSharp.Core" />
1919
<PackageReference Include="KeepAChangelogParser" />
20-
<PackageReference Include="Microsoft.Build.Utilities.Core" />
20+
<PackageReference Include="Microsoft.NET.Test.Sdk" />
21+
<PackageReference Include="Microsoft.Build.Utilities.Core" />
2122
<PackageReference Include="Moq" />
23+
<PackageReference Include="MSTest.TestAdapter" />
24+
<PackageReference Include="MSTest.TestFramework" />
2225
<PackageReference Include="Semver" />
23-
<PackageReference Include="Shouldly" />
2426
<PackageReference Include="SimpleExec" />
2527
</ItemGroup>
2628

2729
<ItemGroup>
2830
<ProjectReference Include="..\src\Ionide.KeepAChangelog.Tasks.fsproj" />
2931
</ItemGroup>
3032

33+
<ItemGroup>
34+
<Content Include="changelogs\*.md" />
35+
</ItemGroup>
36+
3137
</Project>

tests/Setup.fs

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)