Пример #1
0
        private static void RootDir_IsAbsolutePath_WithInMemory(string rootDir, char separator)
        {
            var matcher = new Matcher();

            matcher.AddInclude($"**{separator}*.cs");

            IEnumerable <string>  files   = GetFileList(Path.GetPathRoot(rootDir), separator);
            PatternMatchingResult results = matcher.Match(rootDir, files);

            IEnumerable <string> actual   = results.Files.Select(match => match.Path);
            IEnumerable <string> expected = new string[]
            {
                "source1.cs",
                "sub/source2.cs",
                "sub/source3.cs",
                "sub2/source4.cs",
                "sub2/source5.cs",
                "compiler/preprocess/preprocess-source1.cs",
                "compiler/preprocess/sub/preprocess-source2.cs",
                "compiler/preprocess/sub/sub/preprocess-source3.cs",
                "compiler/shared/shared1.cs",
                "compiler/shared/sub/shared2.cs",
                "compiler/shared/sub/sub/sharedsub.cs"
            };

            Assert.Equal(
                expected.OrderBy(e => e),
                actual.OrderBy(e => e),
                StringComparer.OrdinalIgnoreCase);
        }
Пример #2
0
        public static IEnumerable <string> ExpandGlobs(IAsset asset, IHostingEnvironment env)
        {
            string root  = asset.GetFileProvider(env).GetFileInfo("/").PhysicalPath;
            var    dir   = new DirectoryInfoWrapper(new DirectoryInfo(root));
            var    files = new List <string>();

            foreach (string sourceFile in asset.SourceFiles)
            {
                var matcher = new Matcher();
                matcher.AddInclude(sourceFile);
                PatternMatchingResult globbingResult = matcher.Execute(dir);
                IEnumerable <string>  fileMatches    = globbingResult.Files.Select(f => f.Path.Replace(root, string.Empty));

                if (!fileMatches.Any())
                {
                    throw new FileNotFoundException($"No files found matching \"{sourceFile}\" exist in \"{dir.FullName}\"");
                }

                files.AddRange(fileMatches.Where(f => !files.Contains(f)));
            }

            asset.Items[_physicalFilesKey] = files;

            return(files);
        }
Пример #3
0
        static List <string> FindPdbPaths(Options options, Logger logger)
        {
            var results = new List <string>();

            // If no working directory was specified, use the current
            string workingDir = options.WorkingDir;

            if (string.IsNullOrEmpty(workingDir))
            {
                workingDir = Directory.GetCurrentDirectory();
            }

            logger.Log(VerbosityLevel.Detailed, string.Format("Searching for pdbs with working directory '{0}' and search pattern '{1}'", workingDir, options.PdbSearchExpression));

            Matcher matcher = new Matcher();

            matcher.AddInclude(options.PdbSearchExpression);

            PatternMatchingResult matches = matcher.Execute(new DirectoryInfoWrapper(new DirectoryInfo(workingDir)));

            foreach (var match in matches.Files)
            {
                var path    = Path.Combine(workingDir, match.Path);
                var pdbPath = Path.GetFullPath(path);
                logger.Log(VerbosityLevel.Detailed, string.Format("Found pdb '{0}'", pdbPath));
                results.Add(pdbPath);
            }
            return(results);
        }
        public void FilePatternParserShouldThrowCommandLineExceptionIfFileDoesNotExist()
        {
            var patternMatchingResult = new PatternMatchingResult(new List <FilePatternMatch>());

            this.mockFileHelper.Setup(x => x.Exists(TranslatePath(@"E:\path\to\project\tests\Blame.Tests\\abc.Tests.dll"))).Returns(false);
            this.mockMatcherHelper.Setup(x => x.Execute(It.IsAny <DirectoryInfoWrapper>())).Returns(patternMatchingResult);

            Assert.ThrowsException <TestSourceException>(() => this.filePatternParser.GetMatchingFiles(TranslatePath(@"E:\path\to\project\tests\Blame.Tests\\abc.Tests.dll")));
        }
