• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lucaslorentz / minicover / #156

pending completion
#156

push

azure-devops

web-flow
Merge 64be0ef38 into c4f476c4c

6 of 6 new or added lines in 1 file covered. (100.0%)

902 of 2589 relevant lines covered (34.84%)

7672.37 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/MiniCover/CommandLine/Commands/InstrumentCommand.cs
1
using System;
2
using System.Collections.Generic;
3
using System.IO;
4
using System.IO.Abstractions;
5
using System.Linq;
6
using System.Threading.Tasks;
7
using Microsoft.Extensions.DependencyInjection;
8
using MiniCover.CommandLine;
9
using MiniCover.CommandLine.Options;
10
using MiniCover.Core.Instrumentation;
11
using MiniCover.Core.Model;
12
using MiniCover.Exceptions;
13
using Newtonsoft.Json;
14

15
namespace MiniCover.Commands
16
{
17
    public class InstrumentCommand : ICommand
18
    {
19
        private readonly IServiceProvider _serviceProvider;
20
        private readonly VerbosityOption _verbosityOption;
21
        private readonly WorkingDirectoryOption _workingDirectoryOption;
22
        private readonly ParentDirectoryOption _parentDirOption;
23
        private readonly IncludeAssembliesPatternOption _includeAssembliesOption;
24
        private readonly ExcludeAssembliesPatternOption _excludeAssembliesOption;
25
        private readonly IncludeSourcesPatternOption _includeSourceOption;
26
        private readonly ExcludeSourcesPatternOption _excludeSourceOption;
27
        private readonly IncludeTestsPatternOption _includeTestsOption;
28
        private readonly ExcludeTestsPatternOption _excludeTestsOption;
29
        private readonly HitsDirectoryOption _hitsDirectoryOption;
30
        private readonly CoverageFileOption _coverageFileOption;
31
        private readonly IInstrumenter _instrumenter;
32

33
        public InstrumentCommand(IServiceProvider serviceProvider,
×
34
            VerbosityOption verbosityOption,
×
35
            WorkingDirectoryOption workingDirectoryOption,
×
36
            ParentDirectoryOption parentDirOption,
×
37
            IncludeAssembliesPatternOption includeAssembliesOption,
×
38
            ExcludeAssembliesPatternOption excludeAssembliesOption,
×
39
            IncludeSourcesPatternOption includeSourceOption,
×
40
            ExcludeSourcesPatternOption excludeSourceOption,
×
41
            IncludeTestsPatternOption includeTestsOption,
×
42
            ExcludeTestsPatternOption excludeTestsOption,
×
43
            HitsDirectoryOption hitsDirectoryOption,
×
44
            CoverageFileOption coverageFileOption,
×
45
            IInstrumenter instrumenter)
×
46
        {
47
            _serviceProvider = serviceProvider;
×
48
            _verbosityOption = verbosityOption;
×
49
            _workingDirectoryOption = workingDirectoryOption;
×
50
            _parentDirOption = parentDirOption;
×
51
            _includeAssembliesOption = includeAssembliesOption;
×
52
            _excludeAssembliesOption = excludeAssembliesOption;
×
53
            _includeSourceOption = includeSourceOption;
×
54
            _excludeSourceOption = excludeSourceOption;
×
55
            _includeTestsOption = includeTestsOption;
×
56
            _excludeTestsOption = excludeTestsOption;
×
57
            _hitsDirectoryOption = hitsDirectoryOption;
×
58
            _coverageFileOption = coverageFileOption;
×
59
            _instrumenter = instrumenter;
×
60
        }
61

62
        public string CommandName => "instrument";
×
63
        public string CommandDescription => "Instrument assemblies";
×
64
        public IOption[] Options => new IOption[]
×
65
        {
×
66
            _verbosityOption,
×
67
            _workingDirectoryOption,
×
68
            _parentDirOption,
×
69
            _includeAssembliesOption,
×
70
            _excludeAssembliesOption,
×
71
            _includeSourceOption,
×
72
            _excludeSourceOption,
×
73
            _includeTestsOption,
×
74
            _excludeTestsOption,
×
75
            _hitsDirectoryOption,
×
76
            _coverageFileOption
×
77
        };
×
78

79
        public Task<int> Execute()
80
        {
81
            var assemblies = GetFiles(_includeAssembliesOption.Value, _excludeAssembliesOption.Value, _parentDirOption.DirectoryInfo);
×
82
            if (assemblies.Length == 0)
×
83
                throw new ValidationException("No assemblies found");
×
84

85
            var sourceFiles = GetFiles(_includeSourceOption.Value, _excludeSourceOption.Value, _parentDirOption.DirectoryInfo);
×
86
            if (sourceFiles.Length == 0)
×
87
                throw new ValidationException("No source files found");
×
88

89
            var testFiles = GetFiles(_includeTestsOption.Value, _excludeTestsOption.Value, _parentDirOption.DirectoryInfo);
×
90

91
            var instrumentationContext = new FileBasedInstrumentationContext
×
92
            {
×
93
                Assemblies = assemblies,
×
94
                HitsPath = _hitsDirectoryOption.DirectoryInfo.FullName,
×
95
                Sources = sourceFiles,
×
96
                Tests = testFiles,
×
97
                Workdir = _workingDirectoryOption.DirectoryInfo
×
98
            };
×
99

100
            var result = _instrumenter.Instrument(instrumentationContext);
×
101

102
            var coverageFile = _coverageFileOption.FileInfo;
×
103
            SaveCoverageFile(coverageFile, result);
×
104

105
            return Task.FromResult(0);
×
106
        }
107

108
        private static IFileInfo[] GetFiles(
109
            IEnumerable<string> includes,
110
            IEnumerable<string> excludes,
111
            IDirectoryInfo parentDir)
112
        {
113
            var matcher = new Microsoft.Extensions.FileSystemGlobbing.Matcher();
×
114

115
            foreach (var include in includes)
×
116
            {
117
                matcher.AddInclude(include);
×
118
            }
119

120
            foreach (var exclude in excludes)
×
121
            {
122
                matcher.AddExclude(exclude);
×
123
            }
124

125
            var fileMatchResult = matcher.Execute(new Microsoft.Extensions.FileSystemGlobbing.Abstractions.DirectoryInfoWrapper(new DirectoryInfo(parentDir.FullName)));
×
126

127
            return fileMatchResult.Files
×
128
                .Select(f => parentDir.FileSystem.FileInfo.FromFileName(Path.GetFullPath(Path.Combine(parentDir.ToString(), f.Path))))
×
129
                .ToArray();
×
130
        }
131

132
        private static void SaveCoverageFile(IFileInfo coverageFile, InstrumentationResult result)
133
        {
134
            var settings = new JsonSerializerSettings
×
135
            {
×
136
                PreserveReferencesHandling = PreserveReferencesHandling.Objects
×
137
            };
×
138
            var json = JsonConvert.SerializeObject(result, Formatting.Indented, settings);
×
139
            File.WriteAllText(coverageFile.FullName, json);
×
140
        }
141
    }
142
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc