Skip to content

Commit

Permalink
Localization for the recorder
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveGilham committed Jan 31, 2018
1 parent 8866896 commit fdfecbd
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 48 deletions.
1 change: 1 addition & 0 deletions AltCover.Recorder/AltCover.Recorder.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
<Compile Include="Base.fs" />
<Compile Include="Tracer.fs" />
<Compile Include="Recorder.fs" />
<EmbeddedResource Include="Strings.resx" />
</ItemGroup>
<ItemGroup>
<Reference Include="FSharp.Core, Version=$(TargetFSharpCoreVersion), Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
Expand Down
1 change: 1 addition & 0 deletions AltCover.Recorder/AltCover.Shadow.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
<Compile Include="Base.fs" />
<Compile Include="Tracer.fs" />
<Compile Include="Recorder.fs" />
<EmbeddedResource Include="Strings.resx" />
</ItemGroup>
<ItemGroup>
<Reference Include="mscorlib" />
Expand Down
23 changes: 21 additions & 2 deletions AltCover.Recorder/Recorder.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,28 @@ namespace AltCover.Recorder

open System
open System.Collections.Generic
open System.Reflection
open System.Resources
open System.Runtime.CompilerServices

module Instance =

// Can't hard-code what with .net-core and .net-core tests as well as classic .net
// all giving this a different namespace
let private resource = Assembly.GetExecutingAssembly().GetManifestResourceNames()
|> Seq.map (fun s -> s.Substring(0, s.Length - 10)) // trim ".resources"
|> Seq.find (fun n -> n.EndsWith("Strings", StringComparison.Ordinal))
let internal resources = ResourceManager(resource , Assembly.GetExecutingAssembly())

let GetResource s =
[
System.Globalization.CultureInfo.CurrentUICulture.Name
System.Globalization.CultureInfo.CurrentUICulture.Parent.Name
"en"
]
|> Seq.map (fun l -> resources.GetString(s + "." + l))
|> Seq.tryFind (String.IsNullOrEmpty >> not)

/// <summary>
/// Gets the location of coverage xml file
/// This property's IL code is modified to store actual file location
Expand All @@ -31,7 +49,7 @@ module Instance =
/// <summary>
/// Interlock for report instances
/// </summary>
let private mutex = new System.Threading.Mutex(false, Token + ".mutex");
let internal mutex = new System.Threading.Mutex(false, Token + ".mutex");

/// <summary>
/// Reporting back to the mother-ship; only on the .net core build
Expand Down Expand Up @@ -76,7 +94,8 @@ module Instance =
Visits.Clear()
WithMutex (fun own ->
let delta = Counter.DoFlush own counts ReportFile
Console.Out.WriteLine("Coverage statistics flushing took {0:N} seconds", delta.TotalSeconds)
GetResource "Coverage statistics flushing took {0:N} seconds"
|> Option.iter (fun s -> Console.Out.WriteLine(s, delta.TotalSeconds))
))

let internal TraceVisit moduleId hitPointId =
Expand Down
126 changes: 126 additions & 0 deletions AltCover.Recorder/Strings.resx
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="Coverage statistics flushing took {0:N} seconds.en" xml:space="preserve">
<value>Coverage statistics flushing took {0:N} seconds</value>
</data>
<data name="Coverage statistics flushing took {0:N} seconds.eo" xml:space="preserve">
<value>Kovraj statistika skribado prenis {0:N} sekundojn</value>
</data>
</root>
2 changes: 1 addition & 1 deletion Build/targets.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ Target "Packaging" (fun _ ->
|> Seq.filter (fun n -> n.EndsWith(".fs") || n.EndsWith(".core.fsproj") || n.EndsWith(".resx"))
|> Seq.toList);
((!! "./AltCover.Recorder/*")
|> Seq.filter (fun n -> n.EndsWith(".fs") || n.EndsWith(".core.fsproj"))
|> Seq.filter (fun n -> n.EndsWith(".fs") || n.EndsWith(".core.fsproj") || n.EndsWith(".resx"))
|> Seq.toList);
((!! "./_Generated/*")
|> Seq.toList)
Expand Down
93 changes: 48 additions & 45 deletions Shadow.Tests/Shadow.Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ open System.Xml

open AltCover.Recorder
open NUnit.Framework
open System.Threading

[<TestFixture>]
type AltCoverTests() = class
Expand Down Expand Up @@ -319,54 +320,56 @@ type AltCoverTests() = class

[<Test>]
member self.FlushLeavesExpectedTraces() =
let saved = Console.Out
let here = Directory.GetCurrentDirectory()
let where = Assembly.GetExecutingAssembly().Location |> Path.GetDirectoryName
let unique = Path.Combine(where, Guid.NewGuid().ToString())
try
Instance.Visits.Clear()
use stdout = new StringWriter()
Console.SetOut stdout
Directory.CreateDirectory(unique) |> ignore
Directory.SetCurrentDirectory(unique)
let saved = Console.Out
let here = Directory.GetCurrentDirectory()
let where = Assembly.GetExecutingAssembly().Location |> Path.GetDirectoryName
let unique = Path.Combine(where, Guid.NewGuid().ToString())
try
Instance.Visits.Clear()
use stdout = new StringWriter()
Console.SetOut stdout
Directory.CreateDirectory(unique) |> ignore
Directory.SetCurrentDirectory(unique)

Counter.measureTime <- DateTime.ParseExact("2017-12-29T16:33:40.9564026+00:00", "o", null)
use stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(self.resource)
let size = int stream.Length
let buffer = Array.create size 0uy
Assert.That (stream.Read(buffer, 0, size), Is.EqualTo size)
do
use worker = new FileStream(Instance.ReportFile, FileMode.CreateNew)
worker.Write(buffer, 0, size)
()
Counter.measureTime <- DateTime.ParseExact("2017-12-29T16:33:40.9564026+00:00", "o", null)
use stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(self.resource)
let size = int stream.Length
let buffer = Array.create size 0uy
Assert.That (stream.Read(buffer, 0, size), Is.EqualTo size)
do
use worker = new FileStream(Instance.ReportFile, FileMode.CreateNew)
worker.Write(buffer, 0, size)
()

let payload = Dictionary<int,int>()
[0..9 ]
|> Seq.iter(fun i -> payload.[i] <- (i+1))
Instance.Visits.["f6e3edb3-fb20-44b3-817d-f69d1a22fc2f"] <- payload
let payload = Dictionary<int,int>()
[0..9 ]
|> Seq.iter(fun i -> payload.[i] <- (i+1))
Instance.Visits.["f6e3edb3-fb20-44b3-817d-f69d1a22fc2f"] <- payload

Instance.FlushCounter true ()

let head = "Coverage statistics flushing took "
let tail = " seconds\n"
let recorded = stdout.ToString().Replace("\r\n","\n")
Assert.That (recorded.StartsWith(head, StringComparison.Ordinal))
Assert.That (recorded.EndsWith(tail, StringComparison.Ordinal))
use worker' = new FileStream(Instance.ReportFile, FileMode.Open)
let after = XmlDocument()
after.Load worker'
Assert.That( after.SelectNodes("//seqpnt")
|> Seq.cast<XmlElement>
|> Seq.map (fun x -> x.GetAttribute("visitcount")),
Is.EquivalentTo [ "11"; "10"; "9"; "8"; "7"; "6"; "4"; "3"; "2"; "1"])
finally
if File.Exists Instance.ReportFile then File.Delete Instance.ReportFile
Instance.Visits.Clear()
Console.SetOut saved
Directory.SetCurrentDirectory(here)
try
Directory.Delete(unique)
with
| :? IOException -> ()
Instance.FlushCounter true ()

let head = "Coverage statistics flushing took "
let tail = " seconds\n"
let recorded = stdout.ToString().Replace("\r\n","\n")
Assert.That (recorded.StartsWith(head, StringComparison.Ordinal))
Assert.That (recorded.EndsWith(tail, StringComparison.Ordinal))
use worker' = new FileStream(Instance.ReportFile, FileMode.Open)
let after = XmlDocument()
after.Load worker'
Assert.That( after.SelectNodes("//seqpnt")
|> Seq.cast<XmlElement>
|> Seq.map (fun x -> x.GetAttribute("visitcount")),
Is.EquivalentTo [ "11"; "10"; "9"; "8"; "7"; "6"; "4"; "3"; "2"; "1"])
finally
if File.Exists Instance.ReportFile then File.Delete Instance.ReportFile
Instance.Visits.Clear()
Console.SetOut saved
Directory.SetCurrentDirectory(here)
try
Directory.Delete(unique)
with
| :? IOException -> ()
with
| :? AbandonedMutexException -> Instance.mutex.ReleaseMutex()
end

0 comments on commit fdfecbd

Please sign in to comment.