< Summary

Class:Mklinker.Commands.ValidateCommand
Assembly:Mklinker
File(s):/home/travis/build/rubenchristoffer/Mklinker/Mklinker/Commands/ValidateCommand.cs
Covered lines:53
Uncovered lines:10
Coverable lines:63
Total lines:101
Line coverage:84.1% (53 of 63)
Covered branches:39
Total branches:52
Branch coverage:75% (39 of 52)

Metrics

MethodCyclomatic complexity NPath complexity Sequence coverage Branch coverage
.ctor()100%100%
.ctor(...)10100%100%
Execute(...)40077.5%72.5%
ValidateExistence(...)20100%100%
ValidateLinkType(...)100100%80%
ValidateDuplicate(...)10100%100%

File(s)

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

#LineLine coverage
 1using System;
 2using System.Linq;
 3using System.IO.Abstractions;
 4using CommandLine;
 5using Mklinker.Abstractions;
 6using System.Collections.Generic;
 7
 8namespace Mklinker.Commands {
 9
 10    [Verb ("validate", HelpText = "This will validate the config file to see if it is valid")]
 11    class ValidateCommand : GlobalOptions {
 12
 13        [Option ("all", HelpText = "This will list all entries in config and not just the ones that didn't pass validati
 1714        public bool displayAll { get; private set; }
 15
 016        public ValidateCommand() : base() {}
 17
 818        public ValidateCommand(bool displayAll, string path) : base(path) {
 419            this.displayAll = displayAll;
 420        }
 21
 422        internal void Execute(IConsole console, IConfigHandler configHandler, IFileSystem fileSystem, IPathResolver path
 023            if (!configHandler.DoesConfigExist(path)) {
 024                console.WriteLine($"Config '{ path }' does not exist. Type 'help config' in order to see how you create 
 025                return;
 26            }
 27
 428            IConfig config = configHandler.LoadConfig(path);
 429            bool isValid = true;
 30
 2431            foreach (ConfigLink configLink in config.LinkList) {
 432                string resolvedSourcePath = pathResolver.GetAbsoluteResolvedPath(configLink.sourcePath, config.Variables
 433                string resolvedTargetPath = pathResolver.GetAbsoluteResolvedPath(configLink.targetPath, config.Variables
 34
 435                bool validation1 = ValidateExistence(fileSystem, resolvedSourcePath);
 436                bool validation2 = ValidateLinkType(fileSystem, resolvedSourcePath, configLink.linkType);
 437                bool validation3 = ValidateDuplicate(pathResolver, config, resolvedTargetPath);
 38
 639                if (displayAll || !validation1 || !validation2 || !validation3) {
 240                    console.WriteLine($"\n{ configLink.ToString() }");
 41
 242                    isValid = false;
 243                }
 44
 545                if (!validation1 || displayAll) {
 146                    console.WriteLine ($"\t# Source path exists: { (validation1 ? "Yes" : "No") }",
 147                        validation1
 148                        ? IConsole.ContentType.Positive
 149                        : IConsole.ContentType.Negative);
 150                }
 51
 652                if (!validation2 || displayAll) {
 253                    console.WriteLine ($"\t# Link type acceptable: { (validation2 ? "Yes" : "No") }",
 254                        validation2
 255                        ? IConsole.ContentType.Positive
 256                        : IConsole.ContentType.Negative);
 257                }
 58
 059                if (!validation3 || displayAll) {
 060                    console.WriteLine ($"\t# Duplicate target path exists: { (validation3 ? "False" : "True") }",
 061                        validation3
 062                        ? IConsole.ContentType.Positive
 063                        : IConsole.ContentType.Negative);
 064                }
 465            }
 66
 467            if (config.LinkList.Count == 0)
 168                console.WriteLine("Config is empty");
 369            else if (isValid)
 170                console.WriteLine("Config is 100% valid");
 471        }
 72
 473        internal bool ValidateExistence (IFileSystem fileSystem, string resolvedSourcePath) {
 474            return fileSystem.File.Exists(resolvedSourcePath) || fileSystem.Directory.Exists(resolvedSourcePath);
 475        }
 76
 477        internal bool ValidateLinkType (IFileSystem fileSystem, string resolvedSourcePath, ConfigLink.LinkType linkType)
 478            if (linkType == ConfigLink.LinkType.Default)
 179                return true;
 80
 481            if (fileSystem.File.Exists(resolvedSourcePath)) {
 182                return linkType == ConfigLink.LinkType.Symbolic || linkType == ConfigLink.LinkType.Hard;
 83            }
 84
 385            if (fileSystem.Directory.Exists(resolvedSourcePath)) {
 186                return linkType == ConfigLink.LinkType.Symbolic || linkType == ConfigLink.LinkType.Junction;
 87            }
 88
 189            return false;
 490        }
 91
 492        internal bool ValidateDuplicate (IPathResolver pathResolver, IConfig config, string resolvedTargetPath) {
 493            return config.LinkList
 1094                .Where(link => pathResolver.GetAbsoluteResolvedPath(link.targetPath, config.Variables)
 1095                    .Equals(resolvedTargetPath, StringComparison.OrdinalIgnoreCase))
 496                .Count() <= 1;
 497        }
 98
 99    }
 100
 101}