1+ // Copyright (c) Spekt Contributors. All rights reserved.
2+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+ namespace Spekt . TestLogger . Utilities
5+ {
6+ using System ;
7+ using System . Collections . Generic ;
8+ using System . IO ;
9+ using System . Linq ;
10+ using Microsoft . Testing . Platform . Extensions . Messages ;
11+ using Spekt . TestLogger . Core ;
12+
13+ public static class ArtifactExtensions
14+ {
15+ public static TestAttachmentInfo ToAttachment ( this SessionFileArtifact artifact , string baseDirectory , bool makeRelativePath )
16+ {
17+ var filePath = artifact . FileInfo . FullName ;
18+ if ( makeRelativePath && ! string . IsNullOrEmpty ( baseDirectory ) )
19+ {
20+ filePath = MakeRelativePath ( baseDirectory , filePath ) ;
21+ }
22+
23+ return new TestAttachmentInfo ( filePath , artifact . Description ) ;
24+ }
25+
26+ public static IEnumerable < TestAttachmentInfo > ToAttachments ( this IEnumerable < FileArtifactProperty > artifacts , string baseDirectory , bool makeRelativePaths )
27+ {
28+ if ( makeRelativePaths && ! string . IsNullOrEmpty ( baseDirectory ) )
29+ {
30+ return artifacts . Select ( a =>
31+ {
32+ var relativePath = MakeRelativePath ( baseDirectory , a . FileInfo . FullName ) ;
33+ return new TestAttachmentInfo ( relativePath , a . Description ) ;
34+ } ) ;
35+ }
36+
37+ return artifacts . Select ( a => new TestAttachmentInfo ( a . FileInfo . FullName , a . Description ) ) ;
38+ }
39+
40+ /// <summary>
41+ /// Makes a target path relative to a base directory path.
42+ /// </summary>
43+ /// <param name="baseDirectoryPath">Base directory path.</param>
44+ /// <param name="targetPath">Target file path.</param>
45+ /// <returns>File path relative to base directory path.</returns>
46+ public static string MakeRelativePath ( string baseDirectoryPath , string targetPath )
47+ {
48+ // Example: basePath: C:\a\b\c, targetPath: C:\a\d\e.txt, Output: ..\..\d\e.txt
49+ // Assumes baseDirectoryPath is a directory path in any OS.
50+ if ( ! baseDirectoryPath . EndsWith ( Path . DirectorySeparatorChar . ToString ( ) ) &&
51+ ! baseDirectoryPath . EndsWith ( Path . AltDirectorySeparatorChar . ToString ( ) ) )
52+ {
53+ baseDirectoryPath += Path . DirectorySeparatorChar ;
54+ }
55+
56+ // Target path can be relative, or on a different drive, just return it as is.
57+ if ( ! Path . IsPathRooted ( targetPath ) ||
58+ ! string . Equals ( Path . GetPathRoot ( baseDirectoryPath ) , Path . GetPathRoot ( targetPath ) , StringComparison . OrdinalIgnoreCase ) )
59+ {
60+ return targetPath ;
61+ }
62+
63+ var baseUri = new Uri ( baseDirectoryPath ) ;
64+ var targetUri = new Uri ( targetPath ) ;
65+
66+ var relativeUri = baseUri . MakeRelativeUri ( targetUri ) ;
67+ var relativePath = Uri . UnescapeDataString ( relativeUri . ToString ( ) ) ;
68+
69+ // Convert URI slashes to platform-specific directory separators.
70+ return relativePath . Replace ( '/' , Path . DirectorySeparatorChar ) ;
71+ }
72+ }
73+ }
0 commit comments