-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add specs to get 100 percent coverage
- Loading branch information
Showing
7 changed files
with
114 additions
and
32 deletions.
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
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,5 @@ | ||
const pino = require('pino') | ||
|
||
module.exports = function createLogger (args) { | ||
return pino({ level: args.verbose, prettyPrint: true, base: null }) | ||
} |
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,36 @@ | ||
const inquirer = require('inquirer') | ||
const Npm = require('./npm') | ||
const createLogger = require('./createLogger') | ||
|
||
/** | ||
* @param {object} args | ||
* @param {string} moduleLink | ||
*/ | ||
module.exports = async function publishToNpmWithOTPInquiry (args, moduleLink) { | ||
const npm = Npm(args.path) | ||
const logger = createLogger(args) | ||
|
||
try { | ||
await npm.publish({ tag: args.npmDistTag, access: args.npmAccess, otp: args.npmOtp }) | ||
} catch (err) { | ||
if (err.message.includes('npm ERR! code EOTP') && !args.silent) { | ||
if (args.npmOtp) { | ||
logger.error(`OTP code "${args.npmOtp}" is not valid`) | ||
} | ||
const { inputtedOtp } = await inquirer.prompt({ | ||
type: 'number', | ||
name: 'inputtedOtp', | ||
message: `OTP code is required to publish ${moduleLink} to NPM:`, | ||
validate: (v) => { | ||
Number.isInteger(v) | ||
} | ||
}) | ||
console.log('~ inputtedOtp', inputtedOtp) | ||
|
||
await npm.publish({ tag: args.npmDistTag, access: args.npmAccess, otp: inputtedOtp }) | ||
} else { | ||
throw err | ||
} | ||
} | ||
logger.info('Published new module version %s', moduleLink) | ||
} |
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,57 @@ | ||
|
||
'use strict' | ||
const proxyquire = require('proxyquire') | ||
const { test } = require('tap') | ||
|
||
test('publishToNpmWithOTPInquiry', (t) => { | ||
const publish = proxyquire('../lib/publishToNpmWithOTPInquiry', { | ||
'./npm': () => { | ||
return { | ||
publish: (publishArgs) => { | ||
if (!publishArgs.otp || publishArgs.otp === 'wrong OTP') { | ||
throw new Error('npm ERR! code EOTP') | ||
} | ||
} | ||
} | ||
}, | ||
inquirer: { | ||
prompt: async (promptConfig) => { | ||
promptConfig.validate(123456) | ||
|
||
return { inputtedOtp: '123456' } | ||
} | ||
} | ||
}) | ||
test('publishes', async (t) => { | ||
await publish({ path: process.cwd(), verbose: 'info', npmDistTag: '0.0.2', npmOtp: '111111' }, 'my-module@0.0.1') | ||
t.end() | ||
}) | ||
|
||
test('asks for OTP and retries publishes', async (t) => { | ||
await publish({ path: process.cwd(), verbose: 'info', npmDistTag: '0.0.3', silent: false, npmOtp: 'wrong OTP' }, 'my-module@0.0.1') | ||
await publish({ path: process.cwd(), verbose: 'info', npmDistTag: '0.0.3', silent: false }, 'my-module@0.0.1') | ||
|
||
t.end() | ||
}) | ||
|
||
test('throws other errors than OTP', async (t) => { | ||
const randomError = 'any random error' | ||
|
||
const publish = proxyquire('../lib/publishToNpmWithOTPInquiry', { | ||
'./npm': () => { | ||
return { | ||
publish: () => { | ||
throw new Error(randomError) | ||
} | ||
} | ||
} | ||
}) | ||
try { | ||
await publish({ path: process.cwd(), verbose: 'info', npmDistTag: '0.0.3', silent: false }, 'my-module@0.0.1') | ||
} catch (err) { | ||
t.equal(err.message, randomError) | ||
} | ||
t.end() | ||
}) | ||
t.end() | ||
}) |