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

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

const pkg = path.resolve(__dirname, 'version-no-git')
const cache = path.resolve(pkg, '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()
    })
  })
})

test('cleanup', function (t) {
  process.chdir(osenv.tmpdir())

  rimraf.sync(pkg)
  t.end()
})

function setup (lineEnding) {
  mkdirp.sync(pkg)
  mkdirp.sync(cache)
  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)
}