• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict'
2var fs = require('fs')
3var path = require('path')
4var test = require('tap').test
5var rimraf = require('rimraf')
6var writable = require('../../lib/install/writable.js').fsAccessImplementation
7var writableFallback = require('../../lib/install/writable.js').fsOpenImplementation
8var exists = require('../../lib/install/exists.js').fsAccessImplementation
9var existsFallback = require('../../lib/install/exists.js').fsStatImplementation
10
11const common = require('../common-tap.js')
12var testBase = common.pkg
13var existingDir = path.resolve(testBase, 'exists')
14var nonExistingDir = path.resolve(testBase, 'does-not-exist')
15var writableDir = path.resolve(testBase, 'writable')
16var nonWritableDir = path.resolve(testBase, 'non-writable')
17
18test('setup', function (t) {
19  cleanup()
20  setup()
21  t.end()
22})
23
24test('exists', function (t) {
25  t.plan(2)
26  // fs.access first introduced in node 0.12 / io.js
27  if (fs.access) {
28    existsTests(t, exists)
29  } else {
30    t.pass('# skip fs.access not available in this version')
31    t.pass('# skip fs.access not available in this version')
32  }
33})
34
35test('exists-fallback', function (t) {
36  t.plan(2)
37  existsTests(t, existsFallback)
38})
39
40test('writable', function (t) {
41  t.plan(2)
42  // fs.access first introduced in node 0.12 / io.js
43  if (fs.access) {
44    writableTests(t, writable)
45  } else {
46    t.pass('# skip fs.access not available in this version')
47    t.pass('# skip fs.access not available in this version')
48  }
49})
50
51test('writable-fallback', function (t) {
52  t.plan(2)
53  writableTests(t, writableFallback)
54})
55
56test('cleanup', function (t) {
57  cleanup()
58  t.end()
59})
60
61function setup () {
62  fs.mkdirSync(testBase)
63  fs.mkdirSync(existingDir)
64  fs.mkdirSync(writableDir)
65  fs.mkdirSync(nonWritableDir)
66  fs.chmodSync(nonWritableDir, '555')
67}
68
69function existsTests (t, exists) {
70  exists(existingDir, function (er) {
71    t.error(er, 'exists dir is exists')
72  })
73  exists(nonExistingDir, function (er) {
74    t.ok(er, 'non-existing dir resulted in an error')
75  })
76}
77
78function writableTests (t, writable) {
79  writable(writableDir, function (er) {
80    t.error(er, 'writable dir is writable')
81  })
82  if (process.platform === 'win32') {
83    t.pass('windows folders cannot be read-only')
84  } else if (process.getuid && process.getuid() === 0) {
85    t.pass('root is not blocked by read-only dirs')
86  } else {
87    writable(nonWritableDir, function (er) {
88      t.ok(er, 'non-writable dir resulted in an error')
89    })
90  }
91}
92
93function cleanup () {
94  rimraf.sync(testBase)
95}
96