1'use strict' 2var path = require('path') 3var fs = require('graceful-fs') 4var rimraf = require('rimraf') 5var test = require('tap').test 6var mr = require('npm-registry-mock') 7var Tacks = require('tacks') 8var File = Tacks.File 9var Dir = Tacks.Dir 10var common = require('../common-tap.js') 11 12var basedir = common.pkg 13var testdir = path.join(basedir, 'testdir') 14var cachedir = common.cache 15var globaldir = path.join(basedir, 'global') 16var tmpdir = path.join(basedir, 'tmp') 17var metricsFile = path.join(cachedir, 'anonymous-cli-metrics.json') 18 19var conf = { 20 cwd: testdir, 21 env: Object.assign({}, process.env, { 22 npm_config_cache: cachedir, 23 npm_config_tmp: tmpdir, 24 npm_config_prefix: globaldir, 25 npm_config_registry: common.registry, 26 npm_config_metrics_registry: null, 27 npm_config_loglevel: 'warn' 28 }) 29} 30 31var server 32var fixture = new Tacks(Dir({ 33 cache: Dir(), 34 global: Dir(), 35 tmp: Dir(), 36 testdir: Dir({ 37 failure: Dir({ 38 'package.json': File({ 39 name: 'failure', 40 version: '1.0.0', 41 scripts: { 42 preinstall: 'false' 43 } 44 }) 45 }), 46 success: Dir({ 47 'package.json': File({ 48 name: 'success', 49 version: '1.0.0' 50 }) 51 }), 52 slow: Dir({ 53 'package.json': File({ 54 name: 'slow', 55 version: '1.0.0', 56 scripts: { 57 preinstall: 'node -e "setTimeout(function(){}, 500)"' 58 } 59 }) 60 }), 61 'package.json': File({ 62 name: 'anon-cli-metrics-test', 63 version: '1.0.0' 64 }) 65 }) 66})) 67 68function setup () { 69 cleanup() 70 fixture.create(basedir) 71} 72 73function cleanup () { 74 fixture.remove(basedir) 75} 76 77function reset () { 78 rimraf.sync(testdir + '/' + 'node_modules') 79} 80 81test('setup', function (t) { 82 setup() 83 mr({port: common.port, throwOnUnmatched: true}, function (err, s) { 84 if (err) throw err 85 server = s 86 server.filteringPathRegEx(/([/]-[/]npm[/]anon-metrics[/]v1[/]).*/, '$1:id') 87 server.filteringRequestBody(function (body) { 88 var metrics = typeof body === 'string' ? JSON.parse(body) : body 89 delete metrics.from 90 delete metrics.to 91 return JSON.stringify(metrics) 92 }) 93 t.done() 94 }) 95}) 96 97test('record success', function (t) { 98 common.npm(['install', '--no-save', '--no-send-metrics', 'file:success'], conf, function (err, code, stdout, stderr) { 99 if (err) throw err 100 t.is(code, 0, 'always succeeding install succeeded') 101 t.comment(stdout.trim()) 102 t.comment(stderr.trim()) 103 var data = JSON.parse(fs.readFileSync(metricsFile)) 104 t.is(data.metrics.successfulInstalls, 1, 'successes') 105 t.is(data.metrics.failedInstalls, 0, 'failures') 106 t.done() 107 }) 108}) 109 110test('record failure', function (t) { 111 reset() 112 server.put('/-/npm/anon-metrics/v1/:id', { 113 successfulInstalls: 1, 114 failedInstalls: 0 115 }).reply(500, {ok: false}) 116 common.npm(['install', '--no-save', '--send-metrics', 'file:failure'], conf, function (err, code, stdout, stderr) { 117 if (err) throw err 118 t.notEqual(code, 0, 'always failing install fails') 119 t.comment(stdout.trim()) 120 t.comment(stderr.trim()) 121 var data = JSON.parse(fs.readFileSync(metricsFile)) 122 t.is(data.metrics.successfulInstalls, 1, 'successes') 123 t.is(data.metrics.failedInstalls, 1, 'failures') 124 t.done() 125 }) 126}) 127 128test('report', function (t) { 129 reset() 130 server.put('/-/npm/anon-metrics/v1/:id', { 131 successfulInstalls: 1, 132 failedInstalls: 1 133 }).reply(200, {ok: true}) 134 common.npm(['install', '--no-save', '--send-metrics', 'file:slow'], conf, function (err, code, stdout, stderr) { 135 if (err) throw err 136 t.is(code, 0, 'command ran ok') 137 t.comment(stdout.trim()) 138 t.comment(stderr.trim()) 139 // todo check mock registry for post 140 var data = JSON.parse(fs.readFileSync(metricsFile)) 141 t.is(data.metrics.successfulInstalls, 1, 'successes') 142 t.is(data.metrics.failedInstalls, 0, 'failures') 143 t.done() 144 }) 145}) 146 147test('cleanup', function (t) { 148 server.close() 149 cleanup() 150 t.done() 151}) 152