Skip to content

Commit

Permalink
Merge pull request #32 from tboby/improve-tests
Browse files Browse the repository at this point in the history
Improve tests, correctly trigger warnings, and fix output markdown format
  • Loading branch information
baronfel authored Oct 24, 2024
2 parents 1e09aa1 + 105cd6c commit f6dd104
Show file tree
Hide file tree
Showing 25 changed files with 558 additions and 215 deletions.
9 changes: 6 additions & 3 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@
<CentralPackagePackageVersionOverride>false</CentralPackagePackageVersionOverride>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="BlackFox.CommandLine" Version="1.0.0" />
<PackageVersion Include="EasyBuild.FileSystemProvider" Version="0.3.0" />
<PackageVersion Include="Fixie.TestAdapter" Version="3.4.0" />
<PackageVersion Include="Faqt" Version="4.2.1" />
<PackageVersion Include="FSharp.Core" Version="7.0.300" />
<PackageVersion Include="FsToolkit.ErrorHandling" Version="4.17.0" />
<PackageVersion Include="KeepAChangelogParser" Version="1.2.4" />
<!-- 17.8.* aligns with .NET 8 SDK -->
<PackageVersion Include="Microsoft.Build.Utilities.Core" Version="17.8.3" />
<PackageVersion Include="Moq" Version="4.20.72" />
<PackageVersion Include="MSTest.TestAdapter" Version="3.6.1" />
<PackageVersion Include="MSTest.TestFramework" Version="3.6.1" />
<PackageVersion Include="Semver" Version="2.3.0" />
<PackageVersion Include="Shouldly" Version="4.2.1" />
<PackageVersion Include="SimpleExec" Version="12.0.0" />
<PackageVersion Include="DotNet.ReproducibleBuilds" Version="1.2.25" />
<PackageVersion Include="DotNet.ReproducibleBuilds.Isolated" Version="1.2.25" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
</ItemGroup>
</Project>
</Project>
15 changes: 6 additions & 9 deletions Ionide.KeepAChangelog.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30114.105
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{31D4F8AF-532B-4DDF-BBA3-FD9B4C8FDA73}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Ionide.KeepAChangelog.Tasks", "src\Ionide.KeepAChangelog.Tasks.fsproj", "{6CCAEBD2-9EE9-4540-AECF-02F0221B9C87}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Ionide.KeepAChangelog.Tasks.Test", "tests\Ionide.KeepAChangelog.Tasks.Test.fsproj", "{6456526B-A0F6-4998-A57A-772A055DF8AD}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Ionide.KeepAChangelog.Tasks", "src\Ionide.KeepAChangelog.Tasks.fsproj", "{E2704404-A047-4BAE-8AF9-58DAAFF02952}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -18,16 +16,15 @@ Global
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{6CCAEBD2-9EE9-4540-AECF-02F0221B9C87}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6CCAEBD2-9EE9-4540-AECF-02F0221B9C87}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6CCAEBD2-9EE9-4540-AECF-02F0221B9C87}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6CCAEBD2-9EE9-4540-AECF-02F0221B9C87}.Release|Any CPU.Build.0 = Release|Any CPU
{6456526B-A0F6-4998-A57A-772A055DF8AD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6456526B-A0F6-4998-A57A-772A055DF8AD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6456526B-A0F6-4998-A57A-772A055DF8AD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6456526B-A0F6-4998-A57A-772A055DF8AD}.Release|Any CPU.Build.0 = Release|Any CPU
{E2704404-A047-4BAE-8AF9-58DAAFF02952}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E2704404-A047-4BAE-8AF9-58DAAFF02952}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E2704404-A047-4BAE-8AF9-58DAAFF02952}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E2704404-A047-4BAE-8AF9-58DAAFF02952}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{6CCAEBD2-9EE9-4540-AECF-02F0221B9C87} = {31D4F8AF-532B-4DDF-BBA3-FD9B4C8FDA73}
EndGlobalSection
EndGlobal
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
</PropertyGroup>

