summaryrefslogtreecommitdiff
path: root/deps/npm/test/tap/install-save-consistent-newlines.js
blob: 6250377445a79fe76828fa5dc6c25ceb4ebd4464 (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
'use strict'

const fs = require('graceful-fs')
const path = require('path')

const mkdirp = require('mkdirp')
const mr = require('npm-registry-mock')
const osenv = require('osenv')
const rimraf = require('rimraf')
const test = require('tap').test

const common = require('../common-tap.js')

const pkg = path.join(__dirname, 'install-save-consistent-newlines')

const EXEC_OPTS = { cwd: pkg }

const json = {
  name: 'install-save-consistent-newlines',
  version: '0.0.1',
  description: 'fixture'
}

var server

test('setup', function (t) {
  setup('\n')
  mr({ port: common.port }, function (er, s) {
    server = s
    t.end()
  })
})

test('\'npm install --save\' should keep the original package.json line endings (LF)', function (t) {
  common.npm(
    [
      '--loglevel', 'silent',
      '--registry', common.registry,
      '--save',
      'install', 'underscore@1.3.1'
    ],
    EXEC_OPTS,
    function (err, code) {
      t.ifError(err, 'npm ran without issue')
      t.notOk(code, 'npm install exited without raising an error code')

      const pkgPath = path.resolve(pkg, 'package.json')
      const pkgStr = fs.readFileSync(pkgPath, 'utf8')

      t.match(pkgStr, '\n')
      t.notMatch(pkgStr, '\r')

      const pkgLockPath = path.resolve(pkg, 'package-lock.json')
      const pkgLockStr = fs.readFileSync(pkgLockPath, 'utf8')

      t.match(pkgLockStr, '\n')
      t.notMatch(pkgLockStr, '\r')

      t.end()
    }
  )
})

test('\'npm install --save\' should keep the original package.json line endings (CRLF)', function (t) {
  setup('\r\n')

  common.npm(
    [
      '--loglevel', 'silent',
      '--registry', common.registry,
      '--save',
      'install', 'underscore@1.3.1'
    ],
    EXEC_OPTS,
    function (err, code) {
      t.ifError(err, 'npm ran without issue')
      t.notOk(code, 'npm install exited without raising an error code')

      const pkgPath = path.resolve(pkg, 'package.json')
      const pkgStr = fs.readFileSync(pkgPath, 'utf8')

      t.match(pkgStr, '\r\n')
      t.notMatch(pkgStr, /[^\r]\n/)

      const pkgLockPath = path.resolve(pkg, 'package-lock.json')
      const pkgLockStr = fs.readFileSync(pkgLockPath, 'utf8')

      t.match(pkgLockStr, '\r\n')
      t.notMatch(pkgLockStr, /[^\r]\n/)

      t.end()
    }
  )
})

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

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

function setup (lineEnding) {
  cleanup()
  mkdirp.sync(path.resolve(pkg, 'node_modules'))

  var jsonStr = JSON.stringify(json, null, 2)

  if (lineEnding === '\r\n') {
    jsonStr = jsonStr.replace(/\n/g, '\r\n')
  }

  fs.writeFileSync(
    path.join(pkg, 'package.json'),
    jsonStr
  )
  process.chdir(pkg)
}