1'use strict' 2var test = require('tap').test 3var path = require('path') 4var mkdirp = require('mkdirp') 5var rimraf = require('rimraf') 6var fs = require('graceful-fs') 7var common = require('../common-tap') 8 9var base = common.pkg 10var modA = path.resolve(base, 'modA') 11var modB = path.resolve(base, 'modB') 12 13var json = { 14 'name': 'test-full-warning-messages', 15 'version': '1.0.0', 16 'description': 'abc', 17 'repository': 'git://abc/', 18 'license': 'ISC', 19 'dependencies': { 20 'modA': modA 21 } 22} 23 24var modAJson = { 25 'name': 'modA', 26 'version': '1.0.0', 27 'optionalDependencies': { 28 'modB': modB 29 } 30} 31 32var modBJson = { 33 'name': 'modB', 34 'version': '1.0.0', 35 'os': ['nope'], 36 'cpu': 'invalid' 37} 38 39function modJoin () { 40 var modules = Array.prototype.slice.call(arguments) 41 return modules.reduce(function (a, b) { 42 return path.resolve(a, 'node_modules', b) 43 }) 44} 45 46function writeJson (mod, data) { 47 fs.writeFileSync(path.resolve(mod, 'package.json'), JSON.stringify(data)) 48} 49 50function setup () { 51 cleanup() 52 ;[modA, modB].forEach(function (mod) { mkdirp.sync(mod) }) 53 writeJson(base, json) 54 writeJson(modA, modAJson) 55 writeJson(modB, modBJson) 56} 57 58function cleanup () { 59 rimraf.sync(base) 60} 61 62test('setup', function (t) { 63 setup() 64 t.end() 65}) 66 67function exists (t, filepath, msg) { 68 try { 69 fs.statSync(filepath) 70 t.pass(msg) 71 return true 72 } catch (ex) { 73 t.fail(msg, {found: null, wanted: 'exists', compare: 'fs.stat(' + filepath + ')'}) 74 return false 75 } 76} 77 78function notExists (t, filepath, msg) { 79 try { 80 fs.statSync(filepath) 81 t.fail(msg, {found: 'exists', wanted: null, compare: 'fs.stat(' + filepath + ')'}) 82 return true 83 } catch (ex) { 84 t.pass(msg) 85 return false 86 } 87} 88 89test('tree-style', function (t) { 90 common.npm(['install', '--json', '--loglevel=warn'], {cwd: base}, function (err, code, stdout, stderr) { 91 if (err) throw err 92 t.is(code, 0, 'result code') 93 var result = JSON.parse(stdout) 94 t.is(result.added.length, 1, 'only added one module') 95 t.is(result.added[0].name, 'modA', 'modA got installed') 96 t.is(result.warnings.length, 1, 'one warning') 97 var stderrlines = stderr.trim().split(/\n/) 98 t.is(stderrlines.length, 2, 'two lines of warnings') 99 t.match(stderr, /SKIPPING OPTIONAL DEPENDENCY/, 'expected optional failure warning in stderr') 100 t.match(stderr, /Unsupported platform/, 'reason for optional failure in stderr') 101 t.match(result.warnings[0], /SKIPPING OPTIONAL DEPENDENCY/, 'expected optional failure warning in JSON') 102 t.match(result.warnings[0], /Unsupported platform/, 'reason for optional failure in JSON') 103 exists(t, modJoin(base, 'modA'), 'module A') 104 notExists(t, modJoin(base, 'modB'), 'module B') 105 t.done() 106 }) 107}) 108 109test('cleanup', function (t) { 110 cleanup() 111 t.end() 112}) 113