Skip to content

Commit

Permalink
Started shortening lines.
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewHambley committed Jun 28, 2024
1 parent b663a41 commit 9f2adc8
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 48 deletions.
15 changes: 11 additions & 4 deletions tests/unit_tests/steps/test_compile_fortran.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,9 +439,14 @@ def test_obj_missing(self, content):
res, artefacts = process_file((analysed_file, mp_common_args))

expect_object_fpath = Path(f'/fab/proj/build_output/_prebuild/foofile.{obj_combo_hash}.o')
assert res == CompiledFile(input_fpath=analysed_file.fpath, output_fpath=expect_object_fpath)
assert res == CompiledFile(input_fpath=analysed_file.fpath,
output_fpath=expect_object_fpath)
mock_compile_file.assert_called_once_with(
analysed_file.fpath, flags, output_fpath=expect_object_fpath, mp_common_args=mp_common_args)
analysed_file.fpath,
flags,
output_fpath=expect_object_fpath,
mp_common_args=mp_common_args
)
self.ensure_mods_stored(mock_copy, mods_combo_hash)

# check the correct artefacts were returned
Expand Down Expand Up @@ -469,7 +474,9 @@ def test_vanilla(self, tool_box):
with mock.patch('pathlib.Path.exists', side_effect=[True, True]):
with mock.patch(
'fab.steps.compile_fortran.file_checksum',
side_effect=[mock.Mock(file_hash=123), mock.Mock(file_hash=456)]):
result = get_mod_hashes(analysed_files=analysed_files, config=config)
side_effect=[mock.Mock(file_hash=123),
mock.Mock(file_hash=456)]):
result = get_mod_hashes(analysed_files=analysed_files,
config=config)

assert result == {'foo': 123, 'bar': 456}
25 changes: 20 additions & 5 deletions tests/unit_tests/steps/test_grab.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,17 @@
class TestGrabFolder:

def test_trailing_slash(self):
with pytest.warns(UserWarning, match="_metric_send_conn not set, cannot send metrics"):
with pytest.warns(
UserWarning,
match="_metric_send_conn not set, cannot send metrics"
):
self._common(grab_src='/grab/source/', expect_grab_src='/grab/source/')

def test_no_trailing_slash(self):
with pytest.warns(UserWarning, match="_metric_send_conn not set, cannot send metrics"):
with pytest.warns(
UserWarning,
match="_metric_send_conn not set, cannot send metrics"
):
self._common(grab_src='/grab/source', expect_grab_src='/grab/source/')

def _common(self, grab_src, expect_grab_src):
Expand Down Expand Up @@ -67,12 +73,21 @@ def test_revision(self):
mock_config = SimpleNamespace(source_root=source_root,
tool_box=ToolBox())
with mock.patch('pathlib.Path.mkdir'):
with mock.patch('fab.tools.tool.Tool.run') as mock_run, \
pytest.warns(UserWarning, match="_metric_send_conn not set, cannot send metrics"):
with mock.patch(
'fab.tools.tool.Tool.run'
) as mock_run, pytest.warns(
UserWarning,
match="_metric_send_conn not set, cannot send metrics"
):
fcm_export(mock_config, src=source_url, dst_label=dst_label, revision=revision)

mock_run.assert_called_once_with(
['export', '--force', '--revision', '42', f'{source_url}', str(source_root / dst_label)],
[
'export',
'--force',
'--revision', '42',
f'{source_url}', str(source_root / dst_label)
],
env=None, cwd=None, capture_output=True)

# todo: test missing repo
Expand Down
11 changes: 7 additions & 4 deletions tests/unit_tests/steps/test_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@ def test_run(self, tool_box):
linker._is_available = True
tool_box.add_tool(linker, silent_replace=True)
mock_result = mock.Mock(returncode=0, stdout="abc\ndef".encode())
with mock.patch('fab.tools.tool.subprocess.run',
return_value=mock_result) as tool_run, \
pytest.warns(UserWarning, match="_metric_send_conn not "
"set, cannot send metrics"):
with mock.patch(
'fab.tools.tool.subprocess.run',
return_value=mock_result
) as tool_run, pytest.warns(
UserWarning,
match="_metric_send_conn not set, cannot send metrics"
):
link_exe(config, flags=['-fooflag', '-barflag'])

