-
-
Notifications
You must be signed in to change notification settings - Fork 30.7k
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
Unit test needed for IDLE's GrepDialog.py's findfiles() #67394
Comments
GrepDialog.py's findfiles() method lacks a unit test. The comments in the unit test stub in test_grep.py correctly notes that since findfiles() method does not rely on the GrepDialog class, it can be made into a function. The attached patch does the following:
There were so many changes to make that I eventually just rewrote the entire findfiles function. I've checked that the new version returns the same strings as the old version. |
I have been putting off re-writing findfiles because it partly duplicates os.listdir, which should perhaps be used instead, except that a new, improved, os.scandir is in the works: PEP-471 and bpo-22524. I believe filefiles currently searches depth first, whereas I would generally prefer breadth first. For instance, I would like all hits in /lib together and then hits in /lib/idlelib, /lib/test, /lib/tinter, etc. What do you think? |
I checked with a couple grep programs and they use depth first. Which makes sense, since you'd want the return order to be something like: /a/spam.txt ...instead of the bread-first: /a/spam.txt However, it turns out this is moot since the first thing GrepDialog.py does with the returned results is sort them. |
Findfiles was more like os.walk than os.listdir. os.walk now uses os.scandir instead of os.listdir. From 3.7.2 doc: "By default, errors from the scandir() call are ignored. If optional argument onerror is specified, it should be a function; it will be called with one argument, an OSError instance. It can report the error to continue with the walk, or raise the exception to abort the walk. Note that the filename is available as the filename attribute of the exception object." We should delete the try: except: and pass in a function to print the name and error message. This could be done after getting a working patch. Defaults are not needed as findfiles is called with 3 positional args. The calling method should do the replacement of '' with os.curdir(). The patch must be incomplete as generators do not have a sort method. Replace the first 3 lines of grep_it with
and replace 'list' with 'filelist' in the following code. Cheryl, grab the issue if you want to do this. |
Sure, thanks. grep was on list to add tests to, so I'll take a look at this as well. Thanks! |
I've opened a PR with the changes. I did several commits, one for each stage. That is, the first one adds the test, then the second one moves I added the One change that I didn't commit is that an alternative version of findfiles would be: def findfiles(folder, pattern, recursive):
prefix = '**/' if recursive else ''
yield from(pathlib.Path(folder).glob(f'{prefix}{pattern}')) The tests would have to be reworked, but manual testing showed it gave the same results, albeit without the One other comment about the sorting. If you change the |
Commit as-is. I will consider the Path.glob findfiles while working on bpo-36323. Like walk, it yields in breadth first order. Unlike walk, it requires relative paths (which bpo-37323 may need anyway) and needs code to deal with that. I like the breadth first listing from glob. Would it make any sense to give users a choice? What does unix grep do? |
On linux, grep does depth first, so searching for 'idle' from Lib.idlelib returns: --- cut --- Although, within idle_test, the files aren't in alphabetical order. Also, as you can see __init__ is before iomenu, so underscores seem to be ignored. On SO, it looks like they suggest piping it to sort if one wants a given ordering. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: