From 60f01499e55b99a39e27ec6bda028ccd308c6d84 Mon Sep 17 00:00:00 2001 From: Fabian Lupa Date: Wed, 22 Nov 2023 07:33:51 +0100 Subject: [PATCH] Prevent calls to exit implementations in tests (#6653) Co-authored-by: Marc Bernard <59966492+mbtools@users.noreply.github.com> --- src/exits/zcl_abapgit_exit.clas.abap | 36 ++++++++++++++++--- .../zcl_abapgit_exit.clas.testclasses.abap | 20 +++++++++++ src/exits/zcl_abapgit_exit.clas.xml | 1 + 3 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 src/exits/zcl_abapgit_exit.clas.testclasses.abap diff --git a/src/exits/zcl_abapgit_exit.clas.abap b/src/exits/zcl_abapgit_exit.clas.abap index f2a03cfb0..8e9bbb670 100644 --- a/src/exits/zcl_abapgit_exit.clas.abap +++ b/src/exits/zcl_abapgit_exit.clas.abap @@ -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. diff --git a/src/exits/zcl_abapgit_exit.clas.testclasses.abap b/src/exits/zcl_abapgit_exit.clas.testclasses.abap new file mode 100644 index 000000000..85f4f8cf0 --- /dev/null +++ b/src/exits/zcl_abapgit_exit.clas.testclasses.abap @@ -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. diff --git a/src/exits/zcl_abapgit_exit.clas.xml b/src/exits/zcl_abapgit_exit.clas.xml index cff9e5279..661835b47 100644 --- a/src/exits/zcl_abapgit_exit.clas.xml +++ b/src/exits/zcl_abapgit_exit.clas.xml @@ -10,6 +10,7 @@ X X X + X