< Summary

Information
Class: Amusoft.DotnetNew.Tests.Templating.TemplateSolution
Assembly: Amusoft.DotnetNew.Tests
File(s): /home/runner/work/Amusoft.DotnetNew.Tests/Amusoft.DotnetNew.Tests/src/Amusoft.DotnetNew.Tests/Templating/TemplateSolutionInstaller.cs
Tag: 127_14865883074
Line coverage
100%
Covered lines: 46
Uncovered lines: 0
Coverable lines: 46
Total lines: 125
Line coverage: 100%
Branch coverage
89%
Covered branches: 25
Total branches: 28
Branch coverage: 89.2%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_Solution()100%11100%
.ctor(...)100%66100%
.ctor(...)100%11100%
GetSolutionPathFromAssembly(...)93.75%1616100%
InstallTemplateAsync()50%2.03280%
DiscoverTemplates(...)50%2.01288.88%
InstallTemplatesFromDirectoryAsync()100%22100%

File(s)

/home/runner/work/Amusoft.DotnetNew.Tests/Amusoft.DotnetNew.Tests/src/Amusoft.DotnetNew.Tests/Templating/TemplateSolutionInstaller.cs

#LineLine coverage
 1using System;
 2using System.IO;
 3using System.Linq;
 4using System.Threading;
 5using System.Threading.Tasks;
 6using Amusoft.DotnetNew.Tests.Internals;
 7using Amusoft.DotnetNew.Tests.Rewriters;
 8using Amusoft.DotnetNew.Tests.Scopes;
 9using Amusoft.DotnetNew.Tests.Utility;
 10
 11namespace Amusoft.DotnetNew.Tests.Templating;
 12
 13/// <summary>
 14/// Template solution
 15/// </summary>
 16public class TemplateSolution
 17{
 18  /// <summary>
 19  ///
 20  /// </summary>
 3421  public PathSource Solution { get; }
 22
 23  /// <summary>
 24  /// Constructor that uses the path to the solution file
 25  /// </summary>
 26  /// <param name="solutionPath"></param>
 1227  public TemplateSolution(string solutionPath)
 28  {
 1229    if (!solutionPath.EndsWith(".sln") && !solutionPath.EndsWith(".slnx"))
 130      throw new ArgumentException("Solution files are expected to end with the extension sln or slnx");
 1131    if (!File.Exists(solutionPath))
 132      throw new FileNotFoundException(solutionPath);
 33
 1034    Solution = new PathSource(solutionPath);
 35
 1036    LoggingScope.TryAddRewriter(BackslashRewriter.Instance);
 1037    LoggingScope.TryAddRewriter(new SolutionDirectoryRewriter(Solution.Directory));
 1038  }
 39
 40  /// <summary>
 41  ///
 42  /// </summary>
 43  /// <param name="searchDirectoryStart">directory indicating where to start looking for the solution file</param>
 44  /// <param name="maxParentJumps"></param>
 45  /// <param name="solutionName">filename of the solution</param>
 1246  public TemplateSolution(string searchDirectoryStart, int maxParentJumps, string solutionName) : this(GetSolutionPathFr
 47  {
 1048  }
 49
 50  private static string GetSolutionPathFromAssembly(string searchDirectoryStart, int maxParentJumps, string solutionName
 51  {
 1252    if (Path.GetExtension(searchDirectoryStart) is { Length: > 0})
 1053      searchDirectoryStart = Path.GetDirectoryName(searchDirectoryStart)!;
 1254    if (!Directory.Exists(searchDirectoryStart))
 155      throw new DirectoryNotFoundException($"Directory {searchDirectoryStart} not found");
 56
 1157    var iterations = maxParentJumps;
 1158    var searchPath = searchDirectoryStart;
 6159    while (--iterations >= 0 && searchPath != null)
 60    {
 6061      var slnFiles = Directory.EnumerateFiles(searchPath, "*.sln", SearchOption.AllDirectories)
 6062        .Concat(Directory.EnumerateFiles(searchPath, "*.slnx", SearchOption.AllDirectories));
 8063      var match = slnFiles.FirstOrDefault(d => Path.GetFileName(d).Equals(solutionName, StringComparison.OrdinalIgnoreCa
 6064      if (match is not null)
 1065        return match;
 66
 5067      searchPath = new DirectoryInfo(searchPath).Parent?.FullName;
 68    }
 69
 170    throw new FileNotFoundException($"Solution file {solutionName} not found in {searchDirectoryStart} or it's parent->c
 71  }
 72
 73  /// <summary>
 74  /// Installs a template relative to the solution file
 75  /// </summary>
 76  /// <param name="relativePath">relative path to the template</param>
 77  /// <param name="cancellationToken"></param>
 78  public async Task<TemplateInstallation> InstallTemplateAsync(string relativePath, CancellationToken cancellationToken)
 79  {
 780    var fullPath = Solution.PathTranslator.GetAbsolutePath(relativePath);
 781    if (!Directory.Exists(fullPath.OriginalPath))
 182      throw new DirectoryNotFoundException($"{Solution.Directory.VirtualPath} + {relativePath}");
 83
 684    return await TemplateInstallation.CreateAsync(new ProjectTemplatingContext(fullPath), cancellationToken).ConfigureAw
 685  }
 86
 87  /// <summary>
 88  /// Finds the folders with template.json files in them
 89  /// </summary>
 90  /// <param name="searchFolder">relative path to the folder you want to run discovery in</param>
 91  /// <returns></returns>
 92  /// <exception cref="DirectoryNotFoundException"></exception>
 93  public CrossPlatformPath[] DiscoverTemplates(string searchFolder)
 94  {
 495    var folder = Solution.PathTranslator.GetAbsolutePath(searchFolder);
 496    if (!Directory.Exists(folder.OriginalPath))
 197      throw new DirectoryNotFoundException($"{Solution.Directory.VirtualPath} + {searchFolder}");
 98
 399    var templateFiles = Directory.EnumerateFiles(folder.OriginalPath, "template.json", SearchOption.AllDirectories);
 100
 3101    return templateFiles
 6102      .Select(path => PathHelper.AbsoluteTrimPathEnd(path, 2))
 6103      .Select(path => Solution.PathTranslator.GetRelativePath(path))
 6104      .OrderBy(d => d.VirtualPath)
 3105      .ToArray();
 106  }
 107
 108  /// <summary>
 109  /// Installs all templates that can be found in the given directory
 110  /// </summary>
 111  /// <param name="relativePath"></param>
 112  /// <param name="cancellationToken"></param>
 113  /// <returns></returns>
 114  public async Task<TemplateInstallationGroup> InstallTemplatesFromDirectoryAsync(string relativePath, CancellationToken
 115  {
 2116    var group = new TemplateInstallationGroup();
 12117    foreach (var projectPath in DiscoverTemplates(relativePath))
 118    {
 4119      cancellationToken.ThrowIfCancellationRequested();
 4120      group.Add(await InstallTemplateAsync(projectPath.OriginalPath, cancellationToken));
 121    }
 122
 2123    return group;
 2124  }
 125}