1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ # --- BEGIN_HEADER ---
4
+ #
5
+ # test_support - unit test of the corresponding tests module
6
+ # Copyright (C) 2003-2024 The MiG Project by the Science HPC Center at UCPH
7
+ #
8
+ # This file is part of MiG.
9
+ #
10
+ # MiG is free software: you can redistribute it and/or modify
11
+ # it under the terms of the GNU General Public License as published by
12
+ # the Free Software Foundation; either version 2 of the License, or
13
+ # (at your option) any later version.
14
+ #
15
+ # MiG is distributed in the hope that it will be useful,
16
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
+ # GNU General Public License for more details.
19
+ #
20
+ # You should have received a copy of the GNU General Public License
21
+ # along with this program; if not, write to the Free Software
22
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
23
+ # USA.
24
+ #
25
+ # --- END_HEADER ---
26
+ #
27
+
28
+ """Unit tests for the tests module pointed to in the filename"""
29
+
1
30
from __future__ import print_function
2
31
import os
3
32
import sys
4
33
import unittest
5
34
6
35
from tests .support import MigTestCase , PY2 , testmain , temppath , \
7
- AssertOver
36
+ AssertOver , FakeConfiguration
37
+
38
+ from mig .shared .conf import get_configuration_object
39
+ from mig .shared .configuration import Configuration
8
40
9
41
10
42
class InstrumentedAssertOver (AssertOver ):
43
+ """Helper to keep track of AssertOver runs"""
44
+
11
45
def __init__ (self , * args , ** kwargs ):
12
46
AssertOver .__init__ (self , * args , ** kwargs )
13
47
self ._check_callable = None
@@ -24,6 +58,7 @@ def was_check_callable_called(self):
24
58
25
59
def to_check_callable (self ):
26
60
_check_callable = AssertOver .to_check_callable (self )
61
+
27
62
def _wrapped_check_callable ():
28
63
self ._check_callable_called = True
29
64
_check_callable ()
@@ -32,13 +67,26 @@ def _wrapped_check_callable():
32
67
33
68
34
69
class SupportTestCase (MigTestCase ):
70
+ """Coverage of base Support helpers"""
71
+
35
72
def _class_attribute (self , name , ** kwargs ):
36
73
cls = type (self )
37
74
if 'value' in kwargs :
38
75
setattr (cls , name , kwargs ['value' ])
39
76
else :
40
77
return getattr (cls , name , None )
41
78
79
+ def test_provides_a_fake_configuration (self ):
80
+ configuration = self .configuration
81
+
82
+ self .assertIsInstance (configuration , FakeConfiguration )
83
+
84
+ def test_provides_a_fake_configuration_for_the_duration_of_the_test (self ):
85
+ c1 = self .configuration
86
+ c2 = self .configuration
87
+
88
+ self .assertIs (c2 , c1 )
89
+
42
90
@unittest .skipIf (PY2 , "Python 3 only" )
43
91
def test_unclosed_files_are_recorded (self ):
44
92
tmp_path = temppath ("support-unclosed" , self )
@@ -67,18 +115,21 @@ def test_when_asserting_over_multiple_values(self):
67
115
def assert_is_int (value ):
68
116
assert isinstance (value , int )
69
117
70
- attempt_wrapper = self .assert_over (values = (1 , 2 , 3 ), _AssertOver = InstrumentedAssertOver )
118
+ attempt_wrapper = self .assert_over (
119
+ values = (1 , 2 , 3 ), _AssertOver = InstrumentedAssertOver )
71
120
72
121
# record the wrapper on the test case so the subsequent test can assert against it
73
- self ._class_attribute ('surviving_attempt_wrapper' , value = attempt_wrapper )
122
+ self ._class_attribute ('surviving_attempt_wrapper' ,
123
+ value = attempt_wrapper )
74
124
75
125
with attempt_wrapper as attempt :
76
126
attempt (assert_is_int )
77
127
attempt_wrapper .assert_success ()
78
128
79
129
self .assertTrue (attempt_wrapper .has_check_callable ())
80
130
# cleanup was recorded
81
- self .assertIn (attempt_wrapper .get_check_callable (), self ._cleanup_checks )
131
+ self .assertIn (attempt_wrapper .get_check_callable (),
132
+ self ._cleanup_checks )
82
133
83
134
def test_when_asserting_over_multiple_values_after (self ):
84
135
# test name is purposefully after ..._recorded in sort order
@@ -88,5 +139,25 @@ def test_when_asserting_over_multiple_values_after(self):
88
139
self .assertTrue (attempt_wrapper .was_check_callable_called ())
89
140
90
141
142
+ class SupportTestCase_overridden_configuration (MigTestCase ):
143
+ """Coverage of base Support helpers extension with configuration override"""
144
+
145
+ def _provide_configuration (self ):
146
+ return 'testconfig'
147
+
148
+ def test_provides_the_test_configuration (self ):
149
+ expected_last_dir = 'testconfs-py2' if PY2 else 'testconfs-py3'
150
+
151
+ configuration = self .configuration
152
+
153
+ # check we have a real config object
154
+ self .assertIsInstance (configuration , Configuration )
155
+ # check for having loaded a config file from a test config dir
156
+ config_file_path_parts = configuration .config_file .split (os .path .sep )
157
+ config_file_path_parts .pop () # discard file part
158
+ config_file_last_dir = config_file_path_parts .pop ()
159
+ self .assertTrue (config_file_last_dir , expected_last_dir )
160
+
161
+
91
162
if __name__ == '__main__' :
92
163
testmain ()
0 commit comments