summaryrefslogtreecommitdiff
path: root/deps/npm/test/tap/check-permissions.js
blob: 0a5f2e038adf13f3f17cd52c3fd880af6ea536fd (plain)
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
'use strict'
var fs = require('fs')
var path = require('path')
var test = require('tap').test
var rimraf = require('rimraf')
var writable = require('../../lib/install/writable.js').fsAccessImplementation
var writableFallback = require('../../lib/install/writable.js').fsOpenImplementation
var exists = require('../../lib/install/exists.js').fsAccessImplementation
var existsFallback = require('../../lib/install/exists.js').fsStatImplementation

var testBase = path.resolve(__dirname, 'check-permissions')
var existingDir = path.resolve(testBase, 'exists')
var nonExistingDir = path.resolve(testBase, 'does-not-exist')
var writableDir = path.resolve(testBase, 'writable')
var nonWritableDir = path.resolve(testBase, 'non-writable')

test('setup', function (t) {
  cleanup()
  setup()
  t.end()
})

test('exists', function (t) {
  t.plan(2)
  // fs.access first introduced in node 0.12 / io.js
  if (fs.access) {
    existsTests(t, exists)
  } else {
    t.pass('# skip fs.access not available in this version')
    t.pass('# skip fs.access not available in this version')
  }
})

test('exists-fallback', function (t) {
  t.plan(2)
  existsTests(t, existsFallback)
})

test('writable', function (t) {
  t.plan(2)
  // fs.access first introduced in node 0.12 / io.js
  if (fs.access) {
    writableTests(t, writable)
  } else {
    t.pass('# skip fs.access not available in this version')
    t.pass('# skip fs.access not available in this version')
  }
})

test('writable-fallback', function (t) {
  t.plan(2)
  writableTests(t, writableFallback)
})

test('cleanup', function (t) {
  cleanup()
  t.end()
})

function setup () {
  fs.mkdirSync(testBase)
  fs.mkdirSync(existingDir)
  fs.mkdirSync(writableDir)
  fs.mkdirSync(nonWritableDir)
  fs.chmodSync(nonWritableDir, '555')
}

function existsTests (t, exists) {
  exists(existingDir, function (er) {
    t.error(er, 'exists dir is exists')
  })
  exists(nonExistingDir, function (er) {
    t.ok(er, 'non-existing dir resulted in an error')
  })
}

function writableTests (t, writable) {
  writable(writableDir, function (er) {
    t.error(er, 'writable dir is writable')
  })
  if (process.platform !== 'win32') {
    // Windows folders cannot be set to be read-only.
    writable(nonWritableDir, function (er) {
      t.ok(er, 'non-writable dir resulted in an error')
    })
  } else {
    t.pass('windows folders cannot be read-only')
  }
}

function cleanup () {
  rimraf.sync(testBase)
}