Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove files with invalid characters from the FeatureFiles list (provided by msbuild) #1961

Merged
merged 6 commits into from
Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using BoDi;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using TechTalk.SpecFlow.Generator;
using TechTalk.SpecFlow.Generator.Interfaces;
using TechTalk.SpecFlow.Generator.Project;
Expand All @@ -20,6 +23,7 @@ public FeatureCodeBehindGenerator(ITestGenerator testGenerator)
public TestFileGeneratorResult GenerateCodeBehindFile(string featureFile)
{
var featureFileInput = new FeatureFileInput(featureFile);

var generatedFeatureFileName = Path.GetFileName(_testGenerator.GetTestFullPath(featureFileInput));

var testGeneratorResult = _testGenerator.GenerateTestFile(featureFileInput, new GenerationSettings());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public override bool Execute()
var generateFeatureFileCodeBehindTaskContainerBuilder = new GenerateFeatureFileCodeBehindTaskContainerBuilder();
var generatorPlugins = GeneratorPlugins?.Select(gp => gp.ItemSpec).Select(p => new GeneratorPluginInfo(p)).ToArray() ?? Array.Empty<GeneratorPluginInfo>();
var featureFiles = FeatureFiles?.Select(i => i.ItemSpec).ToArray() ?? Array.Empty<string>();

var msbuildInformationProvider = new MSBuildInformationProvider(MSBuildVersion);
var generateFeatureFileCodeBehindTaskConfiguration = new GenerateFeatureFileCodeBehindTaskConfiguration(AnalyticsTransmitter, CodeBehindGenerator);
var generateFeatureFileCodeBehindTaskInfo = new SpecFlowProjectInfo(generatorPlugins, featureFiles, ProjectPath, ProjectFolder, ProjectGuid, AssemblyName, OutputPath, RootNamespace, TargetFrameworks, TargetFramework);
Expand Down
4 changes: 3 additions & 1 deletion SpecFlow.Tools.MsBuild.Generation/SpecFlowProjectInfo.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using TechTalk.SpecFlow.Generator;
using TechTalk.SpecFlow.Utils;

namespace SpecFlow.Tools.MsBuild.Generation
{
Expand All @@ -18,7 +19,7 @@ public SpecFlowProjectInfo(
string currentTargetFramework)
{
GeneratorPlugins = generatorPlugins;
FeatureFiles = featureFiles;
FeatureFiles = FileFilter.GetValidFiles(featureFiles);
ProjectFolder = projectFolder;
OutputPath = outputPath;
RootNamespace = rootNamespace;
Expand Down Expand Up @@ -48,5 +49,6 @@ public SpecFlowProjectInfo(
public string TargetFrameworks { get; }

public string CurrentTargetFramework { get; }

}
}
50 changes: 50 additions & 0 deletions TechTalk.SpecFlow.Utils/FileFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace TechTalk.SpecFlow.Utils
{
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);
}

}
}
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.Utils;
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();
}
}
}
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Fixes:
+ Update the default C# skeleton template to use context injection instead of the deprecated ScenarioContext.Current
+ Save generated files with UTF8-BOM encoding.
+ Revert "Replace NUnit.Framework.DescriptionAttribute to TestName in NUnit generator #1225" because of problems with the NUnit Test Adapter
+ Remove files with invalid characters from the FeatureFiles list (provided by msbuild)

Features:
+ Created VerifyAllColumnsBound check for `table.CreateSet` and `table.CreateInstance`
Expand Down