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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

End of Options ("--") processing broken #177

Closed
plroebuck opened this issue May 5, 2019 · 6 comments
Closed

End of Options ("--") processing broken #177

plroebuck opened this issue May 5, 2019 · 6 comments

Comments

@plroebuck
Copy link

plroebuck commented May 5, 2019

Description

Mocha now ignores the test files supplied after "--", required to allow consumers to safely generate the command line arguments and avoid conflicts between test files and CLI options.

Steps to Reproduce

Setup

$ PROJECT="/var/tmp/ypdbldash"

$ mkdir ${PROJECT}
$ cd ${PROJECT}
$ mkdir test
$ touch index.js
$ cat << EOF >> "package.json"
{
  "name": "ypdbldash",
  "version": "1.0.0",
  "description": "Test end-of-options (double-dash) argument placement",
  "main": "index.js",
  "directories": {
    "test": "./test"
  },
  "scripts": {
    "test": "mocha"
  },
  "devDependencies": {
    "mocha": "latest",
    "unexpected": "latest",
    "yargs-parser": "latest"
  },
  "engines": {
    "node": ">=6"
  },
  "keywords": [
    "argv",
    "double-dash"
  ],
  "author": "P. Roebuck <plroebuck@users.noreply.github.com>",
  "license": "CC0-1.0"
}
EOF
$ cat << EOF >> "test/3899.spec.js"
'use strict';
  
const expect = require('unexpected');
const yargsParser = require('yargs-parser');

describe('mochajs/mocha#3899', function() {
  const filename = 'somefile';
  const args = [
    '--',
    filename
  ];

  context('when given empty config', () => {
    let result;
    const configuration = {};

    before(function() {
      //console.log('configuration:', configuration);
      result = yargsParser.detailed(args, {configuration});
      //console.log(result);
    });

    it('should not error', () => {
      expect(result.error, 'to be null');
    });

    it('should not create a "-" alias', () => {
      expect(result.newAliases, 'to be empty');
    });

    it('should not populate "-" with positional arg', () => {
      expect(result.argv, 'not to have key', '-');
    });

    it('should populate "_" with positional arg', () => {
      expect(result.argv['_'], 'to equal', [ filename ]);
    });
  });

  context("when given config with 'short-option-groups' false", () => {
    let result;
    const configuration = {
      'short-option-groups': false
    };

    before(function() {
      //console.log('configuration:', configuration);
      result = yargsParser.detailed(args, {configuration});
      //console.log(result);
    });

    it('should denote use of input configuration settings', () => {
      expect(result.configuration, 'to satisfy', {
        ...configuration
      });
    });

    it('should not error', () => {
      expect(result.error, 'to be null');
    });

    it('should not create a "-" alias', () => {
      expect(result.newAliases, 'to be empty');
    });

    it('should not populate "-" with positional arg', () => {
      expect(result.argv, 'not to have key', '-');
    });

    it('should populate "_" with positional arg', () => {
      expect(result.argv['_'], 'to equal', [ filename ]);
    });
  });

  context("when given config with both 'short-option-groups' and 'camel-case-expansion' false", () => {
    let result;
    const configuration = {
      'camel-case-expansion': false,
      'short-option-groups': false
    };

    before(function() {
      //console.log('configuration:', configuration);
      result = yargsParser.detailed(args, { configuration });
      //console.log(result);
    });

    it('should denote use of input configuration settings', () => {
      expect(result.configuration, 'to satisfy', {
        ...configuration
      });
    });

    it('should not error', () => {
      expect(result.error, 'to be null');
    });

    it('should not create a "-" alias', () => {
      expect(result.newAliases, 'to be empty');
    });

    it('should not populate "-" with positional arg', () => {
      expect(result.argv, 'not to have key', '-');
    });

    it('should populate "_" with positional arg', () => {
      expect(result.argv['_'], 'to equal', [ filename ]);
    });
  });
});
EOF
$ npm install

Test

$ npm test

Expected behavior: All tests should complete without errors.

Actual behavior: Multiple tests fail in the provided Mocha specification file.

Versions

  • Node: v10.15.0
  • OS: macOS 10.13.6
  • Shell: bash

Above provided for my testing, but issue unlikely to be platform-specific.

Additional Information

Bug exists in "yargs-parser" v11.1.1 (Mocha's pinned version), but persists in current version.
Upstream: mochajs/mocha#3899

@bcoe
Copy link
Member

bcoe commented May 6, 2019

@plroebuck 👍 thank ou for the bug report.

@MoonBall
Copy link
Contributor

I made a PR to fix it 😁.

@plroebuck
Copy link
Author

@bcoe, anyone taken a look at @MoonBall's fix yet?

@jimthedev
Copy link

Uh looks like it was merged and is in v13.1.1 so does that mean this should be closed?

@bcoe
Copy link
Member

bcoe commented Jul 19, 2019

👍 @jimthedev @plroebuck I believe this should be addressed please go ahead and reopen if it continues causing issues.

@markus456
Copy link

This still seems to be a problem with the latest version of yargs and arguments after the -- end-of-options marker. I'm not sure if this is the problem as was originally described in this issue but I figured it'd be better to report this than not.

This small program (originally from #381) demonstrates the problem:

const yargs = require('yargs/yargs')
const {hideBin} = require('yargs/helpers');
const argv = yargs(hideBin(process.argv))
      .command('$0 <foo>', 'the default command', (yargs) => {
        yargs
          .positional('foo', {type: 'string', required: true})
      }, (argv) => {
        console.log(argv)
      })
      .argv

Passing the string -hello- to the program is not possible without using both single and double quotes:

[markusjm@monolith yargs-test]$ node test.js -hello-
test.js <foo>

the default command

Positionals:
  foo                                                                   [string]

Options:
  --help     Show help                                                 [boolean]
  --version  Show version number                                       [boolean]

Not enough non-option arguments: got 0, need at least 1
[markusjm@monolith yargs-test]$ node test.js -- -hello-
test.js <foo>

the default command

Positionals:
  foo                                                                   [string]

Options:
  --help     Show help                                                 [boolean]
  --version  Show version number                                       [boolean]

Not enough non-option arguments: got 0, need at least 1
[markusjm@monolith yargs-test]$ node test.js '"-hello-"'
{ _: [], '$0': 'test.js', foo: '-hello-' }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants