33using System . Globalization ;
44using System . IO ;
55using System . Linq ;
6+ using System . Runtime . InteropServices ;
67using System . Text ;
78using System . Threading ;
8- using System . Xml ;
99using System . Xml . Linq ;
1010using Xunit ;
1111
@@ -37,7 +37,15 @@ public void TestReport()
3737 classes . Add ( "Coverlet.Core.Reporters.Tests.CoberturaReporterTests" , methods ) ;
3838
3939 Documents documents = new Documents ( ) ;
40- documents . Add ( "doc.cs" , classes ) ;
40+
41+ if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
42+ {
43+ documents . Add ( @"C:\doc.cs" , classes ) ;
44+ }
45+ else
46+ {
47+ documents . Add ( @"/doc.cs" , classes ) ;
48+ }
4149
4250 result . Modules = new Modules ( ) ;
4351 result . Modules . Add ( "module" , documents ) ;
@@ -102,7 +110,14 @@ public void TestEnsureParseMethodStringCorrectly(
102110 classes . Add ( "Google.Protobuf.Reflection.MessageDescriptor" , methods ) ;
103111
104112 Documents documents = new Documents ( ) ;
105- documents . Add ( "doc.cs" , classes ) ;
113+ if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
114+ {
115+ documents . Add ( @"C:\doc.cs" , classes ) ;
116+ }
117+ else
118+ {
119+ documents . Add ( @"/doc.cs" , classes ) ;
120+ }
106121
107122 result . Modules = new Modules ( ) ;
108123 result . Modules . Add ( "module" , documents ) ;
@@ -120,5 +135,77 @@ public void TestEnsureParseMethodStringCorrectly(
120135 Assert . Equal ( expectedMethodName , methodAttrs [ "name" ] ) ;
121136 Assert . Equal ( expectedSignature , methodAttrs [ "signature" ] ) ;
122137 }
138+
139+ [ Fact ]
140+ public void TestReportWithTwoDifferentDirectories ( )
141+ {
142+ CoverageResult result = new CoverageResult ( ) ;
143+ result . Identifier = Guid . NewGuid ( ) . ToString ( ) ;
144+
145+ var isWindows = RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) ;
146+
147+ string absolutePath1 ;
148+ string absolutePath2 ;
149+
150+ if ( isWindows )
151+ {
152+ absolutePath1 = @"C:\projA\file.cs" ;
153+ absolutePath2 = @"E:\projB\file.cs" ;
154+ }
155+ else
156+ {
157+ absolutePath1 = @"/projA/file.cs" ;
158+ absolutePath2 = @"/projB/file.cs" ;
159+ }
160+
161+ var classes = new Classes { { "Class" , new Methods ( ) } } ;
162+ var documents = new Documents { { absolutePath1 , classes } , { absolutePath2 , classes } } ;
163+
164+ result . Modules = new Modules { { "Module" , documents } } ;
165+
166+ CoberturaReporter reporter = new CoberturaReporter ( ) ;
167+ string report = reporter . Report ( result ) ;
168+
169+ var doc = XDocument . Load ( new MemoryStream ( Encoding . UTF8 . GetBytes ( report ) ) ) ;
170+
171+ List < string > rootPaths = doc . Element ( "coverage" ) . Element ( "sources" ) . Elements ( ) . Select ( e => e . Value ) . ToList ( ) ;
172+ List < string > relativePaths = doc . Element ( "coverage" ) . Element ( "packages" ) . Element ( "package" )
173+ . Element ( "classes" ) . Elements ( ) . Select ( e => e . Attribute ( "filename" ) . Value ) . ToList ( ) ;
174+
175+ List < string > possiblePaths = new List < string > ( ) ;
176+ foreach ( string root in rootPaths )
177+ {
178+ foreach ( string relativePath in relativePaths )
179+ {
180+ possiblePaths . Add ( Path . Combine ( root , relativePath ) ) ;
181+ }
182+ }
183+
184+ Assert . Contains ( absolutePath1 , possiblePaths ) ;
185+ Assert . Contains ( absolutePath2 , possiblePaths ) ;
186+ }
187+
188+ [ Fact ]
189+ public void TestReportWithSourcelinkPaths ( )
190+ {
191+ CoverageResult result = new CoverageResult { UseSourceLink = true , Identifier = Guid . NewGuid ( ) . ToString ( ) } ;
192+
193+ var absolutePath =
194+ @"https://raw.githubusercontent.com/johndoe/Coverlet/02c09baa8bfdee3b6cdf4be89bd98c8157b0bc08/Demo.cs" ;
195+
196+ var classes = new Classes { { "Class" , new Methods ( ) } } ;
197+ var documents = new Documents { { absolutePath , classes } } ;
198+
199+ result . Modules = new Modules { { "Module" , documents } } ;
200+
201+ CoberturaReporter reporter = new CoberturaReporter ( ) ;
202+ string report = reporter . Report ( result ) ;
203+
204+ var doc = XDocument . Load ( new MemoryStream ( Encoding . UTF8 . GetBytes ( report ) ) ) ;
205+ var fileName = doc . Element ( "coverage" ) . Element ( "packages" ) . Element ( "package" ) . Element ( "classes" ) . Elements ( )
206+ . Select ( e => e . Attribute ( "filename" ) . Value ) . Single ( ) ;
207+
208+ Assert . Equal ( absolutePath , fileName ) ;
209+ }
123210 }
124211}
0 commit comments