2
2
Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
3
"""
4
4
5
+ import importlib
5
6
import json
6
7
import os
7
8
import re
8
9
import tempfile
9
10
import traceback
10
11
import unittest
11
- from imp import C_BUILTIN
12
12
from io import StringIO
13
13
from tempfile import NamedTemporaryFile
14
- from unittest .mock import patch , Mock , MagicMock
14
+ from unittest .mock import MagicMock , Mock , patch
15
15
16
16
import awslambdaric .bootstrap as bootstrap
17
17
from awslambdaric .lambda_runtime_exception import FaultException
@@ -350,7 +350,7 @@ def __init__(self, message):
350
350
351
351
def test_handle_event_request_no_module (self ):
352
352
def unable_to_import_module (json_input , lambda_context ):
353
- import invalid_module
353
+ import invalid_module # noqa: F401
354
354
355
355
expected_response = {
356
356
"errorType" : "ModuleNotFoundError" ,
@@ -381,8 +381,8 @@ def unable_to_import_module(json_input, lambda_context):
381
381
def test_handle_event_request_fault_exception (self ):
382
382
def raise_exception_handler (json_input , lambda_context ):
383
383
try :
384
- import invalid_module
385
- except ImportError as e :
384
+ import invalid_module # noqa: F401
385
+ except ImportError :
386
386
raise FaultException (
387
387
"FaultExceptionType" ,
388
388
"Fault exception msg" ,
@@ -429,8 +429,8 @@ def raise_exception_handler(json_input, lambda_context):
429
429
def test_handle_event_request_fault_exception_logging (self , mock_stdout ):
430
430
def raise_exception_handler (json_input , lambda_context ):
431
431
try :
432
- import invalid_module
433
- except ImportError as e :
432
+ import invalid_module # noqa: F401
433
+ except ImportError :
434
434
raise bootstrap .FaultException (
435
435
"FaultExceptionType" ,
436
436
"Fault exception msg" ,
@@ -469,8 +469,8 @@ def raise_exception_handler(json_input, lambda_context):
469
469
def test_handle_event_request_fault_exception_logging_notrace (self , mock_stdout ):
470
470
def raise_exception_handler (json_input , lambda_context ):
471
471
try :
472
- import invalid_module
473
- except ImportError as e :
472
+ import invalid_module # noqa: F401
473
+ except ImportError :
474
474
raise bootstrap .FaultException (
475
475
"FaultExceptionType" , "Fault exception msg" , None
476
476
)
@@ -497,8 +497,8 @@ def test_handle_event_request_fault_exception_logging_nomessage_notrace(
497
497
):
498
498
def raise_exception_handler (json_input , lambda_context ):
499
499
try :
500
- import invalid_module
501
- except ImportError as e :
500
+ import invalid_module # noqa: F401
501
+ except ImportError :
502
502
raise bootstrap .FaultException ("FaultExceptionType" , None , None )
503
503
504
504
bootstrap .handle_event_request (
@@ -523,8 +523,8 @@ def test_handle_event_request_fault_exception_logging_notype_notrace(
523
523
):
524
524
def raise_exception_handler (json_input , lambda_context ):
525
525
try :
526
- import invalid_module
527
- except ImportError as e :
526
+ import invalid_module # noqa: F401
527
+ except ImportError :
528
528
raise bootstrap .FaultException (None , "Fault exception msg" , None )
529
529
530
530
bootstrap .handle_event_request (
@@ -549,8 +549,8 @@ def test_handle_event_request_fault_exception_logging_notype_nomessage(
549
549
):
550
550
def raise_exception_handler (json_input , lambda_context ):
551
551
try :
552
- import invalid_module
553
- except ImportError as e :
552
+ import invalid_module # noqa: F401
553
+ except ImportError :
554
554
raise bootstrap .FaultException (
555
555
None ,
556
556
None ,
@@ -585,19 +585,16 @@ def raise_exception_handler(json_input, lambda_context):
585
585
self .assertEqual (mock_stdout .getvalue (), error_logs )
586
586
587
587
@patch ("sys.stdout" , new_callable = StringIO )
588
- @patch ("imp.find_module" )
589
- @patch ("imp.load_module" )
588
+ @patch ("importlib.import_module" )
590
589
def test_handle_event_request_fault_exception_logging_syntax_error (
591
- self , mock_load_module , mock_find_module , mock_stdout
590
+ self , mock_import_module , mock_stdout
592
591
):
593
-
594
592
try :
595
593
eval ("-" )
596
594
except SyntaxError as e :
597
595
syntax_error = e
598
596
599
- mock_find_module .return_value = (None , None , ("" , "" , None ))
600
- mock_load_module .side_effect = syntax_error
597
+ mock_import_module .side_effect = syntax_error
601
598
602
599
response_handler = bootstrap ._get_handler ("a.b" )
603
600
@@ -618,7 +615,10 @@ def test_handle_event_request_fault_exception_logging_syntax_error(
618
615
619
616
sys .stderr .write (mock_stdout .getvalue ())
620
617
621
- error_logs = "[ERROR] Runtime.UserCodeSyntaxError: Syntax error in module 'a': unexpected EOF while parsing (<string>, line 1)\r "
618
+ error_logs = (
619
+ "[ERROR] Runtime.UserCodeSyntaxError: Syntax error in module 'a': "
620
+ "unexpected EOF while parsing (<string>, line 1)\r "
621
+ )
622
622
error_logs += "Traceback (most recent call last):\r "
623
623
error_logs += ' File "<string>" Line 1\r '
624
624
error_logs += " -\n "
@@ -730,56 +730,57 @@ def test_get_event_handler_import_error(self):
730
730
)
731
731
732
732
def test_get_event_handler_syntax_error (self ):
733
- tmp_file = tempfile .NamedTemporaryFile (suffix = ".py" , dir = "." , delete = False )
734
- tmp_file .write (
735
- b"def syntax_error()\n \t print('syntax error, no colon after function')"
736
- )
737
- tmp_file .close ()
738
- filename_w_ext = os .path .basename (tmp_file .name )
739
- filename , _ = os .path .splitext (filename_w_ext )
740
- handler_name = "{}.syntax_error" .format (filename )
741
- response_handler = bootstrap ._get_handler (handler_name )
742
-
743
- with self .assertRaises (FaultException ) as cm :
744
- response_handler ()
745
- returned_exception = cm .exception
746
- self .assertEqual (
747
- self .FaultExceptionMatcher (
748
- "Syntax error in" ,
749
- "Runtime.UserCodeSyntaxError" ,
750
- ".*File.*\\ .py.*Line 1.*" ,
751
- ),
752
- returned_exception ,
753
- )
754
- if os .path .exists (tmp_file .name ):
755
- os .remove (tmp_file .name )
733
+ importlib .invalidate_caches ()
734
+ with tempfile .NamedTemporaryFile (
735
+ suffix = ".py" , dir = "." , delete = False
736
+ ) as tmp_file :
737
+ tmp_file .write (
738
+ b"def syntax_error()\n \t print('syntax error, no colon after function')"
739
+ )
740
+ tmp_file .flush ()
741
+
742
+ filename_w_ext = os .path .basename (tmp_file .name )
743
+ filename , _ = os .path .splitext (filename_w_ext )
744
+ handler_name = "{}.syntax_error" .format (filename )
745
+ response_handler = bootstrap ._get_handler (handler_name )
746
+
747
+ with self .assertRaises (FaultException ) as cm :
748
+ response_handler ()
749
+ returned_exception = cm .exception
750
+ self .assertEqual (
751
+ self .FaultExceptionMatcher (
752
+ "Syntax error in" ,
753
+ "Runtime.UserCodeSyntaxError" ,
754
+ ".*File.*\\ .py.*Line 1.*" ,
755
+ ),
756
+ returned_exception ,
757
+ )
756
758
757
759
def test_get_event_handler_missing_error (self ):
758
- tmp_file = tempfile .NamedTemporaryFile (suffix = ".py" , dir = "." , delete = False )
759
- tmp_file .write (b"def wrong_handler_name():\n \t print('hello')" )
760
- tmp_file .close ()
761
- filename_w_ext = os .path .basename (tmp_file .name )
762
- filename , _ = os .path .splitext (filename_w_ext )
763
- handler_name = "{}.my_handler" .format (filename )
764
- response_handler = bootstrap ._get_handler (handler_name )
765
- with self .assertRaises (FaultException ) as cm :
766
- response_handler ()
767
- returned_exception = cm .exception
768
- self .assertEqual (
769
- self .FaultExceptionMatcher (
770
- "Handler 'my_handler' missing on module '{}'" .format (filename ),
771
- "Runtime.HandlerNotFound" ,
772
- ),
773
- returned_exception ,
774
- )
775
- if os .path .exists (tmp_file .name ):
776
- os .remove (tmp_file .name )
760
+ importlib .invalidate_caches ()
761
+ with tempfile .NamedTemporaryFile (
762
+ suffix = ".py" , dir = "." , delete = False
763
+ ) as tmp_file :
764
+ tmp_file .write (b"def wrong_handler_name():\n \t print('hello')" )
765
+ tmp_file .flush ()
766
+
767
+ filename_w_ext = os .path .basename (tmp_file .name )
768
+ filename , _ = os .path .splitext (filename_w_ext )
769
+ handler_name = "{}.my_handler" .format (filename )
770
+ response_handler = bootstrap ._get_handler (handler_name )
771
+ with self .assertRaises (FaultException ) as cm :
772
+ response_handler ()
773
+ returned_exception = cm .exception
774
+ self .assertEqual (
775
+ self .FaultExceptionMatcher (
776
+ "Handler 'my_handler' missing on module '{}'" .format (filename ),
777
+ "Runtime.HandlerNotFound" ,
778
+ ),
779
+ returned_exception ,
780
+ )
777
781
778
- @patch ("imp.find_module" )
779
- def test_get_event_handler_build_in_conflict (self , mock_find_module ):
780
- handler_name = "sys.hello"
781
- mock_find_module .return_value = (None , None , ("" , "" , C_BUILTIN ))
782
- response_handler = bootstrap ._get_handler (handler_name )
782
+ def test_get_event_handler_build_in_conflict (self ):
783
+ response_handler = bootstrap ._get_handler ("sys.hello" )
783
784
with self .assertRaises (FaultException ) as cm :
784
785
response_handler ()
785
786
returned_exception = cm .exception
@@ -921,7 +922,10 @@ def test_log_error_indentation_standard_log_sink(self, mock_stdout):
921
922
)
922
923
bootstrap .log_error (err_to_log , bootstrap .StandardLogSink ())
923
924
924
- expected_logged_error = "[ERROR] ErrorType: Error message\r Traceback (most recent call last):\r \xa0 \xa0 line1 \r \xa0 \xa0 line2 \r \xa0 \xa0 \n "
925
+ expected_logged_error = (
926
+ "[ERROR] ErrorType: Error message\r Traceback (most recent call last):"
927
+ "\r \xa0 \xa0 line1 \r \xa0 \xa0 line2 \r \xa0 \xa0 \n "
928
+ )
925
929
self .assertEqual (mock_stdout .getvalue (), expected_logged_error )
926
930
927
931
def test_log_error_indentation_framed_log_sink (self ):
@@ -932,7 +936,10 @@ def test_log_error_indentation_framed_log_sink(self):
932
936
)
933
937
bootstrap .log_error (err_to_log , log_sink )
934
938
935
- expected_logged_error = "[ERROR] ErrorType: Error message\n Traceback (most recent call last):\n \xa0 \xa0 line1 \n \xa0 \xa0 line2 \n \xa0 \xa0 "
939
+ expected_logged_error = (
940
+ "[ERROR] ErrorType: Error message\n Traceback (most recent call last):"
941
+ "\n \xa0 \xa0 line1 \n \xa0 \xa0 line2 \n \xa0 \xa0 "
942
+ )
936
943
937
944
with open (temp_file .name , "rb" ) as f :
938
945
content = f .read ()
@@ -964,7 +971,10 @@ def test_log_error_empty_stacktrace_line_framed_log_sink(self):
964
971
)
965
972
bootstrap .log_error (err_to_log , log_sink )
966
973
967
- expected_logged_error = "[ERROR] ErrorType: Error message\n Traceback (most recent call last):\n line1\n \n line2"
974
+ expected_logged_error = (
975
+ "[ERROR] ErrorType: Error message\n Traceback "
976
+ "(most recent call last):\n line1\n \n line2"
977
+ )
968
978
969
979
with open (temp_file .name , "rb" ) as f :
970
980
content = f .read ()
@@ -1082,11 +1092,10 @@ def test_run(self, mock_runtime_client, mock_handle_event_request):
1082
1092
MagicMock (),
1083
1093
]
1084
1094
1085
- with self .assertRaises (TypeError ) as cm :
1095
+ with self .assertRaises (TypeError ):
1086
1096
bootstrap .run (
1087
1097
expected_app_root , expected_handler , expected_lambda_runtime_api_addr
1088
1098
)
1089
- returned_exception = cm .exception
1090
1099
1091
1100
mock_handle_event_request .assert_called_once ()
1092
1101
@@ -1108,11 +1117,10 @@ class TestException(Exception):
1108
1117
1109
1118
mock_sys .exit .side_effect = TestException ("Boom!" )
1110
1119
1111
- with self .assertRaises (TestException ) as cm :
1120
+ with self .assertRaises (TestException ):
1112
1121
bootstrap .run (
1113
1122
expected_app_root , expected_handler , expected_lambda_runtime_api_addr
1114
1123
)
1115
- returned_exception = cm .exception
1116
1124
1117
1125
mock_sys .exit .assert_called_once_with (1 )
1118
1126
0 commit comments