Prevent calls to exit implementations in tests (#6653)

Co-authored-by: Marc Bernard <59966492+mbtools@users.noreply.github.com>
This commit is contained in:
Fabian Lupa 2023-11-22 07:33:51 +01:00 committed by GitHub
parent f2d49c2731
commit 60f01499e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 4 deletions

View File

@ -17,6 +17,10 @@ CLASS zcl_abapgit_exit DEFINITION
CLASS-DATA gi_global_exit TYPE REF TO zif_abapgit_exit.
CLASS-DATA gi_exit TYPE REF TO zif_abapgit_exit.
CLASS-METHODS is_running_in_test_context
RETURNING
VALUE(rv_running_in_test_context) TYPE abap_bool.
ENDCLASS.
@ -40,10 +44,13 @@ CLASS zcl_abapgit_exit IMPLEMENTATION.
lv_class_name = |\\PROGRAM={ sy-repid }\\CLASS={ lv_class_name }|.
ENDIF.
TRY.
CREATE OBJECT gi_exit TYPE (lv_class_name).
CATCH cx_sy_create_object_error ##NO_HANDLER.
ENDTRY.
" Prevent non-mocked exit calls in unit tests
IF is_running_in_test_context( ) = abap_false.
TRY.
CREATE OBJECT gi_exit TYPE (lv_class_name).
CATCH cx_sy_create_object_error ##NO_HANDLER.
ENDTRY.
ENDIF.
CREATE OBJECT gi_global_exit TYPE zcl_abapgit_exit. " this class
@ -51,6 +58,27 @@ CLASS zcl_abapgit_exit IMPLEMENTATION.
ENDMETHOD.
METHOD is_running_in_test_context.
IF sy-sysid = 'ABC'.
" always run on open-abap
rv_running_in_test_context = abap_true.
RETURN.
ENDIF.
" Check if the local test class can be accessed by RTTI. If so the current process is running in a unit test.
" Note this approach only works for the developer version. The standalone version will always report not running in
" test context which should be fine as there are no unit tests delivered in it.
cl_abap_typedescr=>describe_by_name(
EXPORTING
p_name = |\\PROGRAM={ sy-repid }\\CLASS=LTCL_TEST|
EXCEPTIONS
type_not_found = 1
OTHERS = 2 ).
rv_running_in_test_context = boolc( sy-subrc = 0 ).
ENDMETHOD.
METHOD zif_abapgit_exit~adjust_display_commit_url.

View File

@ -0,0 +1,20 @@
*"* use this source file for your ABAP unit test classes
CLASS ltcl_test DEFINITION DEFERRED.
CLASS zcl_abapgit_exit DEFINITION LOCAL FRIENDS ltcl_test.
" The class name ltcl_test is hardcoded in zcl_abapgit_exit=>is_running_in_test_context
CLASS ltcl_test DEFINITION FOR TESTING RISK LEVEL HARMLESS DURATION SHORT.
PRIVATE SECTION.
METHODS:
is_running_in_test_context FOR TESTING.
ENDCLASS.
CLASS ltcl_test IMPLEMENTATION.
METHOD is_running_in_test_context.
cl_abap_unit_assert=>assert_equals(
act = zcl_abapgit_exit=>is_running_in_test_context( )
exp = abap_true ).
ENDMETHOD.
ENDCLASS.

View File

@ -10,6 +10,7 @@
<CLSCCINCL>X</CLSCCINCL>
<FIXPT>X</FIXPT>
<UNICODE>X</UNICODE>
<WITH_UNIT_TESTS>X</WITH_UNIT_TESTS>
</VSEOCLASS>
</asx:values>
</asx:abap>