-
Notifications
You must be signed in to change notification settings - Fork 172
Expand file tree
/
Copy pathutils.py
More file actions
134 lines (110 loc) · 2.69 KB
/
utils.py
File metadata and controls
134 lines (110 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import os
from contextlib import contextmanager
from io import BytesIO
from pathlib import Path
from random import sample
from unittest.mock import Mock, patch
import pkg_resources
from rpdk.core.project import Project
CONTENTS_UTF8 = "💣"
NAMES = [
"Apple",
"Apricot",
"Avocado",
"Banana",
"Boysenberry",
"Blueberry",
"Cherry",
"Cantaloupe",
"Clementine",
"Cucumber",
"Date",
"Dewberry",
"DragonFruit",
"Elderberry",
"Fig",
"Grapefruit",
"Grape",
"Gooseberry",
"Guava",
"Kiwi",
"Kumquat",
"Lime",
"Lemon",
"Lychee",
"Loquat",
"Mango",
"Mandarin",
"Mulberry",
"Melon",
"Nectarine",
"Olive",
"Orange",
"Papaya",
"Persimmon",
"Peach",
"Pomegranate",
"Pineapple",
"Raspberry",
"Strawberry",
"Tomato",
"Tangerine",
"Watermelon",
]
def random_type_name():
return "Test::{0}::{1}".format(*sample(NAMES, 2))
def random_name():
return "-".join(sample(NAMES, 3))
@contextmanager
def chdir(path):
old = os.getcwd()
os.chdir(path)
yield path
os.chdir(old)
def add_dummy_language_plugin():
distribution = pkg_resources.Distribution(__file__)
entry_point = pkg_resources.EntryPoint.parse(
"dummy = rpdk.dummy:DummyLanguagePlugin", dist=distribution
)
distribution._ep_map = { # pylint: disable=protected-access
"rpdk.v1.languages": {"dummy": entry_point}
}
pkg_resources.working_set.add(distribution)
def get_mock_project():
mock_project = Mock(spec=Project)
mock_project.load_settings.side_effect = FileNotFoundError
mock_project.settings_path = ""
mock_project.root = Path(".")
patch_project = patch("rpdk.core.init.Project", return_value=mock_project)
return (mock_project, patch_project)
def get_args(interactive=False, language=True, type_name=True):
args = Mock(
spec_set=[
"force",
"language",
"type_name",
"use_docker",
"namespace",
"codegen_model",
"import_path",
]
)
args.force = False
args.language = (
None if interactive else ("dummy" if language else "invalid_language")
)
args.type_name = (
None if interactive else ("Test::Test::Test" if type_name else "Test")
)
# The arguments below will only be tested by the plugins.
args.use_docker = None
args.namespace = "tested.by.the.plugin"
args.codegen_model = None
args.import_path = None
return args
class UnclosingBytesIO(BytesIO):
_was_closed = False
def close(self):
self._was_closed = True
def _close(self):
super().close()