-
Notifications
You must be signed in to change notification settings - Fork 419
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1161 from filipw/feature/globbing
Added support for excluding search paths via globbing patterns
- Loading branch information
Showing
7 changed files
with
184 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System.Collections.Generic; | ||
using System.Composition; | ||
using System.Linq; | ||
using Microsoft.Extensions.FileSystemGlobbing; | ||
using OmniSharp.Options; | ||
|
||
namespace OmniSharp.FileSystem | ||
{ | ||
[Export, Shared] | ||
public class FileSystemHelper | ||
{ | ||
private readonly OmniSharpOptions _omniSharpOptions; | ||
private readonly IOmniSharpEnvironment _omniSharpEnvironment; | ||
|
||
[ImportingConstructor] | ||
public FileSystemHelper(OmniSharpOptions omniSharpOptions, IOmniSharpEnvironment omniSharpEnvironment) | ||
{ | ||
_omniSharpOptions = omniSharpOptions; | ||
_omniSharpEnvironment = omniSharpEnvironment; | ||
} | ||
|
||
public IEnumerable<string> GetFiles(string includePattern) => GetFiles(includePattern, _omniSharpEnvironment.TargetDirectory); | ||
|
||
public IEnumerable<string> GetFiles(string includePattern, string targetDirectory) | ||
{ | ||
var matcher = new Matcher(); | ||
matcher.AddInclude(includePattern); | ||
|
||
if (_omniSharpOptions.FileOptions.SystemExcludeSearchPatterns != null && _omniSharpOptions.FileOptions.SystemExcludeSearchPatterns.Any()) | ||
{ | ||
matcher.AddExcludePatterns(_omniSharpOptions.FileOptions.SystemExcludeSearchPatterns); | ||
} | ||
|
||
if (_omniSharpOptions.FileOptions.ExcludeSearchPatterns != null && _omniSharpOptions.FileOptions.ExcludeSearchPatterns.Any()) | ||
{ | ||
matcher.AddExcludePatterns(_omniSharpOptions.FileOptions.ExcludeSearchPatterns); | ||
} | ||
|
||
return matcher.GetResultsInFullPath(targetDirectory); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System; | ||
|
||
namespace OmniSharp.Options | ||
{ | ||
public class FileOptions | ||
{ | ||
public string[] SystemExcludeSearchPatterns { get; set; } = new[] { "**/node_modules/**/*", "**/bin/**/*", "**/obj/**/*", "**/.git/**/*" }; | ||
|
||
public string[] ExcludeSearchPatterns { get; set; } = Array.Empty<string>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
using Microsoft.Extensions.Logging; | ||
using OmniSharp.FileSystem; | ||
using OmniSharp.Options; | ||
using OmniSharp.Services; | ||
using System.Linq; | ||
using TestUtility; | ||
using Xunit; | ||
|
||
namespace OmniSharp.Tests | ||
{ | ||
public class FileSystemHelperFacts | ||
{ | ||
[Fact] | ||
public void FileSystemHelperFacts_CanExcludeSearchPath_File() | ||
{ | ||
var helper = CreateFileSystemHelper("**/ProjectWithSdkProperty.csproj"); | ||
|
||
var msbuildProjectFiles = helper.GetFiles("**/*.csproj"); | ||
Assert.NotEmpty(msbuildProjectFiles); | ||
|
||
var projectWithSdkProperty = msbuildProjectFiles.FirstOrDefault(p => p.Contains("ProjectWithSdkProperty")); | ||
Assert.Null(projectWithSdkProperty); | ||
} | ||
|
||
[Fact] | ||
public void FileSystemHelperFacts_CanExcludeSearchPath_MultipleFiles() | ||
{ | ||
var helper = CreateFileSystemHelper("**/MSTestProject.csproj", "**/NUnitTestProject.csproj"); | ||
|
||
var msbuildProjectFiles = helper.GetFiles("**/*.csproj"); | ||
Assert.NotEmpty(msbuildProjectFiles); | ||
|
||
var msTestProject = msbuildProjectFiles.FirstOrDefault(p => p.Contains("MSTestProject")); | ||
Assert.Null(msTestProject); | ||
|
||
var nunitTestProject = msbuildProjectFiles.FirstOrDefault(p => p.Contains("NUnitTestProject")); | ||
Assert.Null(nunitTestProject); | ||
} | ||
|
||
[Fact] | ||
public void FileSystemHelperFacts_CanExcludeSearchPath_Folder() | ||
{ | ||
var helper = CreateFileSystemHelper("**/ProjectWithSdkProperty/**/*"); | ||
|
||
var msbuildProjectFiles = helper.GetFiles("**/*.csproj"); | ||
Assert.NotEmpty(msbuildProjectFiles); | ||
|
||
var projectWithSdkProperty = msbuildProjectFiles.FirstOrDefault(p => p.Contains("ProjectWithSdkProperty")); | ||
Assert.Null(projectWithSdkProperty); | ||
} | ||
|
||
[Fact] | ||
public void FileSystemHelperFacts_CanExcludeSearchPath_MultipleFolders() | ||
{ | ||
var helper = CreateFileSystemHelper("**/MSTestProject/**/*", "**/NUnitTestProject/**/*"); | ||
|
||
var msbuildProjectFiles = helper.GetFiles("**/*.csproj"); | ||
Assert.NotEmpty(msbuildProjectFiles); | ||
|
||
var msTestProject = msbuildProjectFiles.FirstOrDefault(p => p.Contains("MSTestProject")); | ||
Assert.Null(msTestProject); | ||
|
||
var nunitTestProject = msbuildProjectFiles.FirstOrDefault(p => p.Contains("NUnitTestProject")); | ||
Assert.Null(nunitTestProject); | ||
} | ||
|
||
[Fact] | ||
public void FileSystemHelperFacts_CanExcludeSearchPath_MultipleFolders_BothSystemAndUserPaths() | ||
{ | ||
var helper = CreateFileSystemHelper(new[] { "**/MSTestProject/**/*", "**/NUnitTestProject/**/*" }, systemExcludePatterns: new[] { "**/ProjectWithSdkProperty/**/*" }); | ||
|
||
var msbuildProjectFiles = helper.GetFiles("**/*.csproj"); | ||
Assert.NotEmpty(msbuildProjectFiles); | ||
|
||
var msTestProject = msbuildProjectFiles.FirstOrDefault(p => p.Contains("MSTestProject")); | ||
Assert.Null(msTestProject); | ||
|
||
var nunitTestProject = msbuildProjectFiles.FirstOrDefault(p => p.Contains("NUnitTestProject")); | ||
Assert.Null(nunitTestProject); | ||
|
||
var projectWithSdkProperty = msbuildProjectFiles.FirstOrDefault(p => p.Contains("ProjectWithSdkProperty")); | ||
Assert.Null(projectWithSdkProperty); | ||
} | ||
|
||
[Fact] | ||
public void FileSystemHelperFacts_CanHandleInvalidPath() | ||
{ | ||
var helper = CreateFileSystemHelper("!@@#$$@%&&*()_+"); | ||
|
||
var ex = Record.Exception(() => helper.GetFiles("**/*.csproj")); | ||
Assert.Null(ex); | ||
} | ||
|
||
private FileSystemHelper CreateFileSystemHelper(params string[] excludePatterns) | ||
{ | ||
var environment = new OmniSharpEnvironment(TestAssets.Instance.TestAssetsFolder, 1000, LogLevel.Information, null); | ||
var options = new OmniSharpOptions(); | ||
options.FileOptions.ExcludeSearchPatterns = excludePatterns; | ||
var helper = new FileSystemHelper(options, environment); | ||
return helper; | ||
} | ||
|
||
private FileSystemHelper CreateFileSystemHelper(string[] excludePatterns, string[] systemExcludePatterns) | ||
{ | ||
var environment = new OmniSharpEnvironment(TestAssets.Instance.TestAssetsFolder, 1000, LogLevel.Information, null); | ||
var options = new OmniSharpOptions(); | ||
options.FileOptions.ExcludeSearchPatterns = excludePatterns; | ||
options.FileOptions.SystemExcludeSearchPatterns = systemExcludePatterns; | ||
|
||
var helper = new FileSystemHelper(options, environment); | ||
return helper; | ||
} | ||
} | ||
} |