diff --git a/tests/mocks/README.md b/tests/mocks/README.md new file mode 100644 index 00000000..fc0f5073 --- /dev/null +++ b/tests/mocks/README.md @@ -0,0 +1,33 @@ +# Spectra + +> Refer to [Spectra order of insect][spectra], known for mimicking vegetation. + +## Mocking + +* `_mock`: Mock a function using the mock in `tests/mocks/` + * `function_name`: name of the method to mock +* `_mock_exit_status`: Mock a response exit status for a mock function + * `function_name`: name of the method to mock + * `status_code`: response to return +* `_mock_response`: Mock a response for a mock function + * `function_name`: name of the method to mock + * `response`: response to return +* `_clean_mock`: Clean a mock function (warning: erase the function) + * `function_name`: name of the method to mock + +## Spying + +* `_spy`: Create a spy method so, you can check it's been called with `_has_called` + * `function_name`: name of the method to spy +* `_has_called`: check spy method has been called, i.e has written to the /tmp/$function_name.mock_calls + * `function_name`: name of the that have been spied on + * `function_args` # arguments to passed to the spy + +## Cleaning + +* `_clean_all_mocks`: Clean all mock functions created by `_mock` +* `_pure_unmock`: Restore a pure function by reloading its source + * `function_name`: name of the method to mock +* `_clean_all_spy_calls`: Clean all spy calls created by `_spy` + +[spectra]: https://en.wikipedia.org/wiki/Phasmatodea diff --git a/tests/mocks/cleaning.fish b/tests/mocks/cleaning.fish new file mode 100644 index 00000000..55f67619 --- /dev/null +++ b/tests/mocks/cleaning.fish @@ -0,0 +1,40 @@ +function _clean_mock \ + --description "Clean a mock function (warning: erase the function)" \ + --argument-names \ + function_name + + functions --erase $function_name +end + +function _clean_all_mocks \ + --description "Clean all mock function" + + for mock in $__mocks + if functions --query $mock + functions --erase $mock + end + + if functions --query __backup_$function_name # restore original function + functions --copy __backup_$function_name $function_name + functions --erase __backup_$function_name + end + end + set --global __mocks # clear mocks +end + +function _clean_all_spy_calls \ + --description "Clean all spy calls created by `_spy`" + for mock_calls in /tmp/*.mock_calls + if test -r $mock_calls + rm -f $mock_calls + end + end +end + +function _pure_unmock \ + --description "Restore a pure function by reloading its source" \ + --argument-names \ + function_name + + source (status dirname)/../../functions/$function_name.fish +end diff --git a/tests/mocks/_pure_set_color.mock.fish b/tests/mocks/functions/_pure_set_color.mock.fish similarity index 100% rename from tests/mocks/_pure_set_color.mock.fish rename to tests/mocks/functions/_pure_set_color.mock.fish diff --git a/tests/mocks/kubectl.mock.fish b/tests/mocks/functions/kubectl.mock.fish similarity index 100% rename from tests/mocks/kubectl.mock.fish rename to tests/mocks/functions/kubectl.mock.fish diff --git a/tests/mocks/mocking.fish b/tests/mocks/mocking.fish new file mode 100644 index 00000000..9e9d14a6 --- /dev/null +++ b/tests/mocks/mocking.fish @@ -0,0 +1,63 @@ +function _mock \ + --description "Mock a function using the mock in `tests/mocks/`" \ + --argument-names \ + function_name # name of the method to mock + + set mock_filepath $SPECTRA_MOCKS_DIRECTORY/$function_name.mock.fish + if test -e $mock_filepath + source $SPECTRA_MOCKS_DIRECTORY/$function_name.mock.fish + set --global --append __mocks $function_name + end +end + +function _backup_before_mocking \ + --description "Backup a function by copying to prefixed function" \ + --argument-names \ + function_name + + if functions --query $function_name + functions --erase __backup_$function_name # force overwrite + functions --copy $function_name __backup_$function_name + functions --erase $function_name + end +end + +function _mock_exit_status \ + --description "Mock a response exit status for a mock function" \ + --argument-names \ + function_name \ + status_code # response to return + + echo $status_code >/tmp/$function_name.mock_status_code + _backup_before_mocking $function_name + + # redefine function to return mock status_code + function $function_name \ + --description "Mocking $function_name with exit code="$status_code \ + --argument-names status_code + echo (status current-function) >/tmp/(status current-function).mock_calls + return (command cat /tmp/(status current-function).mock_status_code) + end + set --global --append __mocks $function_name + set --global --append __mocks_backup __backup_$function_nameend +end + +function _mock_response \ + --description "Mock a response for a mock function" \ + --argument-names \ + function_name \ + response # response to return + + echo "$response" >/tmp/$function_name.mock_response + _backup_before_mocking $function_name + + # redefine function to return mock response + function $function_name \ + --description "Mocking $function_name response" \ + --argument-names mock_reponse + echo (status current-function) >/tmp/(status current-function).mock_calls + command cat /tmp/(status current-function).mock_response + end + set --global --append __mocks $function_name + set --global --append __mocks_backup __backup_$function_name +end diff --git a/tests/mocks/spectra.fish b/tests/mocks/spectra.fish index ce3c1185..c582e8c1 100644 --- a/tests/mocks/spectra.fish +++ b/tests/mocks/spectra.fish @@ -1,132 +1,8 @@ -# See: https://en.wikipedia.org/wiki/Phasmatodea +# Refer to Spectra order of insect, see https://en.wikipedia.org/wiki/Phasmatodea -function _mock \ - --description "Mock a function using the mock in `tests/mocks/`" \ - --argument-names \ - function_name # name of the method to mock +# Set up the path to located the mocked functions +set --universal SPECTRA_MOCKS_DIRECTORY (status dirname)/functions/ - set mock_filepath (status dirname)/$function_name.mock.fish - if test -e $mock_filepath - source (status dirname)/$function_name.mock.fish - set --global --append __mocks $function_name - end -end - -function _backup_before_mocking \ - --description "Backup a function by copying to prefixed function" \ - --argument-names \ - function_name - - if functions --query $function_name - functions --erase __backup_$function_name # force overwrite - functions --copy $function_name __backup_$function_name - functions --erase $function_name - end -end - -function _mock_exit_status \ - --description "Mock a response exit status for a mock function" \ - --argument-names \ - function_name \ - status_code # response to return - - echo $status_code >/tmp/$function_name.mock_status_code - _backup_before_mocking $function_name - - # redefine function to return mock status_code - function $function_name \ - --description "Mocking $function_name with exit code="$status_code \ - --argument-names status_code - echo (status current-function) >/tmp/(status current-function).mock_calls - return (command cat /tmp/(status current-function).mock_status_code) - end - set --global --append __mocks $function_name - set --global --append __mocks_backup __backup_$function_nameend -end - -function _mock_response \ - --description "Mock a response for a mock function" \ - --argument-names \ - function_name \ - response # response to return - - echo "$response" >/tmp/$function_name.mock_response - _backup_before_mocking $function_name - - # redefine function to return mock response - function $function_name \ - --description "Mocking $function_name response" \ - --argument-names mock_reponse - echo (status current-function) >/tmp/(status current-function).mock_calls - command cat /tmp/(status current-function).mock_response - end - set --global --append __mocks $function_name - set --global --append __mocks_backup __backup_$function_name -end - -function _clean_mock \ - --description "Clean a mock function (warning: erase the function)" \ - --argument-names \ - function_name - - functions --erase $function_name -end - -function _clean_all_mocks \ - --description "Clean all mock function" - - for mock in $__mocks - if functions --query $mock - functions --erase $mock - end - - if functions --query __backup_$function_name # restore original function - functions --copy __backup_$function_name $function_name - functions --erase __backup_$function_name - end - end - set --global __mocks # clear mocks -end - -function _spy \ - --description "Create a spy method so you can check it's been called with `_has_called`" \ - --argument-names \ - function_name # name of the method to spy - - _backup_before_mocking $function_name - - function $function_name - echo (status current-function) >/tmp/(status current-function).mock_calls - end # spy -end - -function _clean_all_spy_calls - for mock_calls in /tmp/*.mock_calls - if test -r $mock_calls - rm -f $mock_calls - end - end -end - -function _has_called \ - --description "check spy method has been called, i.e has written to the /tmp/$function_name.mock_calls" \ - --argument-names \ - function_name \ - function_args # arguments to passed to the spy - - set --query function_args[1]; or set function_args $function_name # ue - if test -r /tmp/$function_name.mock_calls - grep -c -q "$function_args" /tmp/$function_name.mock_calls \ - || printf "DEBUG: %s: received: `%s` expected: `%s`\n\n" (status current-function) (cat /tmp/$spy.mock_calls) $spy # check spy was called - else - return $FAILURE - end -end - -function _pure_unmock \ - --description "Restore a pure function by reloading its source" \ - --argument-names \ - function_name - - source (status dirname)/../../functions/$function_name.fish -end +source (status dirname)/mocking.fish +source (status dirname)/spying.fish +source (status dirname)/cleaning.fish diff --git a/tests/mocks/spying.fish b/tests/mocks/spying.fish new file mode 100644 index 00000000..da127634 --- /dev/null +++ b/tests/mocks/spying.fish @@ -0,0 +1,26 @@ +function _spy \ + --description "Create a spy method so you can check it's been called with `_has_called`" \ + --argument-names \ + function_name # name of the method to spy + + _backup_before_mocking $function_name + + function $function_name + echo (status current-function) >/tmp/(status current-function).mock_calls + end # spy +end + +function _has_called \ + --description "check spy method has been called, i.e has written to the /tmp/$function_name.mock_calls" \ + --argument-names \ + function_name \ + function_args # arguments to passed to the spy + + set --query function_args[1]; or set function_args $function_name # ue + if test -r /tmp/$function_name.mock_calls + grep -c -q "$function_args" /tmp/$function_name.mock_calls \ + || printf "DEBUG: %s: received: `%s` expected: `%s`\n\n" (status current-function) (cat /tmp/$spy.mock_calls) $spy # check spy was called + else + return $FAILURE + end +end