private getFilePath()

in test-reporter/src/parsers/java-junit/java-junit-parser.ts [166:205]


  private getFilePath(tracePath: string, fileName: string): string | undefined {
    // Check if there is any tracked file with given name
    const files = this.trackedFiles[fileName]
    if (files === undefined) {
      return undefined
    }

    // Remove class name and method name from trace.
    // Take parts until first item with capital letter - package names are lowercase while class name is CamelCase.
    const packageParts = tracePath.split(/\./g)
    const packageIndex = packageParts.findIndex(part => part[0] <= 'Z')
    if (packageIndex !== -1) {
      packageParts.splice(packageIndex, packageParts.length - packageIndex)
    }

    if (packageParts.length === 0) {
      return undefined
    }

    // Get right file
    // - file name matches
    // - parent folders structure must reflect the package name
    for (const filePath of files) {
      const dirs = path.dirname(filePath).split(/\//g)
      if (packageParts.length > dirs.length) {
        continue
      }
      // get only N parent folders, where N = length of package name parts
      if (dirs.length > packageParts.length) {
        dirs.splice(0, dirs.length - packageParts.length)
      }
      // check if parent folder structure matches package name
      const isMatch = packageParts.every((part, i) => part === dirs[i])
      if (isMatch) {
        return filePath
      }
    }

    return undefined
  }