Skip to content

Commit d472b42

Browse files
author
Tim Hedger-Gourlay
committed
Add regression test for postprocess hook
Test demonstrates a simple real world multitoken transformation from: asyncio.current_task() to: current_thread() it also has data to confirm that those tokens on their own are not touched (so asyncio remains asyncio and curent_task() remains current_task())
1 parent 4502009 commit d472b42

3 files changed

Lines changed: 72 additions & 0 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
self._update_task = asyncio.current_task()
2+
asyncio
3+
current_task()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
self._update_task = current_thread()
2+
asyncio
3+
current_task()

tests/test_post_process.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import os
2+
3+
from token import NAME
4+
from tokenize import TokenInfo
5+
6+
from unasync import Rule
7+
8+
TEST_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "data")
9+
TEST_DIR = os.path.join(TEST_DIR, "postprocess")
10+
ASYNC_DIR = os.path.join(TEST_DIR, "async")
11+
SYNC_DIR = os.path.join(TEST_DIR, "sync")
12+
TEST_FILES = sorted(f for f in os.listdir(ASYNC_DIR) if f.endswith(".py"))
13+
14+
class PostProcessRule(Rule):
15+
16+
def _postprocess_tokens(self, tokens):
17+
# Replace:
18+
# asyncio.current_task()
19+
# with:
20+
# current_thread()
21+
22+
prev2 = None
23+
prev1 = None
24+
25+
for token in tokens:
26+
27+
if (
28+
prev2 is not None
29+
and prev2.src == "asyncio"
30+
and prev1.src == "."
31+
and token.src == "current_task"
32+
):
33+
yield token._replace(src="current_thread")
34+
35+
prev2 = None
36+
prev1 = None
37+
38+
elif prev2 is not None:
39+
yield prev2
40+
prev2 = prev1
41+
prev1 = token
42+
43+
else:
44+
prev2 = prev1
45+
prev1 = token
46+
47+
if prev2 is not None:
48+
yield prev2
49+
if prev1 is not None:
50+
yield prev1
51+
52+
53+
def test_postprocess(tmpdir):
54+
rule = PostProcessRule(fromdir=ASYNC_DIR, todir=str(tmpdir))
55+
56+
for source_file in TEST_FILES:
57+
rule._unasync_file(os.path.join(ASYNC_DIR, source_file))
58+
59+
for source_file in TEST_FILES:
60+
with open(os.path.join(SYNC_DIR, source_file)) as f:
61+
truth = f.read()
62+
63+
with open(os.path.join(str(tmpdir), source_file)) as f:
64+
unasynced = f.read()
65+
66+
assert unasynced == truth

0 commit comments

Comments
 (0)