All files / src/rules smim_consistency.ts

97.18% Statements 69/71
82.35% Branches 14/17
100% Functions 8/8
97.18% Lines 69/71

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 711x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11509x 11509x 11509x 11509x 11509x 34364x 34364x 34364x 34364x 34364x 34364x 34364x 11509x 11509x 11006x 11006x 11509x 11509x 235x 235x 11509x 11509x 238x 238x 238x 11509x 11509x 308x 308x 308x 305x 305x 3x 308x 308x 1x 1x 1x 1x 1x 3x 3x 3x 11509x 11509x 3x 3x 3x 3x 11509x 11509x 1x 1x 1x     1x 1x 1x 11509x
import {Issue} from "../issue";
import {BasicRuleConfig} from "./_basic_rule_config";
import {IRegistry} from "../_iregistry";
import {MIMEObject} from "../objects";
import {IRule, IRuleMetadata} from "./_irule";
import {IObject} from "../objects/_iobject";
import {Position} from "../position";
 
export class SMIMConsistencyConf extends BasicRuleConfig {
}
 
export class SMIMConsistency implements IRule {
  private conf = new SMIMConsistencyConf();
  private reg: IRegistry;
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "smim_consistency",
      title: "SMIM consistency check",
      shortDescription: `SMIM consistency check`,
      extendedInformation: "Checks that the parent folder of each MIME object exists in the registry. The SAP system folder /SAP/PUBLIC is always allowed as a parent even if not present in the repository.",
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: SMIMConsistencyConf) {
    this.conf = conf;
  }
 
  public initialize(reg: IRegistry) {
    this.reg = reg;
    return this;
  }
 
  public run(obj: IObject): Issue[] {
    const issues: Issue[] = [];
 
    if (!(obj instanceof MIMEObject)) {
      return [];
    }
 
    const base = this.base(obj.getURL() || "");
    if (base !== "" && base !== "/SAP/PUBLIC" && this.findFolder(base) === false) {
      const message = `Parent folder "${base}" not found`;
      const position = new Position(1, 1);
      const issue = Issue.atPosition(obj.getFiles()[0], position, message, this.getMetadata().key, this.conf.severity);
      issues.push(issue);
    }
 
    return issues;
  }
 
  private base(full: string): string {
    const components = full.split("/");
    components.pop();
    return components.join("/");
  }
 
  private findFolder(base: string): boolean {
    for (const smim of this.reg.getObjectsByType("SMIM")) {
      const mime = smim as MIMEObject;
      if (base === mime.getURL() && mime.isFolder() === true) {
        return true;
      }
    }
    return false;
  }
}