< Summary

Class:Mklinker.Commands.AddLinkCommand
Assembly:Mklinker
File(s):/home/travis/build/rubenchristoffer/Mklinker/Mklinker/Commands/AddLinkCommand.cs
Covered lines:26
Uncovered lines:5
Coverable lines:31
Total lines:66
Line coverage:83.8% (26 of 31)
Covered branches:10
Total branches:12
Branch coverage:83.3% (10 of 12)

Metrics

MethodCyclomatic complexity NPath complexity Sequence coverage Branch coverage
.ctor()100%100%
.ctor(...)10100%100%
Execute(...)12085.71%83.33%

File(s)

/home/travis/build/rubenchristoffer/Mklinker/Mklinker/Commands/AddLinkCommand.cs

#LineLine coverage
 1using System.Linq;
 2using CommandLine;
 3using System.IO.Abstractions;
 4using Mklinker.Abstractions;
 5
 6namespace Mklinker.Commands {
 7
 8  [Verb("addlink", HelpText = "Adds a new link to config file")]
 9  class AddLinkCommand : GlobalOptions {
 10
 11    [Value(0, HelpText = "The path to the source file", Required = true)]
 87212    public string sourcePath { get; private set; }
 13
 14    [Value(1, HelpText = "The path to new link file", Required = true)]
 87215    public string targetPath { get; private set; }
 16
 17    [Value(2, Default = ConfigLink.LinkType.Default, HelpText = "The type of link you want to create. Default is Symboli
 43318    public ConfigLink.LinkType linkType { get; private set; }
 19
 20    [Option('f', "force", Default = false, HelpText = "If this flag is set it will ignore validation checks and add it n
 021    public bool force { get; private set; }
 22
 023    public AddLinkCommand() : base() {}
 24
 29025    public AddLinkCommand(string sourcePath, string targetPath, ConfigLink.LinkType linkType, string path) : base(path) 
 14526      this.targetPath = targetPath;
 14527      this.sourcePath = sourcePath;
 14528      this.linkType = linkType;
 14529    }
 30
 14631    internal void Execute(IConsole console, IConfigHandler configHandler, IFileSystem fileSystem, IPathResolver pathReso
 032      if (!configHandler.DoesConfigExist(path)) {
 033        console.WriteLine($"Config '{ path }' does not exist. Type 'help config' in order to see how you create a config
 034        return;
 35      }
 36
 14637      IConfig config = configHandler.LoadConfig(path);
 38
 39      // Force forward slash
 14640      sourcePath = sourcePath.Replace ('\\', '/');
 14641      targetPath = targetPath.Replace ('\\', '/');
 42
 14643      string formattedSourcePath = pathResolver.GetAbsoluteResolvedPath(sourcePath, config.Variables);
 14644      string formattedTargetPath = pathResolver.GetAbsoluteResolvedPath(targetPath, config.Variables);
 45
 14746      if (!force && !fileSystem.File.Exists(formattedSourcePath) && !fileSystem.Directory.Exists(formattedSourcePath)) {
 147        console.WriteLine($"\nThe sourcePath '{sourcePath}' is invalid because it does not exist", IConsole.ContentType.
 148        return;
 49      }
 50
 57851      if (!force && config.LinkList.Any(link => pathResolver.GetAbsoluteResolvedPath(link.targetPath, config.Variables).
 152        console.WriteLine($"\nThe targetPath '{targetPath}' is invalid because it already exists in config file", IConso
 153        return;
 54      }
 55
 14456      config.LinkList.Add(new ConfigLink(sourcePath, targetPath, linkType));
 14457      configHandler.SaveConfig(config, path);
 58
 14459      console.WriteLine($"\nAdded new { linkType.ToString() } link to config file: \n" +
 14460        $"Source: '{ sourcePath }'\n" +
 14461        $"Target: '{ targetPath }'");
 14662    }
 63
 64  }
 65
 66}