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 common = require('../common-tap.js') 9 10var basedir = common.pkg 11var testdir = path.join(basedir, 'testdir') 12var withScope = path.join(testdir, 'with-scope') 13var withoutScope = path.join(testdir, 'without-scope') 14var onlyProjectScope = path.join(testdir, 'only-project-scope') 15var cachedir = common.cache 16var globaldir = path.join(basedir, 'global') 17var tmpdir = path.join(basedir, 'tmp') 18 19var env = common.newEnv().extend({ 20 npm_config_cache: cachedir, 21 npm_config_tmp: tmpdir, 22 npm_config_prefix: globaldir, 23 npm_config_registry: common.registry, 24 npm_config_loglevel: 'warn' 25}) 26 27var conf = function (cwd) { 28 return { 29 cwd: cwd || testdir, 30 env: env 31 } 32} 33var server 34var fixture = new Tacks(Dir({ 35 cache: Dir(), 36 global: Dir(), 37 tmp: Dir(), 38 testdir: Dir({ 39 'with-scope': Dir({ 40 'package.json': File({ 41 name: '@example/with-scope', 42 version: '1.0.0' 43 }) 44 }), 45 'without-scope': Dir({ 46 'package.json': File({ 47 name: 'without-scope', 48 version: '1.0.0' 49 }) 50 }), 51 'only-project-scope': Dir({ 52 '.npmrc': File('@example:registry=thisisinvalid\n'), 53 'package.json': File({ 54 name: '@example/only-project-scope', 55 version: '1.0.0' 56 }) 57 }) 58 }) 59})) 60 61function setup () { 62 cleanup() 63 fixture.create(basedir) 64} 65 66function cleanup () { 67 fixture.remove(basedir) 68} 69 70var scopeHeaderTestGotScope = { 71 'time': {'1.0.0': ''}, 72 'dist-tags': {'latest': '1.0.0'}, 73 'versions': { 74 '1.0.0': { 75 name: 'scope-header-test', 76 version: '1.0.0', 77 gotScope: true 78 } 79 } 80} 81var scopeHeaderTestNoScope = { 82 'time': {'1.0.0': ''}, 83 'dist-tags': {'latest': '1.0.0'}, 84 'versions': { 85 '1.0.0': { 86 name: 'scope-header-test', 87 version: '1.0.0', 88 gotScope: false 89 } 90 } 91} 92 93test('setup', function (t) { 94 setup() 95 mr({port: common.port, throwOnUnmatched: true}, function (err, s) { 96 if (err) throw err 97 server = s 98 server.get('/scope-header-test', { 99 'NPM-Scope': '@example' 100 }).many().reply('200', scopeHeaderTestGotScope) 101 server.get('/scope-header-test', {}).many().reply('200', scopeHeaderTestNoScope) 102 t.done() 103 }) 104}) 105 106test('with-scope', function (t) { 107 common.npm(['view', '--cache-min=0', 'scope-header-test', 'gotScope'], conf(withScope), function (err, code, stdout, stderr) { 108 if (err) throw err 109 t.is(code, 0, 'command ran ok') 110 t.comment(stderr.trim()) 111 t.is(stdout.trim(), 'true', 'got version matched to scope header') 112 t.done() 113 }) 114}) 115 116test('without-scope', function (t) { 117 common.npm(['view', '--cache-min=0', 'scope-header-test', 'gotScope'], conf(withoutScope), function (err, code, stdout, stderr) { 118 if (err) throw err 119 t.is(code, 0, 'command ran ok') 120 t.comment(stderr.trim()) 121 t.is(stdout.trim(), 'false', 'got version matched to NO scope header') 122 t.done() 123 }) 124}) 125 126test('scope can be forced by having an explicit one', function (t) { 127 common.npm([ 128 'view', 129 '--cache-min=0', 130 '--scope=example', 131 'scope-header-test', 132 'gotScope' 133 ], conf(withoutScope), function (err, code, stdout, stderr) { 134 if (err) throw err 135 t.is(code, 0, 'command ran ok') 136 t.comment(stderr.trim()) 137 t.is(stdout.trim(), 'true', 'got version matched to scope header') 138 t.done() 139 }) 140}) 141 142test('only the project scope is used', function (t) { 143 common.npm([ 144 'view', 145 '--cache-min=0', 146 'scope-header-test', 'gotScope' 147 ], conf(onlyProjectScope), function (err, code, stdout, stderr) { 148 if (err) throw err 149 t.is(code, 0, 'command ran ok') 150 t.comment(stderr.trim()) 151 t.is(stdout.trim(), 'true', 'got version scope') 152 t.done() 153 }) 154}) 155 156test('cleanup', function (t) { 157 server.close() 158 cleanup() 159 t.done() 160}) 161