| 1 |
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE. |
| 2 |
# Copyright (C) NIWA & British Crown (Met Office) & Contributors. |
| 3 |
# |
| 4 |
# This program is free software: you can redistribute it and/or modify |
| 5 |
# it under the terms of the GNU General Public License as published by |
| 6 |
# the Free Software Foundation, either version 3 of the License, or |
| 7 |
# (at your option) any later version. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
import pytest |
| 18 |
|
| 19 |
from pathlib import Path |
| 20 |
from shlex import split |
| 21 |
from subprocess import run |
| 22 |
|
| 23 |
from cylc.flow.resources import ( |
| 24 |
RESOURCE_NAMES, |
| 25 |
get_resources, |
| 26 |
_backup, |
| 27 |
) |
| 28 |
|
| 29 |
|
| 30 |
def test_get_resources_one(tmpdir): |
| 31 |
"""Test extraction of a specific resource. |
| 32 |
|
| 33 |
Check that a file of the right name gets extracted. |
| 34 |
Do not check file content becuase there is no assurance that it will |
| 35 |
remain constant. |
| 36 |
""" |
| 37 |
get_resources('job.sh', tmpdir) |
| 38 |
assert (tmpdir / 'job.sh').isfile() |
| 39 |
|
| 40 |
|
| 41 |
@pytest.mark.parametrize( |
| 42 |
'resource', |
| 43 |
[ |
| 44 |
r for r |
| 45 |
in list(RESOURCE_NAMES.keys()) |
| 46 |
if r[0] != '!' |
| 47 |
] + ['tutorial/runtime-tutorial'] |
| 48 |
) |
| 49 |
def test_get_resources_all(resource, tmpdir): |
| 50 |
get_resources(resource, tmpdir) |
| 51 |
assert (tmpdir / Path(resource).name).exists() |
| 52 |
|
| 53 |
|
| 54 |
def test_cli(tmpdir): |
| 55 |
result = run( |
| 56 |
split(f'cylc get-resources job.sh {str(tmpdir)}'), |
| 57 |
capture_output=True |
| 58 |
) |
| 59 |
if result.returncode != 0: |
| 60 |
raise AssertionError( |
| 61 |
f'{result.stderr}' |
| 62 |
) |
| 63 |
|
| 64 |
|
| 65 |
def test_backup(tmp_path, caplog): |
| 66 |
a = tmp_path / 'a' |
| 67 |
abc = tmp_path / 'a' / 'b' / 'c' |
| 68 |
abc.mkdir(parents=True) |
| 69 |
before = set(tmp_path.glob('*')) |
| 70 |
|
| 71 |
_backup(a) |
| 72 |
assert len(caplog.record_tuples) == 1 |
| 73 |
|
| 74 |
after = set(tmp_path.glob('*')) |
| 75 |
assert len(after - before) == 1 |
| 76 |
|
| 77 |
new = list(after - before)[0] |
| 78 |
assert new.name.startswith(a.name) |
| 79 |
|
| 80 |
new_abc = new / 'b' / 'c' |
| 81 |
assert new_abc.exists() |
| 82 |
|
| 83 |
|
| 84 |
def test_vim_deprecated(): |
| 85 |
"""It fails, returning a warning if user asks for obsolete syntax file |
| 86 |
""" |
| 87 |
output = run( |
| 88 |
['cylc', 'get-resources', 'syntax/cylc.vim'], |
| 89 |
capture_output=True |
| 90 |
) |
| 91 |
assert 'has been replaced' in output.stderr.decode() |