[PATCH] D51709: [lit] Correctly expand globs relative to their intended directory.

Graydon Hoare via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 5 16:44:54 PDT 2018


graydon created this revision.
graydon added a reviewer: ddunbar.
Herald added subscribers: llvm-commits, delcypher.

Lit emulates shell semantics in its RUN: line interpretation. As
part of this emulation, it sniffs 'cd' commands in the command
to execute (such as "cd /some/path && ...") and then uses the
argument to 'cd' as a directory in which it interprets subsequent
elements of the command, in particular globs.

When expanding a non-absolute (relative) glob, however, it currently
takes a naive and (I argue) incorrect approach to them: it prepends
the inferred working directory's absolute path to the glob and then
expands the glob as an absolute path. This identifies the correct
_files_ but it names them absolutely, where the glob was written
as a relative glob. In a few corner cases, the difference between
an absolute and a relative filename is _meaningful_ to the program
under test, and should be preserved even when expanding globs.

This change works by stripping the absolute prefix back off the
expansion of a relative glob (using relpath), which makes the
behaviour at least more shell-like.


Repository:
  rL LLVM

https://reviews.llvm.org/D51709

Files:
  utils/lit/lit/ShCommands.py


Index: utils/lit/lit/ShCommands.py
===================================================================
--- utils/lit/lit/ShCommands.py
+++ utils/lit/lit/ShCommands.py
@@ -52,10 +52,18 @@
         import glob
         import os
         if os.path.isabs(self.pattern):
-           abspath = self.pattern
+            results = glob.glob(self.pattern)
         else:
-            abspath = os.path.join(cwd, self.pattern)
-        results = glob.glob(abspath)
+            # For a non-absolute pattern, we want to match "relative to
+            # cwd"; unfortunately glob.glob() will only match relative to
+            # the process working directory, or else an absolute path. So
+            # rather than change worknig directory to and from cwd in order
+            # to glob, we extend the relative pattern to an absolute
+            # pattern, then glob, then map the results back to relative
+            # paths using relpath.
+            abs_pat = os.path.join(cwd, self.pattern)
+            abs_glob = glob.glob(abs_pat)
+            results = [os.path.relpath(f, cwd) for f in abs_glob]
         return [self.pattern] if len(results) == 0 else results
 
 class Pipeline:


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D51709.164126.patch
Type: text/x-patch
Size: 1186 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180905/df000216/attachment.bin>


More information about the llvm-commits mailing list