I am writing a codemod with libcst which inherits from VisitorBasedCodemodCommand
. It works fine but is rather slow. One simple trick would be to skip all test files which start with test_
by convention. However I haven't been able to find a place to add such logic in my codemod.
I saw a SkipFile
exception but I don't know from where I could trigger it.
How can I ignore my test files?
You can raise
SkipFile
anywhere in the codemod when you detect that the current file can not be modified by the codemod. Since you know upfront you want to skip the test files completely, you can bail out on module node visit already, for example:Now, when you invoke
libcst.tool
with the codemod, you should see a log line for each skipped file: