1'use strict' 2 3const common = require('../common-tap.js') 4const test = require('tap').test 5const npm = require('../../') 6const path = require('path') 7const fs = require('fs') 8const mkdirp = require('mkdirp') 9const requireInject = require('require-inject') 10 11const pkg = common.pkg 12const cache = common.cache 13const gitDir = path.resolve(pkg, '.git') 14 15test('npm version does not alter the line endings in package.json (LF)', function (t) { 16 setup('\n') 17 18 npm.load({cache: cache, registry: common.registry}, function () { 19 const version = requireInject('../../lib/version', { 20 which: function (cmd, cb) { 21 process.nextTick(function () { 22 cb(new Error('ENOGIT!')) 23 }) 24 } 25 }) 26 27 version(['patch'], function (err) { 28 if (!t.error(err)) return t.end() 29 30 const pkgPath = path.resolve(pkg, 'package.json') 31 const pkgStr = fs.readFileSync(pkgPath, 'utf8') 32 33 t.match(pkgStr, '\n') 34 t.notMatch(pkgStr, '\r') 35 36 t.end() 37 }) 38 }) 39}) 40 41test('npm version does not alter the line endings in package.json (CRLF)', function (t) { 42 setup('\r\n') 43 44 npm.load({cache: cache, registry: common.registry}, function () { 45 const version = requireInject('../../lib/version', { 46 which: function (cmd, cb) { 47 process.nextTick(function () { 48 cb(new Error('ENOGIT!')) 49 }) 50 } 51 }) 52 53 version(['patch'], function (err) { 54 if (!t.error(err)) return t.end() 55 56 const pkgPath = path.resolve(pkg, 'package.json') 57 const pkgStr = fs.readFileSync(pkgPath, 'utf8') 58 59 t.match(pkgStr, '\r\n') 60 t.notMatch(pkgStr, /[^\r]\n/) 61 62 t.end() 63 }) 64 }) 65}) 66 67function setup (lineEnding) { 68 mkdirp.sync(gitDir) 69 fs.writeFileSync( 70 path.resolve(pkg, 'package.json'), 71 JSON.stringify({ 72 author: 'Terin Stock', 73 name: 'version-no-git-test', 74 version: '0.0.0', 75 description: "Test for npm version if git binary doesn't exist" 76 }, null, 2).replace(/\n/g, lineEnding), 77 'utf8' 78 ) 79 process.chdir(pkg) 80} 81