Esempio n. 1
0
        public void InstrumentType(
            InstrumentationContext context,
            TypeDefinition typeDefinition,
            InstrumentedAssembly instrumentedAssembly)
        {
            var typeDocuments = typeDefinition.GetAllDocuments();

            if (!typeDocuments.Any(d => context.IsSource(d) || context.IsTest(d)))
            {
                return;
            }

            var methods = typeDefinition.GetAllMethods();

            foreach (var methodDefinition in methods)
            {
                var methodDocuments = methodDefinition.GetAllDocuments();

                var isSource = methodDocuments.Any(d => context.IsSource(d));
                var isTest   = methodDocuments.Any(d => context.IsTest(d));

                if (!isSource && !isTest)
                {
                    continue;
                }

                _methodInstrumenter.InstrumentMethod(
                    context,
                    isSource,
                    methodDefinition,
                    instrumentedAssembly);
            }
        }
Esempio n. 2
0
        public void InstrumentType(
            InstrumentationContext context,
            TypeDefinition typeDefinition,
            InstrumentedAssembly instrumentedAssembly)
        {
            foreach (var methodDefinition in typeDefinition.Methods)
            {
                if (!methodDefinition.HasBody || !methodDefinition.DebugInformation.HasSequencePoints)
                {
                    continue;
                }

                var methodDocuments = methodDefinition.GetAllDocuments();

                var isSource = methodDocuments.Any(d => context.IsSource(d.Url));
                var isTest   = methodDocuments.Any(d => context.IsTest(d.Url));

                if (!isSource && !isTest)
                {
                    continue;
                }

                _methodInstrumenter.InstrumentMethod(
                    context,
                    isSource,
                    methodDefinition,
                    instrumentedAssembly);
            }

            foreach (var nestedType in typeDefinition.NestedTypes)
            {
                InstrumentType(context, nestedType, instrumentedAssembly);
            }
        }
Esempio n. 3
0
        private InstrumentedAssembly InstrumentAssemblyDefinition(
            InstrumentationContext context,
            AssemblyDefinition assemblyDefinition)
        {
            if (assemblyDefinition.CustomAttributes.Any(a => a.AttributeType.Name == "InstrumentedAttribute"))
            {
                _logger.LogInformation("Already instrumented");
                return(null);
            }

            var assemblyDocuments = assemblyDefinition.GetAllDocuments();

            if (!assemblyDocuments.Any(d => context.IsSource(d.Url) || context.IsTest(d.Url)))
            {
                _logger.LogInformation("No link to source files or test files");
                return(null);
            }

            var changedDocuments = assemblyDocuments.Where(d => d.FileHasChanged()).ToArray();

            if (changedDocuments.Any())
            {
                if (_logger.IsEnabled(LogLevel.Debug))
                {
                    var changedFiles = changedDocuments.Select(d => d.Url).Distinct().ToArray();
                    _logger.LogDebug("Source files has changed: {changedFiles}", new object[] { changedFiles });
                }
                else
                {
                    _logger.LogInformation("Source files has changed");
                }
                return(null);
            }

            _logger.LogInformation("Instrumenting");

            var instrumentedAssembly           = new InstrumentedAssembly(assemblyDefinition.Name.Name);
            var instrumentedAttributeReference = assemblyDefinition.MainModule.ImportReference(instrumentedAttributeConstructor);

            assemblyDefinition.CustomAttributes.Add(new CustomAttribute(instrumentedAttributeReference));

            foreach (var type in assemblyDefinition.MainModule.GetTypes())
            {
                _typeInstrumenter.InstrumentType(
                    context,
                    type,
                    instrumentedAssembly);
            }

            var miniCoverTempPath = GetMiniCoverTempPath();

            var instrumentedAssemblyFile = new FileInfo(Path.Combine(miniCoverTempPath, $"{Guid.NewGuid()}.dll"));
            var instrumentedPdbFile      = FileUtils.GetPdbFile(instrumentedAssemblyFile);

            assemblyDefinition.Write(instrumentedAssemblyFile.FullName, new WriterParameters {
                WriteSymbols = true
            });

            instrumentedAssembly.TempAssemblyFile = instrumentedAssemblyFile.FullName;
            instrumentedAssembly.TempPdbFile      = instrumentedPdbFile.FullName;

            return(instrumentedAssembly);
        }
Esempio n. 4
0
        public InstrumentedAssembly InstrumentAssembly(
            InstrumentationContext context,
            FileInfo assemblyFile)
        {
            var assemblyDirectory = assemblyFile.Directory;

            using (_logger.BeginScope("Checking assembly file {assembly}", assemblyFile.FullName, LogLevel.Information))
            {
                if (assemblyFile.Name == "MiniCover.HitServices.dll")
                {
                    _logger.LogInformation("Skipping HitServices");
                    return(null);
                }

                if (_loadedAssemblyFiles.Contains(assemblyFile.FullName))
                {
                    _logger.LogInformation("Can't instrument loaded assembly");
                    return(null);
                }

                var resolver = new CustomAssemblyResolver(assemblyDirectory, _assemblyResolverLogger);

                _logger.LogTrace("Assembly resolver search directories: {directories}", new object[] { resolver.GetSearchDirectories() });

                using (var assemblyDefinition = AssemblyDefinition.ReadAssembly(assemblyFile.FullName, new ReaderParameters {
                    ReadSymbols = true, AssemblyResolver = resolver
                }))
                {
                    if (assemblyDefinition.CustomAttributes.Any(a => a.AttributeType.Name == "InstrumentedAttribute"))
                    {
                        _logger.LogInformation("Already instrumented");
                        return(null);
                    }

                    var assemblyDocuments = assemblyDefinition.GetAllDocuments();
                    if (!assemblyDocuments.Any(d => context.IsSource(d) || context.IsTest(d)))
                    {
                        _logger.LogInformation("No link to source files or test files");
                        return(null);
                    }

                    _logger.LogInformation("Instrumenting");

                    var instrumentedAssembly           = new InstrumentedAssembly(assemblyDefinition.Name.Name);
                    var instrumentedAttributeReference = assemblyDefinition.MainModule.ImportReference(instrumentedAttributeConstructor);
                    assemblyDefinition.CustomAttributes.Add(new CustomAttribute(instrumentedAttributeReference));

                    foreach (var type in assemblyDefinition.MainModule.GetTypes())
                    {
                        _typeInstrumenter.InstrumentType(
                            context,
                            type,
                            instrumentedAssembly);
                    }

                    var miniCoverTempPath = GetMiniCoverTempPath();

                    var instrumentedAssemblyFile = new FileInfo(Path.Combine(miniCoverTempPath, $"{Guid.NewGuid()}.dll"));
                    var instrumentedPdbFile      = FileUtils.GetPdbFile(instrumentedAssemblyFile);

                    assemblyDefinition.Write(instrumentedAssemblyFile.FullName, new WriterParameters {
                        WriteSymbols = true
                    });

                    instrumentedAssembly.TempAssemblyFile = instrumentedAssemblyFile.FullName;
                    instrumentedAssembly.TempPdbFile      = instrumentedPdbFile.FullName;

                    return(instrumentedAssembly);
                }
            }
        }