summaryrefslogtreecommitdiff
path: root/deps/npm/test/tap/config-envReplace.js
blob: 0b4f628d67339aa66f81dfddab073e1fedff9826 (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
/* eslint-disable no-template-curly-in-string */

const fs = require('fs')
const mkdirp = require('mkdirp')
const rimraf = require('rimraf')
const path = require('path')
const ini = require('ini')
const test = require('tap').test
const npmconf = require('../../lib/config/core.js')

const packagePath = path.resolve(__dirname, 'config-envReplace')

const packageJsonFile = JSON.stringify({
  name: 'config-envReplace'
})

const inputConfigFile = [
  'registry=${NPM_REGISTRY_URL}',
  '//${NPM_REGISTRY_HOST}/:_authToken=${NPM_AUTH_TOKEN}',
  'always-auth=true',
  ''
].join('\n')

const expectConfigFile = [
  'registry=http://my.registry.com/',
  '//my.registry.com/:_authToken=xxxxxxxxxxxxxxx',
  'always-auth=true',
  ''
].join('\n')

test('environment variables replacing in configs', function (t) {
  process.env = Object.assign(process.env, {
    NPM_REGISTRY_URL: 'http://my.registry.com/',
    NPM_REGISTRY_HOST: 'my.registry.com',
    NPM_AUTH_TOKEN: 'xxxxxxxxxxxxxxx'
  })
  mkdirp.sync(packagePath)
  const packageJsonPath = path.resolve(packagePath, 'package.json')
  const configPath = path.resolve(packagePath, '.npmrc')
  fs.writeFileSync(packageJsonPath, packageJsonFile)
  fs.writeFileSync(configPath, inputConfigFile)

  const originalCwdPath = process.cwd()
  process.chdir(packagePath)
  npmconf.load(function (error, conf) {
    if (error) throw error

    const foundConfigFile = ini.stringify(conf.sources.project.data)
    t.same(ini.parse(foundConfigFile), ini.parse(expectConfigFile))

    fs.unlinkSync(packageJsonPath)
    fs.unlinkSync(configPath)
    rimraf.sync(packagePath)
    process.chdir(originalCwdPath)
    t.end()
  })
})