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

Braces don't expand if they start with the range operator #48

Open
tim-hitchins-ekkosense opened this issue Jul 4, 2024 · 5 comments · May be fixed by #49
Open

Braces don't expand if they start with the range operator #48

tim-hitchins-ekkosense opened this issue Jul 4, 2024 · 5 comments · May be fixed by #49
Labels

Comments

@tim-hitchins-ekkosense
Copy link

tim-hitchins-ekkosense commented Jul 4, 2024

Example:

braces.expand('{..a,b}/c')

Expected result:

[ '..ac', 'bc' ]

Actual result:

[ '{..a,b}c' ]

Note that the current behaviour is differing from the bash implementation:

tim@EK-22-LT-04:~/virtuoso-test-data$ echo {..a,b}c
..ac bc

Note that it does work if the .. comes anywhere except the first parameter to the brace, so

'{b,..a}c'
{b,z,..a}c

do each give

[ 'bc', '..ac' ]
[ 'bc', 'zc', '..ac' ]
@jonschlinkert
Copy link
Member

@tim-hitchins-ekkosense thanks for reporting this. That's definitely a bug.

@tim-hitchins-ekkosense
Copy link
Author

@jonschlinkert I'm happy to take a stab at a PR for this if it would be helpful, but I've not looked into it yet, so it may take me a while to find a fix!

@jonschlinkert
Copy link
Member

Sure, that would be great!

tim-hitchins-ekkosense added a commit to jakub-ekkosense/braces that referenced this issue Jul 4, 2024
@tim-hitchins-ekkosense
Copy link
Author

tim-hitchins-ekkosense commented Jul 4, 2024

Okay, used

# test.sh
echo {..a,b}
echo {b,..a}
echo {a..,b}
echo {...,b}
echo {a,..,b}
echo ..{a,b}
echo {{..,..},a,b}
echo {{{..,..}a,b}c,d}
echo ..{..,{..,{..,..}a,b}c,d}
echo ..
echo {,..}
echo {..,}
echo {..,..,..}
echo {...,..,..}
echo {..,...,..}
echo {./../.,../../}
echo {../../../,../../}
echo {...,...,...}
echo {.,..,.}
echo ,{..,..,.},
echo {1..2,3..4}
echo {..2,3..4}
echo {1..,2}
echo {1..,..2..,..3}
echo ..{1..3}
echo ..{1..3}{..,..}

echo {..a,b
echo {b,..a
echo {a..,b
echo {...,b
echo {a,..,b
echo ..{a,b
echo {{..,..},a,b
echo {{{..,..}a,b}c,d
echo {{{..,..}a,bc,d}
echo ..{..,{..,{..,..}a,b}c,d
echo ..
echo {,..
echo {..,
echo {..,..,..
echo {...,..,..
echo {..,...,..
echo {./../.,../../
echo {../../../,../../
echo {...,...,...
echo {.,..,.
echo ,{..,..,.},
echo {1..2,3..4
echo {..2,3..4
echo {1..,2
echo {1..,..2..,..3
echo ..{1..3
echo ..{1..3}{..,..

echo ..a,b}
echo b,..a}
echo a..,b}
echo ...,b}
echo a,..,b}
echo ..a,b}
echo {..,..},a,b}
echo {{..,..}a,b}c,d}
echo ....,{..,{..,..}a,b}c,d}
echo ..
echo ,..}
echo ..,}
echo ..,..,..}
echo ...,..,..}
echo ..,...,..}
echo ./../.,../../}
echo ../../../,../../}
echo ...,...,...}
echo .,..,.}
echo ,..,..,.},
echo 1..2,3..4}
echo ..2,3..4}
echo 1..,2}
echo 1..,..2..,..3}
echo ..1..3}
echo ..1..3}{..,..}

and then

./test.sh  | sed "s/ /', '/g" | xargs -d$"\n" printf "['%s']\n"

to assess a load of test cases and generate the expected results.


Aside, I don't know if this is something you'd want, but because this library is a) aiming to copy bash (almost - I think you support spaces in braces) and b) it's a constrained character set, it's a good case for some fuzz testing.

import random

alphabet = ['a', 'b', 'c', '1', '2', '3', '{', '}', '.', ',']

for i in range (int(1e2)):
    string = ''
    for i in range (random.randint(3, 25)):
        string += random.choice(alphabet)
    print (string)

and then run with the very ugly

python3 test.py | xargs -d$"\n" -I % bash -c "echo -n \"%|\"; echo %" | sed "s/ /', '/g" | sed "s/|/', {}, ['/" | xargs -d$"\n" printf "['%s']]\n"

for some output like

['3b{33abbb,b1ba,c{{,3}', {}, ['3b{33abbb,b1ba,c{', '3b{33abbb,b1ba,c{3']]
['3.c,,c{bc2}cc', {}, ['3.c,,c{bc2}cc']]
['b}c22.b{b,2,{12aaa3cc3c', {}, ['b}c22.b{b,2,{12aaa3cc3c']]
['11}..122', {}, ['11}..122']]
['2c{', {}, ['2c{']]
['}bc22b{3a{3a22a3..', {}, ['}bc22b{3a{3a22a3..']]
['}1,{ca{,{{,}.c1c321,a1}1', {}, ['}1,{ca{,.c1c3211', '}1,{ca{,.c1c3211', '}1,{ca{,a11']]
['c2b3b2b}c,,.}23b12,a,b}11', {}, ['c2b3b2b}c,,.}23b12,a,b}11']]

to give a load of test cases. Add | grep "', '" to get only cases that actually have some expansion.

Let me know if you'd like this sort of thing added (in another task).

@tim-hitchins-ekkosense
Copy link
Author

Anyway, @jonschlinkert - PR raised.

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

Successfully merging a pull request may close this issue.

2 participants