forked from twilio/twilio-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: DII 47 hyperlinks in help (twilio#290)
* Merging changes from main (twilio#286) * Removed sonar-code scan step * [Librarian] Regenerated @ fdad267944635962308083659322c23f28226702 * Release 2.29.0 * fix: Added missing require statement (twilio#285) Co-authored-by: lakshmiravali <[email protected]> * [Librarian] Regenerated @ 9a313923ef0eae61a7da7210b7d5de59e65a697c * Release 2.29.1 Co-authored-by: Anuj <[email protected]> Co-authored-by: Twilio <[email protected]> Co-authored-by: ravali-rimmalapudi <[email protected]> Co-authored-by: lakshmiravali <[email protected]> * Changes to make url a hyperlink Changes to make hyperLink for URLs in supported terminals * Segregated testcases for different terminals * Fix for failing test cases in Travis * Fix for failing test cases in Travis * Testing travis CI build failed testcases * Testing travis CI build failed testcases Co-authored-by: Anuj <[email protected]> Co-authored-by: Twilio <[email protected]> Co-authored-by: ravali-rimmalapudi <[email protected]> Co-authored-by: lakshmiravali <[email protected]>
- Loading branch information
1 parent
7e9d233
commit ce15661
Showing
5 changed files
with
197 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
function supportsHyperlink() { | ||
const { env } = process; | ||
const supports = require('supports-hyperlinks'); | ||
if (supports.stdout) { | ||
return true; | ||
} | ||
// support for Windows terminal | ||
if ('WT_SESSION' in env) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
function convertToHyperlink(text, link, params) { | ||
if (supportsHyperlink()) { | ||
const hyperlinker = require('hyperlinker'); | ||
return { url: hyperlinker(text, link, params), isSupported: true }; | ||
} | ||
return { url: link, isSupported: false }; | ||
} | ||
module.exports = { convertToHyperlink, supportsHyperlink }; |
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,164 @@ | ||
/* eslint-disable import/no-extraneous-dependencies */ | ||
const { expect, test } = require('@twilio/cli-test'); | ||
const { Command } = require('@oclif/command'); | ||
const flush = require('flush-cache'); | ||
|
||
const ORIG_ENV = { ...process.env }; | ||
const { supportsHyperlink, convertToHyperlink } = require('../../src/services/hyperlink-utility'); | ||
const TwilioHelp = require('../../src/services/twilio-help/custom-help'); | ||
|
||
class TestCommand extends Command { | ||
constructor(argv, config) { | ||
super(argv, config); | ||
this.id = 'api:core'; | ||
this.description = 'This is a dummy description'; | ||
this.args = [{ name: 'arg_1', description: 'argument description' }]; | ||
} | ||
} | ||
const testThisConfig = ({ platform, env, argv, stream }) => { | ||
platform = platform || 'darwin'; | ||
env = env || {}; | ||
argv = argv || []; | ||
// back up the original env | ||
const oldPlatform = process.platform; | ||
const oldEnv = process.env; | ||
const oldArgv = process.argv; | ||
|
||
// Inject new env properties from args | ||
Object.defineProperties(process, { | ||
platform: { value: platform }, | ||
env: { value: env }, | ||
argv: { value: [process.argv[0], ...argv] }, | ||
}); | ||
|
||
const result = supportsHyperlink(stream); | ||
// restore the original env | ||
Object.defineProperties(process, { | ||
platform: { value: oldPlatform }, | ||
env: { value: oldEnv }, | ||
argv: { value: oldArgv }, | ||
}); | ||
return result; | ||
}; | ||
|
||
const testLink = test | ||
.loadConfig() | ||
.add('help', (ctx) => new TwilioHelp(ctx.config)) | ||
.register('cmdTestLink', (args) => { | ||
return { | ||
async run(ctx) { | ||
const dummyHelpCommand = new TestCommand(args, ctx.config); | ||
dummyHelpCommand.docLink = 'https://twilio.com/docs/dummyCmd'; | ||
const help = ctx.help.formatCommand(dummyHelpCommand); | ||
ctx.cmdTestLink = help | ||
.split('\n') | ||
.map((s) => s.trimRight()) | ||
.join('\n'); | ||
}, | ||
}; | ||
}); | ||
|
||
describe('supportsHyperlink', () => { | ||
describe('test hyperlink generation', () => { | ||
describe('test for Mac terminals', () => { | ||
afterEach(() => { | ||
flush(); | ||
}); | ||
test.it('not supported in Mac Terminal', () => { | ||
expect( | ||
testThisConfig({ | ||
env: { | ||
TERM_PROGRAM: '', | ||
}, | ||
stream: { | ||
isTTY: false, | ||
}, | ||
}), | ||
).to.be.false; | ||
}); | ||
test.it('testing convertToHyperlink, supported iTerm.app 3.1, tty stream', () => { | ||
const result = testThisConfig({ | ||
env: { | ||
TERM_PROGRAM: 'iTerm.app', | ||
TERM_PROGRAM_VERSION: '3.1.0', | ||
}, | ||
stream: { | ||
isTTY: true, | ||
}, | ||
}); | ||
if ('CI' in process.env) { | ||
expect(result).to.be.false; | ||
expect(convertToHyperlink('MORE INFO', 'https://twilio.com/docs/dummyCmd').isSupported).to.be.false; | ||
} else { | ||
expect(result).to.be.true; | ||
expect(convertToHyperlink('MORE INFO', 'https://twilio.com/docs/dummyCmd').isSupported).to.be.true; | ||
} | ||
}); | ||
}); | ||
|
||
describe('test for iTerm terminals', () => { | ||
afterEach(() => { | ||
flush(); | ||
}); | ||
test.it('supported in iTerm.app 3.1, tty stream', () => { | ||
const result = testThisConfig({ | ||
env: { | ||
TERM_PROGRAM: 'iTerm.app', | ||
TERM_PROGRAM_VERSION: '3.1.0', | ||
}, | ||
stream: { | ||
isTTY: true, | ||
}, | ||
}); | ||
if ('CI' in process.env) { | ||
expect(result).to.be.false; | ||
} else { | ||
expect(result).to.be.true; | ||
} | ||
}); | ||
}); | ||
|
||
describe('test for Windows', () => { | ||
afterEach(() => { | ||
flush(); | ||
}); | ||
test.it('supported in Windows Terminal', () => { | ||
expect( | ||
testThisConfig({ | ||
env: { | ||
WT_SESSION: '', | ||
}, | ||
stream: { | ||
isTTY: false, | ||
}, | ||
}), | ||
).to.be.true; | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('convertToHyperlink', () => { | ||
describe('test hyperlink generation for dummyURL and dummyText on macOS', () => { | ||
describe('test for iTerm', () => { | ||
beforeEach(() => { | ||
process.env.TERM_PROGRAM = 'iTerm.app'; | ||
process.env.TERM_PROGRAM_VERSION = '3.1.0'; | ||
}); | ||
afterEach(() => { | ||
flush(); | ||
process.env = ORIG_ENV; | ||
}); | ||
testLink.cmdTestLink([]).it('test', (ctx) => { | ||
const result = convertToHyperlink('MORE INFO', 'https://twilio.com/docs/dummyCmd').isSupported; | ||
if ('CI' in process.env) { | ||
expect(result).to.be.false; | ||
} else { | ||
expect(result).to.be.true; | ||
} | ||
expect(ctx.cmdTestLink).to.contain('MORE INFO'); | ||
expect(ctx.cmdTestLink).to.contain('https://twilio.com/docs/dummyCmd'); | ||
}); | ||
}); | ||
}); | ||
}); |
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