forked from microsoft/vstest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FilePatternParser.cs
99 lines (81 loc) · 3.94 KB
/
FilePatternParser.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace vstest.console.Internal
{
using Microsoft.Extensions.FileSystemGlobbing;
using Microsoft.Extensions.FileSystemGlobbing.Abstractions;
using Microsoft.VisualStudio.TestPlatform.CommandLine;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers;
using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.Interfaces;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using CommandLineResources = Microsoft.VisualStudio.TestPlatform.CommandLine.Resources.Resources;
/// <summary>
/// Class for getting matching files from wild card pattern file name
/// Microsoft.Extensions.FileSystemGlobbing methods used to get matching file names
/// </summary>
public class FilePatternParser
{
private Matcher matcher;
private IFileHelper fileHelper;
private char[] wildCardCharacters = { '*' };
public FilePatternParser()
: this(new Matcher(), new FileHelper())
{
}
internal FilePatternParser(Matcher matcher, IFileHelper fileHelper)
{
this.matcher = matcher;
this.fileHelper = fileHelper;
}
/// <summary>
/// Used to get matching files with pattern
/// </summary>
/// <returns>Returns the list of matching files</returns>
public List<string> GetMatchingFiles(string filePattern)
{
var matchingFiles = new List<string>();
// If there is no wildcard simply add the file to the list of matching files.
if(filePattern.IndexOfAny(wildCardCharacters) == -1)
{
EqtTrace.Info($"FilePatternParser: The given file {filePattern} is a full path.");
// Check if the file exists.
if (!this.fileHelper.Exists(filePattern))
{
throw new CommandLineException(
string.Format(CultureInfo.CurrentUICulture, CommandLineResources.TestSourceFileNotFound, filePattern));
}
matchingFiles.Add(filePattern);
return matchingFiles;
}
// Split the given wildcard into search directory and pattern to be searched.
var splitPattern = SplitFilePatternOnWildCard(filePattern);
EqtTrace.Info($"FilePatternParser: Matching file pattern '{splitPattern.Item2}' within directory '{splitPattern.Item1}'");
this.matcher.AddInclude(splitPattern.Item2);
// Execute the given pattern in the search directory.
var matches = this.matcher.Execute(new DirectoryInfoWrapper(new DirectoryInfo(splitPattern.Item1)));
// Add all the files to the list of matching files.
foreach (var match in matches.Files)
{
matchingFiles.Add(Path.Combine(splitPattern.Item1, match.Path));
}
return matchingFiles;
}
/// <summary>
/// Splits full pattern into search directory and pattern.
/// </summary>
private Tuple<string, string> SplitFilePatternOnWildCard(string filePattern)
{
// Split the pattern based on first wildcard character found.
var splitOnWildCardIndex = filePattern.IndexOfAny(wildCardCharacters);
var directorySeparatorIndex = filePattern.Substring(0, splitOnWildCardIndex).LastIndexOf(Path.DirectorySeparatorChar);
string searchDir = filePattern.Substring(0, directorySeparatorIndex);
string pattern = filePattern.Substring(directorySeparatorIndex + 1);
Tuple<string, string> splitPattern = new Tuple<string, string>(searchDir, pattern);
return splitPattern;
}
}
}