<Import Project="../Directory.Build.props" />
</Project>
</Project>
7 changes: 5 additions & 2 deletions src/Library.fs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type ChangelogExtensions =
static member Unwrapped(sections: ChangelogSectionCollection) =
sections
|> Seq.choose (fun section ->
match SemVersion.TryParse section.MarkdownVersion with
match SemVersion.TryParse(section.MarkdownVersion, SemVersionStyles.Any) with
| false, _ -> None
| true, version ->
Some
Expand All @@ -63,11 +63,14 @@ type ChangelogExtensions =
|> Seq.fold
(fun (builder: StringBuilder) subsection ->
let state = builder.AppendLine $"### {subsection.Type}"
|> (fun x -> x.AppendLine "")

subsection.ItemCollection
|> Seq.fold (fun (builder: StringBuilder) line -> builder.AppendLine line.MarkdownText) state)
|> Seq.fold (fun (builder: StringBuilder) line -> builder.AppendLine $"- {line.MarkdownText}") state
|> (fun x -> x.AppendLine ""))
builder
|> _.ToString()
|> _.Trim()

type ParseChangeLogs() =
inherit Task()
Expand Down
2 changes: 2 additions & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test-package-cache
test-nupkgs
191 changes: 172 additions & 19 deletions tests/IntegrationTests.fs
Original file line number Diff line number Diff line change
@@ -1,27 +1,180 @@
module Tests.IntegrationTests

open Tests.Setup
open Moq
open Microsoft.Build.Framework
open Ionide.KeepAChangelog.Tasks
open Shouldly
open System.IO
open System.Runtime.CompilerServices
open System.Threading.Tasks
open Microsoft.VisualStudio.TestTools.UnitTesting
open BlackFox.CommandLine
open Faqt
open SimpleExec
open Workspace

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

// buildEngine
// .Setup(fun x -> x.LogErrorEvent(It.IsAny<BuildErrorEventArgs>()))
// .Callback(fun args -> errors.Add(args))
// |> ignore
module Utils =
let packAndGetPackageProperties projectName =
let packageCache = VirtualWorkspace.``test-package-cache``.``.``
if Directory.Exists packageCache then
Directory.Delete(packageCache, true)
Directory.CreateDirectory packageCache |> ignore
Command.Run(
"dotnet",
CmdLine.empty
|> CmdLine.appendPrefix "restore" projectName
|> CmdLine.appendPrefix "--packages" VirtualWorkspace.``test-package-cache``.``.``
|> CmdLine.toString,
workingDirectory = Workspace.fixtures.``.``
)
Command.ReadAsync(
"dotnet",
CmdLine.empty
|> CmdLine.appendPrefix "pack" projectName
|> CmdLine.appendPrefix "-c" "Release"
|> CmdLine.append "--no-restore"
|> CmdLine.appendRaw "--getProperty:Version"
|> CmdLine.appendRaw "--getProperty:PackageVersion"
|> CmdLine.appendRaw "--getProperty:PackageReleaseNotes"
|> CmdLine.toString,
workingDirectory = Workspace.fixtures.``.``
)

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

// let myTask = ParseChangelog(ChangelogFile = "MyChangelog.md")
// myTask.BuildEngine <- buildEngine.Object
[<TestClass>]
type IntegrationTests() =

// let success = myTask.Execute()
member val testPackageVersion = null with get, set

// success.ShouldBeTrue()
member this.AddPackageReference(projectName: string) =
let suffix = projectName.Replace(".fsproj", "")

this.testPackageVersion <- $"0.0.1-test-{suffix}"


// Create a package to be used in the tests
// I didn't find a way to test the MSBuild tasks execution using MSBuild only
// So each fsproj, will use a package reference to the package created here
Command.Run(
"dotnet",
CmdLine.empty
|> CmdLine.appendPrefix "pack" "src"
|> CmdLine.appendPrefix "-c" "Release"
|> CmdLine.appendPrefix "-o" VirtualWorkspace.``test-nupkgs``.``.``
|> CmdLine.appendRaw $"-p:PackageVersion=%s{this.testPackageVersion}"
|> CmdLine.toString,
workingDirectory = Workspace.``..``.``.``
)

