1 |
commit: b156582a59fe271fa641e4b5f199d378ba9be8f4 |
2 |
Author: Marco Sirabella <marco <AT> sirabella <DOT> org> |
3 |
AuthorDate: Sat Apr 24 07:12:40 2021 +0000 |
4 |
Commit: Zac Medico <zmedico <AT> gentoo <DOT> org> |
5 |
CommitDate: Sat Apr 24 19:54:59 2021 +0000 |
6 |
URL: https://gitweb.gentoo.org/proj/portage.git/commit/?id=b156582a |
7 |
|
8 |
portage/tests: Use pathlib for path handling |
9 |
|
10 |
This is mostly just a POC |
11 |
|
12 |
While we don't have tests for the testing harness, it does successfully |
13 |
test all the tests |
14 |
|
15 |
Used pathlib docs as a reference for translation: |
16 |
https://docs.python.org/3/library/pathlib.html#correspondence-to-tools-in-the-os-module |
17 |
|
18 |
Signed-off-by: Marco Sirabella <marco <AT> sirabella.org> |
19 |
Signed-off-by: Zac Medico <zmedico <AT> gentoo.org> |
20 |
|
21 |
lib/portage/tests/__init__.py | 47 +++++++++++++++++++------------------------ |
22 |
1 file changed, 21 insertions(+), 26 deletions(-) |
23 |
|
24 |
diff --git a/lib/portage/tests/__init__.py b/lib/portage/tests/__init__.py |
25 |
index 3faa15103..8712937f8 100644 |
26 |
--- a/lib/portage/tests/__init__.py |
27 |
+++ b/lib/portage/tests/__init__.py |
28 |
@@ -6,6 +6,7 @@ import argparse |
29 |
import sys |
30 |
import time |
31 |
import unittest |
32 |
+from pathlib import Path |
33 |
|
34 |
from unittest.runner import TextTestResult as _TextTestResult |
35 |
|
36 |
@@ -59,9 +60,9 @@ def cnf_sbindir(): |
37 |
|
38 |
def main(): |
39 |
suite = unittest.TestSuite() |
40 |
- basedir = os.path.dirname(os.path.realpath(__file__)) |
41 |
+ basedir = Path(__file__).resolve().parent |
42 |
|
43 |
- usage = "usage: %s [options] [tests to run]" % os.path.basename(sys.argv[0]) |
44 |
+ usage = "usage: %s [options] [tests to run]" % Path(sys.argv[0]).name |
45 |
parser = argparse.ArgumentParser(usage=usage) |
46 |
parser.add_argument("-l", "--list", help="list all tests", |
47 |
action="store_true", dest="list_tests") |
48 |
@@ -73,9 +74,9 @@ def main(): |
49 |
portage.output.nocolor() |
50 |
|
51 |
if options.list_tests: |
52 |
- testdir = os.path.dirname(sys.argv[0]) |
53 |
+ testdir = Path(sys.argv[0]).parent |
54 |
for mydir in getTestDirs(basedir): |
55 |
- testsubdir = os.path.basename(mydir) |
56 |
+ testsubdir = mydir.name |
57 |
for name in getTestNames(mydir): |
58 |
print("%s/%s/%s.py" % (testdir, testsubdir, name)) |
59 |
return os.EX_OK |
60 |
@@ -84,7 +85,7 @@ def main(): |
61 |
suite.addTests(getTestFromCommandLine(args[1:], basedir)) |
62 |
else: |
63 |
for mydir in getTestDirs(basedir): |
64 |
- suite.addTests(getTests(os.path.join(basedir, mydir), basedir)) |
65 |
+ suite.addTests(getTests(mydir, basedir)) |
66 |
|
67 |
result = TextTestRunner(verbosity=2).run(suite) |
68 |
if not result.wasSuccessful(): |
69 |
@@ -101,47 +102,40 @@ def my_import(name): |
70 |
def getTestFromCommandLine(args, base_path): |
71 |
result = [] |
72 |
for arg in args: |
73 |
- realpath = os.path.realpath(arg) |
74 |
- path = os.path.dirname(realpath) |
75 |
- f = realpath[len(path)+1:] |
76 |
+ realpath = Path(arg).resolve() |
77 |
+ path = realpath.parent |
78 |
+ f = realpath.relative_to(path) |
79 |
|
80 |
- if not f.startswith("test") or not f.endswith(".py"): |
81 |
+ if not f.name.startswith("test") or not f.suffix == ".py": |
82 |
raise Exception("Invalid argument: '%s'" % arg) |
83 |
|
84 |
- mymodule = f[:-3] |
85 |
+ mymodule = f.stem |
86 |
result.extend(getTestsFromFiles(path, base_path, [mymodule])) |
87 |
return result |
88 |
|
89 |
def getTestDirs(base_path): |
90 |
- TEST_FILE = b'__test__.py' |
91 |
+ TEST_FILE = '__test__.py' |
92 |
testDirs = [] |
93 |
|
94 |
# the os.walk help mentions relative paths as being quirky |
95 |
# I was tired of adding dirs to the list, so now we add __test__.py |
96 |
# to each dir we want tested. |
97 |
- for root, dirs, files in os.walk(base_path): |
98 |
- try: |
99 |
- root = _unicode_decode(root, |
100 |
- encoding=_encodings['fs'], errors='strict') |
101 |
- except UnicodeDecodeError: |
102 |
- continue |
103 |
- |
104 |
- if TEST_FILE in files: |
105 |
- testDirs.append(root) |
106 |
+ for testFile in base_path.rglob(TEST_FILE): |
107 |
+ testDirs.append(testFile.parent) |
108 |
|
109 |
testDirs.sort() |
110 |
return testDirs |
111 |
|
112 |
def getTestNames(path): |
113 |
- files = os.listdir(path) |
114 |
- files = [f[:-3] for f in files if f.startswith("test") and f.endswith(".py")] |
115 |
+ files = path.glob('*') |
116 |
+ files = [f.stem for f in files |
117 |
+ if f.name.startswith('test') and f.suffix == ".py"] |
118 |
files.sort() |
119 |
return files |
120 |
|
121 |
def getTestsFromFiles(path, base_path, files): |
122 |
- parent_path = path[len(base_path)+1:] |
123 |
- parent_module = ".".join(("portage", "tests", parent_path)) |
124 |
- parent_module = parent_module.replace('/', '.') |
125 |
+ parent_path = path.relative_to(base_path) |
126 |
+ parent_module = ".".join(("portage", "tests") + parent_path.parts) |
127 |
result = [] |
128 |
for mymodule in files: |
129 |
# Make the trailing / a . for module importing |
130 |
@@ -287,7 +281,8 @@ class TestCase(unittest.TestCase): |
131 |
|
132 |
def assertNotExists(self, path): |
133 |
"""Make sure |path| does not exist""" |
134 |
- if os.path.exists(path): |
135 |
+ path = Path(path) |
136 |
+ if path.exists(): |
137 |
raise self.failureException('path exists when it should not: %s' % path) |
138 |
|
139 |
class TextTestRunner(unittest.TextTestRunner): |