11cb0ef41Sopenharmony_civar t = require('tap')
21cb0ef41Sopenharmony_civar fs = require('fs')
31cb0ef41Sopenharmony_civar path = require('path')
41cb0ef41Sopenharmony_civar fixture = path.resolve(__dirname, 'fixtures')
51cb0ef41Sopenharmony_civar meow = fixture + '/meow.cat'
61cb0ef41Sopenharmony_civar mine = fixture + '/mine.cat'
71cb0ef41Sopenharmony_civar ours = fixture + '/ours.cat'
81cb0ef41Sopenharmony_civar fail = fixture + '/fail.false'
91cb0ef41Sopenharmony_civar noent = fixture + '/enoent.exe'
101cb0ef41Sopenharmony_civar mkdirp = require('mkdirp')
111cb0ef41Sopenharmony_civar rimraf = require('rimraf')
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_civar isWindows = process.platform === 'win32'
141cb0ef41Sopenharmony_civar hasAccess = typeof fs.access === 'function'
151cb0ef41Sopenharmony_civar winSkip = isWindows && 'windows'
161cb0ef41Sopenharmony_civar accessSkip = !hasAccess && 'no fs.access function'
171cb0ef41Sopenharmony_civar hasPromise = typeof Promise === 'function'
181cb0ef41Sopenharmony_civar promiseSkip = !hasPromise && 'no global Promise'
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_cifunction reset () {
211cb0ef41Sopenharmony_ci  delete require.cache[require.resolve('../')]
221cb0ef41Sopenharmony_ci  return require('../')
231cb0ef41Sopenharmony_ci}
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_cit.test('setup fixtures', function (t) {
261cb0ef41Sopenharmony_ci  rimraf.sync(fixture)
271cb0ef41Sopenharmony_ci  mkdirp.sync(fixture)
281cb0ef41Sopenharmony_ci  fs.writeFileSync(meow, '#!/usr/bin/env cat\nmeow\n')
291cb0ef41Sopenharmony_ci  fs.chmodSync(meow, parseInt('0755', 8))
301cb0ef41Sopenharmony_ci  fs.writeFileSync(fail, '#!/usr/bin/env false\n')
311cb0ef41Sopenharmony_ci  fs.chmodSync(fail, parseInt('0644', 8))
321cb0ef41Sopenharmony_ci  fs.writeFileSync(mine, '#!/usr/bin/env cat\nmine\n')
331cb0ef41Sopenharmony_ci  fs.chmodSync(mine, parseInt('0744', 8))
341cb0ef41Sopenharmony_ci  fs.writeFileSync(ours, '#!/usr/bin/env cat\nours\n')
351cb0ef41Sopenharmony_ci  fs.chmodSync(ours, parseInt('0754', 8))
361cb0ef41Sopenharmony_ci  t.end()
371cb0ef41Sopenharmony_ci})
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_cit.test('promise', { skip: promiseSkip }, function (t) {
401cb0ef41Sopenharmony_ci  var isexe = reset()
411cb0ef41Sopenharmony_ci  t.test('meow async', function (t) {
421cb0ef41Sopenharmony_ci    isexe(meow).then(function (is) {
431cb0ef41Sopenharmony_ci      t.ok(is)
441cb0ef41Sopenharmony_ci      t.end()
451cb0ef41Sopenharmony_ci    })
461cb0ef41Sopenharmony_ci  })
471cb0ef41Sopenharmony_ci  t.test('fail async', function (t) {
481cb0ef41Sopenharmony_ci    isexe(fail).then(function (is) {
491cb0ef41Sopenharmony_ci      t.notOk(is)
501cb0ef41Sopenharmony_ci      t.end()
511cb0ef41Sopenharmony_ci    })
521cb0ef41Sopenharmony_ci  })
531cb0ef41Sopenharmony_ci  t.test('noent async', function (t) {
541cb0ef41Sopenharmony_ci    isexe(noent).catch(function (er) {
551cb0ef41Sopenharmony_ci      t.ok(er)
561cb0ef41Sopenharmony_ci      t.end()
571cb0ef41Sopenharmony_ci    })
581cb0ef41Sopenharmony_ci  })
591cb0ef41Sopenharmony_ci  t.test('noent ignore async', function (t) {
601cb0ef41Sopenharmony_ci    isexe(noent, { ignoreErrors: true }).then(function (is) {
611cb0ef41Sopenharmony_ci      t.notOk(is)
621cb0ef41Sopenharmony_ci      t.end()
631cb0ef41Sopenharmony_ci    })
641cb0ef41Sopenharmony_ci  })
651cb0ef41Sopenharmony_ci  t.end()
661cb0ef41Sopenharmony_ci})
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_cit.test('no promise', function (t) {
691cb0ef41Sopenharmony_ci  global.Promise = null
701cb0ef41Sopenharmony_ci  var isexe = reset()
711cb0ef41Sopenharmony_ci  t.throws('try to meow a promise', function () {
721cb0ef41Sopenharmony_ci    isexe(meow)
731cb0ef41Sopenharmony_ci  })
741cb0ef41Sopenharmony_ci  t.end()
751cb0ef41Sopenharmony_ci})
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_cit.test('access', { skip: accessSkip || winSkip }, function (t) {
781cb0ef41Sopenharmony_ci  runTest(t)
791cb0ef41Sopenharmony_ci})
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_cit.test('mode', { skip: winSkip }, function (t) {
821cb0ef41Sopenharmony_ci  delete fs.access
831cb0ef41Sopenharmony_ci  delete fs.accessSync
841cb0ef41Sopenharmony_ci  var isexe = reset()
851cb0ef41Sopenharmony_ci  t.ok(isexe.sync(ours, { uid: 0, gid: 0 }))
861cb0ef41Sopenharmony_ci  t.ok(isexe.sync(mine, { uid: 0, gid: 0 }))
871cb0ef41Sopenharmony_ci  runTest(t)
881cb0ef41Sopenharmony_ci})
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_cit.test('windows', function (t) {
911cb0ef41Sopenharmony_ci  global.TESTING_WINDOWS = true
921cb0ef41Sopenharmony_ci  var pathExt = '.EXE;.CAT;.CMD;.COM'
931cb0ef41Sopenharmony_ci  t.test('pathExt option', function (t) {
941cb0ef41Sopenharmony_ci    runTest(t, { pathExt: '.EXE;.CAT;.CMD;.COM' })
951cb0ef41Sopenharmony_ci  })
961cb0ef41Sopenharmony_ci  t.test('pathExt env', function (t) {
971cb0ef41Sopenharmony_ci    process.env.PATHEXT = pathExt
981cb0ef41Sopenharmony_ci    runTest(t)
991cb0ef41Sopenharmony_ci  })
1001cb0ef41Sopenharmony_ci  t.test('no pathExt', function (t) {
1011cb0ef41Sopenharmony_ci    // with a pathExt of '', any filename is fine.
1021cb0ef41Sopenharmony_ci    // so the "fail" one would still pass.
1031cb0ef41Sopenharmony_ci    runTest(t, { pathExt: '', skipFail: true })
1041cb0ef41Sopenharmony_ci  })
1051cb0ef41Sopenharmony_ci  t.test('pathext with empty entry', function (t) {
1061cb0ef41Sopenharmony_ci    // with a pathExt of '', any filename is fine.
1071cb0ef41Sopenharmony_ci    // so the "fail" one would still pass.
1081cb0ef41Sopenharmony_ci    runTest(t, { pathExt: ';' + pathExt, skipFail: true })
1091cb0ef41Sopenharmony_ci  })
1101cb0ef41Sopenharmony_ci  t.end()
1111cb0ef41Sopenharmony_ci})
1121cb0ef41Sopenharmony_ci
1131cb0ef41Sopenharmony_cit.test('cleanup', function (t) {
1141cb0ef41Sopenharmony_ci  rimraf.sync(fixture)
1151cb0ef41Sopenharmony_ci  t.end()
1161cb0ef41Sopenharmony_ci})
1171cb0ef41Sopenharmony_ci
1181cb0ef41Sopenharmony_cifunction runTest (t, options) {
1191cb0ef41Sopenharmony_ci  var isexe = reset()
1201cb0ef41Sopenharmony_ci
1211cb0ef41Sopenharmony_ci  var optionsIgnore = Object.create(options || {})
1221cb0ef41Sopenharmony_ci  optionsIgnore.ignoreErrors = true
1231cb0ef41Sopenharmony_ci
1241cb0ef41Sopenharmony_ci  if (!options || !options.skipFail) {
1251cb0ef41Sopenharmony_ci    t.notOk(isexe.sync(fail, options))
1261cb0ef41Sopenharmony_ci  }
1271cb0ef41Sopenharmony_ci  t.notOk(isexe.sync(noent, optionsIgnore))
1281cb0ef41Sopenharmony_ci  if (!options) {
1291cb0ef41Sopenharmony_ci    t.ok(isexe.sync(meow))
1301cb0ef41Sopenharmony_ci  } else {
1311cb0ef41Sopenharmony_ci    t.ok(isexe.sync(meow, options))
1321cb0ef41Sopenharmony_ci  }
1331cb0ef41Sopenharmony_ci
1341cb0ef41Sopenharmony_ci  t.ok(isexe.sync(mine, options))
1351cb0ef41Sopenharmony_ci  t.ok(isexe.sync(ours, options))
1361cb0ef41Sopenharmony_ci  t.throws(function () {
1371cb0ef41Sopenharmony_ci    isexe.sync(noent, options)
1381cb0ef41Sopenharmony_ci  })
1391cb0ef41Sopenharmony_ci
1401cb0ef41Sopenharmony_ci  t.test('meow async', function (t) {
1411cb0ef41Sopenharmony_ci    if (!options) {
1421cb0ef41Sopenharmony_ci      isexe(meow, function (er, is) {
1431cb0ef41Sopenharmony_ci        if (er) {
1441cb0ef41Sopenharmony_ci          throw er
1451cb0ef41Sopenharmony_ci        }
1461cb0ef41Sopenharmony_ci        t.ok(is)
1471cb0ef41Sopenharmony_ci        t.end()
1481cb0ef41Sopenharmony_ci      })
1491cb0ef41Sopenharmony_ci    } else {
1501cb0ef41Sopenharmony_ci      isexe(meow, options, function (er, is) {
1511cb0ef41Sopenharmony_ci        if (er) {
1521cb0ef41Sopenharmony_ci          throw er
1531cb0ef41Sopenharmony_ci        }
1541cb0ef41Sopenharmony_ci        t.ok(is)
1551cb0ef41Sopenharmony_ci        t.end()
1561cb0ef41Sopenharmony_ci      })
1571cb0ef41Sopenharmony_ci    }
1581cb0ef41Sopenharmony_ci  })
1591cb0ef41Sopenharmony_ci
1601cb0ef41Sopenharmony_ci  t.test('mine async', function (t) {
1611cb0ef41Sopenharmony_ci    isexe(mine, options, function (er, is) {
1621cb0ef41Sopenharmony_ci      if (er) {
1631cb0ef41Sopenharmony_ci        throw er
1641cb0ef41Sopenharmony_ci      }
1651cb0ef41Sopenharmony_ci      t.ok(is)
1661cb0ef41Sopenharmony_ci      t.end()
1671cb0ef41Sopenharmony_ci    })
1681cb0ef41Sopenharmony_ci  })
1691cb0ef41Sopenharmony_ci
1701cb0ef41Sopenharmony_ci  t.test('ours async', function (t) {
1711cb0ef41Sopenharmony_ci    isexe(ours, options, function (er, is) {
1721cb0ef41Sopenharmony_ci      if (er) {
1731cb0ef41Sopenharmony_ci        throw er
1741cb0ef41Sopenharmony_ci      }
1751cb0ef41Sopenharmony_ci      t.ok(is)
1761cb0ef41Sopenharmony_ci      t.end()
1771cb0ef41Sopenharmony_ci    })
1781cb0ef41Sopenharmony_ci  })
1791cb0ef41Sopenharmony_ci
1801cb0ef41Sopenharmony_ci  if (!options || !options.skipFail) {
1811cb0ef41Sopenharmony_ci    t.test('fail async', function (t) {
1821cb0ef41Sopenharmony_ci      isexe(fail, options, function (er, is) {
1831cb0ef41Sopenharmony_ci        if (er) {
1841cb0ef41Sopenharmony_ci          throw er
1851cb0ef41Sopenharmony_ci        }
1861cb0ef41Sopenharmony_ci        t.notOk(is)
1871cb0ef41Sopenharmony_ci        t.end()
1881cb0ef41Sopenharmony_ci      })
1891cb0ef41Sopenharmony_ci    })
1901cb0ef41Sopenharmony_ci  }
1911cb0ef41Sopenharmony_ci
1921cb0ef41Sopenharmony_ci  t.test('noent async', function (t) {
1931cb0ef41Sopenharmony_ci    isexe(noent, options, function (er, is) {
1941cb0ef41Sopenharmony_ci      t.ok(er)
1951cb0ef41Sopenharmony_ci      t.notOk(is)
1961cb0ef41Sopenharmony_ci      t.end()
1971cb0ef41Sopenharmony_ci    })
1981cb0ef41Sopenharmony_ci  })
1991cb0ef41Sopenharmony_ci
2001cb0ef41Sopenharmony_ci  t.test('noent ignore async', function (t) {
2011cb0ef41Sopenharmony_ci    isexe(noent, optionsIgnore, function (er, is) {
2021cb0ef41Sopenharmony_ci      if (er) {
2031cb0ef41Sopenharmony_ci        throw er
2041cb0ef41Sopenharmony_ci      }
2051cb0ef41Sopenharmony_ci      t.notOk(is)
2061cb0ef41Sopenharmony_ci      t.end()
2071cb0ef41Sopenharmony_ci    })
2081cb0ef41Sopenharmony_ci  })
2091cb0ef41Sopenharmony_ci
2101cb0ef41Sopenharmony_ci  t.test('directory is not executable', function (t) {
2111cb0ef41Sopenharmony_ci    isexe(__dirname, options, function (er, is) {
2121cb0ef41Sopenharmony_ci      if (er) {
2131cb0ef41Sopenharmony_ci        throw er
2141cb0ef41Sopenharmony_ci      }
2151cb0ef41Sopenharmony_ci      t.notOk(is)
2161cb0ef41Sopenharmony_ci      t.end()
2171cb0ef41Sopenharmony_ci    })
2181cb0ef41Sopenharmony_ci  })
2191cb0ef41Sopenharmony_ci
2201cb0ef41Sopenharmony_ci  t.end()
2211cb0ef41Sopenharmony_ci}
222