Beispiel #1
0
        public override async Task ExecuteAsync(IOperationExecutionContext context)
        {
            var vsTestPath = await this.GetVsTestPathAsync(context);

            if (string.IsNullOrEmpty(vsTestPath))
            {
                return;
            }

            var fileOps = await context.Agent.GetServiceAsync <IFileOperationsExecuter>();

            var containerPath   = context.ResolvePath(this.TestContainer);
            var sourceDirectory = PathEx.GetDirectoryName(containerPath);
            var resultsPath     = PathEx.Combine(sourceDirectory, "TestResults");

            if (this.ClearExistingTestResults)
            {
                this.LogDebug($"Clearing {resultsPath} directory...");
                await fileOps.ClearDirectoryAsync(resultsPath);
            }

            await this.ExecuteCommandLineAsync(
                context,
                new RemoteProcessStartInfo
            {
                FileName         = vsTestPath,
                Arguments        = $"\"{containerPath}\" /logger:trx {this.AdditionalArguments}",
                WorkingDirectory = sourceDirectory
            }
                );

            if (!await fileOps.DirectoryExistsAsync(resultsPath))
            {
                this.LogError("Could not find the generated \"TestResults\" directory after running unit tests at: " + sourceDirectory);
                return;
            }

            var trxFiles = (await fileOps.GetFileSystemInfosAsync(resultsPath, new MaskingContext(new[] { "*.trx" }, Enumerable.Empty <string>())))
                           .OfType <SlimFileInfo>()
                           .ToList();

            if (trxFiles.Count == 0)
            {
                this.LogError("There are no .trx files in the \"TestResults\" directory.");
                return;
            }

            var trxPath = trxFiles
                          .Aggregate((latest, next) => next.LastWriteTimeUtc > latest.LastWriteTimeUtc ? next : latest)
                          .FullName;

            await context.RecordUnitTestResultsAsync(trxPath, this.TestGroup);
        }