Пример #5
0
        public static IEnumerable <string> ExpandGlobs(IAsset asset, IHostingEnvironment env)
        {
            var files = new List <string>();

            foreach (string sourceFile in asset.SourceFiles)
            {
                string outSourceFile;
                var    provider = asset.GetFileProvider(env, sourceFile, out outSourceFile);

                if (provider.GetFileInfo(outSourceFile).Exists)
                {
                    if (!files.Contains(sourceFile))
                    {
                        files.Add(sourceFile);
                    }
                }
                else
                {
                    var    fileInfo = provider.GetFileInfo("/");
                    string root     = fileInfo.PhysicalPath;

                    if (root != null)
                    {
                        var dir     = new DirectoryInfoWrapper(new DirectoryInfo(root));
                        var matcher = new Matcher();

                        outSourceFile = outSourceFile.TrimStart('~', '/');

                        matcher.AddInclude(outSourceFile);
                        PatternMatchingResult globbingResult = matcher.Execute(dir);
                        IEnumerable <string>  fileMatches    = globbingResult.Files.Select(f => f.Path.Replace(root, string.Empty));

                        //if (!fileMatches.Any())
                        //{
                        //    throw new FileNotFoundException($"No files found matching \"{sourceFile}\" exist in \"{dir.FullName}\"");
                        //}

                        if (fileMatches.Any())
                        {
                            files.AddRange(fileMatches.Where(f => !files.Contains(f)));
                        }
                    }
                    else
                    {
                        if (!files.Contains(sourceFile))
                        {
                            files.Add(sourceFile);
                        }
                    }
                }
            }

            asset.Items[PhysicalFilesKey] = files;

            return(files);
        }
        public IEnumerable <IFileSystemInfo> EnumerateFileSystemInfos(string[] includePatterns, string[] excludePatterns)
        {
            var matcher = GetMatcher(includePatterns, excludePatterns);
            PatternMatchingResult matchResult = SafeExecuteMatcher(matcher);

            return(matchResult.Files.Select((filePatternMatch) => {
                var path = PathUtils.NormalizePath(filePatternMatch.Path);
                return CreateFileSystemInfoProxy(new FileInfo(path));
            }));
        }
Пример #7
0
        public void FilePatternParserShouldCorrectlySplitPatternAndDirectory()
        {
            var patternMatchingResult = new PatternMatchingResult(new List <FilePatternMatch>());

            this.mockMatcherHelper.Setup(x => x.Execute(It.IsAny <DirectoryInfoWrapper>())).Returns(patternMatchingResult);
            this.filePatternParser.GetMatchingFiles(@"C:\Users\vanidhi\Desktop\a\c\*bc.dll");

            // Assert
            this.mockMatcherHelper.Verify(x => x.AddInclude(@"*bc.dll"));
            this.mockMatcherHelper.Verify(x => x.Execute(It.Is <DirectoryInfoWrapper>(y => y.FullName.Equals(@"C:\Users\vanidhi\Desktop\a\c"))));
        }
        public void FilePatternParserShouldCorrectlySplitWithMultpleWildCardInMultipleDirectory()
        {
            var patternMatchingResult = new PatternMatchingResult(new List <FilePatternMatch>());

            this.mockMatcherHelper.Setup(x => x.Execute(It.IsAny <DirectoryInfoWrapper>())).Returns(patternMatchingResult);
            this.filePatternParser.GetMatchingFiles(TranslatePath(@"E:\path\to\project\*tests\Tests*.Blame*.dll"));

            // Assert
            this.mockMatcherHelper.Verify(x => x.AddInclude(TranslatePath(@"*tests\Tests*.Blame*.dll")));
            this.mockMatcherHelper.Verify(x => x.Execute(It.Is <DirectoryInfoWrapper>(y => y.FullName.Equals(TranslatePath(@"E:\path\to\project")))));
        }
        public void FilePatternParserShouldCorrectlySplitWithArbitraryDirectoryDepth()
        {
            var patternMatchingResult = new PatternMatchingResult(new List <FilePatternMatch>());

            this.mockMatcherHelper.Setup(x => x.Execute(It.IsAny <DirectoryInfoWrapper>())).Returns(patternMatchingResult);
            this.filePatternParser.GetMatchingFiles(TranslatePath(@"C:\Users\vanidhi\**\c\*bc.txt"));

            // Assert
            this.mockMatcherHelper.Verify(x => x.AddInclude(TranslatePath(@"**\c\*bc.txt")));
            this.mockMatcherHelper.Verify(x => x.Execute(It.Is <DirectoryInfoWrapper>(y => y.FullName.Equals(TranslatePath(@"C:\Users\vanidhi")))));
        }
