This repository was archived by the owner on May 14, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathjava_unit_tests.py
More file actions
300 lines (271 loc) · 11 KB
/
Copy pathjava_unit_tests.py
File metadata and controls
300 lines (271 loc) · 11 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import shutil
import tempfile
import unittest
import xml.etree.ElementTree as elementTree
from unittest import mock
import yaml
from pathlib import Path
from synthtool.languages import java
from library_generation.tests.owlbot import util
TEST_OWLBOT = Path(__file__).parent.parent / "resources" / "test-owlbot"
FIXTURES = Path(__file__).parent.parent / "resources" / "test-owlbot" / "fixtures"
TEMPLATES_PATH = Path(__file__).parent.parent.parent / "owlbot" / "templates"
CURRENT_YEAR_OVERRIDE = "2025"
SAMPLE_METADATA = """
<metadata>
<groupId>com.google.cloud</groupId>
<artifactId>libraries-bom</artifactId>
<versioning>
<latest>3.3.0</latest>
<release>3.3.0</release>
<versions>
<version>1.0.0</version>
<version>1.1.0</version>
<version>1.1.1</version>
<version>1.2.0</version>
<version>2.0.0</version>
<version>2.1.0</version>
<version>2.2.0</version>
<version>2.2.1</version>
<version>2.3.0</version>
<version>2.4.0</version>
<version>2.5.0</version>
<version>2.6.0</version>
<version>2.7.0</version>
<version>2.7.1</version>
<version>2.8.0</version>
<version>2.9.0</version>
<version>3.0.0</version>
<version>3.1.0</version>
<version>3.1.1</version>
<version>3.2.0</version>
<version>3.3.0</version>
</versions>
<lastUpdated>20191218182827</lastUpdated>
</versioning>
</metadata>
"""
class JavaUnitTests(unittest.TestCase):
def test_version_from_maven_metadata(self):
self.assertEqual("3.3.0", java.version_from_maven_metadata(SAMPLE_METADATA))
@mock.patch.dict(os.environ, {"SYNTHTOOL_TEMPLATES": f"{TEMPLATES_PATH}"})
def test_working_common_templates(self):
def assert_valid_xml(file):
try:
elementTree.parse(file)
except elementTree.ParseError:
self.fail(f"unable to parse XML: {file}")
def assert_valid_yaml(file):
with open(file, "r") as stream:
try:
yaml.safe_load(stream)
except yaml.YAMLError:
self.fail(f"unable to parse YAML: {file}")
with util.copied_fixtures_dir(
FIXTURES / "java_templates" / "standard"
) as workdir:
# generate the common templates
java.common_templates(template_path=TEMPLATES_PATH)
self.assertTrue(os.path.isfile("renovate.json"))
# lint xml, yaml files
# use os.walk because glob ignores hidden directories
for dirpath, _, filenames in os.walk(workdir):
for file in filenames:
(_, ext) = os.path.splitext(file)
if ext == ".xml":
assert_valid_xml(os.path.join(dirpath, file))
elif ext == ".yaml" or ext == ".yml":
assert_valid_yaml(os.path.join(dirpath, file))
def test_remove_method(self):
with tempfile.TemporaryDirectory() as tempdir:
cwd = os.getcwd()
os.chdir(TEST_OWLBOT)
shutil.copyfile("testdata/SampleClass.java", tempdir + "/SampleClass.java")
java.remove_method(
tempdir + "/SampleClass.java", "public static void foo()"
)
java.remove_method(tempdir + "/SampleClass.java", "public void asdf()")
self.assert_matches_golden(
"testdata/SampleClassGolden.java", tempdir + "/SampleClass.java"
)
os.chdir(cwd)
def test_copy_and_rename_method(self):
with tempfile.TemporaryDirectory() as tempdir:
cwd = os.getcwd()
os.chdir(TEST_OWLBOT)
shutil.copyfile("testdata/SampleClass.java", tempdir + "/SampleClass.java")
java.copy_and_rename_method(
tempdir + "/SampleClass.java",
"public static void foo()",
"foo",
"foobar",
)
java.copy_and_rename_method(
tempdir + "/SampleClass.java", "public void asdf()", "asdf", "xyz"
)
self.assert_matches_golden(
"testdata/SampleCopyMethodGolden.java",
tempdir + "/SampleClass.java",
)
os.chdir(cwd)
def test_deprecate_method(self):
with tempfile.TemporaryDirectory() as tempdir:
cwd = os.getcwd()
os.chdir(TEST_OWLBOT)
shutil.copyfile(
"testdata/SampleDeprecateClass.java",
tempdir + "/SampleDeprecateClass.java",
)
deprecation_warning = """This method will be removed in the next major version.\nUse {{@link #{new_method}()}} instead"""
additional_comment = (
"""{new_method} has the same functionality as foobar."""
)
java.deprecate_method(
tempdir + "/SampleDeprecateClass.java",
"public void foo(String bar)",
deprecation_warning.format(new_method="sample"),
)
# adding a comment when a javadoc and annotation already exists
java.deprecate_method(
tempdir + "/SampleDeprecateClass.java",
"public void bar(String bar)",
deprecation_warning.format(new_method="sample"),
)
java.deprecate_method(
tempdir + "/SampleDeprecateClass.java",
"public void cat(String bar)",
additional_comment.format(new_method="sample"),
)
self.assert_matches_golden(
"testdata/SampleDeprecateMethodGolden.java",
tempdir + "/SampleDeprecateClass.java",
)
os.chdir(cwd)
@mock.patch.dict(os.environ, {"CURRENT_YEAR_OVERRIDE": CURRENT_YEAR_OVERRIDE})
def test_fix_proto_license(self):
with tempfile.TemporaryDirectory() as tempdir:
cwd = os.getcwd()
os.chdir(TEST_OWLBOT)
temppath = Path(tempdir).resolve()
os.mkdir(temppath / "src")
shutil.copyfile(
"testdata/src/foo/FooProto.java", temppath / "src/FooProto.java"
)
java.fix_proto_headers(temppath)
self.assert_matches_golden(
"testdata/FooProtoGolden.java", temppath / "src/FooProto.java"
)
os.chdir(cwd)
@mock.patch.dict(os.environ, {"CURRENT_YEAR_OVERRIDE": CURRENT_YEAR_OVERRIDE})
def test_fix_proto_license_idempotent(self):
with tempfile.TemporaryDirectory() as tempdir:
cwd = os.getcwd()
os.chdir(TEST_OWLBOT)
temppath = Path(tempdir).resolve()
os.mkdir(temppath / "src")
shutil.copyfile(
"testdata/src/foo/FooProto.java", temppath / "src/FooProto.java"
)
# run the header fix twice
java.fix_proto_headers(temppath)
java.fix_proto_headers(temppath)
self.assert_matches_golden(
"testdata/FooProtoGolden.java", temppath / "src/FooProto.java"
)
os.chdir(cwd)
@mock.patch.dict(os.environ, {"CURRENT_YEAR_OVERRIDE": CURRENT_YEAR_OVERRIDE})
def test_fix_grpc_license(self):
with tempfile.TemporaryDirectory() as tempdir:
cwd = os.getcwd()
os.chdir(TEST_OWLBOT)
temppath = Path(tempdir).resolve()
os.mkdir(temppath / "src")
shutil.copyfile(
"testdata/src/foo/FooGrpc.java", temppath / "src/FooGrpc.java"
)
java.fix_grpc_headers(temppath)
self.assert_matches_golden(
"testdata/FooGrpcGolden.java", temppath / "src/FooGrpc.java"
)
os.chdir(cwd)
@mock.patch.dict(os.environ, {"CURRENT_YEAR_OVERRIDE": CURRENT_YEAR_OVERRIDE})
def test_fix_grpc_license_idempotent(self):
with tempfile.TemporaryDirectory() as tempdir:
cwd = os.getcwd()
os.chdir(TEST_OWLBOT)
temppath = Path(tempdir).resolve()
os.mkdir(temppath / "src")
shutil.copyfile(
"testdata/src/foo/FooGrpc.java", temppath / "src/FooGrpc.java"
)
# run the header fix twice
java.fix_grpc_headers(temppath)
java.fix_grpc_headers(temppath)
self.assert_matches_golden(
"testdata/FooGrpcGolden.java", temppath / "src/FooGrpc.java"
)
os.chdir(cwd)
@mock.patch.dict(os.environ, {"SYNTHTOOL_TEMPLATES": f"{TEMPLATES_PATH}"})
def test_release_please_handle_releases(self):
with util.copied_fixtures_dir(
FIXTURES / "java_templates" / "release-please-update"
):
# generate the common templates
java.common_templates(template_path=TEMPLATES_PATH)
self.assertTrue(os.path.isfile(".github/release-please.yml"))
with open(".github/release-please.yml") as fp:
self.assertEqual(
fp.read(),
"""branches:
- branch: 1.127.12-sp
bumpMinorPreMajor: true
handleGHRelease: true
releaseType: java-lts
bumpMinorPreMajor: true
handleGHRelease: true
releaseType: java-yoshi
""",
)
@mock.patch.dict(
os.environ,
{
"SYNTHTOOL_TEMPLATES": f"{TEMPLATES_PATH}",
"SYNTHTOOL_LIBRARY_VERSION": "1.2.3",
},
)
def test_render_readme_success(self):
golden_path = os.path.abspath(f"{TEST_OWLBOT}/testdata/README-golden.md")
with util.copied_fixtures_dir(FIXTURES / "java_templates" / "render-readme"):
# This method needs read .repo-metadata.json to render templates.
# The file is located in FIXTURES/java_templates/render-readme.
java.common_templates(
template_path=TEMPLATES_PATH,
)
self.assertTrue(os.path.isfile("README.md"))
self.assert_matches_golden(golden_path, "README.md")
def assert_matches_golden(self, expected, actual):
matching_lines = 0
with open(actual, "rt") as fp:
with open(expected, "rt") as golden:
while True:
matching_lines += 1
log_line = fp.readline()
expected = golden.readline()
self.assertEqual(repr(log_line), repr(expected))
if not log_line:
break
assert matching_lines > 0