-
Notifications
You must be signed in to change notification settings - Fork 78
/
test_node_mono_repo.py
430 lines (358 loc) · 14.4 KB
/
test_node_mono_repo.py
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# 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 filecmp
import pathlib
import re
import os
from pathlib import Path
from unittest import TestCase
from unittest.mock import patch
from datetime import date
import pytest
import synthtool as s
from synthtool.languages import node_mono_repo
from . import util
FIXTURES = Path(__file__).parent / "fixtures"
TEMPLATES = Path(__file__).parent.parent / "synthtool" / "gcp" / "templates"
def test_quickstart_metadata_with_snippet():
with util.chdir(FIXTURES / "node_templates" / "standard"):
metadata = node_mono_repo.template_metadata(
FIXTURES / "node_templates" / "standard"
)
# should have loaded the special quickstart sample (ignoring header).
assert "ID of the Cloud Bigtable instance" in metadata["quickstart"]
assert "limitations under the License" not in metadata["quickstart"]
assert isinstance(metadata["samples"], list)
# should have a link to the quickstart in the samples
sample_names = list(map(lambda sample: sample["file"], metadata["samples"]))
assert (
len(
list(filter((re.compile("samples/quickstart.js$")).match, sample_names))
)
== 0
)
def test_package_json_mono_repo():
with util.chdir(FIXTURES / "node_templates" / "package_json_mono_repo"):
metadata = node_mono_repo.template_metadata(
FIXTURES / "node_templates" / "package_json_mono_repo"
)
assert (
"https://github.com/googleapis/google-cloud-node/tree/main/packages/google-cloud-dlp"
in metadata["homepage"]
)
print(metadata["full_directory_path"])
assert (
"google-cloud-node/packages/google-cloud-dlp"
in metadata["full_directory_path"]
)
def test_metadata_engines_field():
with util.chdir(FIXTURES / "node_templates" / "standard"):
metadata = node_mono_repo.template_metadata(
FIXTURES / "node_templates" / "standard"
)
assert "10" in metadata["engine"]
def test_quickstart_metadata_without_snippet():
with util.chdir(FIXTURES / "node_templates" / "no_quickstart_snippet"):
metadata = node_mono_repo.template_metadata(
FIXTURES / "node_templates" / "no_quickstart_snippet"
)
# should not have populated the quickstart for the README
assert not metadata["quickstart"]
assert isinstance(metadata["samples"], list)
# should not have a link to the quickstart in the samples
sample_names = list(map(lambda sample: sample["file"], metadata["samples"]))
assert "samples/quickstart.js" not in sample_names
def test_no_samples():
# use a non-nodejs template directory
with util.chdir(FIXTURES):
metadata = node_mono_repo.template_metadata(FIXTURES)
# should not have populated the quickstart for the README
assert not metadata["quickstart"]
assert isinstance(metadata["samples"], list)
assert len(metadata["samples"]) == 0
def test_fix_sample_file_path():
with util.copied_fixtures_dir(FIXTURES / "nodejs_mono_repo_with_samples"):
metadata = node_mono_repo.template_metadata(
FIXTURES / "nodejs_mono_repo_with_samples" / "packages" / "datastore"
)
assert metadata["samples"] == [
{
"title": "Compare_to_quickstart",
"file": "packages/datastore/samples/generated/compare_to_quickstart.js",
},
{
"title": "Dataproc_metastore.create_backup",
"file": "packages/datastore/samples/generated/v1/dataproc_metastore.create_backup.js",
},
{
"title": "Dataproc_metastore.list_backups",
"file": "packages/datastore/samples/generated/v1/dataproc_metastore.list_backups.js",
},
]
def test_extract_clients_no_file():
index_ts_path = pathlib.Path(
FIXTURES / "node_templates" / "index_samples" / "no_exist_index.ts"
)
with pytest.raises(FileNotFoundError):
clients = node_mono_repo.extract_clients(index_ts_path)
assert not clients
def test_extract_single_clients():
index_ts_path = pathlib.Path(
FIXTURES / "node_templates" / "index_samples" / "single_index.ts"
)
clients = node_mono_repo.extract_clients(index_ts_path)
assert len(clients) == 1
assert clients[0] == "TextToSpeechClient"
def test_extract_multiple_clients():
index_ts_path = pathlib.Path(
FIXTURES / "node_templates" / "index_samples" / "multiple_index.ts"
)
clients = node_mono_repo.extract_clients(index_ts_path)
assert len(clients) == 2
assert clients[0] == "StreamingVideoIntelligenceServiceClient"
assert clients[1] == "VideoIntelligenceServiceClient"
def test_generate_index_ts():
# use a non-nodejs template directory
with util.chdir(FIXTURES / "node_templates" / "index_samples"):
node_mono_repo.generate_index_ts(
["v1", "v1beta1"],
"v1",
relative_dir=(FIXTURES / "node_templates" / "index_samples"),
year="2020",
)
generated_index_path = pathlib.Path(
FIXTURES / "node_templates" / "index_samples" / "src" / "index.ts"
)
sample_index_path = pathlib.Path(
FIXTURES / "node_templates" / "index_samples" / "sample_index.ts"
)
assert filecmp.cmp(generated_index_path, sample_index_path)
def test_write_release_please_config():
# use a non-nodejs template directory
with util.copied_fixtures_dir(FIXTURES / "node_templates" / "release_please"):
node_mono_repo.write_release_please_config(
[
"google-cloud-node/packages/gapic-node-templating",
"Users/person/google-cloud-node/packages/dlp",
"Users/person/google-cloud-node/packages/asset",
"packages/bigquery-migration",
]
)
assert filecmp.cmp(
pathlib.Path("release-please-config.json"),
pathlib.Path("release-please-config-post.json"),
)
def test_copy_quickstart():
with util.copied_fixtures_dir(
FIXTURES / "nodejs_mono_repo_with_samples" / "packages" / "datastore"
):
node_mono_repo.copy_list_sample_to_quickstart(
FIXTURES / "nodejs_mono_repo_with_samples" / "packages" / "datastore"
)
assert filecmp.cmp(
pathlib.Path(
FIXTURES
/ "nodejs_mono_repo_with_samples"
/ "packages"
/ "datastore"
/ "samples"
/ "quickstart.js"
),
pathlib.Path(
FIXTURES
/ "nodejs_mono_repo_with_samples"
/ "packages"
/ "datastore"
/ "samples"
/ "generated"
/ "compare_to_quickstart.js"
),
)
os.remove(
FIXTURES
/ "nodejs_mono_repo_with_samples"
/ "packages"
/ "datastore"
/ "samples"
/ "quickstart.js"
)
def test_generate_index_ts_empty_versions():
# use a non-nodejs template directory
with util.chdir(FIXTURES / "node_templates" / "index_samples"):
with pytest.raises(AttributeError) as err:
node_mono_repo.generate_index_ts(
[],
"v1",
relative_dir=(FIXTURES / "node_templates" / "index_samples",),
year=date.today().year,
)
assert "can't be empty" in err.args
def test_generate_index_ts_invalid_default_version():
# use a non-nodejs template directory
with util.chdir(FIXTURES / "node_templates" / "index_samples"):
versions = ["v1beta1"]
default_version = "v1"
with pytest.raises(AttributeError) as err:
node_mono_repo.generate_index_ts(
versions,
default_version,
relative_dir=(FIXTURES / "node_templates" / "index_samples"),
year=date.today().year,
)
assert f"must contain default version {default_version}" in err.args
def test_generate_index_ts_no_clients():
# use a non-nodejs template directory
with util.chdir(FIXTURES / "node_templates" / "index_samples"):
versions = ["v1", "v1beta1", "invalid_index"]
default_version = "invalid_index"
with pytest.raises(AttributeError) as err:
node_mono_repo.generate_index_ts(
versions,
default_version,
relative_dir=(FIXTURES / "node_templates" / "index_samples"),
year=date.today().year,
)
assert (
f"No client is exported in the default version's({default_version}) index.ts ."
in err.args
)
class TestPostprocess(TestCase):
@patch("synthtool.shell.run")
def test_install(self, shell_run_mock):
node_mono_repo.install()
calls = shell_run_mock.call_args_list
assert any(["npm install" in " ".join(call[0][0]) for call in calls])
@patch("synthtool.shell.run")
def test_fix(self, shell_run_mock):
node_mono_repo.fix()
calls = shell_run_mock.call_args_list
assert any(["npm run fix" in " ".join(call[0][0]) for call in calls])
@patch("synthtool.shell.run")
def test_compile_protos(self, shell_run_mock):
node_mono_repo.compile_protos()
calls = shell_run_mock.call_args_list
assert any(["npx compileProtos src" in " ".join(call[0][0]) for call in calls])
@patch("synthtool.shell.run")
def test_postprocess_gapic_library(self, shell_run_mock):
node_mono_repo.postprocess_gapic_library()
calls = shell_run_mock.call_args_list
assert any(["npm install" in " ".join(call[0][0]) for call in calls])
assert any(["npm run fix" in " ".join(call[0][0]) for call in calls])
assert any(["npx compileProtos src" in " ".join(call[0][0]) for call in calls])
# postprocess_gapic_library_hermetic() must be mocked because it depends on node modules
# present in the docker image but absent while running unit tests.
@patch("synthtool.languages.node_mono_repo.postprocess_gapic_library_hermetic")
def test_owlbot_main(hermetic_mock):
with util.copied_fixtures_dir(FIXTURES / "nodejs_mono_repo_with_staging"):
# just confirm it doesn't throw an exception.
node_mono_repo.owlbot_entrypoint(template_path=TEMPLATES)
@pytest.fixture
def nodejs_mono_repo():
"""chdir to a copy of nodejs-dlp-with-staging."""
with util.copied_fixtures_dir(
FIXTURES / "nodejs_mono_repo_with_staging"
) as workdir:
yield workdir
@patch("synthtool.languages.node_mono_repo.postprocess_gapic_library_hermetic")
def test_owlbot_main_with_staging(hermetic_mock, nodejs_mono_repo):
original_text = open(
FIXTURES
/ "nodejs_mono_repo_with_staging"
/ "packages"
/ "dlp"
/ "src"
/ "index.ts",
"rt",
).read()
node_mono_repo.owlbot_entrypoint(template_path=TEMPLATES)
# confirm index.ts was overwritten by template-generated index.ts.
staging_text = open(
FIXTURES
/ "nodejs_mono_repo_with_staging"
/ "owl-bot-staging"
/ "dlp"
/ "v2"
/ "src"
/ "index.ts",
"rt",
).read()
text = open("./packages/dlp/src/v2/index.ts", "rt").read()
assert staging_text != text
assert original_text != text
@patch("synthtool.languages.node_mono_repo.postprocess_gapic_library_hermetic")
def test_owlbot_main_with_staging_index_from_staging(hermetic_mock, nodejs_mono_repo):
node_mono_repo.owlbot_entrypoint(
template_path=TEMPLATES,
staging_excludes=["README.md", "package.json"],
templates_excludes=["src/index.ts"],
)
# confirm index.ts was overwritten by staging index.ts.
staging_text = open(
FIXTURES
/ "nodejs_mono_repo_with_staging"
/ "owl-bot-staging"
/ "dlp"
/ "v2"
/ "src"
/ "index.ts",
"rt",
).read()
text = open("./packages/dlp/src/index.ts", "rt").read()
assert staging_text == text
@patch("synthtool.languages.node_mono_repo.postprocess_gapic_library_hermetic")
def test_owlbot_main_with_staging_ignore_index(hermetic_mock, nodejs_mono_repo):
original_text = open(
FIXTURES
/ "nodejs_mono_repo_with_staging"
/ "packages"
/ "dlp"
/ "src"
/ "index.ts",
"rt",
).read()
node_mono_repo.owlbot_entrypoint(
template_path=TEMPLATES, templates_excludes=["src/index.ts"]
)
# confirm index.ts was overwritten by staging index.ts.
text = open("./packages/dlp/src/index.ts", "rt").read()
assert original_text == text
@patch("synthtool.languages.node_mono_repo.postprocess_gapic_library_hermetic")
def test_owlbot_main_with_staging_patch_staging(hermetic_mock, nodejs_mono_repo):
def patch(library: Path):
s.replace(library / "src" / "index.ts", "import", "export")
node_mono_repo.owlbot_entrypoint(
template_path=TEMPLATES,
staging_excludes=["README.md", "package.json"],
templates_excludes=["src/index.ts"],
patch_staging=patch,
)
# confirm index.ts was overwritten by staging index.ts.
staging_text = open(
FIXTURES
/ "nodejs_mono_repo_with_staging"
/ "owl-bot-staging"
/ "dlp"
/ "v2"
/ "src"
/ "index.ts",
"rt",
).read()
text = open("./packages/dlp/src/index.ts", "rt").read()
assert "import * as v2" in staging_text
assert "export * as v2" not in staging_text
assert "export * as v2" in text
def test_owlbot_main_without_version():
with util.copied_fixtures_dir(FIXTURES / "node_templates" / "no_version"):
# just confirm it doesn't throw an exception.
node_mono_repo.owlbot_entrypoint(template_path=TEMPLATES)