@@ -2,6 +2,10 @@ import * as Config from '@oclif/config'
22import { expect , fancy } from 'fancy-test'
33
44import Base , { flags } from '../src'
5+ import { TestHelpClassConfig } from './helpers/test-help-in-src/src/test-help-plugin'
6+ import * as PluginHelp from '@oclif/plugin-help'
7+
8+ const originalgetHelpClass = PluginHelp . getHelpClass
59
610// const pjson = require('../package.json')
711
@@ -267,6 +271,144 @@ USAGE
267271
268272` )
269273 } )
274+
275+ fancy
276+ . stdout ( )
277+ . add ( 'config' , async ( ) => {
278+ const config : TestHelpClassConfig = await Config . load ( )
279+ config . pjson . oclif . helpClass = 'help-class-does-not-exist'
280+ return config
281+ } )
282+ . do ( async ( { config} ) => {
283+ class CMD extends Command {
284+ config = config
285+ }
286+ await CMD . run ( [ '-h' ] )
287+ } )
288+ . catch ( ( error : Error ) => expect ( error . message ) . to . contain ( 'Unable to load configured help class "help-class-does-not-exist", failed with message:\n' ) )
289+ . it ( 'shows useful error message when configured help class cannot be loaded' )
290+
291+ fancy
292+ . stdout ( )
293+ . stub ( PluginHelp , 'getHelpClass' , function ( config : any ) {
294+ return originalgetHelpClass ( config , '' )
295+ } )
296+ . add ( 'config' , async ( ) => {
297+ const config : TestHelpClassConfig = await Config . load ( )
298+ config . pjson . oclif . helpClass = undefined
299+ return config
300+ } )
301+ . do ( async ( { config} ) => {
302+ class CMD extends Command {
303+ config = config
304+ }
305+ await CMD . run ( [ '-h' ] )
306+ } )
307+ . catch ( ( error : Error ) => expect ( error . message ) . to . contain ( 'Could not load a help class, consider installing the @oclif/plugin-help package, failed with message:\n' ) )
308+ . it ( 'shows useful error message when no help class has been configured and the default cannot be loaded' )
309+
310+ describe ( 'from a help class' , ( ) => {
311+ fancy
312+ . stdout ( )
313+ . stub ( PluginHelp , 'getHelpClass' , function ( config : Config . IConfig ) {
314+ const patchedConfig = {
315+ ...config ,
316+ root : `${ __dirname } /helpers/test-help-in-lib/` ,
317+ }
318+
319+ return originalgetHelpClass ( patchedConfig )
320+ } )
321+ . add ( 'config' , async ( ) => {
322+ const config : TestHelpClassConfig = await Config . load ( )
323+ config . pjson . oclif . helpClass = './lib/test-help-plugin'
324+ return config
325+ } )
326+ . do ( async ( { config} ) => {
327+ class CMD extends Command {
328+ static id = 'test-command-for-help-plugin'
329+
330+ config = config
331+ }
332+ await CMD . run ( [ '-h' ] )
333+ } )
334+ . catch ( / E E X I T : 0 / )
335+ . it ( '-h via a plugin in lib dir (compiled to js)' , ctx => {
336+ expect ( ctx . stdout ) . to . equal ( 'hello from test-help-plugin #showCommandHelp in the lib folder and in compiled javascript\n' )
337+ expect ( ctx . config . showCommandHelpSpy ! . getCalls ( ) . length ) . to . equal ( 1 )
338+ expect ( ctx . config . showHelpSpy ! . getCalls ( ) . length ) . to . equal ( 0 )
339+ const [ Command , Topics ] = ctx . config . showCommandHelpSpy ! . firstCall . args
340+ expect ( Command . id ) . to . deep . equal ( 'test-command-for-help-plugin' )
341+ expect ( Topics ) . to . be . an ( 'array' )
342+ } )
343+
344+ fancy
345+ . stdout ( )
346+ . stub ( PluginHelp , 'getHelpClass' , function ( config : Config . IConfig ) {
347+ const patchedConfig = {
348+ ...config ,
349+ root : `${ __dirname } /helpers/test-help-in-src/` ,
350+ }
351+
352+ return originalgetHelpClass ( patchedConfig )
353+ } )
354+ . add ( 'config' , async ( ) => {
355+ const config : TestHelpClassConfig = await Config . load ( )
356+ config . pjson . oclif . helpClass = './lib/test-help-plugin'
357+ return config
358+ } )
359+ . do ( async ( { config} ) => {
360+ class CMD extends Command {
361+ static id = 'test-command-for-help-plugin'
362+
363+ config = config
364+ }
365+ await CMD . run ( [ '-h' ] )
366+ } )
367+ . catch ( / E E X I T : 0 / )
368+ . it ( '-h via a plugin in src dir (source in ts)' , ctx => {
369+ expect ( ctx . stdout ) . to . equal ( 'hello from test-help-plugin #showCommandHelp\n' )
370+ expect ( ctx . config . showCommandHelpSpy ! . getCalls ( ) . length ) . to . equal ( 1 )
371+ expect ( ctx . config . showHelpSpy ! . getCalls ( ) . length ) . to . equal ( 0 )
372+ const [ Command , Topics ] = ctx . config . showCommandHelpSpy ! . firstCall . args
373+ expect ( Command . id ) . to . deep . equal ( 'test-command-for-help-plugin' )
374+ expect ( Topics ) . to . be . an ( 'array' )
375+ } )
376+
377+ fancy
378+ . stdout ( )
379+ . stub ( PluginHelp , 'getHelpClass' , function ( config : Config . IConfig ) {
380+ const patchedConfig = {
381+ ...config ,
382+ root : `${ __dirname } /helpers/test-help-in-src/` ,
383+ }
384+
385+ return originalgetHelpClass ( patchedConfig )
386+ } )
387+ . add ( 'config' , async ( ) => {
388+ const config : TestHelpClassConfig = await Config . load ( )
389+ config . pjson . oclif . helpClass = './lib/test-help-plugin'
390+ return config
391+ } )
392+ . do ( async ( { config} ) => {
393+ class CMD extends Command {
394+ static id = 'test-command-for-help-plugin'
395+
396+ config = config
397+
398+ static flags = { help : flags . help ( ) }
399+ }
400+ return CMD . run ( [ '--help' ] )
401+ } )
402+ . catch ( / E E X I T : 0 / )
403+ . it ( '--help via a plugin in src dir (source in ts)' , ctx => {
404+ expect ( ctx . stdout ) . to . equal ( 'hello from test-help-plugin #showCommandHelp\n' )
405+ expect ( ctx . config . showCommandHelpSpy ! . getCalls ( ) . length ) . to . equal ( 1 )
406+ expect ( ctx . config . showHelpSpy ! . getCalls ( ) . length ) . to . equal ( 0 )
407+ const [ Command , Topics ] = ctx . config . showCommandHelpSpy ! . firstCall . args
408+ expect ( Command . id ) . to . deep . equal ( 'test-command-for-help-plugin' )
409+ expect ( Topics ) . to . be . an ( 'array' )
410+ } )
411+ } )
270412 } )
271413
272414 describe ( '.log()' , ( ) => {
0 commit comments