Skip to content

Commit

Permalink
Merge pull request #33 from tboby/apply-formatting
Browse files Browse the repository at this point in the history
Add formatting and Warnings as Errors
  • Loading branch information
baronfel authored Oct 25, 2024
2 parents f6dd104 + 2a952a0 commit 9946101
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 74 deletions.
20 changes: 6 additions & 14 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
root = true

[*]
[*.{fs,fsi,fsx}]
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4
trim_trailing_whitespace = true

[*.{fs,fsx,fsi}]
max_line_length = 100
fsharp_alternative_long_member_definitions = true
fsharp_multi_line_lambda_closing_newline = true
fsharp_multiline_bracket_style = aligned
fsharp_keep_max_number_of_blank_lines = 1
fsharp_align_function_signature_to_indentation = true
fsharp_max_if_then_else_short_width = 0
fsharp_experimental_keep_indent_in_branch = true
fsharp_bar_before_discriminated_union_declaration = true

fsharp_experimental_elmish = true
fsharp_record_multiline_formatter = number_of_items
fsharp_array_or_list_multiline_formatter = number_of_items
fsharp_max_record_number_of_items = 0
fsharp_max_array_or_list_number_of_items = 0
# Expecto looks a bit nicer with stroustrup
[tests/**/*.fs]
fsharp_multiline_bracket_style = stroustrup
7 changes: 7 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Automatically normalize line endings
* text=auto

# Always use lf for F# files
*.fs text eol=lf
*.fsx text eol=lf
*.fsi text eol=lf
4 changes: 4 additions & 0 deletions .github/workflows/buildAndTest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ jobs:
- uses: actions/checkout@v2
- name: Setup .NET Core
uses: actions/setup-dotnet@v4
- name: Tool restore
run: dotnet tool restore
- name: Format Check
run: dotnet fantomas . --check || { if [ $? -eq 99 ]; then echo "The code was not formatted, run 'dotnet fantomas .' to format all code."; exit 1; fi; }
- name: Restore
run: dotnet restore
- name: Run Build
Expand Down
7 changes: 7 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
<RestoreLockedMode Condition="'$(ContinuousIntegrationBuild)' == 'true'">true</RestoreLockedMode>
</PropertyGroup>
<PropertyGroup>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<NoWarn>$(NoWarn);3186,0042</NoWarn><!-- circumvent an error with the fake dependencymanager for paket: https://github.com/dotnet/fsharp/issues/8678 -->
<NoWarn>$(NoWarn);NU1902</NoWarn><!-- NU1902 - package vulnerability detected -->
<WarnOn>$(WarnOn);1182</WarnOn> <!-- Unused variables,https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/compiler-options#opt-in-warnings -->
<WarnOn>$(WarnOn);3390</WarnOn><!-- Malformed XML doc comments -->
</PropertyGroup>
<ItemGroup>
<!-- Automatically set RepositoryUrl, DebugType embedded, ContinuousIntegrationBuild -->
<PackageReference Include="DotNet.ReproducibleBuilds" PrivateAssets="All"/>
Expand Down
22 changes: 14 additions & 8 deletions src/Library.fs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ type ChangelogExtensions =
(string section.Type,
section.ItemCollection
|> Seq.map _.MarkdownText
|> String.concat Environment.NewLine))
|> String.concat Environment.NewLine)
)

[<Extension>]
static member ToTaskItem(unreleased: ChangelogSectionUnreleased) =
Expand All @@ -51,9 +52,12 @@ type ChangelogExtensions =
| false, _ -> None
| true, version ->
Some
{| version = version
date = section.ToDateTime()
collection = section.SubSectionCollection |})
{|
version = version
date = section.ToDateTime()
collection = section.SubSectionCollection
|}
)

[<Extension>]
static member ToMarkdown(subsections: ChangelogSubSectionCollection) =
Expand All @@ -62,12 +66,13 @@ type ChangelogExtensions =
subsections
|> Seq.fold
(fun (builder: StringBuilder) subsection ->
let state = builder.AppendLine $"### {subsection.Type}"
|> (fun x -> x.AppendLine "")
let state =
builder.AppendLine $"### {subsection.Type}" |> (fun x -> x.AppendLine "")

subsection.ItemCollection
|> Seq.fold (fun (builder: StringBuilder) line -> builder.AppendLine $"- {line.MarkdownText}") state
|> (fun x -> x.AppendLine ""))
|> (fun x -> x.AppendLine "")
)
builder
|> _.ToString()
|> _.Trim()
Expand Down Expand Up @@ -146,7 +151,8 @@ type ParseChangeLogs() =
for (key, value) in x.collection.ToTaskItemMetadata() do
taskItem.SetMetadata(key, value)

taskItem :> ITaskItem)
taskItem :> ITaskItem
)

