3434import re
3535import sys
3636from test .framework .utilities import EnhancedTestCase , TestLoaderFiltered , init_config
37+ from time import gmtime
3738from unittest import TextTestRunner
3839
40+ import easybuild .tools .testing
3941from easybuild .base .rest import RestClient
4042from easybuild .framework .easyconfig .tools import categorize_files_by_type
4143from easybuild .tools .build_log import EasyBuildError
4547from easybuild .tools .github import GITHUB_EASYCONFIGS_REPO , GITHUB_EASYBLOCKS_REPO , GITHUB_MERGEABLE_STATE_CLEAN
4648from easybuild .tools .github import VALID_CLOSE_PR_REASONS
4749from easybuild .tools .github import pick_default_branch
48- from easybuild .tools .testing import post_pr_test_report , session_state
50+ from easybuild .tools .testing import create_test_report , post_pr_test_report , session_state
4951from easybuild .tools .py2vs3 import HTTPError , URLError , ascii_letters
5052import easybuild .tools .github as gh
5153
@@ -71,7 +73,7 @@ class GithubTest(EnhancedTestCase):
7173 for non authenticated users of 50"""
7274
7375 def setUp (self ):
74- """setup"""
76+ """Test setup. """
7577 super (GithubTest , self ).setUp ()
7678
7779 self .github_token = gh .fetch_github_token (GITHUB_TEST_ACCOUNT )
@@ -85,6 +87,14 @@ def setUp(self):
8587
8688 self .skip_github_tests = self .github_token is None and os .getenv ('FORCE_EB_GITHUB_TESTS' ) is None
8789
90+ self .orig_testing_create_gist = easybuild .tools .testing .create_gist
91+
92+ def tearDown (self ):
93+ """Cleanup after running test."""
94+ easybuild .tools .testing .create_gist = self .orig_testing_create_gist
95+
96+ super (GithubTest , self ).tearDown ()
97+
8898 def test_pick_default_branch (self ):
8999 """Test pick_default_branch function."""
90100
@@ -1041,6 +1051,59 @@ def test_pr_test_report(self):
10411051 regex = re .compile (pattern , re .M )
10421052 self .assertTrue (regex .search (stdout ), "Pattern '%s' should be found in: %s" % (regex .pattern , stdout ))
10431053
1054+ def test_create_test_report (self ):
1055+ """Test create_test_report function."""
1056+ logfile = os .path .join (self .test_prefix , 'log.txt' )
1057+ write_file (logfile , "Bazel failed with: error" )
1058+ ecs_with_res = [
1059+ ({'spec' : 'test.eb' }, {'success' : True }),
1060+ ({'spec' : 'fail.eb' }, {
1061+ 'success' : False ,
1062+ 'err' : EasyBuildError ("error: bazel" ),
1063+ 'traceback' : "in bazel" ,
1064+ 'log_file' : logfile ,
1065+ }),
1066+ ]
1067+ init_session_state = {
1068+ 'easybuild_configuration' : ['EASYBUILD_DEBUG=1' ],
1069+ 'environment' : {'USER' : 'test' },
1070+ 'module_list' : [{'mod_name' : 'test' }],
1071+ 'system_info' : {'name' : 'test' },
1072+ 'time' : gmtime (0 ),
1073+ }
1074+ res = create_test_report ("just a test" , ecs_with_res , init_session_state )
1075+ patterns = [
1076+ "**SUCCESS** _test.eb_" ,
1077+ "**FAIL (build issue)** _fail.eb_" ,
1078+ "01 Jan 1970 00:00:00" ,
1079+ "EASYBUILD_DEBUG=1" ,
1080+ ]
1081+ for pattern in patterns :
1082+ self .assertTrue (pattern in res ['full' ], "Pattern '%s' found in: %s" % (pattern , res ['full' ]))
1083+
1084+ for pattern in patterns [:2 ]:
1085+ self .assertTrue (pattern in res ['full' ], "Pattern '%s' found in: %s" % (pattern , res ['overview' ]))
1086+
1087+ # mock create_gist function, we don't want to actually create a gist every time we run this test...
1088+ def fake_create_gist (* args , ** kwargs ):
1089+ return 'https://gist.github.com/test'
1090+
1091+ easybuild .tools .testing .create_gist = fake_create_gist
1092+
1093+ res = create_test_report ("just a test" , ecs_with_res , init_session_state , pr_nrs = [123 ], gist_log = True )
1094+
1095+ patterns .insert (2 , "https://gist.github.com/test" )
1096+ patterns .extend ([
1097+ "https://github.com/easybuilders/easybuild-easyconfigs/pull/123" ,
1098+ ])
1099+ for pattern in patterns :
1100+ self .assertTrue (pattern in res ['full' ], "Pattern '%s' found in: %s" % (pattern , res ['full' ]))
1101+
1102+ for pattern in patterns [:3 ]:
1103+ self .assertTrue (pattern in res ['full' ], "Pattern '%s' found in: %s" % (pattern , res ['overview' ]))
1104+
1105+ self .assertTrue ("**SUCCESS** _test.eb_" in res ['overview' ])
1106+
10441107
10451108def suite ():
10461109 """ returns all the testcases in this module """
0 commit comments