示例#1
0
        private string MergeCodeCoverageFiles(IList <string> files)
        {
            string fileName       = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + CoverageFileExtension);
            string outputfileName = files[0];

            File.Create(fileName).Dispose();
            var assemblyPath = Path.Combine(Path.GetDirectoryName(typeof(CodeCoverageDataAttachmentsHandler).GetTypeInfo().Assembly.GetAssemblyLocation()), CodeCoverageAnalysisAssemblyName + ".dll");

            try
            {
                Assembly assembly = new PlatformAssemblyLoadContext().LoadAssemblyFromPath(assemblyPath);
                var      type     = assembly.GetType(CodeCoverageAnalysisAssemblyName + "." + CoverageInfoTypeName);

                var methodInfo = type?.GetMethod(MergeMethodName);

                if (methodInfo != null)
                {
                    for (int i = 1; i < files.Count; i++)
                    {
                        methodInfo.Invoke(null, new object[] { files[i], outputfileName, fileName, true });
                        File.Copy(fileName, outputfileName, true);

                        File.Delete(files[i]);
                    }

                    File.Delete(fileName);
                }

                return(outputfileName);
            }
            catch (Exception ex)
            {
                if (EqtTrace.IsErrorEnabled)
                {
                    EqtTrace.Error("CodeCoverageDataCollectorAttachmentsHandler: Failed to load datacollector of type : {0} from location : {1}. Error : {2}", CodeCoverageAnalysisAssemblyName, assemblyPath, ex.ToString());
                }
            }

            return(string.Empty);
        }
        private async Task <string> MergeCodeCoverageFilesAsync(IList <string> files, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            var assemblyPath = Path.Combine(Path.GetDirectoryName(typeof(CodeCoverageDataAttachmentsHandler).GetTypeInfo().Assembly.GetAssemblyLocation()), CodeCoverageCoreLibNetAssemblyName + ".dll");

            // Get assembly, type and methods
            Assembly assembly        = new PlatformAssemblyLoadContext().LoadAssemblyFromPath(assemblyPath);
            var      classType       = assembly.GetType($"{CodeCoverageCoreLibNetAssemblyName}.{CoverageFileUtilityTypeName}");
            var      classInstance   = Activator.CreateInstance(classType);
            var      mergeMethodInfo = classType?.GetMethod(MergeMethodName, new[] { typeof(IList <string>), typeof(CancellationToken) });
            var      writeMethodInfo = classType?.GetMethod(WriteMethodName);

            // Invoke methods
            var task = (Task)mergeMethodInfo.Invoke(classInstance, new object[] { files, cancellationToken });
            await task.ConfigureAwait(false);

            var coverageData = task.GetType().GetProperty("Result").GetValue(task, null);

            writeMethodInfo.Invoke(classInstance, new object[] { files[0], coverageData });

            // Delete original files and keep merged file only
            foreach (var file in files.Skip(1))
            {
                try
                {
                    File.Delete(file);
                }
                catch (Exception ex)
                {
                    EqtTrace.Error($"CodeCoverageDataCollectorAttachmentsHandler: Failed to remove {file}. Error: {ex}");
                }
            }

            return(files[0]);
        }
示例#3
0
        private string MergeCodeCoverageFiles(IList <string> files, IProgress <int> progressReporter, CancellationToken cancellationToken)
        {
            if (files.Count == 1)
            {
                return(files[0]);
            }

            string tempFileName   = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + CoverageFileExtension);
            string outputfileName = files[0];

            File.Create(tempFileName).Dispose();
            var assemblyPath = Path.Combine(Path.GetDirectoryName(typeof(CodeCoverageDataAttachmentsHandler).GetTypeInfo().Assembly.GetAssemblyLocation()), CodeCoverageAnalysisAssemblyName + ".dll");

            try
            {
                cancellationToken.ThrowIfCancellationRequested();
                Assembly assembly = new PlatformAssemblyLoadContext().LoadAssemblyFromPath(assemblyPath);
                var      type     = assembly.GetType(CodeCoverageAnalysisAssemblyName + "." + CoverageInfoTypeName);

                var methodInfo = type?.GetMethod(MergeMethodName);

                if (methodInfo != null)
                {
                    IList <string> filesToDelete = new List <string>(files.Count)
                    {
                        tempFileName
                    };

                    for (int i = 1; i < files.Count; i++)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        progressReporter?.Report(100 * i / files.Count);

                        cancellationToken.ThrowIfCancellationRequested();
                        methodInfo.Invoke(null, new object[] { files[i], outputfileName, tempFileName, true });

                        cancellationToken.ThrowIfCancellationRequested();
                        File.Copy(tempFileName, outputfileName, true);

                        filesToDelete.Add(files[i]);
                    }

                    cancellationToken.ThrowIfCancellationRequested();
                    foreach (string fileName in filesToDelete)
                    {
                        File.Delete(fileName);
                    }
                }

                progressReporter?.Report(100);
                return(outputfileName);
            }
            catch (OperationCanceledException)
            {
                if (EqtTrace.IsWarningEnabled)
                {
                    EqtTrace.Warning("CodeCoverageDataCollectorAttachmentsHandler: operation was cancelled.");
                }
                throw;
            }
            catch (Exception ex)
            {
                if (EqtTrace.IsErrorEnabled)
                {
                    EqtTrace.Error("CodeCoverageDataCollectorAttachmentsHandler: Failed to load datacollector of type : {0} from location : {1}. Error : {2}", CodeCoverageAnalysisAssemblyName, assemblyPath, ex.ToString());
                }
            }

            return(string.Empty);
        }