# Code coverage report for ./../tap/bin/run.js

## Statements: 36.05% (84 / 233) Branches: 28.57% (44 / 154) Functions: 13.33% (2 / 15) Lines: 36.21% (84 / 232) Ignored: none

All files » ./../tap/bin/ » run.js

    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
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    1

    1
    1

    1

    1

    1
    1
    1
    1

    1

    1
    1

    1
    1

    1
    1
    1
    1

    1

    1

    1

    1

    1
    1

    1
    2
    2
    1
    1

    1

    1

    1
    1
    1

    1

    1
    1

    1

    1

    1

    1
    1

    1

    1

    1
    1

    1

    1
    1
    1

    1

    1
    1
    1

    1
    1

    1

    1

    1
    1

    1
    1

    1
    1
    1
    1
    1

    1

    1
    1
    1

    1

    1
    1

    177
    177
    177

    1
    1
    1

    1

    1
    1

    1
     #!/usr/bin/env node
    var args = process.argv.slice(2)

    var fs = require('fs')
    Iif (!args.length && process.stdin.isTTY) {
      console.error(usage())
      process.exit(1)
    }

    process.stdout.on('error', function (er) {
      if (er.code === 'EPIPE')
        process.exit()
      else
        throw er
    })

    // defaults
    var nodeArgs = []

    var spawn = require('child_process').spawn
    var fg = require('foreground-child')
    var signalExit = require('signal-exit')
    var opener = require('opener')

    var timeout = process.env.TAP_TIMEOUT || 30
    // coverage tools run slow.
    Eif (global.__coverage__ || global.coverage)
      timeout = 240

    var color = require('supports-color')
    Iif (process.env.TAP_COLORS !== undefined)
      color = !!(+process.env.TAP_COLORS)
    var reporter
    var files = []
    var bail = false
    var saveFile = null

    var singleFlags = {
      b: 'bail',
      B: 'no-bail',
      c: 'color',
      C: 'no-color',
      h: 'help',
      '?': 'help',
      v: 'version'
    }
    var singleOpts = {
      R: 'reporter',
      t: 'timeout',
      s: 'save'
    }

    // If we're running under Travis-CI with a Coveralls.io token,
    // then it's a safe bet that we ought to output coverage.
    var coverage = !!process.env.COVERALLS_REPO_TOKEN

    var coverageReport

    var nycBin = require.resolve('nyc/bin/nyc.js')
    var coverallsBin = require.resolve('coveralls/bin/coveralls.js')

    for (var i = 0; i < args.length; i++) {
      var arg = args[i]
      if (arg.charAt(0) !== '-' || arg === '-') {
        files.push(arg)
        continue
      }

      // short-flags
      Iif (arg.charAt(1) !== '-' && arg !== '-gc') {
        var expand = []
        for (var f = 1; f < arg.length; f++) {
          var fc = arg.charAt(f)
          var sf = singleFlags[fc]
          var so = singleOpts[fc]
          if (sf)
            expand.push('--' + sf)
          else if (so) {
            expand.push('--' + so + '=' + arg.slice(f + 1))
            f = arg.length
          }
        }
        if (expand.length) {
          args.splice.apply(args, [i, 1].concat(expand))
          i --
          continue
        }
      }

      // support -Rspec as well as --reporter=spec
      arg = arg.replace(/^-R/, '--reporter=')

      var key = arg
      var val
      Iif (key.match(/^--/) && arg.indexOf('=') !== -1) {
        var kv = arg.split('=')
        key = kv.shift()
        val = kv.join('=')
      }

      switch (key) {
        case '--help':
          return console.log(usage())

        case '--version':
          return console.log(require('../package.json').version)

        case '--coverage-report':
          coverageReport = val || args[++i]
          if (!coverageReport) {
            if (!!process.env.COVERALLS_REPO_TOKEN)
              coverageReport = 'text-lcov'
            else
              coverageReport = 'text'
          }
          continue

        case '--no-cov': case '--no-coverage':
          coverage = false
          continue

        case '--cov': case '--coverage':
          coverage = true
          continue

        case '--save':
          val = val || args[++i]
          saveFile = val
          continue

        case '--reporter':
          val = val || args[++i]
          reporter = val
          continue

        case '--gc': case '-gc': case '--expose-gc':
          nodeArgs.push('--expose-gc')
          continue

        case '--strict':
          nodeArgs.push('--use_strict')
          continue

        case '--debug':
          nodeArgs.push('--debug')
          continue

        case '--debug-brk':
          nodeArgs.push('--debug-brk')
          continue

        case '--harmony':
          nodeArgs.push('--harmony')
          continue

        case '--color':
          color = true
          continue

        case '--no-color':
          color = false
          continue

        case '--timeout':
          val = val || args[++i]
          timeout = +val
          continue

        case '--bail':
          bail = true
          continue

        case '--no-bail':
          bail = false
          continue

        case '--':
          files = files.concat(args.slice(i + 1))
          i = args.length
          continue

        default:
          throw new Error('Unknown argument: ' + arg)
      }
    }

    Iif (coverage && !global.__coverage__) {
      // Re-spawn with coverage
      var node = process.execPath
      var args = [nycBin].concat(process.execArgv, process.argv.slice(1))
      var child = fg(node, args)
      child.removeAllListeners('close')
      child.on('close', function (code, signal) {
        if (signal)
          return process.kill(process.pid, signal)
        if (code)
          return process.exit(code)
        args = [__filename, '--no-coverage', '--coverage-report']
        if (coverageReport)
          args.push(coverageReport)
        fg(node, args)
      })
      return
    }

    Iif (coverageReport && !global.__coverage__ && files.length === 0) {
      var node = process.execPath
      var args = [nycBin, 'report', '--reporter', coverageReport]
      var child

      // automatically hook into coveralls
      if (coverageReport === 'text-lcov' && process.env.COVERALLS_REPO_TOKEN) {
        child = spawn(node, args)
        var ca = spawn(node, [coverallsBin], {
          stdio: [ 'pipe', 1, 2 ],
          env: process.env
        })
        child.stdout.pipe(ca.stdin)
        ca.on('close', function (code, signal) {
          if (signal)
            process.kill(process.pid, signal)
          else if (code)
            process.exit(code)
          else
            console.log('Successfully piped to Coveralls')
        })
        signalExit(function (code, signal) {
          child.kill('SIGHUP')
          ca.kill('SIGHUP')
        })
      } else {
        // otherwise just run the reporter
        var child = fg(node, args)
        if (coverageReport === 'lcov') {
          child.on('exit', function () {
            opener('coverage/lcov-report/index.html')
          })
        }
      }
      return
    }

    Iif (process.env.TAP === '1')
      reporter = 'tap'

    // default to tap for non-tty envs
    Eif (!reporter)
      reporter = color ? 'classic' : 'tap'

    function usage () {
      return fs.readFileSync(__dirname + '/usage.txt', 'utf8')
        .split('@@REPORTERS@@')
        .join(getReporters())
    }

    function getReporters () {
      var types = require('tap-mocha-reporter').types
      types = types.reduce(function (str, t) {
        var ll = str.split('\n').pop().length + t.length
        if (ll < 40)
          return str + ' ' + t
        else
          return str + '\n' + t
      }, '').trim()
      var ind = '                              '
      return ind + types.split('\n').join('\n' + ind)
    }

    var isExe
    Iif (process.platform == "win32") {
      // On windows, there is no good way to check that a file is executable
      isExe = function isExe () { return true }
    } else {
      isExe = function isExe (stat) {
        var mod = stat.mode
        var uid = stat.uid
        var gid = stat.gid
        var u = parseInt('100', 8)
        var g = parseInt('010', 8)
        var o = parseInt('001', 8)
        var ug = u | g

        var ret = (mod & o)
            || (mod & g) && process.getgid && gid === process.getgid()
            || (mod & u) && process.getuid && uid === process.getuid()
            || (mod & ug) && process.getuid && 0 === process.getuid()

        return ret
      }
    }

    process.env.TAP_TIMEOUT = timeout
    Eif (color)
      process.env.TAP_COLORS = 1
    else
      process.env.TAP_COLORS = 0

    Iif (bail)
      process.env.TAP_BAIL = '1'

    var glob = require('glob')
    files = files.reduce(function(acc, f) {
      Iif (f === '-') {
        acc.push(f)
        return acc
      }

      // glob claims patterns MUST not include any '\'s
      Eif (!/\\/.test(f)) {
        f = glob.sync(f) || f
      }
      return acc.concat(f)
    }, [])

    Iif (files.length === 0) {
      console.error('Reading TAP data from stdin (use "-" argument to suppress)')
      files.push('-')
    }

    var TMR = require('tap-mocha-reporter')
    Iif (files.length === 1 && files[0] === '-') {
      // if we didn't specify any files, then just passthrough
      // to the reporter, so we don't get '/dev/stdin' in the suite list.
      reporter = new TMR(reporter)
      process.stdin.pipe(reporter)
      return
    }

    var saved = files
    Iif (saveFile) {
      try {
        saved = fs.readFileSync(saveFile, 'utf8').trim().split('\n')
      } catch (er) {}
    }

    // At this point, we know we need to use the tap root,
    // because there are 1 or more files to spawn.
    var tap = require('../lib/root.js')
    Eif (reporter !== 'tap') {
      tap.unpipe(process.stdout)
      reporter = new TMR(reporter)
      tap.pipe(reporter)
    }

    Iif (saveFile) {
      var fails = []
      tap.on('result', function (res) {
        // we will continue to re-run todo tests, even though they're
        // not technically "failures".
        if (!res.ok && !res.extra.skip) {
          fails.push(res.extra.file)
        }
      })

      tap.on('end', function () {
        if (!fails.length)
          try {
            fs.unlinkSync(saveFile)
          } catch (er) {}
        else
          try {
            fs.writeFileSync(saveFile, fails.join('\n')+'\n')
          } catch (er){}
      })
    }

    for (var i = 0; i < files.length; i++) {
      var file = files[i]
      Iif (saved.indexOf(file) === -1)
        continue

      // Pick up stdin after all the other files are handled.
      Iif (file === '-') {
        tap.stdin()
        continue
      }

      var st = fs.statSync(files[i])
      var opt = {
        env: Object.keys(process.env).reduce(function (env, k) {
          Eif (!env[k])
            env[k] = process.env[k]
          return env
        }, {
          TAP: 1
        })
      }

      var extra = {}
      Eif (timeout)
        extra.timeout = timeout * 1000

      extra.file = file

      Eif (file.match(/\.js$/))
        tap.spawn(process.execPath, nodeArgs.concat(file), opt, file, extra)
      else if (st.isDirectory()) {
        files.push.apply(files, fs.readdirSync(file).map(function (f) {
          return file + '/' + f
        }))
      }
      else if (isExe(st))
        tap.spawn(files[i], [], opt, file, extra)
    }

    tap.end()