Skip to content

test mis-parse with compound expression involving string comparison where LHS is a flag known to test #5947

Closed

Description

@lilyball

test takes a number of flags, such as test -g filename, but it prioritizes binary operators over interpreting an argument as a flag, e.g. test -g = -g is parsed as a string comparison. This is useful when the first arg is actually a variable, e.g. test "$flag" = -g.

Unfortunately, this fails once a compound expression is involved. test -g = -g -o -g = --graph (which comes from test "$flag" = -g -o "$flag" = --graph). In this case it instead parses the -g as the unary operator and emits the error

test: Expected a combining operator like '-a' at index 2

This is true even when using parentheses to group, e.g. test \( -g = -g \) -o \( -g = --graph \).

Reproduced with recent master (fish, version 3.0.2-1297-gb41e5cbbb-dirty) as well as version 3.0.2.

Activity

  1. added
    bug
    on Jun 19, 2019
  2. lilyball commented on Jun 19, 2019

    @lilyball
    ContributorAuthor

    Looks like this has been filed before as #5186, but that issue was closed saying this is just an edge case. Closing that was wrong; bash is able to parse this correctly.

  3. faho commented on Jun 21, 2019

    @faho
    Member

    Note that this isn't actually even a violation of POSIX, because:

     >4 arguments:
        The results are unspecified. 
    

    To be perfectly frank, test sucks, and should be replaced. And the replacement should not have combining operators, and most of all should not include a one argument mode.

    If anyone cares enough to write a patch for this, we'll merge it.

  4. added this to the fish-future milestone on Jun 22, 2019
  5. faho commented on Jul 2, 2019

    @faho
    Member

    Okay, I'm gonna close this as a duplicate of #1337. @lilyball even commented essentially the same example.

  6. removed this from the fish-future milestone on Jul 2, 2019
  7. added and removed
    bug
    on Jul 2, 2019
  8. locked as resolved and limited conversation to collaborators on Apr 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions