From d0d843c161143f6efaa440137104ca527747c2a9 Mon Sep 17 00:00:00 2001 From: MchKosticyn Date: Wed, 14 Dec 2022 18:24:56 +0300 Subject: [PATCH] [feat] added checker for .NET framework version --- .../dotnet/UtBot/UtBot.VSharp/VSharpMain.cs | 7 ++- .../UtBot/UtBot/TargetFrameworkChecker.cs | 51 +++++++++++++++++++ .../src/dotnet/UtBot/UtBot/UnitTestBuilder.cs | 15 +++--- 3 files changed, 63 insertions(+), 10 deletions(-) create mode 100644 utbot-rider/src/dotnet/UtBot/UtBot/TargetFrameworkChecker.cs diff --git a/utbot-rider/src/dotnet/UtBot/UtBot.VSharp/VSharpMain.cs b/utbot-rider/src/dotnet/UtBot/UtBot.VSharp/VSharpMain.cs index be1bee9ffd..54c91bdca3 100644 --- a/utbot-rider/src/dotnet/UtBot/UtBot.VSharp/VSharpMain.cs +++ b/utbot-rider/src/dotnet/UtBot/UtBot.VSharp/VSharpMain.cs @@ -49,9 +49,8 @@ public static void Main(string[] args) var solution = new FileInfo(solutionFilePath); var declaringType = methodBase.DeclaringType; Debug.Assert(declaringType != null); - var (generatedProject, renderedFiles) = Renderer.Render(stat.Results(), targetProject, - declaringType, - assemblyLoadContext, solution); + var (generatedProject, renderedFiles) = + Renderer.Render(stat.Results(), targetProject, declaringType, assemblyLoadContext, solution); var result = new GenerateResults(generatedProject.FullName, renderedFiles.ToArray()); blockingQueue.Add("End"); return result; @@ -68,4 +67,4 @@ public static void Main(string[] args) blockingQueue.Take(); ldef.Terminate(); } -} \ No newline at end of file +} diff --git a/utbot-rider/src/dotnet/UtBot/UtBot/TargetFrameworkChecker.cs b/utbot-rider/src/dotnet/UtBot/UtBot/TargetFrameworkChecker.cs new file mode 100644 index 0000000000..499fa0fd16 --- /dev/null +++ b/utbot-rider/src/dotnet/UtBot/UtBot/TargetFrameworkChecker.cs @@ -0,0 +1,51 @@ +#nullable enable +using System; +using System.Diagnostics; +using System.Text.RegularExpressions; + +namespace UtBot; + +public class FrameworkChecker +{ + private readonly int _majorVersion; + + public FrameworkChecker() + { + var dotnetInfo = RunDotnet(new ProcessStartInfo + { + Arguments = "--info", + }); + if (dotnetInfo == null) + throw new Exception("Could not get dotnet info"); + + MatchCollection matches = Regex.Matches(dotnetInfo, @".*?Version:\s*?(\d{1})\.\d{1}\.\d{3}"); + if (matches.Count < 1) + throw new Exception("Could not find dotnet cli"); + + var match = matches[0]; + if (!int.TryParse(match.Groups[1].Value, out _majorVersion)) + throw new Exception("Could not parse dotnet version"); + } + + private static string? RunDotnet(ProcessStartInfo startInfo) + { + startInfo.FileName = "dotnet"; + startInfo.RedirectStandardError = true; + startInfo.RedirectStandardOutput = true; + + var pi = Process.Start(startInfo); + var s = pi?.StandardOutput.ReadToEnd(); + pi?.WaitForExit(); + return s; + } + + public bool FrameworkSupportsVSharp() + { + return _majorVersion >= 6; + } + + public bool FrameworkSupportsProject(Version tfm) + { + return _majorVersion >= tfm.Major; + } +} diff --git a/utbot-rider/src/dotnet/UtBot/UtBot/UnitTestBuilder.cs b/utbot-rider/src/dotnet/UtBot/UtBot/UnitTestBuilder.cs index 5316799652..eaf80f0a74 100644 --- a/utbot-rider/src/dotnet/UtBot/UtBot/UnitTestBuilder.cs +++ b/utbot-rider/src/dotnet/UtBot/UtBot/UnitTestBuilder.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.IO; using System.Linq; using System.Reflection; @@ -26,6 +27,7 @@ using JetBrains.ReSharper.UnitTestFramework.Exploration.Artifacts; using JetBrains.Rider.Model; using JetBrains.Util; +using JetBrains.Util.Dotnet.TargetFrameworkIds; using UtBot.Rd.Generated; using UtBot.VSharp; using Thread = System.Threading.Thread; @@ -68,7 +70,8 @@ protected override void Process(CSharpGeneratorContext context, IProgressIndicat var typeElement = context.ClassDeclaration.DeclaredElement; if (typeElement == null) return; if (!(typeElement is IClass) && !(typeElement is IStruct)) return; - var assembly = project.GetOutputFilePath(context.PsiModule.TargetFrameworkId); + var tfm = context.PsiModule.TargetFrameworkId; + var assembly = project.GetOutputFilePath(tfm); var descriptors = new List(); foreach (var inputElement in context.InputElements.WithProgress(progress, "Generating Unit tests") .OfType>()) @@ -83,7 +86,7 @@ protected override void Process(CSharpGeneratorContext context, IProgressIndicat { try { - Generate(indicator, project, assembly, descriptors); + Generate(indicator, project, assembly, descriptors, tfm); } finally { @@ -93,7 +96,7 @@ protected override void Process(CSharpGeneratorContext context, IProgressIndicat } private void Generate(IBackgroundProgressIndicator progressIndicator, IProject project, - VirtualFileSystemPath assemblyPath, List descriptors) + VirtualFileSystemPath assemblyPath, List descriptors, TargetFrameworkId tfm) { SolutionBuilderRequest buildRequest; var contextUnloaded = false; @@ -136,7 +139,7 @@ private void Generate(IBackgroundProgressIndicator progressIndicator, IProject p _logger.Verbose("Start Generation"); _logger.Catch(() => { - project.Locks.AssertNonMainThread(); + project.Locks.AssertNonMainThread(); var pluginPath = FileSystemPath.Parse(Assembly.GetExecutingAssembly().Location) .Parent; var vsharpRunner = pluginPath.Combine("UtBot.VSharp.dll"); @@ -148,7 +151,7 @@ private void Generate(IBackgroundProgressIndicator progressIndicator, IProject p var args = new GenerateArguments(assemblyPath.FullPath, projectCsprojPath, solutionFilePath, moduleFqnName, methodToken, _generationTimeout); var result = proc.VSharpModel.Generate.Sync(args, RpcTimeouts.Maximal); - unitTestProjectLocation = result?.GeneratedProjectPath ?? ""; + unitTestProjectLocation = result?.GeneratedProjectPath ?? ""; _shellLocks.ExecuteOrQueue(_lifetime, "UnitTestBuilder::Generate", () => { if (solution.IsValid()) @@ -233,4 +236,4 @@ private IProject TryToFindProject(ISolution solution, VirtualFileSystemPath unit { return solution.FindProjectItemsByLocation(unitTestProjectPath).SingleItem() as IProject; } -} \ No newline at end of file +}