Пример #10
0
    static List <string> GetFiles(string globSearch)
    {
        Matcher matcher = new();

        matcher.AddIncludePatterns(new[] { globSearch });
        PatternMatchingResult result = matcher.Execute(
            new DirectoryInfoWrapper(
                new DirectoryInfo(".")));

        return(result.Files.Select(p => p.Path).ToList());
    }
        public void FilePatternParserShouldCheckIfFileExistsIfFullPathGiven()
        {
            var patternMatchingResult = new PatternMatchingResult(new List <FilePatternMatch>());

            this.mockFileHelper.Setup(x => x.Exists(TranslatePath(@"E:\path\to\project\tests\Blame.Tests\\abc.Tests.dll"))).Returns(true);
            this.mockMatcherHelper.Setup(x => x.Execute(It.IsAny <DirectoryInfoWrapper>())).Returns(patternMatchingResult);
            var matchingFiles = this.filePatternParser.GetMatchingFiles(TranslatePath(@"E:\path\to\project\tests\Blame.Tests\\abc.Tests.dll"));

            // Assert
            this.mockFileHelper.Verify(x => x.Exists(TranslatePath(@"E:\path\to\project\tests\Blame.Tests\\abc.Tests.dll")));
            Assert.IsTrue(matchingFiles.Contains(TranslatePath(@"E:\path\to\project\tests\Blame.Tests\\abc.Tests.dll")));
        }
        /// <summary>
        /// Determines whether this permission rule is a match for the target resource name and claim type.
        /// Uses globbing to match to pattern.
        /// </summary>
        /// <param name="resourceUri">The URI of the target resource.</param>
        /// <param name="accessType">The claim type of the target resource.</param>
        /// <returns>True if a match.</returns>
        public bool IsMatch(Uri resourceUri, string accessType)
        {
            var resourceNameMatcher = new Matcher();

            resourceNameMatcher.AddInclude(this.Resource.Uri.ToString());
            PatternMatchingResult resourceNameMatchResult = resourceNameMatcher.Match(resourceUri.ToString());

            var accessTypeMatcher = new Matcher();

            accessTypeMatcher.AddInclude(this.AccessType);
            PatternMatchingResult accessTypeMatchResult = accessTypeMatcher.Match(accessType);

            return(resourceNameMatchResult.HasMatches && accessTypeMatchResult.HasMatches);
        }
Пример #13
0
        public static string[] Glob([NotNull] DirectoryInfo folder, [NotNull] Matcher matcher)
        {
            if (folder is null)
            {
                throw new ArgumentNullException(nameof(folder));
            }
            if (matcher is null)
            {
                throw new ArgumentNullException(nameof(matcher));
            }

            PatternMatchingResult result = matcher.Execute(new DirectoryInfoWrapper(folder));

            return(result.HasMatches
                ? result.Files.Select(match => Path.Combine(folder.FullName, match.Path)).ToArray()
                : Array.Empty <string>());
        }
Пример #14
0
        /// <summary>
        /// Gets files from the specified directory using globbing patterns.
        /// </summary>
        /// <param name="directory">The directory to search.</param>
        /// <param name="patterns">The globbing pattern(s) to use.</param>
        /// <returns>Files that match the globbing pattern(s).</returns>
        public static IEnumerable <FileInfo> GetFiles(DirectoryInfo directory, IEnumerable <string> patterns)
        {
            // Initially based on code from Reliak.FileSystemGlobbingExtensions (https://github.com/reliak/Reliak.FileSystemGlobbingExtensions)

            Matcher matcher = new Matcher(StringComparison.OrdinalIgnoreCase);

            // Expand braces
            IEnumerable <string> expandedPatterns = patterns
                                                    .SelectMany(ExpandBraces)
                                                    .Select(f => f.Replace("\\{", "{").Replace("\\}", "}")); // Unescape braces

            // Add the patterns, any that start with ! are exclusions
            foreach (string expandedPattern in expandedPatterns)
            {
                bool   isExclude    = expandedPattern[0] == '!';
                string finalPattern = isExclude ? expandedPattern.Substring(1) : expandedPattern;
                finalPattern = finalPattern
                               .Replace("\\!", "!") // Unescape negation
                               .Replace("\\", "/"); // Normalize slashes

                // No support for absolute paths
                if (System.IO.Path.IsPathRooted(finalPattern))
                {
                    throw new ArgumentException($"Rooted globbing patterns are not supported ({expandedPattern})", nameof(patterns));
                }

                // Add exclude or include pattern to matcher
                if (isExclude)
                {
                    matcher.AddExclude(finalPattern);
                }
                else
                {
                    matcher.AddInclude(finalPattern);
                }
            }

            DirectoryInfoBase     directoryInfo = new DirectoryInfoWrapper(directory);
            PatternMatchingResult result        = matcher.Execute(directoryInfo);

            return(result.Files.Select(match => directoryInfo.GetFile(match.Path)).Select(fb => new FileInfo(fb.FullName)));
        }
Пример #15
0
        protected virtual Task CollectFilesAsync(List <IFileBundleSourceFilterItem> fileList, IBundleBuildContext context)
        {
            var directoryInfo = new GlobbingDirectoryInfo(_fileProvider, string.Empty);

            var n = _includes.Length;

            for (var i = 0; i < n; i++)
            {
                context.CancellationToken.ThrowIfCancellationRequested();

                Include include = _includes[i];

                PatternMatchingResult matchingResult = include.Matcher.Execute(directoryInfo);
                if (matchingResult.HasMatches)
                {
                    fileList.AddRange(matchingResult.Files.Select(m => CreateBuildItem(include, UrlUtils.NormalizePath(m.Path), context)));
                }
            }

            return(Task.CompletedTask);
        }
