-
Notifications
You must be signed in to change notification settings - Fork 29
/
test_init.py
471 lines (369 loc) · 18.9 KB
/
test_init.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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# -*- coding: utf-8 -*-
#
# Copyright 2018-2022- Swiss Data Science Center (SDSC)
# A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and
# Eidgenössische Technische Hochschule Zürich (ETHZ).
#
# 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
#
# http://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.
"""Test ``init`` command."""
import json
import os
import shutil
from pathlib import Path
from urllib.parse import urlparse
import pytest
from renku.core import errors
from renku.infrastructure.database import Database
from renku.infrastructure.repository import Repository
from renku.ui.cli import cli
from renku.ui.cli.init import parse_parameters
from tests.utils import format_result_exception, raises
def test_parse_parameters(project_init):
"""Test parsing parameters."""
def clean_param(p):
return [v for v in p if v != "--parameter"]
data, commands = project_init
parsed = parse_parameters(None, None, clean_param(commands["parameters"]))
keys = parsed.keys()
assert 2 == len(keys)
assert "p1" in keys
assert "p2" in keys
assert "v1" == parsed["p1"]
assert "v2" == parsed["p2"]
with raises(errors.ParameterError):
parse_parameters(
None, None, clean_param(commands["parameters"]) + clean_param(commands["parameters_equal_missing"])
)
with raises(errors.ParameterError):
parse_parameters(
None, None, clean_param(commands["parameters"]) + clean_param(commands["parameters_equal_early"])
)
def test_template_selection_helpers(isolated_runner):
"""Test template selection is displayed."""
url = "https://github.com/SwissDataScienceCenter/renku-project-template"
result = isolated_runner.invoke(cli, ["init", "-s", url, "-r", "0.3.0"], "2\n")
stripped_output = " ".join(result.output.split())
assert "Please choose a template by typing its index:" in stripped_output
assert "1 python" in stripped_output
assert "2 R" in stripped_output
def test_init(isolated_runner, project_init):
"""Test project initialization from template."""
data, commands = project_init
# create the project
new_project = Path(data["test_project"])
assert not new_project.exists()
result = isolated_runner.invoke(cli, commands["init_test"] + commands["id"], commands["confirm"])
assert 0 == result.exit_code, format_result_exception(result)
assert new_project.exists()
assert "RENKU HOOK. DO NOT REMOVE OR MODIFY." in (new_project / ".git" / "hooks" / "pre-commit").read_text()
assert (new_project / ".renku").exists()
assert (new_project / ".renku" / "renku.ini").exists()
assert (new_project / ".renku" / "metadata").exists()
# try to re-create in the same folder
result = isolated_runner.invoke(cli, commands["init_test"] + commands["id"], commands["confirm"])
assert 0 != result.exit_code
# force re-create in the same folder
result = isolated_runner.invoke(
cli, commands["init_test"] + commands["id"] + commands["force"], commands["confirm"]
)
assert 0 == result.exit_code, format_result_exception(result)
assert new_project.exists()
assert (new_project / ".renku").exists()
assert (new_project / ".renku" / "renku.ini").exists()
assert (new_project / ".renku" / "metadata").exists()
# init using index instead of id
new_project_2 = Path(data["test_project_alt"])
result = isolated_runner.invoke(cli, commands["init_alt"] + commands["id"], commands["confirm"])
assert 0 == result.exit_code, format_result_exception(result)
assert new_project_2.exists()
assert (new_project_2 / ".renku").exists()
assert (new_project_2 / ".renku" / "renku.ini").exists()
assert (new_project_2 / ".renku" / "metadata").exists()
# verify both init lead to the same result
template_files = [f for f in new_project.glob("**/*") if ".git" not in str(f) and ".renku/metadata/" not in str(f)]
for template_file in template_files:
expected_file = new_project_2 / template_file.relative_to(new_project)
assert expected_file.exists()
def test_init_initial_branch(isolated_runner, project_init):
"""Test project initialization from template."""
data, commands = project_init
# create the project
new_project = Path(data["test_project"])
assert not new_project.exists()
result = isolated_runner.invoke(
cli, commands["init_test"] + commands["id"] + commands["initial_branch_main"], commands["confirm"]
)
assert 0 == result.exit_code, format_result_exception(result)
assert new_project.exists()
assert (new_project / ".renku").exists()
assert (new_project / ".renku" / "renku.ini").exists()
assert (new_project / ".renku" / "metadata").exists()
assert Repository(new_project).active_branch.name == data["main_branch"]
@pytest.mark.parametrize(
"remote",
[
(
"https://user:password@gitlab.dev.renku.ch/group/subgroup/project.git",
"https://dev.renku.ch/projects/group/subgroup/project",
),
("ssh://@dev.renku.ch:group/subgroup/project.git", "https://dev.renku.ch/projects/group/subgroup/project"),
(
"https://user:password@gitlab.dev.renku.ch/group/subgroup/sub-subgroup/project.git",
"https://dev.renku.ch/projects/group/subgroup/sub-subgroup/project",
),
(
"https://user:password@dev.renku.ch/group/subgroup/project.git",
"https://dev.renku.ch/projects/group/subgroup/project",
),
("https://user:password@dev.renku.ch/user/project.git", "https://dev.renku.ch/projects/user/project"),
],
)
def test_init_with_git_remote(isolated_runner, project_init, remote):
"""Test project initialization with remote and possibly gitlab groups set."""
data, commands = project_init
# create the project
new_project = Path(data["test_project"])
new_project.mkdir()
repository = Repository.initialize(new_project)
repository.remotes.add("origin", remote[0])
result = isolated_runner.invoke(
cli, commands["init_test"] + commands["id"] + commands["force"], commands["confirm"]
)
assert 0 == result.exit_code, format_result_exception(result)
assert new_project.exists()
assert (new_project / ".renku").exists()
assert (new_project / ".renku" / "renku.ini").exists()
assert (new_project / ".renku" / "metadata").exists()
url = urlparse(remote[1])
assert url.path in (new_project / ".renku" / "metadata" / "project").read_text()
def test_init_force_in_empty_dir(isolated_runner, project_init):
"""Run init --force in empty directory."""
data, commands = project_init
new_project = Path(data["test_project"])
assert not new_project.exists()
result = isolated_runner.invoke(
cli, commands["init_test"] + commands["id"] + commands["force"], commands["confirm"]
)
assert 0 == result.exit_code, format_result_exception(result)
def test_init_force_in_dirty_dir(isolated_runner, project_init):
"""Run init --force in dirty directory."""
data, commands = project_init
new_project = Path(data["test_project"])
assert not new_project.exists()
new_project.mkdir(parents=True)
random_file = new_project / "random_file.txt"
with random_file.open("w") as destination:
destination.writelines(["random text"])
assert random_file.exists()
result = isolated_runner.invoke(cli, commands["init_test"] + commands["id"], commands["confirm"])
assert 0 == result.exit_code, format_result_exception(result)
assert "Project initialized" in result.output
shutil.rmtree(new_project)
new_project.mkdir(parents=True)
dockerfile = new_project / "Dockerfile"
with dockerfile.open("w") as destination:
destination.writelines(["not a dockerfile"])
assert dockerfile.exists()
readme = new_project / "README.md"
with readme.open("w") as destination:
destination.writelines(["My first project!"])
assert readme.exists()
random_file = new_project / "random_file.txt"
with random_file.open("w") as destination:
destination.writelines(["random text"])
assert random_file.exists()
gitignore = new_project / ".gitignore"
with gitignore.open("w") as destination:
destination.writelines(["dummy text that's definitely not in the actual gitignore"])
assert gitignore.exists()
result = isolated_runner.invoke(cli, commands["init_test"] + commands["id"], commands["confirm"])
assert 1 == result.exit_code
assert "The following files exist in the directory and will be overwritten" in result.output
assert "The following files exist in the directory and will be appended to" in result.output
assert "\tDockerfile\n" in result.output
assert "\t.gitignore\n" in result.output
result = isolated_runner.invoke(
cli, commands["init_test"] + commands["id"] + commands["force"], commands["confirm"]
)
assert 0 == result.exit_code, format_result_exception(result)
assert random_file.exists()
assert dockerfile.exists()
assert "not a dockerfile" not in dockerfile.read_text()
assert gitignore.exists()
assert "dummy text that's definitely not in the actual gitignore" in gitignore.read_text()
assert readme.exists()
assert "My first project!" == readme.read_text()
def test_init_on_cloned_repo(isolated_runner, data_repository, project_init):
"""Run init --force in directory containing another repository."""
data, commands = project_init
new_project = Path(data["test_project"])
import shutil
shutil.copytree(str(data_repository.path), str(new_project))
assert new_project.exists()
# try to create in a dirty folder
result = isolated_runner.invoke(cli, commands["init_test"] + commands["id"], commands["confirm"])
assert 0 == result.exit_code, format_result_exception(result)
assert new_project.exists()
assert (new_project / ".renku").exists()
assert (new_project / ".renku" / "renku.ini").exists()
assert (new_project / ".renku" / "metadata").exists()
@pytest.mark.integration
def test_init_remote(isolated_runner, project_init):
"""Test project initialization from a remote template."""
data, commands = project_init
# create the project
new_project = Path(data["test_project"])
assert not new_project.exists()
result = isolated_runner.invoke(
cli, commands["init_test"] + commands["id"] + commands["force"], commands["confirm"]
)
assert 0 == result.exit_code, format_result_exception(result)
assert new_project.exists()
assert (new_project / ".renku").exists()
assert (new_project / ".renku" / "renku.ini").exists()
assert (new_project / ".renku" / "metadata").exists()
@pytest.mark.integration
def test_init_new_metadata_defaults(isolated_runner, project_init):
"""Test project initialization from a remote template with defaults doesn't prompt for those values."""
data, commands = project_init
template_source = ["--template-source", commands["init_custom_template"]]
# NOTE: Set values for parameters that don't have default values
parameters = ["-p", "description=some description", "-p", "number_val=70.12"]
result = isolated_runner.invoke(cli, commands["init_custom"] + template_source + parameters)
assert 0 == result.exit_code, format_result_exception(result)
project = Database.from_path(Path(data["test_project"]) / ".renku" / "metadata").get("project")
metadata = json.loads(project.template_metadata.metadata)
assert True is metadata["bool_var"]
assert "ask again" == metadata["enum_var"]
assert "some description" == metadata["description"]
assert 70.12 == metadata["number_val"]
assert "Enter a value for" not in result.output
@pytest.mark.integration
def test_init_new_metadata_defaults_is_overwritten(isolated_runner, project_init):
"""Test passed parameters overwrite default parameters values."""
data, commands = project_init
template_source = ["--template-source", commands["init_custom_template"]]
parameters = ["-p", "description=some description", "-p", "number_val=70.12"]
parameters += ["-p", "enum_var=maybe", "-p", "bool_var=false"]
result = isolated_runner.invoke(cli, commands["init_custom"] + template_source + parameters)
assert 0 == result.exit_code, format_result_exception(result)
project = Database.from_path(Path(data["test_project"]) / ".renku" / "metadata").get("project")
metadata = json.loads(project.template_metadata.metadata)
assert metadata["bool_var"] is False
assert "maybe" == metadata["enum_var"]
assert "some description" == metadata["description"]
assert 70.12 == metadata["number_val"]
assert "Enter a value for" not in result.output
@pytest.mark.integration
def test_init_new_metadata_invalid_param_value(isolated_runner, project_init):
"""Test project initialization from a remote template."""
data, commands = project_init
template_source = ["--template-source", commands["init_custom_template"]]
# NOTE: Set values for parameters
parameters = ["-p", "description=some description", "-p", "number_val=invalid-number"]
result = isolated_runner.invoke(cli, commands["init_custom"] + template_source + parameters, "42\n")
assert 0 == result.exit_code, format_result_exception(result)
assert "Enter a value for 'number_val'" in result.output
assert "Enter a value for 'bool_var'" not in result.output
def test_init_with_parameters(isolated_runner, project_init, template):
"""Test project initialization using custom metadata."""
data, commands = project_init
# create the project
new_project = Path(data["test_project"])
assert not new_project.exists()
result = isolated_runner.invoke(
cli, commands["init_test"] + commands["id"] + commands["parameters"] + commands["parameters_equal_missing"]
)
assert 0 != result.exit_code
assert (
f'Error: Invalid parameter value for --parameter "{ commands["parameters_equal_missing"][1]}"' in result.output
)
result = isolated_runner.invoke(
cli, commands["init_test"] + commands["id"] + commands["parameters"] + commands["parameters_equal_early"]
)
assert 0 != result.exit_code
assert f'Error: Invalid parameter value for --parameter "{commands["parameters_equal_early"][1]}"' in result.output
result = isolated_runner.invoke(
cli, commands["init_test"] + commands["id"] + commands["parameters"], commands["confirm"]
)
assert 0 == result.exit_code, format_result_exception(result)
# TODO: Re-enable this check once parameters are added to the template.
# assert "The template requires a value for" in result.output
for param in set(template["metadata"].keys()):
assert param in result.output
assert "These parameters are not used by the template and were ignored:" in result.output
def test_init_with_custom_metadata(isolated_runner, project_init, template):
"""Test project initialization using custom metadata."""
data, commands = project_init
metadata = {
"@id": "https://example.com/annotation1",
"@type": "https://schema.org/specialType",
"https://schema.org/specialProperty": "some_unique_value",
}
metadata_path = Path("metadata.json")
metadata_path.write_text(json.dumps(metadata))
# create the project
new_project = Path(data["test_project"])
assert not new_project.exists()
result = isolated_runner.invoke(cli, commands["init_test"] + commands["id"] + ["--metadata", str(metadata_path)])
assert 0 == result.exit_code
database = Database.from_path(new_project / ".renku" / "metadata")
project = database.get("project")
assert metadata == project.annotations[0].body
@pytest.mark.parametrize("data_dir", ["dir", "nested/dir/s"])
def test_init_with_data_dir(isolated_runner, data_dir, directory_tree, project_init):
"""Test initializing with data directory."""
data, commands = project_init
new_project = Path(data["test_project"])
result = isolated_runner.invoke(cli, commands["init_test"] + commands["id"] + ["--datadir", data_dir])
assert 0 == result.exit_code, format_result_exception(result)
assert (new_project / data_dir).exists()
assert (new_project / data_dir / ".gitkeep").exists()
assert not Repository(new_project).is_dirty(untracked_files=True)
os.chdir(new_project.resolve())
result = isolated_runner.invoke(cli, ["dataset", "add", "--copy", "-c", "my-data", str(directory_tree)])
assert 0 == result.exit_code, format_result_exception(result)
assert (Path(data_dir) / "my-data" / directory_tree.name / "file1").exists()
@pytest.mark.parametrize("data_dir", ["/absolute/path/outside", "../relative/path/outside"])
def test_init_with_wrong_data_dir(isolated_runner, data_dir, project_init):
"""Test initialization fails with wrong data directory."""
data, commands = project_init
result = isolated_runner.invoke(cli, commands["init_test"] + commands["id"] + ["--datadir", data_dir])
assert 2 == result.exit_code
assert f"Data directory {data_dir} is not within project" in result.output
@pytest.mark.parametrize("data_dir", [".", ".git", ".renku", ".git/"])
def test_init_with_invalid_data_dir(isolated_runner, data_dir, project_init):
"""Test initialization fails with invalid data directory."""
data, commands = project_init
result = isolated_runner.invoke(cli, commands["init_test"] + commands["id"] + ["--datadir", data_dir])
assert 2 == result.exit_code
data_dir = data_dir.rstrip("/")
assert f"Cannot use {data_dir} as data directory." in result.output
def test_init_with_description(isolated_runner, template):
"""Test project initialization with description."""
result = isolated_runner.invoke(
cli, ["init", "--description", "my project description", "new project", "--template-id", template["id"]]
)
assert 0 == result.exit_code, format_result_exception(result)
database = Database.from_path(Path("new project") / ".renku" / "metadata")
project = database.get("project")
assert "new project" == project.name
assert project.id.endswith("new-project") # make sure id uses slug version of name without space
assert "my project description" in project.template_metadata.metadata
assert "my project description" == project.description
readme_content = (Path("new project") / "README.md").read_text()
assert "my project description" in readme_content
os.chdir("new project")
result = isolated_runner.invoke(cli, ["graph", "export", "--strict"])
assert 0 == result.exit_code, format_result_exception(result)
assert "my project description" in result.output