1'use strict' 2var path = require('path') 3var test = require('tap').test 4var mr = require('npm-registry-mock') 5var Tacks = require('tacks') 6var File = Tacks.File 7var Dir = Tacks.Dir 8var chain = require('slide').chain 9var common = require('../common-tap.js') 10 11var basedir = common.pkg 12var testdir = path.join(basedir, 'testdir') 13var cachedir = common.cache 14var globaldir = path.join(basedir, 'global') 15var tmpdir = path.join(basedir, 'tmp') 16 17var ciKeys = ['CI', 'TDDIUM', 'JENKINS_URL', 'bamboo.buildKey'] 18 19var filteredEnv = common.newEnv().delete(ciKeys) 20 21var conf = function (extraEnv) { 22 return { 23 cwd: testdir, 24 env: filteredEnv.clone().extend(extraEnv, { 25 npm_config_cache: cachedir, 26 npm_config_tmp: tmpdir, 27 npm_config_prefix: globaldir, 28 npm_config_registry: common.registry, 29 npm_config_loglevel: 'warn' 30 }) 31 } 32} 33 34var server 35var fixture = new Tacks(Dir({ 36 cache: Dir(), 37 global: Dir(), 38 tmp: Dir(), 39 testdir: Dir({ 40 'package.json': File({ 41 name: 'example', 42 version: '1.0.0' 43 }) 44 }) 45})) 46 47function setup () { 48 cleanup() 49 fixture.create(basedir) 50} 51 52function cleanup () { 53 fixture.remove(basedir) 54} 55 56var ciHeaderTestCI = { 57 'time': {'1.0.0': ''}, 58 'dist-tags': {'latest': '1.0.0'}, 59 'versions': { 60 '1.0.0': { 61 name: 'ci-header-test', 62 version: '1.0.0', 63 gotCI: true 64 } 65 } 66} 67var ciHeaderTestNotCI = { 68 'time': {'1.0.0': ''}, 69 'dist-tags': {'latest': '1.0.0'}, 70 'versions': { 71 '1.0.0': { 72 name: 'ci-header-test', 73 version: '1.0.0', 74 gotCI: false 75 } 76 } 77} 78 79var ciHeaderTestNoCI = { 80 'time': {'1.0.0': ''}, 81 'dist-tags': {'latest': '1.0.0'}, 82 'versions': { 83 '1.0.0': { 84 name: 'ci-header-test', 85 version: '1.0.0', 86 gotCI: null 87 } 88 } 89} 90 91test('setup', function (t) { 92 setup() 93 mr({port: common.port, throwOnUnmatched: true}, function (err, s) { 94 if (err) throw err 95 server = s 96 server.get('/ci-header-test', { 97 'NPM-In-CI': 'true' 98 }).many().reply('200', ciHeaderTestCI) 99 server.get('/ci-header-test', { 100 'NPM-In-CI': 'false' 101 }).many().reply('200', ciHeaderTestNotCI) 102 server.get('/ci-header-test', {}).many().reply('200', ciHeaderTestNoCI) 103 t.done() 104 }) 105}) 106 107test('various-ci', function (t) { 108 var todo = ciKeys.map(function (key) { return [checkKey, key] }) 109 return chain(todo, t.done) 110 111 function checkKey (key, next) { 112 var env = {} 113 env[key] = 'true' 114 115 common.npm(['view', '--cache-min=0', 'ci-header-test', 'gotCI'], conf(env), function (err, code, stdout, stderr) { 116 if (err) throw err 117 t.is(code, 0, key + ' command ran ok') 118 if (stderr.trim()) t.comment(stderr.trim()) 119 t.is(stdout.trim(), 'true', key + ' set results in CI header') 120 next() 121 }) 122 } 123}) 124 125test('no-ci', function (t) { 126 common.npm(['view', '--cache-min=0', 'ci-header-test', 'gotCI'], conf(), function (err, code, stdout, stderr) { 127 if (err) throw err 128 t.is(code, 0, 'No CI env, command ran ok') 129 if (stderr.trim()) t.comment(stderr.trim()) 130 t.is(stdout.trim(), 'false', 'No CI env, not in CI') 131 t.done() 132 }) 133}) 134 135test('cleanup', function (t) { 136 server.close() 137 cleanup() 138 t.done() 139}) 140