Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test and fix the gitlab reporter #2484

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions src/app/Fake.BuildServer.GitLab/GitLab.fs
Original file line number Diff line number Diff line change
Expand Up @@ -172,32 +172,33 @@ module GitLab =
member x.LogSectionName =
sprintf "%s_%s" (x.Type.Trim()) (x.Name.Trim())

type Writer = bool -> System.ConsoleColor -> bool -> string -> unit
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should those aliases be internal?

type Ticks = unit -> int64
type ColorMapper = TraceData -> System.ConsoleColor
/// Implements a TraceListener for TeamCity build servers.
/// ## Parameters
/// - `importantMessagesToStdErr` - Defines whether to trace important messages to StdErr.
/// - `colorMap` - A function which maps TracePriorities to ConsoleColors.
type internal GitLabTraceListener() =

type internal GitLabTraceListener(write: Writer, colorMapper: ColorMapper, getTicks: Ticks) =
interface ITraceListener with
/// Writes the given message to the Console.
member __.Write msg =
let color = ConsoleWriter.colorMap msg
let color = colorMapper msg
let importantMessagesToStdErr = true
let write = ConsoleWriter.write
match msg with
| TraceData.ImportantMessage text | TraceData.ErrorMessage text ->
write importantMessagesToStdErr color true text
| TraceData.LogMessage(text, newLine) | TraceData.TraceMessage(text, newLine) ->
write false color newLine text
| TraceData.OpenTag (tag, descr) ->
let unixTimestamp = System.DateTimeOffset.UtcNow.ToUnixTimeSeconds()
let sectionHeader = sprintf "section_start:%d:%s\r\e[0K%s" unixTimestamp tag.LogSectionName
let unixTimestamp = getTicks ()
let sectionHeader = sprintf @"section_start:%d:%s\r\e[0K%s" unixTimestamp tag.LogSectionName
match descr with
| Some d -> write false color true (sectionHeader d)
| None -> write false color true (sectionHeader System.String.Empty)
| TraceData.CloseTag (tag, time, state) ->
let unixTimestamp = System.DateTimeOffset.UtcNow.ToUnixTimeSeconds()
let sectionFooter = sprintf "section_end:%d:%s\r\e[0K" unixTimestamp tag.LogSectionName
let unixTimestamp = getTicks()
let sectionFooter = sprintf @"section_end:%d:%s\r\e[0K" unixTimestamp tag.LogSectionName
write false color true sectionFooter
| TraceData.BuildState (state, _) ->
write false color true (sprintf "Changing BuildState to: %A" state)
Expand All @@ -214,9 +215,10 @@ module GitLab =
write false color true (sprintf "Build Number: %s" number)
| TraceData.TestStatus (test, status) ->
write false color true (sprintf "Test '%s' status: %A" test status)

let currentTicks () =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let currentTicks () =
let internal currentTicks () =

System.DateTimeOffset.UtcNow.ToUnixTimeSeconds()
let defaultTraceListener =
GitLabTraceListener() :> ITraceListener
GitLabTraceListener(ConsoleWriter.write, ConsoleWriter.colorMap, currentTicks) :> ITraceListener
let detect () =
BuildServer.buildServer = BuildServer.GitLabCI
let install(force:bool) =
Expand Down
3 changes: 3 additions & 0 deletions src/app/Fake.BuildServer.GitLab/GitLabInternal.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ namespace Fake.BuildServer

open Fake.Core

[<assembly:System.Runtime.CompilerServices.InternalsVisibleTo("Fake.Core.UnitTests")>]
do ()

module internal GitLabInternal =
let environVar = Environment.environVar
let getJobId () = environVar "CI_JOB_ID"
Expand Down
38 changes: 38 additions & 0 deletions src/test/Fake.Core.UnitTests/Fake.BuildServer.GitLab.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module Fake.BuildServer.GitLab

open Expecto
open Fake.Core
open Fake.BuildServer

let testCases = [
test "can emit correct collapsed output" {
let mutable messages = []
let writer _ _ newLine message =
let message = if newLine then message + @"\n" else message
messages <- message :: messages
let sameTicks _ = 1L
let sameColorMapper _ = System.ConsoleColor.White
let listener = GitLab.GitLabTraceListener(writer, sameColorMapper, sameTicks) :> Fake.Core.ITraceListener

let tag = KnownTags.Target("my_first_section")
let traceMessages = [
TraceData.OpenTag(tag, Some "Header of the 1st collapsible section")
TraceData.LogMessage("this line should be hidden when collapsed", true)
TraceData.CloseTag(tag, System.TimeSpan.FromSeconds 1., TagStatus.Success)
]
for trace in traceMessages do
listener.Write trace

let inOrder = List.rev messages
let expected = [
@"section_start:1:target_my_first_section\r\e[0KHeader of the 1st collapsible section\n"
@"this line should be hidden when collapsed\n"
@"section_end:1:target_my_first_section\r\e[0K\n"
]
Expect.equal inOrder expected "should have output the correct collapsible trace"
}
]


[<Tests>]
let tests = testList "Fake.BuildeServer.GitLab.Tests" testCases
4 changes: 3 additions & 1 deletion src/test/Fake.Core.UnitTests/Fake.Core.UnitTests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@
<ProjectReference Include="..\..\app\Fake.Tools.Octo\Fake.Tools.Octo.fsproj" />
<ProjectReference Include="..\Fake.ExpectoSupport\Fake.ExpectoSupport.fsproj" />
<ProjectReference Include="..\..\app\Fake.Tools.SignTool\Fake.Tools.SignTool.fsproj" />
<ProjectReference Include="..\..\app\Fake.BuildServer.GitLab\Fake.BuildServer.GitLab.fsproj" />
</ItemGroup>
<ItemGroup>
<Content Include="runtimeconfig.template.json" />
<None Include="paket.references" />
<Compile Include="Fake.BuildServer.GitLab.fs" />
<Compile Include="Fake.ContextHelper.fs" />
<Compile Include="Fake.Testing.ArgumentHelper.fs" />
<Compile Include="Fake.IO.Globbing.fs" />
Expand Down Expand Up @@ -79,4 +81,4 @@
<None Include="TestFiles/Fake.DotNet.Xdt.Files/web.transformed.config" CopyToOutputDirectory="Always" />
</ItemGroup>
<Import Project="..\..\..\.paket\Paket.Restore.targets" />
</Project>
</Project>