• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

gturi / nca / 8277672099

14 Mar 2024 08:32AM UTC coverage: 86.091% (+0.1%) from 85.979%
8277672099

push

github

web-flow
Merge pull request #213 from gturi/feature/nca-alias-command

Feature/nca alias command

221 of 316 branches covered (69.94%)

Branch coverage included in aggregate %.

598 of 661 new or added lines in 62 files covered. (90.47%)

1 existing line in 1 file now uncovered.

1116 of 1237 relevant lines covered (90.22%)

1211.08 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

79.41
/src/validator/nca-command-validator.ts
1
import { NcaConfig } from "../config/nca-config";
162✔
2
import { NcaCommand } from "../model/api/nca-command";
3
import { CommandType } from "../model/api/command-type";
162✔
4
import { OptionParam } from "../model/api/option-param";
5
import { PositionalArgument } from "../model/api/positional-argument";
6
import { ArrayUtils } from "../util/array-utils";
162✔
7
import { StringUtils } from "../util/string-utils";
162✔
8
import { DuplicatesValidator } from "./duplicates-validator";
162✔
9
import { EnumValidator } from "./enum-validator";
162✔
10
import { OptionParamValidator } from "./option-param-validator";
162✔
11
import { PositionalArgumentValidator } from "./positional-argument-validator";
162✔
12
import { WhiteSpaceValidator } from "./white-space-validator";
162✔
13

14
type Parent = {
15
  options: OptionParam[],
16
  positionalArguments: PositionalArgument[]
17
}
18

19
export class NcaCommandValidator {
162✔
20

21
  static validate(ncaCommands: NcaCommand[]) {
22
    this.privateValidator(ncaCommands, {
162✔
23
      options: [],
24
      positionalArguments: []
25
    });
26
  }
27

28
  private static privateValidator(ncaCommands: NcaCommand[], parent: Parent) {
29
    const names = ncaCommands.map(ncaCommand => ncaCommand.name);
4,348✔
30
    this.checkNamesFormat(names);
806✔
31
    this.checkForDuplicateNames(names);
806✔
32

33
    ncaCommands.forEach(ncaCommand => this.validateNcaCommand(ncaCommand, parent));
4,348✔
34
  }
35

36
  private static checkNamesFormat(names: string[]) {
37
    WhiteSpaceValidator.validate(names, elementsWithWhitespaces => {
806✔
NEW
38
      return `Nca command names cannot contain whitespaces [${elementsWithWhitespaces}]`;
×
39
    });
40

41
    this.checkForbiddenNames(names);
806✔
42
  }
43

44
  private static checkForbiddenNames(ncaCommandNames: string[]) {
45
    const forbiddenNames = NcaConfig.getForbiddenNames();
806✔
46
    const forbiddenNamesFound = ncaCommandNames.filter(element => forbiddenNames.includes(element));
4,348✔
47
    if (forbiddenNamesFound.length > 0) {
806!
NEW
48
      throw new Error(`The following nca command names are not allowed: [${forbiddenNamesFound}]`);
×
49
    }
50
  }
51

52
  private static checkForDuplicateNames(names: string[]) {
53
    const getErrorMessage = (duplicates: string[]) => {
806✔
NEW
54
      return `Multiple nca commands have been defined with the same name: [${duplicates}]`;
×
55
    };
56

57
    DuplicatesValidator.validate(names, getErrorMessage);
806✔
58
  }
59

60
  private static validateNcaCommand(ncaCommand: NcaCommand, parent: Parent) {
61
    this.validateCommand(ncaCommand);
4,348✔
62

63
    const options = ArrayUtils.concat(parent.options, ncaCommand.options);
4,348✔
64
    OptionParamValidator.validate(ncaCommand.name, options);
4,348✔
65

66
    const positionalArguments = ArrayUtils.concat(
4,348✔
67
      parent.positionalArguments, ncaCommand.positionalArguments
68
    );
69
    PositionalArgumentValidator.validate(ncaCommand.name, positionalArguments);
4,348✔
70

71
    this.checkSubCommandType(ncaCommand);
4,348✔
72

73
    if (ncaCommand.subCommands) {
4,347✔
74
      this.checkSubCommandsAndPositionalArgumentNames(ncaCommand, positionalArguments);
644✔
75

76
      this.privateValidator(ncaCommand.subCommands, {
644✔
77
        options: options,
78
        positionalArguments: positionalArguments
79
      });
80
    }
81
  }
82

83
  private static validateCommand(ncaCommand: NcaCommand) {
84
    if (StringUtils.isEmpty(ncaCommand.command) && (ncaCommand.subCommands ?? []).length == 0) {
4,348!
NEW
85
      throw new Error(
×
86
        `Nca command '${ncaCommand.name}' must define sub commands when its command is not defined`
87
      );
88
    }
89
  }
90

91
  private static checkSubCommandsAndPositionalArgumentNames(
92
    ncaCommand: NcaCommand, parentPositionalArguments: PositionalArgument[]
93
  ) {
94
    if (ncaCommand.subCommands && ncaCommand.positionalArguments) {
644!
95
      const match = ncaCommand.subCommands.find(subCommand => {
644✔
96
        return parentPositionalArguments.some(positionalArgument => {
1,288✔
97
          return positionalArgument.name === subCommand.name
2,576✔
98
        });
99
      });
100
      if (match) {
644!
NEW
101
        throw new Error(
×
102
          `Nca command name '${match.name}' cannot be used in subcommand: ` +
103
          'a positional argument with the same name already exists'
104
        );
105
      }
106
    }
107
  }
108

109
  private static checkSubCommandType(ncaCommand: NcaCommand) {
110
    const getErrorMessage = () =>
4,348✔
111
      `Nca command '${ncaCommand.name}' commandType '${ncaCommand.commandType}' is not valid: ` +
1✔
112
      `supported values are ${Object.keys(CommandType)}`;
113
    const commandType = ncaCommand.commandType ?? CommandType.Simple;
4,348✔
114
    EnumValidator.validate(CommandType, commandType, getErrorMessage)
4,348✔
115
  }
116
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc