|
| 1 | +const test = require('ava'); |
| 2 | +const {stub} = require('sinon'); |
| 3 | +const tempy = require('tempy'); |
| 4 | + |
| 5 | +const getRegistryPath = require.resolve('../lib/get-registry'); |
| 6 | +const verifyAuthPath = require.resolve('../lib/verify-auth'); |
| 7 | +const setNmprcAuthPath = require.resolve('../lib/set-npmrc-auth'); |
| 8 | +const execaPath = require.resolve('execa'); |
| 9 | + |
| 10 | +const resetModuleCache = () => { |
| 11 | + require.cache[getRegistryPath] = undefined; |
| 12 | + require.cache[verifyAuthPath] = undefined; |
| 13 | + require.cache[setNmprcAuthPath] = undefined; |
| 14 | + require.cache[execaPath] = undefined; |
| 15 | +}; |
| 16 | + |
| 17 | +test.before(resetModuleCache); |
| 18 | +test.after(resetModuleCache); |
| 19 | + |
| 20 | +test('Verify `npm-whoami` calls memoization', async (t) => { |
| 21 | + const pkg = {}; |
| 22 | + const context = {cwd: tempy.directory(), env: {}}; |
| 23 | + const fakeExeca = stub().returns({stdout: {pipe() {}}, stderr: {pipe() {}}}); |
| 24 | + |
| 25 | + require.cache[getRegistryPath] = {id: getRegistryPath, exports: () => 'https://registry.npmjs.org/'}; |
| 26 | + require.cache[setNmprcAuthPath] = {id: setNmprcAuthPath, exports: () => {}}; |
| 27 | + require.cache[execaPath] = {id: execaPath, exports: fakeExeca}; |
| 28 | + |
| 29 | + const verifyAuth = require('../lib/verify-auth'); |
| 30 | + |
| 31 | + await verifyAuth('foo', pkg, context); |
| 32 | + await verifyAuth('foo', pkg, context); |
| 33 | + await verifyAuth('foo', pkg, context); |
| 34 | + |
| 35 | + t.assert(fakeExeca.calledOnce); |
| 36 | + |
| 37 | + fakeExeca.resetHistory(); |
| 38 | + |
| 39 | + await verifyAuth('foo', pkg, context); |
| 40 | + await verifyAuth('bar', pkg, context); |
| 41 | + |
| 42 | + t.assert(fakeExeca.calledTwice); |
| 43 | +}); |
0 commit comments