this.CurrentReleaseChangelog <- mapped[0]
this.AllReleasedChangelogs <- mapped
Expand Down
11 changes: 2 additions & 9 deletions src/Log.fs
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,13 @@ let changelogFileNotFound (filePath: string) =
ErrorCode = "IKC0001"
HelpKeyword = "Missing Changelog file"
Message = "The Changelog file {0} was not found."
MessageArgs =
[|
box filePath
|]
MessageArgs = [| box filePath |]
}

let invalidChangelog (filePath: string) (error: string) =
{
ErrorCode = "IKC0002"
HelpKeyword = "Invalid Changelog file"
Message = "The Changelog file {0} is invalid. The error was: {1}"
MessageArgs =
[|
box filePath
box error
|]
MessageArgs = [| box filePath; box error |]
}
32 changes: 32 additions & 0 deletions tests/Helpers.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module Ionide.KeepAChangelog.Tasks.Test.Helpers

open System.Runtime.CompilerServices
open Faqt
open Faqt.AssertionHelpers

[<Extension>]
type Assertions =

/// Asserts that the subject is equal to the specified string when CLRF is replaced with LF in both raw and
/// escaped forms.
[<Extension>]
static member BeLineEndingEquivalent(t: Testable<string>, expected: string, ?because) : And<string> =
use _ = t.Assert()

if isNull expected then
nullArg (nameof expected)

if isNull t.Subject then
t.With("Expected", expected).With("But was", t.Subject).Fail(because)

let expectedNormalised = expected.Replace("\r\n", "\n").Replace("\\r\\n", "\\n")

let subjectNormalised = t.Subject.Replace("\r\n", "\n").Replace("\\r\\n", "\\n")

if subjectNormalised <> expectedNormalised then
t
.With("Expected", expectedNormalised)
.With("But was", subjectNormalised)
.Fail(because)

And(t)
27 changes: 10 additions & 17 deletions tests/IntegrationTests.fs
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
module Tests.IntegrationTests

open System.IO
open System.Runtime.CompilerServices
open System.Threading.Tasks
open Ionide.KeepAChangelog.Tasks.Test
open Microsoft.VisualStudio.TestTools.UnitTesting
open BlackFox.CommandLine
open Faqt
open SimpleExec
open Workspace

open Helpers

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
Expand All @@ -24,6 +27,7 @@ module Utils =
|> CmdLine.toString,
workingDirectory = Workspace.fixtures.``.``
)

Command.ReadAsync(
"dotnet",
CmdLine.empty
Expand All @@ -37,11 +41,6 @@ module Utils =
workingDirectory = Workspace.fixtures.``.``
)

type StringHelper =
[<Extension>]
static member ReplaceEscapedNewLines (s: string) =
s.ReplaceLineEndings().Replace("\\r\\n","\\n")

[<TestClass>]
type IntegrationTests() =

Expand All @@ -52,7 +51,6 @@ type IntegrationTests() =

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
Expand All @@ -78,7 +76,6 @@ type IntegrationTests() =
workingDirectory = Workspace.fixtures.``.``
)


[<TestMethod>]
member this.``works for absolute path with keep a changelog``() : Task =
task {
Expand All @@ -89,9 +86,8 @@ type IntegrationTests() =
let! struct (stdout, _) = Utils.packAndGetPackageProperties projectName

stdout
.ReplaceEscapedNewLines()
.Should()
.Be(
.BeLineEndingEquivalent(
"""{
"Properties": {
"Version": "0.1.0",
Expand All @@ -114,9 +110,8 @@ type IntegrationTests() =
let! struct (stdout, _) = Utils.packAndGetPackageProperties projectName

stdout
.ReplaceEscapedNewLines()
.Should()
.Be(
.BeLineEndingEquivalent(
"""{
"Properties": {
"Version": "0.1.0",
Expand All @@ -139,9 +134,8 @@ type IntegrationTests() =
let! struct (stdout, _) = Utils.packAndGetPackageProperties projectName

stdout
.ReplaceEscapedNewLines()
.Should()
.Be(
.BeLineEndingEquivalent(
"""{
"Properties": {
"Version": "1.0.0",
Expand All @@ -164,9 +158,8 @@ type IntegrationTests() =
let! struct (stdout, _) = Utils.packAndGetPackageProperties projectName

stdout
.ReplaceEscapedNewLines()
.Should()
.Be(
.BeLineEndingEquivalent(
"""{
"Properties": {
"Version": "1.0.0",
Expand Down
1 change: 1 addition & 0 deletions tests/Ionide.KeepAChangelog.Tasks.Test.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

<ItemGroup>
<Compile Include="Workspace.fs" />
<Compile Include="Helpers.fs" />
<Compile Include="UnitTests.fs" />
<Compile Include="IntegrationTests.fs" />
</ItemGroup>
Expand Down
Loading

0 comments on commit 9946101

Please sign in to comment.