Command.Run(
"dotnet",
CmdLine.empty
|> CmdLine.appendPrefix "add" projectName
|> CmdLine.appendPrefix "package" "Ionide.KeepAChangelog.Tasks"
// |> CmdLine.appendPrefix "--source" VirtualWorkspace.``test-nupkgs``.``.``
|> CmdLine.appendPrefix "--version" $"[{this.testPackageVersion}]"
|> CmdLine.toString,
workingDirectory = Workspace.fixtures.``.``
)


[<TestMethod>]
member this.``works for absolute path with keep a changelog``() : Task =
task {
let projectName = "WorksForAbsolutePathWithKeepAChangelog.fsproj"

this.AddPackageReference projectName

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

stdout
.ReplaceEscapedNewLines()
.Should()
.Be(
"""{
"Properties": {
"Version": "0.1.0",
"PackageVersion": "0.1.0",
"PackageReleaseNotes": "### Added\n\n- Created the package\n- Added a second line\n\n### Changed\n\n- Updated the package"
}
}
"""
)
|> ignore
}

[<TestMethod>]
member this.``works for relative path with keep a changelog``() : Task =
task {
let projectName = "WorksForRelativePathWithKeepAChangelog.fsproj"

this.AddPackageReference projectName

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

stdout
.ReplaceEscapedNewLines()
.Should()
.Be(
"""{
"Properties": {
"Version": "0.1.0",
"PackageVersion": "0.1.0",
"PackageReleaseNotes": "### Added\n\n- Created the package\n- Added a second line\n\n### Changed\n\n- Updated the package"
}
}
"""
)
|> ignore
}

[<TestMethod>]
member this.``fails if no changelog is specified``() : Task =
task {
let projectName = "FailIfChangelogNotSpecified.fsproj"

this.AddPackageReference projectName

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

stdout
.ReplaceEscapedNewLines()
.Should()
.Be(
"""{
"Properties": {
"Version": "1.0.0",
"PackageVersion": "1.0.0",
"PackageReleaseNotes": ""
}
}
"""
)
|> ignore
}

[<TestMethod>]
member this.``fails if changelog specified doesn't exist``() : Task =
task {
let projectName = "FailIfChangelogDoesNotExist.fsproj"

this.AddPackageReference projectName

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

stdout
.ReplaceEscapedNewLines()
.Should()
.Be(
"""{
"Properties": {
"Version": "1.0.0",
"PackageVersion": "1.0.0",
"PackageReleaseNotes": ""
}
}
"""
)
|> ignore
}
14 changes: 10 additions & 4 deletions tests/Ionide.KeepAChangelog.Tasks.Test.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,31 @@

<ItemGroup>
<Compile Include="Workspace.fs" />
<Compile Include="Setup.fs" />
<Compile Include="UnitTests.fs" />
<Compile Include="IntegrationTests.fs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="BlackFox.CommandLine" />
<PackageReference Include="EasyBuild.FileSystemProvider" />
<PackageReference Include="Fixie.TestAdapter" />
<PackageReference Include="Faqt" />
<PackageReference Include="FSharp.Core" />
<PackageReference Include="KeepAChangelogParser" />
<PackageReference Include="Microsoft.Build.Utilities.Core" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Microsoft.Build.Utilities.Core" />
<PackageReference Include="Moq" />
<PackageReference Include="MSTest.TestAdapter" />
<PackageReference Include="MSTest.TestFramework" />
<PackageReference Include="Semver" />
<PackageReference Include="Shouldly" />
<PackageReference Include="SimpleExec" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\src\Ionide.KeepAChangelog.Tasks.fsproj" />
</ItemGroup>

<ItemGroup>
<Content Include="changelogs\*.md" />
</ItemGroup>

</Project>
27 changes: 0 additions & 27 deletions tests/Setup.fs

This file was deleted.

Loading

0 comments on commit f6dd104

Please sign in to comment.