-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(verify-auth): apply memoization directly to npm-whoami calls
- Loading branch information
1 parent
ac7ffc8
commit 683f0da
Showing
3 changed files
with
56 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
const test = require('ava'); | ||
const {stub} = require('sinon'); | ||
const tempy = require('tempy'); | ||
|
||
const getRegistryPath = require.resolve('../lib/get-registry'); | ||
const verifyAuthPath = require.resolve('../lib/verify-auth'); | ||
const setNmprcAuthPath = require.resolve('../lib/set-npmrc-auth'); | ||
const execaPath = require.resolve('execa'); | ||
|
||
const resetModuleCache = () => { | ||
require.cache[getRegistryPath] = undefined; | ||
require.cache[verifyAuthPath] = undefined; | ||
require.cache[setNmprcAuthPath] = undefined; | ||
require.cache[execaPath] = undefined; | ||
}; | ||
|
||
test.before(resetModuleCache); | ||
test.after(resetModuleCache); | ||
|
||
test('Verify `npm-whoami` calls memoization', async (t) => { | ||
const cwd = tempy.directory(); | ||
const pkg = {}; | ||
const env = {}; | ||
const context = {cwd, env}; | ||
const fakeExeca = stub().returns({stdout: {pipe() {}}, stderr: {pipe() {}}}); | ||
|
||
require.cache[getRegistryPath] = {id: getRegistryPath, exports: () => 'https://registry.npmjs.org/'}; | ||
require.cache[setNmprcAuthPath] = {id: setNmprcAuthPath, exports: () => {}}; | ||
require.cache[execaPath] = {id: execaPath, exports: fakeExeca}; | ||
|
||
const verifyAuth = require('../lib/verify-auth'); | ||
|
||
await verifyAuth('foo', pkg, context); | ||
await verifyAuth('foo', pkg, context); | ||
await verifyAuth('foo', pkg, context); | ||
|
||
t.assert(fakeExeca.calledOnce); | ||
|
||
fakeExeca.resetHistory(); | ||
|
||
await verifyAuth('foo', pkg, context); | ||
await verifyAuth('bar', pkg, context); | ||
|
||
t.assert(fakeExeca.calledTwice); | ||
}); |