Skip to content

Commit

Permalink
improve mocking fs in integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
idevat committed Jan 16, 2018
1 parent 1e7f67c commit 4cbf853
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions pcs/test/tools/command_env/mock_fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
)

import os.path
import site


CALL_TYPE_FS = "CALL_TYPE_FS"
Expand Down Expand Up @@ -75,27 +76,33 @@ def __ne__(self, other):
)

def get_fs_mock(call_queue):
package_dir = os.path.realpath(
package_dir_list = site.getsitepackages()
package_dir_list.append(os.path.realpath(
os.path.dirname(os.path.abspath(__file__))+"/../../.."
)
))
def get_fs_call(func_name, original_call):
def call_fs(*args, **kwargs):
# Standard python unittest tries to open the test file (for caching)
# when the test raises AssertionError. Unittest do it before it
# calls cleanup so at this moment the function open is still mocked.
# Pcs should not open file inside its package in the command so
# Standard python unittest tries to open some python code (e.g. the
# test file for caching when the test raises AssertionError).
# It is before it the cleanup is called so at this moment the
# function open is still mocked.
# Pcs should not open file inside python package in the command so
# attempt to open file inside pcs package is almost certainly
# outside of library command and we will provide the original
# function.
if func_name == "open" and args[0].startswith(package_dir):
return original_call(*args, **kwargs)

if func_name == "open":
for python_package_dir in package_dir_list:
if args[0].startswith(python_package_dir):
return original_call(*args, **kwargs)

dummy_i, expected_call = call_queue.take(CALL_TYPE_FS)
real_call = Call(
func_name,
call_kwargs=_get_all_args_as_kwargs(func_name, args, kwargs)
)
dummy_i, expected_call = call_queue.take(
CALL_TYPE_FS,
repr(real_call)
)

if expected_call != real_call:
raise call_queue.error_with_context(
Expand Down

0 comments on commit 4cbf853

Please sign in to comment.