Skip to content

Commit

Permalink
Remove invalid files from FeatureFiles list
Browse files Browse the repository at this point in the history
  • Loading branch information
epresi committed Apr 28, 2020
1 parent 9b473ab commit 22e9959
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 17 deletions.
19 changes: 2 additions & 17 deletions SpecFlow.Tools.MsBuild.Generation/SpecFlowProjectInfo.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -52,18 +50,5 @@ public SpecFlowProjectInfo(

public string CurrentTargetFramework { get; }

private IReadOnlyCollection<string> RemoveFilesWithInvalidChars(IEnumerable<string> 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));
}
}
}
73 changes: 73 additions & 0 deletions TechTalk.SpecFlow/Assist/FileFilter.cs
Original file line number Diff line number Diff line change
@@ -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<string> GetValidFiles(IReadOnlyCollection<string> 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);
}
}
}
71 changes: 71 additions & 0 deletions Tests/TechTalk.SpecFlow.GeneratorTests/FileFilterTests.cs
Original file line number Diff line number Diff line change
@@ -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<string> _validFeatureFilePaths;

public FileFilterTests()
{
_validFeatureFilePaths = new List<string>()
{
@"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<string>()
{
@"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<string>()
{
@"**\*.feature"
};

var featureFilePaths = _validFeatureFilePaths.Concat(wildCards).ToList();
var validatedPaths = FileFilter.GetValidFiles(featureFilePaths);
validatedPaths.Should().BeEquivalentTo(_validFeatureFilePaths);
}

[Fact]
public void ShouldBeAnEmptyListIfOnlyWildCards()
{
var wildCards = new List<string>()
{
@"**\*.feature"
};

var validatedPaths = FileFilter.GetValidFiles(wildCards);
validatedPaths.Should().BeEmpty();
}
}
}

0 comments on commit 22e9959

Please sign in to comment.