Пример #16
0
        private async Task LoadFromDirectoryAsync(string rootDir, PatternMatchingResult matchResult)
        {
            foreach (var file in matchResult.Files)
            {
                if (_sessionTokenSource.IsCancellationRequested)
                {
                    break;
                }

                var path = Path.Combine(rootDir, PathUtils.NormalizePath(file.Path));
                if (!ModulePath.IsPythonSourceFile(path))
                {
                    if (ModulePath.IsPythonFile(path, true, true, true))
                    {
                        // TODO: Deal with scrapable files (if we need to do anything?)
                    }
                    continue;
                }
                await _server.LoadFileAsync(new Uri(path));
            }
        }
        private static Matcher MakeMatcher(List <string> includePatterns, List <string> excludePatterns)
        {
            var matcher = new Mock <Matcher>();

            matcher.Setup(m => m.AddInclude(It.IsAny <string>()))
            .Returns <string>(pattern =>
            {
                includePatterns.Add(pattern);
                return(matcher.Object);
            });
            matcher.Setup(m => m.AddExclude(It.IsAny <string>()))
            .Returns <string>(pattern =>
            {
                excludePatterns.Add(pattern);
                return(matcher.Object);
            });
            var patternMatchingResult = new PatternMatchingResult(Enumerable.Empty <FilePatternMatch>());

            matcher.Setup(m => m.Execute(It.IsAny <DirectoryInfoBase>())).Returns(patternMatchingResult);
            return(matcher.Object);
        }
Пример #18
0
        /// <summary>
        /// Gets files from the specified directory using globbing patterns.
        /// </summary>
        /// <param name="directory">The directory to search.</param>
        /// <param name="patterns">The globbing pattern(s) to use.</param>
        /// <returns>Files that match the globbing pattern(s).</returns>
        /// <remarks>Initially based on code from Reliak.FileSystemGlobbingExtensions (https://github.com/reliak/Reliak.FileSystemGlobbingExtensions).</remarks>
        public static IEnumerable <IFile> GetFiles(IDirectory directory, IEnumerable <string> patterns)
        {
            Matcher matcher = new Matcher(NormalizedPath.DefaultComparisonType);

            // Expand braces
            IEnumerable <string> expandedPatterns = patterns
                                                    .SelectMany(ExpandBraces)
                                                    .Select(f => f.Replace("\\{", "{").Replace("\\}", "}")); // Unescape braces

            // Add the patterns, any that start with ! are exclusions
            foreach (string expandedPattern in expandedPatterns)
            {
                bool   isExclude    = expandedPattern[0] == '!';
                string finalPattern = isExclude ? expandedPattern.Substring(1) : expandedPattern;
                finalPattern = finalPattern
                               .Replace("\\!", "!") // Unescape negation
                               .Replace("\\", "/"); // Normalize slashes

                // No support for absolute paths
                if (Path.IsPathRooted(finalPattern))
                {
                    throw new ArgumentException($"Rooted globbing patterns are not supported ({expandedPattern})", nameof(patterns));
                }

                // Add exclude or include pattern to matcher
                if (isExclude)
                {
                    matcher.AddExclude(finalPattern);
                }
                else
                {
                    matcher.AddInclude(finalPattern);
                }
            }

            DirectoryInfoBase     directoryInfo = new DirectoryInfo(directory);
            PatternMatchingResult result        = matcher.Execute(directoryInfo);

            return(result.Files.Select(match => directory.GetFile(match.Path)));
        }
Пример #19
0
        private void ReportChangeForMatchedEntries(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                // System.IO.FileSystemWatcher may trigger events that are missing the file name,
                // which makes it appear as if the root directory is renamed or deleted. Moving the root directory
                // of the file watcher is not supported, so this type of event is ignored.
                return;
            }

            path = NormalizePath(path);

            bool matched = false;

            if (_filePathTokenLookup.TryRemove(path, out ChangeTokenInfo matchInfo))
            {
                CancelToken(matchInfo);
                matched = true;
            }

            foreach (System.Collections.Generic.KeyValuePair <string, ChangeTokenInfo> wildCardEntry in _wildcardTokenLookup)
            {
                PatternMatchingResult matchResult = wildCardEntry.Value.Matcher.Match(path);
                if (matchResult.HasMatches &&
                    _wildcardTokenLookup.TryRemove(wildCardEntry.Key, out matchInfo))
                {
                    CancelToken(matchInfo);
                    matched = true;
                }
            }

            if (matched)
            {
                TryDisableFileSystemWatcher();
            }
        }
        public FileSystemGlobbingTestContext Execute()
        {
            Result = _patternMatching.Execute(_directoryInfo);

            return this;
        }
Пример #21
0
        public FileSystemGlobbingTestContext Execute()
        {
            Result = _patternMatching.Execute(_directoryInfo);

            return(this);
        }