< Summary

Class:Mklinker.Commands.AddVariableCommand
Assembly:Mklinker
File(s):/home/travis/build/rubenchristoffer/Mklinker/Mklinker/Commands/AddVariableCommand.cs
Covered lines:25
Uncovered lines:4
Coverable lines:29
Total lines:59
Line coverage:86.2% (25 of 29)
Covered branches:5
Total branches:6
Branch coverage:83.3% (5 of 6)

Metrics

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

File(s)

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

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.IO.Abstractions;
 4using System.Text;
 5using System.Linq;
 6using CommandLine;
 7using Mklinker.Abstractions;
 8
 9namespace Mklinker.Commands {
 10
 11  [Verb("addvar", HelpText = "Adds a new variable to config file")]
 12  class AddVariableCommand : GlobalOptions, IDefaultCommandHandler {
 13
 14    [Value(0, HelpText = "The name of the variable", Required = true)]
 915    public string name { get; private set; }
 16
 17    [Value(1, HelpText = "The value of the variable", Required = true)]
 718    public string value { get; private set; }
 19
 20    [Option('f', "force", Default = false, HelpText = "If this flag is set it will override existing variable if name al
 521    public bool force { get; private set; }
 22
 023    public AddVariableCommand() : base() {}
 24
 625    public AddVariableCommand (string name, string value, bool force, string path) : base(path) {
 326      this.name = name;
 327      this.value = value;
 328      this.force = force;
 329    }
 30
 331    public void Execute(IConsole console, IConfigHandler configHandler, IFileSystem fileSystem) {
 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
 337      IConfig config = configHandler.LoadConfig(path);
 538      Variable existingVariable = config.Variables.FirstOrDefault(variable => variable.name.Equals(name, StringCompariso
 39
 440      if (existingVariable == null) {
 141        config.Variables.Add (new Variable (name, value));
 142        console.WriteLine($"Added new variable '{ name }' with value '{ value }'");
 43
 144        configHandler.SaveConfig(config, path);
 345      } else {
 346        if (!force) {
 147          console.WriteLine($"A variable with the name '{ name }' already exists", IConsole.ContentType.Negative);
 248        } else {
 149          existingVariable.value = value;
 150          console.WriteLine($"Replaced existing variable value for '{ name }' with '{ value }'");
 51
 152          configHandler.SaveConfig(config, path);
 153        }
 254      }
 355    }
 56
 57  }
 58
 59}