I need to run a specific test depending on which file the changes were in.
I have 3 tasks:
- task: Cake@2
displayName: Restore FE and Sitecore modules
inputs:
script: "$(Build.Repository.LocalPath)/src/build.cake"
target: "001-Restore"
verbosity: "Quiet"
Version: "1.3.0"
- task: Cake@2
displayName: Generate and Build FE and Sitecore
inputs:
script: "$(Build.Repository.LocalPath)/src/build.cake"
target: "002-Build"
verbosity: "Quiet"
arguments: '--BuildConfiguration "Release" --ScSiteUrl $(IntEnvUrl)'
Version: "1.3.0"
- task: Cake@2
displayName: Run Unit tests
inputs:
script: "$(Build.Repository.LocalPath)/src/build.cake"
target: "003-Tests"
verbosity: "Quiet"
Version: "1.3.0"
The tests are written in the build.cake file, they look like this:
Task("001-Restore")
.IsDependentOn("Server :: Restore")
.IsDependentOn("Client :: Restore")
;
Task("Server :: Restore")
.Does(() =>
{
NuGetRestore($"{SolutionName}.sln");
})
;
Task("Client :: Restore")
.Does(() =>
{
NpmInstall();
var settings = new NpmInstallSettings();
settings.LogLevel = NpmLogLevel.Error;
settings.FromPath(ClientFolderPath);
settings.WithForce(true);
NpmInstall(settings);
})
;
Task("002-Build")
.IsDependentOn("Server :: Build")
.IsDependentOn("Client :: Build")
;
Task("Server :: Build")
.Does(() =>
{
var msBuildConfig = new MSBuildSettings()
.SetConfiguration(BuildConfiguration)
.SetVerbosity(Verbosity.Minimal)
.UseToolVersion(MsBuildToolVersion)
.SetMaxCpuCount(0)
.SetNodeReuse(false)
.WithTarget("Rebuild")
.WithProperty("Retries", "10")
.WithProperty("RetryDelayMilliseconds", "10000")
.WithProperty("BuildInParallel", "true");
MSBuild($"{SolutionName}.sln", msBuildConfig);
})
;
Task("Client :: Build")
.Does(() =>
{
var setupSettings = new NpmRunScriptSettings();
setupSettings.ScriptName = $"jss";
setupSettings.LogLevel = NpmLogLevel.Error;
setupSettings.FromPath(SrcDir + ClientFolderPath);
setupSettings
.WithArguments("setup")
.WithArguments("--nonInteractive")
.WithArguments("--skipValidation")
.WithArguments("--deployUrl " + ScSiteUrl + "sitecore/api/jss/import")
.WithArguments("--deploySecret " + ClientDeploySecret)
.WithArguments("--layoutServiceHost " + ScSiteUrl)
.WithArguments("--apiKey " + JssApiKey);
NpmRunScript(setupSettings);
})
.Does(() =>
{
var buildSettings = new NpmRunScriptSettings();
buildSettings.ScriptName = $"build";
buildSettings.LogLevel = NpmLogLevel.Verbose;
buildSettings.FromPath(SrcDir + ClientFolderPath);
NpmRunScript(buildSettings);
})
;
Task("003-Tests")
.IsDependentOn("Server :: Tests")
//.IsDependentOn("Client :: Tests")
;
Task("Server :: Tests")
.Does(() =>
{
var coverSettings = new OpenCoverSettings()
.WithFilter($"+[{SolutionName}.*]*")
.WithFilter($"-[{SolutionName}.*.Tests*]*");
coverSettings.SkipAutoProps = true;
coverSettings.Register = "Path64";
coverSettings.MergeByHash = true;
coverSettings.NoDefaultFilters = true;
coverSettings.ReturnTargetCodeOffset = 0;
void applyExclude<T>(ISet<T> filtersSet, string paramValue, Func<string, T> mapper)
{
if (!string.IsNullOrEmpty(paramValue))
{
var excludes = paramValue.Split(',').Select(mapper);
filtersSet.UnionWith(excludes);
}
}
applyExclude(coverSettings.ExcludedAttributeFilters, "*ExcludeFromCodeCoverage*", x => x);
applyExclude(coverSettings.ExcludedFileFilters, "*.Generated.cs;*\\App_Start\\*", x => x);
var directories = GetDirectories(
$"{SrcDir}/**/bin",
new GlobberSettings { Predicate = fileSystemInfo => !fileSystemInfo.Path.FullPath.EndsWith("node_modules", StringComparison.OrdinalIgnoreCase) }
);
foreach (var directory in directories)
{
coverSettings.SearchDirectories.Add(directory);
}
var OutputDirectory = $"{ReportTargetDir}/tests/";
var xUnitTestsCoverageOutputDir = $"{OutputDirectory}/xUnit";
EnsureDirectoryExists(xUnitTestsCoverageOutputDir);
var openCoverResultsFilePath = new FilePath($"{xUnitTestsCoverageOutputDir}/coverage.xml");
var xUnit2Settings = new XUnit2Settings {
XmlReport = true,
Parallelism = ParallelismOption.None,
NoAppDomain = false,
OutputDirectory = OutputDirectory,
ReportName = "xUnitTestResults",
ShadowCopy = false
};
OpenCover(
tool => { tool.XUnit2($"{SrcDir}/**/tests/bin/*.Tests.dll", xUnit2Settings); },
openCoverResultsFilePath,
coverSettings
);
ReportGenerator(openCoverResultsFilePath, xUnitTestsCoverageOutputDir);
var converterExecutablePath = Context.Tools.Resolve("OpenCoverToCoberturaConverter.exe");
StartProcess(converterExecutablePath, new ProcessSettings {
Arguments = new ProcessArgumentBuilder()
.Append($"-input:\"{openCoverResultsFilePath}\"")
.Append($"-output:\"{xUnitTestsCoverageOutputDir}/cobertura-coverage.xml\"")
});
})
.OnError(exception =>
{
Error(exception);
})
;
Task("Client :: Tests")
.Does(() =>
{
var settings = new NpmRunScriptSettings();
settings.ScriptName = $"test";
settings.LogLevel = NpmLogLevel.Error;
settings.FromPath(SrcDir + ClientFolderPath);
NpmRunScript(settings);
})
;
If a file from the backend has been changed, then you need to call not both tests, but only : ".Depends On("Server :: Restore")"
And if the file in the frontend has been changed, then call the test: "Depends On ("Client :: Restore")"
As I understand it, I need to make conditions for all three tasks in the pipeline.