From 22e99595cd1676317282661d3a0d4109dcd74bba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Epresi?= Date: Tue, 28 Apr 2020 08:02:40 +0200 Subject: [PATCH] Remove invalid files from FeatureFiles list --- .../SpecFlowProjectInfo.cs | 19 +---- TechTalk.SpecFlow/Assist/FileFilter.cs | 73 +++++++++++++++++++ .../FileFilterTests.cs | 71 ++++++++++++++++++ 3 files changed, 146 insertions(+), 17 deletions(-) create mode 100644 TechTalk.SpecFlow/Assist/FileFilter.cs create mode 100644 Tests/TechTalk.SpecFlow.GeneratorTests/FileFilterTests.cs diff --git a/SpecFlow.Tools.MsBuild.Generation/SpecFlowProjectInfo.cs b/SpecFlow.Tools.MsBuild.Generation/SpecFlowProjectInfo.cs index f0b0d88f3..6c5b9852a 100644 --- a/SpecFlow.Tools.MsBuild.Generation/SpecFlowProjectInfo.cs +++ b/SpecFlow.Tools.MsBuild.Generation/SpecFlowProjectInfo.cs @@ -1,6 +1,5 @@ using System.Collections.Generic; -using System.IO; -using System.Linq; +using TechTalk.SpecFlow.Assist; using TechTalk.SpecFlow.Generator; namespace SpecFlow.Tools.MsBuild.Generation @@ -20,8 +19,7 @@ public SpecFlowProjectInfo( string currentTargetFramework) { GeneratorPlugins = generatorPlugins; - FeatureFiles = RemoveFilesWithInvalidChars(featureFiles); - //FeatureFiles = featureFiles; + FeatureFiles = FileFilter.GetValidFiles(featureFiles); ProjectFolder = projectFolder; OutputPath = outputPath; RootNamespace = rootNamespace; @@ -52,18 +50,5 @@ public SpecFlowProjectInfo( public string CurrentTargetFramework { get; } - private IReadOnlyCollection RemoveFilesWithInvalidChars(IEnumerable featureFilePaths) - { - return featureFilePaths.Where(p => !InvalidFileName(p)).ToList(); - } - - private bool InvalidFileName(string featureFilePath) - { - var featureFileName = Path.GetFileName(featureFilePath); - var invalidCharacters = Path.GetInvalidFileNameChars(); - - return !string.IsNullOrEmpty(featureFileName) && - featureFileName.Any(s => invalidCharacters.Contains(s)); - } } } diff --git a/TechTalk.SpecFlow/Assist/FileFilter.cs b/TechTalk.SpecFlow/Assist/FileFilter.cs new file mode 100644 index 000000000..e7134a005 --- /dev/null +++ b/TechTalk.SpecFlow/Assist/FileFilter.cs @@ -0,0 +1,73 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; + +namespace TechTalk.SpecFlow.Assist +{ + public static class FileFilter + { + public static IReadOnlyCollection GetValidFiles(IReadOnlyCollection filePaths) + { + var valids = filePaths.Where(ValidFile).ToList(); + return valids; + } + + private static bool ValidFile(string filePath) + { + try + { + return ValidateFileName(filePath) && ValidateFilePath(filePath); + } + catch (Exception) + { + return false; + } + } + + private static bool ValidateFileName(string filePath) + { + var invalidFileNameChars = Path.GetInvalidFileNameChars(); + var fileName = Path.GetFileName(filePath); + + return !string.IsNullOrEmpty(fileName) && + !fileName.Any(s => invalidFileNameChars.Contains(s)); + } + + private static bool ValidateFilePath(string filePath) + { + string pathWithoutSeparator = filePath + .Replace(Path.DirectorySeparatorChar.ToString(), string.Empty) + .Replace(Path.AltDirectorySeparatorChar.ToString(), string.Empty) + .Replace(Path.VolumeSeparatorChar.ToString(), string.Empty); + var invalidPathChars = Path.GetInvalidPathChars(); + + return !string.IsNullOrEmpty(pathWithoutSeparator) && + !pathWithoutSeparator.Any(invalidPathChars.Contains); + } + + private static void placebolder() + { + + //string pathWithoutSeparator = filePath + // .Replace(Path.DirectorySeparatorChar.ToString(), string.Empty) + // .Replace(Path.AltDirectorySeparatorChar.ToString(), string.Empty) + // .Replace(Path.VolumeSeparatorChar.ToString(), string.Empty); + //var invalidPathChars = Path.GetInvalidPathChars(); + //var invalidFileNameChars = Path.GetInvalidFileNameChars(); + //var fileName = Path.GetFileName(filePath); + //return !string.IsNullOrEmpty(filePath) && + // !pathWithoutSeparator.Any(invalidPathChars.Contains) && + // !fileName.Any(invalidFileNameChars.Contains); + + //var invalidCharacters = Path.GetInvalidFileNameChars(); + + //var emp = !string.IsNullOrEmpty(filePath); + //var invalids = !filePath.Any(invalidCharacters.Contains); + //var valid = !string.IsNullOrEmpty(filePath) && !filePath.Any(invalidCharacters.Contains); + + //return !string.IsNullOrEmpty(filePath) && + // !filePath.Any(invalidCharacters.Contains); + } + } +} diff --git a/Tests/TechTalk.SpecFlow.GeneratorTests/FileFilterTests.cs b/Tests/TechTalk.SpecFlow.GeneratorTests/FileFilterTests.cs new file mode 100644 index 000000000..8d80cbdf1 --- /dev/null +++ b/Tests/TechTalk.SpecFlow.GeneratorTests/FileFilterTests.cs @@ -0,0 +1,71 @@ +using System.Collections.Generic; +using System.Linq; +using FluentAssertions; +using TechTalk.SpecFlow.Assist; +using Xunit; + +namespace TechTalk.SpecFlow.GeneratorTests +{ + public class FileFilterTests + { + private readonly List _validFeatureFilePaths; + + public FileFilterTests() + { + _validFeatureFilePaths = new List() + { + @"Features\SpecFlowFeature.feature", + @"Features\Math.feature", + @"Features\見.feature" + }; + } + + [Fact] + public void ShouldReturnValidFilePaths() + { + var validatedFilePaths = FileFilter.GetValidFiles(_validFeatureFilePaths); + validatedFilePaths.Should().BeEquivalentTo(_validFeatureFilePaths); + } + + [Fact] + public void ShouldRemoveInvalidFilePaths() + { + var notValidFeatureFilePaths = new List() + { + @"Features\SpecFlowFeature*.feature", + @"Features\Math?.feature", + @"Features\Project|Impossible.feature" + }; + + var featureFilePaths = _validFeatureFilePaths.Concat(notValidFeatureFilePaths).ToList(); + + var validatedPaths = FileFilter.GetValidFiles(featureFilePaths); + validatedPaths.Should().BeEquivalentTo(_validFeatureFilePaths); + } + + [Fact] + public void ShouldRemoveWildChars() + { + var wildCards = new List() + { + @"**\*.feature" + }; + + var featureFilePaths = _validFeatureFilePaths.Concat(wildCards).ToList(); + var validatedPaths = FileFilter.GetValidFiles(featureFilePaths); + validatedPaths.Should().BeEquivalentTo(_validFeatureFilePaths); + } + + [Fact] + public void ShouldBeAnEmptyListIfOnlyWildCards() + { + var wildCards = new List() + { + @"**\*.feature" + }; + + var validatedPaths = FileFilter.GetValidFiles(wildCards); + validatedPaths.Should().BeEmpty(); + } + } +}