-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathRunnerProgram.cs
More file actions
232 lines (211 loc) · 10.2 KB
/
Copy pathRunnerProgram.cs
File metadata and controls
232 lines (211 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
using System;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.IO;
using System.Reflection;
namespace VSharp.Runner
{
public static class RunnerProgram
{
private static Assembly ResolveAssembly(FileInfo assemblyPath)
{
try
{
return Assembly.LoadFrom(assemblyPath.FullName);
}
catch (Exception)
{
Console.Error.WriteLine("I did not found assembly {0}", assemblyPath.FullName);
return null;
}
}
private static Type ResolveType(Assembly assembly, string classArgumentValue)
{
if (classArgumentValue == null)
{
Console.Error.WriteLine("Specified class can not be null");
return null;
}
var specificClass = assembly.GetType(classArgumentValue);
if (specificClass == null)
{
Console.Error.WriteLine("I did not found type you specified {0} in assembly {1}", classArgumentValue,
assembly.Location);
return null;
}
return specificClass;
}
private static MethodBase ResolveMethod(Assembly assembly, string methodArgumentValue)
{
if (methodArgumentValue == null)
{
Console.Error.WriteLine("Specified method can not be null");
return null;
}
MethodBase method = null;
int metadataToken;
if (Int32.TryParse(methodArgumentValue, out metadataToken))
{
foreach (var module in assembly.GetModules())
{
try
{
var nullOrMethod = module.ResolveMethod(metadataToken);
if (nullOrMethod == null) continue;
method = nullOrMethod;
break;
}
catch (Exception)
{
// ignored
}
}
if (method == null)
{
Console.Error.WriteLine("I did not found method you specified by token {0} in assembly {1}",
metadataToken, assembly.Location);
return null;
}
}
else
{
foreach (var type in assembly.GetTypes())
{
try
{
var nullOrMethod = type.GetMethod(methodArgumentValue, Reflection.allBindingFlags);
if (nullOrMethod == null) continue;
method = nullOrMethod;
break;
}
catch (Exception)
{
// ignored
}
}
if (method == null)
{
Console.Error.WriteLine("I did not found method you specified by name {0} in assembly {1}",
methodArgumentValue, assembly.Location);
return null;
}
}
return method;
}
private static void PostProcess(Statistics statistics)
{
statistics.GenerateReport(Console.Out);
}
public static int Main(string[] args)
{
var assemblyPathArgument =
new Argument<FileInfo>("assembly-path", description: "Path to the target assembly");
var outputOption =
new Option<DirectoryInfo>(aliases: new[] { "--output", "-o" },
() => new DirectoryInfo(Directory.GetCurrentDirectory()),
"Path where unit tests will be generated");
var concreteArguments =
new Argument<string[]>("args", description: "Command line arguments");
var unknownArgsOption =
new Option("--unknown-args", description: "Force engine to generate various input console arguments");
var constraintIndependenceOption = new Option<bool>("--c-independence", description: "Advanced: maintain independent constraint sets (constraint independence optimization)");
var conditionEvaluationOption = new Option<bool>("--eval-condition", description: "Advanced: evaluate branch condition with current model to avoid extra SMT solver queries");
var incrementalityOption = new Option<bool>("--incrementality", description: "Advanced: enable SMT solver incremental mode");
var rootCommand = new RootCommand();
var entryPointCommand =
new Command("--entry-point", "Generate test coverage from the entry point of assembly (assembly must contain Main method)");
rootCommand.AddCommand(entryPointCommand);
entryPointCommand.AddArgument(assemblyPathArgument);
entryPointCommand.AddArgument(concreteArguments);
entryPointCommand.AddGlobalOption(outputOption);
entryPointCommand.AddOption(unknownArgsOption);
entryPointCommand.AddOption(constraintIndependenceOption);
entryPointCommand.AddOption(conditionEvaluationOption);
entryPointCommand.AddOption(incrementalityOption);
var allPublicMethodsCommand =
new Command("--all-public-methods", "Generate unit tests for all public methods of all public classes of assembly");
rootCommand.AddCommand(allPublicMethodsCommand);
allPublicMethodsCommand.AddArgument(assemblyPathArgument);
allPublicMethodsCommand.AddGlobalOption(outputOption);
allPublicMethodsCommand.AddOption(constraintIndependenceOption);
allPublicMethodsCommand.AddOption(conditionEvaluationOption);
allPublicMethodsCommand.AddOption(incrementalityOption);
var publicMethodsOfClassCommand =
new Command("--public-methods-of-class", "Generate unit tests for all public methods of specified class");
rootCommand.AddCommand(publicMethodsOfClassCommand);
var classArgument = new Argument<string>("class-name");
publicMethodsOfClassCommand.AddArgument(classArgument);
publicMethodsOfClassCommand.AddArgument(assemblyPathArgument);
publicMethodsOfClassCommand.AddGlobalOption(outputOption);
publicMethodsOfClassCommand.AddOption(constraintIndependenceOption);
publicMethodsOfClassCommand.AddOption(conditionEvaluationOption);
publicMethodsOfClassCommand.AddOption(incrementalityOption);
var specificMethodCommand =
new Command("--method", "Try to resolve and generate unit test coverage for the specified method");
rootCommand.AddCommand(specificMethodCommand);
var methodArgument = new Argument<string>("method-name");
specificMethodCommand.AddArgument(methodArgument);
specificMethodCommand.AddArgument(assemblyPathArgument);
specificMethodCommand.AddGlobalOption(outputOption);
specificMethodCommand.AddOption(constraintIndependenceOption);
specificMethodCommand.AddOption(conditionEvaluationOption);
specificMethodCommand.AddOption(incrementalityOption);
rootCommand.Description = "Symbolic execution engine for .NET";
entryPointCommand.Handler = CommandHandler.Create<FileInfo, string[], DirectoryInfo, bool, bool, bool, bool>
(
(assemblyPath, args, output, unknownArgs, cIndependence, evalCondition, incrementality) =>
{
var options = new SvmOptions(cIndependence, evalCondition, incrementality);
var assembly = ResolveAssembly(assemblyPath);
if (unknownArgs)
args = null;
if (assembly != null)
PostProcess(TestGenerator.Cover(assembly, args, output.FullName, options));
}
);
allPublicMethodsCommand.Handler = CommandHandler.Create<FileInfo, DirectoryInfo, bool, bool, bool>
(
(assemblyPath, output, cIndependence, evalCondition, incrementality) =>
{
var options = new SvmOptions(cIndependence, evalCondition, incrementality);
var assembly = ResolveAssembly(assemblyPath);
if (assembly != null)
PostProcess(TestGenerator.Cover(assembly, output.FullName, options));
}
);
publicMethodsOfClassCommand.Handler = CommandHandler.Create<string, FileInfo, DirectoryInfo, bool, bool, bool>
(
(className, assemblyPath, output, cIndependence, evalCondition, incrementality) =>
{
var options = new SvmOptions(cIndependence, evalCondition, incrementality);
var assembly = ResolveAssembly(assemblyPath);
if (assembly != null)
{
var type = ResolveType(assembly, className);
if (type != null)
{
PostProcess(TestGenerator.Cover(type, output.FullName, options));
}
}
}
);
specificMethodCommand.Handler = CommandHandler.Create<string, FileInfo, DirectoryInfo, bool, bool, bool>
(
(methodName, assemblyPath, output, cIndependence, evalCondition, incrementality) =>
{
var options = new SvmOptions(cIndependence, evalCondition, incrementality);
var assembly = ResolveAssembly(assemblyPath);
if (assembly != null)
{
var method = ResolveMethod(assembly, methodName);
if (method != null)
{
PostProcess(TestGenerator.Cover(method, output.FullName, options));
}
}
}
);
return rootCommand.InvokeAsync(args).Result;
}
}
}