[PATCH] D54636: [lit] Add test directory to sys.path before invoking it.

Zachary Turner via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 16 10:18:05 PST 2018


zturner created this revision.
zturner added reviewers: rnk, stella.stamenova.
Herald added a subscriber: delcypher.

Test suites with complicated configuration logic may wish to split this logic up over multiple packages / modules.  This is currently tricky because in order to do `import x` from the same directory, the directory has to be in sys.path.   What I'd really like is to find a way to make the test suite an actual package.  It seems that due to the way we compile / exec it, it's still part of the `lit.llvm` package.  (For example, the value of `__package__` from within one of these files is `'lit.llvm'`. It would be nice if we could detach it from the top-level lit package and make it be its own package.  This way `from . import foo` would "just work".

Anyway, that's for another day, for now this at least allows it to work.


https://reviews.llvm.org/D54636

Files:
  llvm/utils/lit/lit/TestingConfig.py


Index: llvm/utils/lit/lit/TestingConfig.py
===================================================================
--- llvm/utils/lit/lit/TestingConfig.py
+++ llvm/utils/lit/lit/TestingConfig.py
@@ -85,7 +85,13 @@
         cfg_globals['config'] = self
         cfg_globals['lit_config'] = litConfig
         cfg_globals['__file__'] = path
+        original_sys_path = list(sys.path)
         try:
+            # Add the directory name to sys.path.  This way individual
+            # test suites can write "import foo" or "from foo import bar"
+            # so that they can split complex logic up amongst multiple
+            # modules
+            sys.path.append(os.path.dirname(path))
             exec(compile(data, path, 'exec'), cfg_globals, None)
             if litConfig.debug:
                 litConfig.note('... loaded config %r' % path)
@@ -100,7 +106,8 @@
             litConfig.fatal(
                 'unable to parse config file %r, traceback: %s' % (
                     path, traceback.format_exc()))
-
+        finally:
+            sys.path = original_sys_path
         self.finish(litConfig)
 
     def __init__(self, parent, name, suffixes, test_format,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D54636.174393.patch
Type: text/x-patch
Size: 1177 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20181116/37ee9c85/attachment.bin>


More information about the llvm-commits mailing list