summaryrefslogtreecommitdiff
path: root/deps/npm/test/tap/version-consistent-newlines.js
blob: 583874db7a3051fe516df37d93b86c44d54245ba (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
'use strict'

const common = require('../common-tap.js')
const test = require('tap').test
const npm = require('../../')
const path = require('path')
const fs = require('fs')
const mkdirp = require('mkdirp')
const requireInject = require('require-inject')

const pkg = common.pkg
const cache = common.cache
const gitDir = path.resolve(pkg, '.git')

test('npm version does not alter the line endings in package.json (LF)', function (t) {
  setup('\n')

  npm.load({cache: cache, registry: common.registry}, function () {
    const version = requireInject('../../lib/version', {
      which: function (cmd, cb) {
        process.nextTick(function () {
          cb(new Error('ENOGIT!'))
        })
      }
    })

    version(['patch'], function (err) {
      if (!t.error(err)) return t.end()

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

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

      t.end()
    })
  })
})

test('npm version does not alter the line endings in package.json (CRLF)', function (t) {
  setup('\r\n')

  npm.load({cache: cache, registry: common.registry}, function () {
    const version = requireInject('../../lib/version', {
      which: function (cmd, cb) {
        process.nextTick(function () {
          cb(new Error('ENOGIT!'))
        })
      }
    })

    version(['patch'], function (err) {
      if (!t.error(err)) return t.end()

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

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

      t.end()
    })
  })
})

function setup (lineEnding) {
  mkdirp.sync(gitDir)
  fs.writeFileSync(
    path.resolve(pkg, 'package.json'),
    JSON.stringify({
      author: 'Terin Stock',
      name: 'version-no-git-test',
      version: '0.0.0',
      description: "Test for npm version if git binary doesn't exist"
    }, null, 2).replace(/\n/g, lineEnding),
    'utf8'
  )
  process.chdir(pkg)
}