summaryrefslogtreecommitdiff
path: root/tests/resources.py
diff options
context:
space:
mode:
authorAshley Whetter <AWhetter@users.noreply.github.com>2019-10-15 01:49:26 -0700
committerClaudiu Popa <pcmanticore@gmail.com>2019-10-15 10:49:26 +0200
commit2f288598de485c6af25788fc917139b48c31c474 (patch)
tree3b52b2994c90018a2db2854adca0928c4bfe1162 /tests/resources.py
parent73babe3d536ffc4da94e59c705eb6a8c3e5822ef (diff)
downloadastroid-git-2f288598de485c6af25788fc917139b48c31c474.tar.gz
Moved tests out of package directory (#704)
Diffstat (limited to 'tests/resources.py')
-rw-r--r--tests/resources.py62
1 files changed, 62 insertions, 0 deletions
diff --git a/tests/resources.py b/tests/resources.py
new file mode 100644
index 00000000..ee57b9fe
--- /dev/null
+++ b/tests/resources.py
@@ -0,0 +1,62 @@
+# Copyright (c) 2014 Google, Inc.
+# Copyright (c) 2015-2016 Claudiu Popa <pcmanticore@gmail.com>
+# Copyright (c) 2016 Ceridwen <ceridwenv@gmail.com>
+# Copyright (c) 2018 Nick Drozd <nicholasdrozd@gmail.com>
+
+# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
+# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER
+
+import os
+import sys
+
+from astroid import builder
+from astroid import MANAGER
+from astroid.bases import BUILTINS
+
+
+DATA_DIR = os.path.join("testdata", "python{}".format(sys.version_info[0]))
+RESOURCE_PATH = os.path.join(os.path.dirname(__file__), DATA_DIR, "data")
+
+
+def find(name):
+ return os.path.normpath(os.path.join(os.path.dirname(__file__), DATA_DIR, name))
+
+
+def build_file(path, modname=None):
+ return builder.AstroidBuilder().file_build(find(path), modname)
+
+
+class SysPathSetup:
+ def setUp(self):
+ sys.path.insert(0, find(""))
+
+ def tearDown(self):
+ del sys.path[0]
+ datadir = find("")
+ for key in list(sys.path_importer_cache):
+ if key.startswith(datadir):
+ del sys.path_importer_cache[key]
+
+
+class AstroidCacheSetupMixin:
+ """Mixin for handling the astroid cache problems.
+
+ When clearing the astroid cache, some tests fails due to
+ cache inconsistencies, where some objects had a different
+ builtins object referenced.
+ This saves the builtins module and makes sure to add it
+ back to the astroid_cache after the tests finishes.
+ The builtins module is special, since some of the
+ transforms for a couple of its objects (str, bytes etc)
+ are executed only once, so astroid_bootstrapping will be
+ useless for retrieving the original builtins module.
+ """
+
+ @classmethod
+ def setup_class(cls):
+ cls._builtins = MANAGER.astroid_cache.get(BUILTINS)
+
+ @classmethod
+ def teardown_class(cls):
+ if cls._builtins:
+ MANAGER.astroid_cache[BUILTINS] = cls._builtins