tool_run.assert_called_with(
Expand Down
20 changes: 14 additions & 6 deletions tests/unit_tests/steps/test_psyclone_unit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import pytest

from fab.parse.x90 import AnalysedX90
from fab.steps.psyclone import _check_override, _gen_prebuild_hash, MpCommonArgs
from fab.steps.psyclone import (MpCommonArgs,
_check_override,
_gen_prebuild_hash)
from fab.util import file_checksum


Expand Down Expand Up @@ -38,7 +40,9 @@ def data(self, tmp_path) -> Tuple[MpCommonArgs, Path, int]:
# the script is just hashed later, so any one will do - use this file!
mock_transformation_script = mock.Mock(return_value=__file__)

expect_hash = 223133492 + file_checksum(__file__).file_hash # add the transformation_script_hash
# add the transformation_script_hash
#
expect_hash = 223133492 + file_checksum(__file__).file_hash

mp_payload = MpCommonArgs(
analysed_x90=analysed_x90,
Expand Down Expand Up @@ -75,8 +79,10 @@ def test_trans_script(self, data):
# changing the transformation script should change the hash
mp_payload, x90_file, expect_hash = data
mp_payload.transformation_script = None
with pytest.warns(UserWarning, match="no transformation script specified"):
result = _gen_prebuild_hash(x90_file=x90_file, mp_payload=mp_payload)
with pytest.warns(UserWarning,
match="no transformation script specified"):
result = _gen_prebuild_hash(x90_file=x90_file,
mp_payload=mp_payload)
# transformation_script_hash = 0
assert result == expect_hash - file_checksum(__file__).file_hash

Expand All @@ -91,14 +97,16 @@ def test_cli_args(self, data):
class Test_check_override(object):

def test_no_override(self):
mp_payload = mock.Mock(overrides_folder=Path('/foo'), override_files=[Path('/foo/bar.f90')])
mp_payload = mock.Mock(overrides_folder=Path('/foo'),
override_files=[Path('/foo/bar.f90')])

check_path = Path('/not_foo/bar.f90')
result = _check_override(check_path=check_path, mp_payload=mp_payload)
assert result == check_path

def test_override(self):
mp_payload = mock.Mock(overrides_folder=Path('/foo'), override_files=[Path('/foo/bar.f90')])
mp_payload = mock.Mock(overrides_folder=Path('/foo'),
override_files=[Path('/foo/bar.f90')])

check_path = Path('/foo/bar.f90')
result = _check_override(check_path=check_path, mp_payload=mp_payload)
Expand Down
34 changes: 24 additions & 10 deletions tests/unit_tests/steps/test_root_inc_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,35 @@ def test_vanilla(self):
config.artefact_store['all_source'] = inc_files

with mock.patch('fab.steps.root_inc_files.shutil') as mock_shutil:
with mock.patch('fab.steps.root_inc_files.Path.mkdir'), \
pytest.warns(UserWarning, match="_metric_send_conn not set, cannot send metrics"):
with mock.patch(
'fab.steps.root_inc_files.Path.mkdir'
), pytest.warns(
UserWarning,
match="_metric_send_conn not set, cannot send metrics"
):
root_inc_files(config)

mock_shutil.copy.assert_called_once_with(inc_files[0], config.build_output)
mock_shutil.copy.assert_called_once_with(inc_files[0],
config.build_output)

def test_skip_output_folder(self):
# ensure it doesn't try to copy a file in the build output
config = BuildConfig('proj', ToolBox())
inc_files = [Path('/foo/source/bar.inc'), config.build_output / 'fab.inc']
inc_files = [Path('/foo/source/bar.inc'),
config.build_output / 'fab.inc']
config.artefact_store['all_source'] = inc_files

with mock.patch('fab.steps.root_inc_files.shutil') as mock_shutil:
with mock.patch('fab.steps.root_inc_files.Path.mkdir'), \
pytest.warns(UserWarning, match="_metric_send_conn not set, cannot send metrics"):
with mock.patch(
'fab.steps.root_inc_files.Path.mkdir'
), pytest.warns(
UserWarning,
match="_metric_send_conn not set, cannot send metrics"
):
root_inc_files(config)

mock_shutil.copy.assert_called_once_with(inc_files[0], config.build_output)
mock_shutil.copy.assert_called_once_with(
inc_files[0], config.build_output)

def test_name_clash(self):
# ensure raises an exception if there is a name clash
Expand All @@ -46,7 +57,10 @@ def test_name_clash(self):

with pytest.raises(FileExistsError):
with mock.patch('fab.steps.root_inc_files.shutil'):
with mock.patch('fab.steps.root_inc_files.Path.mkdir'), \
pytest.warns(DeprecationWarning,
match="RootIncFiles is deprecated as .inc files are due to be removed."):
with mock.patch(
'fab.steps.root_inc_files.Path.mkdir'
), pytest.warns(
DeprecationWarning,
match="RootIncFiles is deprecated as .inc files are due to be removed." # noqa: E501
):
root_inc_files(config)
26 changes: 18 additions & 8 deletions tests/unit_tests/test_artefacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,27 +59,37 @@ def artefact_store(self):
return artefact_store

def test_single_suffix(self, artefact_store):
# ensure the artefact getter passes through the trees properly to the filter func

"""
Ensures the artefact getter passes through the trees properly to the
filter function.
"""
# run the artefact getter
filter_build_trees = FilterBuildTrees('.foo', BUILD_TREES)
with mock.patch('fab.artefacts.filter_source_tree') as mock_filter_func:
with mock.patch(
'fab.artefacts.filter_source_tree'
) as mock_filter_func:
filter_build_trees(artefact_store)

mock_filter_func.assert_has_calls([
call(source_tree=artefact_store[BUILD_TREES]['tree1'], suffixes=['.foo']),
call(source_tree=artefact_store[BUILD_TREES]['tree2'], suffixes=['.foo']),
call(source_tree=artefact_store[BUILD_TREES]['tree1'],
suffixes=['.foo']),
call(source_tree=artefact_store[BUILD_TREES]['tree2'],
suffixes=['.foo']),
])

def test_multiple_suffixes(self, artefact_store):
# test it works with multiple suffixes provided
filter_build_trees = FilterBuildTrees(['.foo', '.bar'], BUILD_TREES)
with mock.patch('fab.artefacts.filter_source_tree') as mock_filter_func:
with mock.patch(
'fab.artefacts.filter_source_tree'
) as mock_filter_func:
filter_build_trees(artefact_store)

mock_filter_func.assert_has_calls([
call(source_tree=artefact_store[BUILD_TREES]['tree1'], suffixes=['.foo', '.bar']),
call(source_tree=artefact_store[BUILD_TREES]['tree2'], suffixes=['.foo', '.bar']),
call(source_tree=artefact_store[BUILD_TREES]['tree1'],
suffixes=['.foo', '.bar']),
call(source_tree=artefact_store[BUILD_TREES]['tree2'],
suffixes=['.foo', '.bar']),
])


Expand Down
8 changes: 6 additions & 2 deletions tests/unit_tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
class TestAddFlags:

def test_run(self):
add_flags = AddFlags(match="$source/foo/*", flags=['-I', '$relative/include'])
add_flags = AddFlags(match="$source/foo/*",
flags=['-I', '$relative/include'])
config = BuildConfig('proj', ToolBox(),
fab_workspace=Path("/fab_workspace"))

Expand All @@ -18,7 +19,10 @@ def test_run(self):
fpath=Path(f"/fab_workspace/proj/{SOURCE_ROOT}/foo/bar.c"),
input_flags=my_flags,
config=config)
assert my_flags == ['-foo', '-I', f'/fab_workspace/proj/{SOURCE_ROOT}/foo/include']
assert my_flags == [
'-foo',
'-I', f'/fab_workspace/proj/{SOURCE_ROOT}/foo/include'
]

# anything in $source/bar should NOT get the include folder
my_flags = ["-foo"]
Expand Down
5 changes: 4 additions & 1 deletion tests/unit_tests/test_dep_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ def src_tree():
return {
Path('foo.f90'): AnalysedDependent(fpath=Path('foo.f90'), file_hash=0),
Path('root.f90'): AnalysedDependent(
fpath=Path('root.f90'), file_deps={Path('a.f90'), Path('b.f90')}, file_hash=0),
fpath=Path('root.f90'),
file_deps={Path('a.f90'), Path('b.f90')},
file_hash=0
),
Path('a.f90'): AnalysedDependent(
fpath=Path('a.f90'), file_deps={Path('c.f90')}, file_hash=0),
Path('b.f90'): AnalysedDependent(
Expand Down
15 changes: 12 additions & 3 deletions tests/unit_tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,20 @@ class TestSuffixFilter(object):

def test_constructor_suffix_scalar(self):
getter = SuffixFilter('barz', '.c')
result = getter(artefact_store={'barz': [Path('bar.a'), Path('bar.b'), Path('bar.c')]})
result = getter(
artefact_store={
'barz': [Path('bar.a'), Path('bar.b'), Path('bar.c')]
}
)
assert result == [Path('bar.c')]

def test_constructor_suffix_vector(self):
getter = SuffixFilter('barz', ['.b', '.c'])
result = getter(artefact_store={'barz': [Path('bar.a'), Path('bar.b'), Path('bar.c')]})
result = getter(
artefact_store={
'barz': [Path('bar.a'), Path('bar.b'), Path('bar.c')]
}
)
assert result == [Path('bar.b'), Path('bar.c')]


Expand Down Expand Up @@ -78,7 +86,8 @@ class Test_input_to_output_fpath(object):

@pytest.fixture
def config(self):
return mock.Mock(source_root=Path('/proj/source'), build_output=Path('/proj/build_output'))
return mock.Mock(source_root=Path('/proj/source'),
build_output=Path('/proj/build_output'))

def test_vanilla(self, config):
input_path = Path('/proj/source/folder/file.txt')
Expand Down
10 changes: 5 additions & 5 deletions tests/unit_tests/tools/test_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def test_gfortran_4(self):
under the terms of the GNU General Public License.
For more information about these matters, see the file named COPYING
""")
""") # noqa: E501

self._check(full_version_string=full_version_string, expected='4.8.5')

Expand All @@ -207,7 +207,7 @@ def test_gfortran_6(self):
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
""")
""") # noqa: E501

self._check(full_version_string=full_version_string, expected='6.1.0')

Expand All @@ -219,7 +219,7 @@ def test_gfortran_8(self):
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
""")
""") # noqa: E501

self._check(full_version_string=full_version_string, expected='8.5.0')

Expand All @@ -231,7 +231,7 @@ def test_gfortran_10(self):
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
""")
""") # noqa: E501

self._check(full_version_string=full_version_string, expected='10.4.0')

Expand All @@ -243,7 +243,7 @@ def test_gfortran_12(self):
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
""")
""") # noqa: E501

self._check(full_version_string=full_version_string, expected='12.1.0')

Expand Down

0 comments on commit 9f2adc8

Please sign in to comment.