|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# --- BEGIN_HEADER --- |
| 4 | +# |
| 5 | +# test_mig_shared_safeeval - unit test of the corresponding mig shared module |
| 6 | +# Copyright (C) 2003-2025 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 test safeeval functions""" |
| 29 | + |
| 30 | +import os |
| 31 | +import sys |
| 32 | + |
| 33 | +from tests.support import MigTestCase, testmain |
| 34 | + |
| 35 | +from mig.shared.safeeval import * |
| 36 | + |
| 37 | + |
| 38 | +PWD_STR = os.getcwd() |
| 39 | +PWD_BYTES = PWD_STR.encode('utf8') |
| 40 | + |
| 41 | + |
| 42 | +class MigSharedSafeeval(MigTestCase): |
| 43 | + """Wrap unit tests for the corresponding module""" |
| 44 | + |
| 45 | + def test_subprocess_call(self): |
| 46 | + """Check that pwd call without args succeeds""" |
| 47 | + retval = subprocess_call(['pwd'], stdout=subprocess_pipe) |
| 48 | + self.assertEqual(retval, 0, "unexpected subprocess call pwd retval") |
| 49 | + |
| 50 | + def test_subprocess_call_invalid(self): |
| 51 | + """Check that pwd call with invalid arg fails""" |
| 52 | + retval = subprocess_call(['pwd', '-h'], stderr=subprocess_pipe) |
| 53 | + self.assertNotEqual(retval, 0, |
| 54 | + "unexpected subprocess call nosuchcommand retval") |
| 55 | + |
| 56 | + def test_subprocess_check_output(self): |
| 57 | + """Check that pwd command output matches getcwd as bytes""" |
| 58 | + data = subprocess_check_output(['pwd'], stdout=subprocess_pipe, |
| 59 | + stderr=subprocess_pipe).strip() |
| 60 | + self.assertEqual(data, PWD_BYTES, |
| 61 | + "mismatch in subprocess check pwd output") |
| 62 | + |
| 63 | + def test_subprocess_check_output_text(self): |
| 64 | + """Check that pwd command output matches getcwd as string""" |
| 65 | + data = subprocess_check_output(['pwd'], stdout=subprocess_pipe, |
| 66 | + stderr=subprocess_pipe, |
| 67 | + text=True).strip() |
| 68 | + self.assertEqual(data, PWD_STR, |
| 69 | + "mismatch in subprocess check pwd output") |
| 70 | + |
| 71 | + def test_subprocess_popen(self): |
| 72 | + """Check that pwd popen output matches getcwd as bytes""" |
| 73 | + proc = subprocess_popen(['pwd'], stdout=subprocess_pipe, |
| 74 | + stderr=subprocess_stdout) |
| 75 | + retval = proc.wait() |
| 76 | + data = proc.stdout.read().strip() |
| 77 | + self.assertEqual(data, PWD_BYTES, |
| 78 | + "mismatch in subprocess popen pwd output") |
| 79 | + |
| 80 | + def test_subprocess_popen_text(self): |
| 81 | + """Check that pwd popen output matches getcwd as string""" |
| 82 | + orig = os.getcwd() |
| 83 | + proc = subprocess_popen(['pwd'], stdout=subprocess_pipe, |
| 84 | + stderr=subprocess_stdout, text=True) |
| 85 | + retval = proc.wait() |
| 86 | + data = proc.stdout.read().strip() |
| 87 | + self.assertEqual(data, PWD_STR, |
| 88 | + "mismatch in subprocess popen pwd output") |
| 89 | + |
| 90 | + |
| 91 | +if __name__ == '__main__': |
| 92 | + testmain() |
0 commit comments