1'use strict' 2 3const BB = require('bluebird') 4 5const common = require('../common-tap.js') 6const mockTar = require('../util/mock-tarball.js') 7const mr = common.fakeRegistry.compat 8const path = require('path') 9const rimraf = BB.promisify(require('rimraf')) 10const Tacks = require('tacks') 11const { test } = require('tap') 12 13const { Dir, File } = Tacks 14 15const testDir = common.pkg 16 17let server 18test('setup', t => { 19 mr({}, (err, s) => { 20 t.ifError(err, 'registry mocked successfully') 21 server = s 22 t.end() 23 }) 24}) 25 26test('installs an npm package before a certain date', t => { 27 const fixture = new Tacks(Dir({ 28 'package.json': File({}) 29 })) 30 fixture.create(testDir) 31 const packument = { 32 name: 'foo', 33 'dist-tags': { latest: '1.2.4' }, 34 versions: { 35 '1.2.3': { 36 name: 'foo', 37 version: '1.2.3', 38 dist: { 39 tarball: `${server.registry}/foo/-/foo-1.2.3.tgz` 40 } 41 }, 42 '1.2.4': { 43 name: 'foo', 44 version: '1.2.4', 45 dist: { 46 tarball: `${server.registry}/foo/-/foo-1.2.4.tgz` 47 } 48 } 49 }, 50 time: { 51 created: '2017-01-01T00:00:01.000Z', 52 modified: '2018-01-01T00:00:01.000Z', 53 '1.2.3': '2017-01-01T00:00:01.000Z', 54 '1.2.4': '2018-01-01T00:00:01.000Z' 55 } 56 } 57 server.get('/foo').reply(200, packument) 58 return mockTar({ 59 'package.json': JSON.stringify({ 60 name: 'foo', 61 version: '1.2.3' 62 }) 63 }).then(tarball => { 64 server.get('/foo/-/foo-1.2.3.tgz').reply(200, tarball) 65 server.get('/foo/-/foo-1.2.4.tgz').reply(500) 66 return common.npm([ 67 'install', 'foo', 68 '--before', '2018', 69 '--json', 70 '--cache', path.join(testDir, 'npmcache'), 71 '--registry', server.registry 72 ], { cwd: testDir }) 73 }).then(([code, stdout, stderr]) => { 74 t.comment(stdout) 75 t.comment(stderr) 76 t.like(JSON.parse(stdout), { 77 added: [{ 78 action: 'add', 79 name: 'foo', 80 version: '1.2.3' 81 }] 82 }, 'installed the 2017 version of the package') 83 }) 84}) 85 86test('cleanup', t => { 87 server.close() 88 return rimraf(testDir) 89}) 90