summaryrefslogtreecommitdiff
path: root/deps/npm/test/tap/install-save-exact.js
blob: c9f48666012e0814ca93fb3af1e5c890f7b0a56b (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
var common = require('../common-tap.js')
var test = require('tap').test
var npm = require('../../')
var path = require('path')
var fs = require('fs')
var rimraf = require('rimraf')
var mkdirp = require('mkdirp')
var pkg = path.join(__dirname, 'install-save-exact')
var mr = require("npm-registry-mock")

test("setup", function (t) {
  mkdirp.sync(pkg)
  mkdirp.sync(path.resolve(pkg, 'node_modules'))
  process.chdir(pkg)
  t.end()
})

test('"npm install --save --save-exact should install local pkg', function(t) {
  resetPackageJSON(pkg)
  mr(common.port, function (s) {
    npm.load({
      cache: pkg + "/cache",
      loglevel: 'silent',
      registry: common.registry }, function(err) {
        t.ifError(err)
        npm.config.set('save', true)
        npm.config.set('save-exact', true)
        npm.commands.install(['underscore@1.3.1'], function(err) {
          t.ifError(err)
          var p = path.resolve(pkg, 'node_modules/underscore/package.json')
          t.ok(JSON.parse(fs.readFileSync(p)))
          var pkgJson = JSON.parse(fs.readFileSync(pkg + '/package.json', 'utf8'))
          t.deepEqual(pkgJson.dependencies, {
            'underscore': '1.3.1'
          }, 'Underscore dependency should specify exactly 1.3.1')
          npm.config.set('save', undefined)
          npm.config.set('save-exact', undefined)
          s.close()
          t.end()
        })
      })
  })
})

test('"npm install --save-dev --save-exact should install local pkg', function(t) {
  resetPackageJSON(pkg)

  mr(common.port, function (s) {
    npm.load({
      cache: pkg + "/cache",
      loglevel: 'silent',
      registry: common.registry }, function(err) {
        t.ifError(err)
        npm.config.set('save-dev', true)
        npm.config.set('save-exact', true)
        npm.commands.install(['underscore@1.3.1'], function(err) {
          t.ifError(err)
          var p = path.resolve(pkg, 'node_modules/underscore/package.json')
          t.ok(JSON.parse(fs.readFileSync(p)))
          var pkgJson = JSON.parse(fs.readFileSync(pkg + '/package.json', 'utf8'))
          console.log(pkgJson)
          t.deepEqual(pkgJson.devDependencies, {
            'underscore': '1.3.1'
          }, 'underscore devDependency should specify exactly 1.3.1')
          s.close()
          npm.config.set('save-dev', undefined)
          npm.config.set('save-exact', undefined)
          t.end()
        })
      })
  })
})

test('cleanup', function(t) {
  process.chdir(__dirname)
  rimraf.sync(path.resolve(pkg, 'node_modules'))
  rimraf.sync(path.resolve(pkg, 'cache'))
  resetPackageJSON(pkg)
  t.end()
})

function resetPackageJSON(pkg) {
  var pkgJson = JSON.parse(fs.readFileSync(pkg + '/package.json', 'utf8'))
  delete pkgJson.dependencies
  delete pkgJson.devDependencies
  delete pkgJson.optionalDependencies
  var json = JSON.stringify(pkgJson, null, 2) + "\n"
  fs.writeFileSync(pkg + '/package.json', json, "ascii")
}