• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict'
2
3const fs = require('graceful-fs')
4const path = require('path')
5
6const mkdirp = require('mkdirp')
7const mr = require('npm-registry-mock')
8const rimraf = require('rimraf')
9const test = require('tap').test
10
11const common = require('../common-tap.js')
12
13const pkg = common.pkg
14
15const EXEC_OPTS = { cwd: pkg }
16
17const json = {
18  name: 'install-save-consistent-newlines',
19  version: '0.0.1',
20  description: 'fixture'
21}
22
23test('mock registry', function (t) {
24  mr({ port: common.port }, function (er, s) {
25    t.parent.teardown(() => s.close())
26    t.end()
27  })
28})
29
30const runTest = (t, opts) => {
31  t.test('setup', setup(opts.ending))
32  t.test('check', check(opts))
33  t.end()
34}
35
36const setup = lineEnding => t => {
37  rimraf(pkg, er => {
38    if (er) {
39      throw er
40    }
41    mkdirp.sync(path.resolve(pkg, 'node_modules'))
42
43    var jsonStr = JSON.stringify(json, null, 2)
44
45    if (lineEnding === '\r\n') {
46      jsonStr = jsonStr.replace(/\n/g, '\r\n')
47    }
48
49    fs.writeFileSync(
50      path.join(pkg, 'package.json'),
51      jsonStr
52    )
53
54    t.end()
55  })
56}
57
58const check = opts => t => common.npm(
59  [
60    '--loglevel', 'silent',
61    '--registry', common.registry,
62    '--save',
63    'install', 'underscore@1.3.1'
64  ],
65  EXEC_OPTS
66).then(([code, err, out]) => {
67  t.notOk(code, 'npm install exited without raising an error code')
68
69  const pkgPath = path.resolve(pkg, 'package.json')
70  const pkgStr = fs.readFileSync(pkgPath, 'utf8')
71
72  t.match(pkgStr, opts.match)
73  t.notMatch(pkgStr, opts.notMatch)
74
75  const pkgLockPath = path.resolve(pkg, 'package-lock.json')
76  const pkgLockStr = fs.readFileSync(pkgLockPath, 'utf8')
77
78  t.match(pkgLockStr, opts.match)
79  t.notMatch(pkgLockStr, opts.notMatch)
80
81  t.end()
82})
83
84test('keep LF line endings', t => {
85  runTest(t, {
86    ending: '\n',
87    match: '\n',
88    notMatch: '\r'
89  })
90})
91
92test('keep CRLF line endings', t => {
93  runTest(t, {
94    ending: '\r\n',
95    match: '\r\n',
96    notMatch: /[^\r]\n/
97  })
98})
99