source: pandokia/trunk/commands/shunit2_plugin_pdk

Last change on this file was 972, checked in by sienkiew, 4 years ago

adapt shunit2 plugin to solaris shell

  • Property svn:executable set to *
File size: 5.3 KB
Line 
1# shunit2 plugin for pandokia
2#
3# ** This requires shunit2 2.1.6plugin as modified at stsci.
4# see http://stsdas.stsci.edu/shunit
5#
6
7#-----------------------------------------------------------------------------
8# helper functions to be used by pandokia-aware tests
9#
10
11# log a pandokia attribute.  All attributes logged for a particular test
12# are part of the pandokia test report for that test.
13#
14# arg1 is the name of the attribute, INCLUDING the prefix "tda_" or "tra_"
15# arg2 is the value
16#
17#   pdk_attribute tda_thing 1
18#
19# Attributes are not multi-line values; newlines are deleted if necessary.
20#
21
22pdk_attribute() {
23        # skip the disk I/O if we are not running in a pandokia context
24        if [ "${PDK_LOG-}" != "" ]
25        then
26                case "$1"
27                in
28                t[dr]a_*)
29                        ( echo "$1=$2" | tr -d '\n' ; echo '' ) >> $PDK_LOG
30                        ;;
31                *)
32                        # have to do something for this case; might as well
33                        # assume a TRA
34                        ( echo "tra_$1=$2" | tr -d '\n' ; echo '' ) >> $PDK_LOG
35                        ;;
36                esac
37        fi
38}
39
40pdk_tda() {
41        pdk_attribute tda_"$1" "$2"
42}
43
44pdk_tra() {
45        pdk_attribute tra_"$1" "$2"
46}
47
48# declare that the test result is Error.  shunit2 does not have this
49# natively, so we call it fail for shunit2 and then change the status
50# to E for pandokia.
51pdk_error() {
52        echo "PDK_ERROR"
53        _shunit_assertFail "$*"
54
55        # E is the most severe status, so we don't need to guard against
56        # moving to a lower status.
57        _shunit_last_test_status='E'
58}
59
60
61#-----------------------------------------------------------------------------
62
63shunit_plugin_init() {
64
65        # when we run in the context of pandokia, the environment PDK_FILE
66        # is the name of the file we are running tests from.  Pandokia
67        # will run only a single file in any invocation of shunit, so
68        # we can use PDK_FILE to derive that part of the test name.
69        if [ -z "${PDK_LOG:-}" ]
70        then
71                echo 'pandokia plugin activated but no pandokia context present -- faking it for testing' >&2
72
73                # name of the file we are processing
74                __pdk_file_part=`basename $__shunit_script .sh`
75
76                # imaginary prefix
77                PDK_TESTPREFIX='prefix/'
78
79                # test log file
80                PDK_LOG=PDK_arf
81
82                export PDK_TESTPREFIX PDK_LOG
83        else
84
85                case "$PDK_FILE"
86                in
87                '')
88                        # this only happens when the test file alters
89                        # PDK_FILE; this has the effect of hiding the
90                        # file name from the test name.  Normally, this
91                        # is not the right thing to do in pandokia, but
92                        # there are contexts where it makes sense in shunit2.
93                         __pdk_file_part=''
94                        ;;
95                *.sh)     
96                        __pdk_file_part=`basename $PDK_FILE .sh`
97                        ;;
98                *.shunit)
99                        __pdk_file_part=`basename $PDK_FILE .shunit`
100                        ;;
101                *.shunit2)
102                        __pdk_file_part=`basename $PDK_FILE .shunit2`
103                        ;;
104                *)               
105                        __pdk_file_part=`basename $PDK_FILE`
106                        ;;
107                esac
108        fi
109
110}
111
112
113#-----------------------------------------------------------------------------
114# Pandokia collects a lot of information about the test:
115#       - stdout/stderr
116#       - start/end time
117#       - pandokia attributes (through pdk_attribute above)
118#
119
120shunit_plugin_execute() {
121
122        _shunit_test_="$1"
123
124        # this output file is safe to use because __shunit_tmpDir is a
125        # unique temporary directory created for this process.
126        _pdk_output=${__shunit_tmpDir}/pdk_test_ouput
127
128        # the test name as pandokia knows it.  Recall that the prefix
129        # alread ends with a slash.
130        test_name=${PDK_TESTPREFIX}${__pdk_file_part}.${_shunit_test_}
131
132        # collect start/end time outside the { } because some shells
133        # don't remember variables that are set within
134        start_time=` date '+%Y-%m-%d %H:%M:%S' `
135
136        # run the test, collecting output into the file
137        {
138
139        # execute the per-test setup function
140        setUp
141
142        # execute the test
143        eval ${_shunit_test_}
144
145        # execute the per-test tear-down function
146        tearDown
147
148        } > $_pdk_output 2>&1
149
150        end_time=` date '+%Y-%m-%d %H:%M:%S' `
151
152        # write the test results to the pdk log file
153        {
154        echo 'test_name='${test_name}
155        echo 'test_runner=shunit2'
156
157        # shunit2 status and pandokia status are _almost_ identical.
158        # Fix the minor differences.
159        case "$_shunit_last_test_status"
160        in
161        U)      # unspecified means there were no assertions performed;
162                # we assume that means Pass
163                echo 'status=P'
164                ;;
165        [PFE])  # these statuses are the same in pandokia and shunit
166                echo 'status='$_shunit_last_test_status
167                ;;
168        S)      # in pandokia, tests are "Disabled" not "Skipped"
169                echo 'status=D'
170                ;;
171        esac
172
173        echo 'start_time='$start_time
174        echo 'end_time='$end_time
175        if [ -s ${__shunit_tmpDir}/pdk_test_ouput ]
176        then
177                echo 'log:'
178                sed 's/^/./' < ${__shunit_tmpDir}/pdk_test_ouput
179                echo ''
180                echo ''
181        fi
182        echo 'END'
183        } >> $PDK_LOG
184
185        rm -f  ${__shunit_tmpDir}/pdk_test_ouput
186
187        # You don't normally do progress tracking in pandokia.  Instead,
188        # you read the information out of the test report.
189        # echo ${test_name}: $_shunit_last_test_status
190
191        # this is important:  tell shunit that we generated a report
192        # for this test, otherwise it generates another report of its own.
193        __shunit_reportGenerated=${SHUNIT_TRUE}
194
195}
196
197#-----------------------------------------------------------------------------
198
199shunit_plugin_final_report() {
200        # The pandokia plugin writes the report as a record for each test.
201        # There is nothing left to do here.
202        :
203}
204
205#-----------------------------------------------------------------------------
206
207shunit_plugin_finish() {
208        # the pandokia plugin does not require finalization
209        :
210}
211
Note: See TracBrowser for help on using the repository browser.