Skip to content

Commit 7becfd0

Browse files
authored
Checker for .NET framework version (#1518)
[feat] added checker for .NET framework version
1 parent e28e14c commit 7becfd0

File tree

3 files changed

+63
-10
lines changed

3 files changed

+63
-10
lines changed

utbot-rider/src/dotnet/UtBot/UtBot.VSharp/VSharpMain.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,8 @@ public static void Main(string[] args)
4949
var solution = new FileInfo(solutionFilePath);
5050
var declaringType = methodBase.DeclaringType;
5151
Debug.Assert(declaringType != null);
52-
var (generatedProject, renderedFiles) = Renderer.Render(stat.Results(), targetProject,
53-
declaringType,
54-
assemblyLoadContext, solution);
52+
var (generatedProject, renderedFiles) =
53+
Renderer.Render(stat.Results(), targetProject, declaringType, assemblyLoadContext, solution);
5554
var result = new GenerateResults(generatedProject.FullName, renderedFiles.ToArray());
5655
blockingQueue.Add("End");
5756
return result;
@@ -68,4 +67,4 @@ public static void Main(string[] args)
6867
blockingQueue.Take();
6968
ldef.Terminate();
7069
}
71-
}
70+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#nullable enable
2+
using System;
3+
using System.Diagnostics;
4+
using System.Text.RegularExpressions;
5+
6+
namespace UtBot;
7+
8+
public class FrameworkChecker
9+
{
10+
private readonly int _majorVersion;
11+
12+
public FrameworkChecker()
13+
{
14+
var dotnetInfo = RunDotnet(new ProcessStartInfo
15+
{
16+
Arguments = "--info",
17+
});
18+
if (dotnetInfo == null)
19+
throw new Exception("Could not get dotnet info");
20+
21+
MatchCollection matches = Regex.Matches(dotnetInfo, @".*?Version:\s*?(\d{1})\.\d{1}\.\d{3}");
22+
if (matches.Count < 1)
23+
throw new Exception("Could not find dotnet cli");
24+
25+
var match = matches[0];
26+
if (!int.TryParse(match.Groups[1].Value, out _majorVersion))
27+
throw new Exception("Could not parse dotnet version");
28+
}
29+
30+
private static string? RunDotnet(ProcessStartInfo startInfo)
31+
{
32+
startInfo.FileName = "dotnet";
33+
startInfo.RedirectStandardError = true;
34+
startInfo.RedirectStandardOutput = true;
35+
36+
var pi = Process.Start(startInfo);
37+
var s = pi?.StandardOutput.ReadToEnd();
38+
pi?.WaitForExit();
39+
return s;
40+
}
41+
42+
public bool FrameworkSupportsVSharp()
43+
{
44+
return _majorVersion >= 6;
45+
}
46+
47+
public bool FrameworkSupportsProject(Version tfm)
48+
{
49+
return _majorVersion >= tfm.Major;
50+
}
51+
}

utbot-rider/src/dotnet/UtBot/UtBot/UnitTestBuilder.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.IO;
45
using System.Linq;
56
using System.Reflection;
@@ -26,6 +27,7 @@
2627
using JetBrains.ReSharper.UnitTestFramework.Exploration.Artifacts;
2728
using JetBrains.Rider.Model;
2829
using JetBrains.Util;
30+
using JetBrains.Util.Dotnet.TargetFrameworkIds;
2931
using UtBot.Rd.Generated;
3032
using UtBot.VSharp;
3133
using Thread = System.Threading.Thread;
@@ -68,7 +70,8 @@ protected override void Process(CSharpGeneratorContext context, IProgressIndicat
6870
var typeElement = context.ClassDeclaration.DeclaredElement;
6971
if (typeElement == null) return;
7072
if (!(typeElement is IClass) && !(typeElement is IStruct)) return;
71-
var assembly = project.GetOutputFilePath(context.PsiModule.TargetFrameworkId);
73+
var tfm = context.PsiModule.TargetFrameworkId;
74+
var assembly = project.GetOutputFilePath(tfm);
7275
var descriptors = new List<UnitTestMethodDescriptor>();
7376
foreach (var inputElement in context.InputElements.WithProgress(progress, "Generating Unit tests")
7477
.OfType<GeneratorDeclaredElement<IMethod>>())
@@ -83,7 +86,7 @@ protected override void Process(CSharpGeneratorContext context, IProgressIndicat
8386
{
8487
try
8588
{
86-
Generate(indicator, project, assembly, descriptors);
89+
Generate(indicator, project, assembly, descriptors, tfm);
8790
}
8891
finally
8992
{
@@ -93,7 +96,7 @@ protected override void Process(CSharpGeneratorContext context, IProgressIndicat
9396
}
9497

9598
private void Generate(IBackgroundProgressIndicator progressIndicator, IProject project,
96-
VirtualFileSystemPath assemblyPath, List<UnitTestMethodDescriptor> descriptors)
99+
VirtualFileSystemPath assemblyPath, List<UnitTestMethodDescriptor> descriptors, TargetFrameworkId tfm)
97100
{
98101
SolutionBuilderRequest buildRequest;
99102
var contextUnloaded = false;
@@ -136,7 +139,7 @@ private void Generate(IBackgroundProgressIndicator progressIndicator, IProject p
136139
_logger.Verbose("Start Generation");
137140
_logger.Catch(() =>
138141
{
139-
project.Locks.AssertNonMainThread();
142+
project.Locks.AssertNonMainThread();
140143
var pluginPath = FileSystemPath.Parse(Assembly.GetExecutingAssembly().Location)
141144
.Parent;
142145
var vsharpRunner = pluginPath.Combine("UtBot.VSharp.dll");
@@ -148,7 +151,7 @@ private void Generate(IBackgroundProgressIndicator progressIndicator, IProject p
148151
var args = new GenerateArguments(assemblyPath.FullPath, projectCsprojPath, solutionFilePath,
149152
moduleFqnName, methodToken, _generationTimeout);
150153
var result = proc.VSharpModel.Generate.Sync(args, RpcTimeouts.Maximal);
151-
unitTestProjectLocation = result?.GeneratedProjectPath ?? "";
154+
unitTestProjectLocation = result?.GeneratedProjectPath ?? "";
152155
_shellLocks.ExecuteOrQueue(_lifetime, "UnitTestBuilder::Generate", () =>
153156
{
154157
if (solution.IsValid())
@@ -233,4 +236,4 @@ private IProject TryToFindProject(ISolution solution, VirtualFileSystemPath unit
233236
{
234237
return solution.FindProjectItemsByLocation(unitTestProjectPath).SingleItem() as IProject;
235238
}
236-
}
239+
}

0 commit comments

Comments
 (0)