1/* eslint-disable no-template-curly-in-string */ 2 3const fs = require('fs') 4const mkdirp = require('mkdirp') 5const rimraf = require('rimraf') 6const path = require('path') 7const ini = require('ini') 8const test = require('tap').test 9const npmconf = require('../../lib/config/core.js') 10 11const common = require('../common-tap.js') 12const packagePath = common.pkg 13 14const packageJsonFile = JSON.stringify({ 15 name: 'config-envReplace' 16}) 17 18const inputConfigFile = [ 19 'registry=${NPM_REGISTRY_URL}', 20 '//${NPM_REGISTRY_HOST}/:_authToken=${NPM_AUTH_TOKEN}', 21 'always-auth=true', 22 '' 23].join('\n') 24 25const expectConfigFile = [ 26 'registry=http://my.registry.com/', 27 '//my.registry.com/:_authToken=xxxxxxxxxxxxxxx', 28 'always-auth=true', 29 '' 30].join('\n') 31 32test('environment variables replacing in configs', function (t) { 33 process.env = Object.assign(process.env, { 34 NPM_REGISTRY_URL: 'http://my.registry.com/', 35 NPM_REGISTRY_HOST: 'my.registry.com', 36 NPM_AUTH_TOKEN: 'xxxxxxxxxxxxxxx' 37 }) 38 mkdirp.sync(packagePath) 39 const packageJsonPath = path.resolve(packagePath, 'package.json') 40 const configPath = path.resolve(packagePath, '.npmrc') 41 fs.writeFileSync(packageJsonPath, packageJsonFile) 42 fs.writeFileSync(configPath, inputConfigFile) 43 44 const originalCwdPath = process.cwd() 45 process.chdir(packagePath) 46 npmconf.load(function (error, conf) { 47 if (error) throw error 48 49 const foundConfigFile = ini.stringify(conf.sources.project.data) 50 t.same(ini.parse(foundConfigFile), ini.parse(expectConfigFile)) 51 52 fs.unlinkSync(packageJsonPath) 53 fs.unlinkSync(configPath) 54 rimraf.sync(packagePath) 55 process.chdir(originalCwdPath) 56 t.end() 57 }) 58}) 59