Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(usage): add YARGS_DISABLE_WRAP env variable to disable wrap #2210

Merged
merged 5 commits into from Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/usage.ts
Expand Up @@ -164,6 +164,9 @@ export function usage(yargs: YargsInstance, shim: PlatformShim) {
};

function getWrap() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So that we can maintain our coverage #s, I would do this:

this.getWrap = () => {
  ...
}

And then your test can simply be:

const yargs = yargs().wrap(99);
process.env.YARGS_DISABLE_WRAP = 'true';
expect(yargs.getInternalMethods().getUsageInstance().getWrap()).to.equal(null);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made the changes 馃憤

if (process.env.YARGS_DISABLE_WRAP) {
return null;
}
if (!wrapSet) {
wrap = windowWidth();
wrapSet = true;
Expand Down
28 changes: 27 additions & 1 deletion test/usage.cjs
Expand Up @@ -1912,7 +1912,33 @@ describe('usage tests', () => {

// the long description should cause several line
// breaks when wrapped.
r.errors[0].split('\n').length.should.gte(4);
r.errors[0].split('\n').length.should.gte(5);
});

it('should not wrap when YARGS_DISABLED_WRAP is provided', function () {
if (!process.stdout.isTTY) {
return this.skip();
}

const width = process.stdout.columns;
process.env.YARGS_DISABLE_WRAP = 'true';

const r = checkUsage(() =>
yargs([])
.option('fairly-long-option', {
alias: 'f',
// create a giant string that should not wrap.
description: new Array((width + 1) * 5).join('s'),
})
.demand('foo')
.parse()
);

delete process.env.YARGS_DISABLE_WRAP;

// the long description should not cause several line
// breaks when using YARGS_DISABLED_WRAP.
r.errors[0].split('\n').length.should.eql(5);
});

it('should not raise an exception when long default and description are provided', () =>
Expand Down