summaryrefslogtreecommitdiff
path: root/deps/npm/test/tap/add-local.js
blob: cc615b8997fb8f68790a5720e409a5e5f3f5c4e8 (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
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
var path = require('path')
var test = require('tap').test
var mkdirp = require('mkdirp')
var osenv = require('osenv')
var rimraf = require('rimraf')
var requireInject = require('require-inject')

var pkg = path.join(__dirname, '/local-dir')
var cache = path.join(pkg, '/cache')
var tmp = path.join(pkg, '/tmp')
var prefix = path.join(pkg, '/prefix')

var Tacks = require('tacks')
var File = Tacks.File
var Dir = Tacks.Dir

test('addLocal directory race on Windows', function (t) {
  setup()
  var p = {
    name: 'test',
    version: '1.0.0',
    type: 'directory',
    spec: pkg
  }
  var fixture = new Tacks(
    Dir({
      'package.json': File(p)
    })
  )
  var addLocal = requireInject('../../lib/cache/add-local', {
    '../../lib/npm.js': {
      cache: cache,
      tmp: tmp,
      prefix: prefix
    },
    '../../lib/cache/get-stat': function (cb) {
      cb(null, {})
    },
    chownr: function (x, y, z, cb) {
      cb(new Error('chownr should never have been called'))
    },
    '../../lib/cache/add-local-tarball.js': function (tgz, data, shasum, cb) {
      cb(null)
    },
    '../../lib/utils/lifecycle.js': function (data, cycle, p, cb) {
      cb(null)
    },
    '../../lib/utils/tar.js': {
      pack: function (tgz, p, data, cb) {
        cb(null)
      }
    },
    'sha': {
      get: function (tgz, cb) {
        cb(null, 'deadbeef')
      }
    }
  })

  fixture.create(pkg)
  addLocal(p, null, function (err) {
    t.ifErr(err, 'addLocal completed without error')
    t.done()
  })
})

test('addLocal temporary cache file race', function (t) {
  // See https://github.com/npm/npm/issues/12669
  setup()
  var p = {
    name: 'test',
    version: '1.0.0',
    type: 'directory',
    spec: pkg
  }
  var fixture = new Tacks(
    Dir({
      'package.json': File(p)
    })
  )
  var addLocal = requireInject('../../lib/cache/add-local', {
    // basic setup/mock stuff
    '../../lib/npm.js': {
      cache: cache,
      tmp: tmp,
      prefix: prefix
    },
    '../../lib/cache/add-local-tarball.js': function (tgz, data, shasum, cb) {
      cb(null)
    },
    '../../lib/utils/lifecycle.js': function (data, cycle, p, cb) {
      cb(null)
    },
    '../../lib/utils/tar.js': {
      pack: function (tgz, p, data, cb) {
        cb(null)
      }
    },
    'sha': {
      get: function (tgz, cb) {
        cb(null, 'deadbeef')
      }
    },

    // Test-specific mocked values to simulate race.
    '../../lib/cache/get-stat': function (cb) {
      cb(null, {uid: 1, gid: 2})
    },
    chownr: function (x, y, z, cb) {
      // Simulate a race condition between `tar.pack` and `chownr`
      // where the latter will return `ENOENT` when an async process
      // removes a file that its internal `fs.readdir` listed.
      cb({code: 'ENOENT'})
    }
  })

  fixture.create(pkg)
  addLocal(p, null, function (err) {
    t.ifErr(err, 'addLocal completed without error')
    t.done()
  })
})

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

function setup () {
  mkdirp.sync(cache)
  mkdirp.sync(tmp)
}

function cleanup () {
  process.chdir(osenv.tmpdir())
  rimraf.sync(pkg)
}