Skip to content

Commit f278d9e

Browse files
tewaldseagleflo
authored andcommitted
Fix the unit tests.
- Mock stdout. - Close the replay file properly. - Make the unit test runnable. - Add the extra dependencies to setup.py.
1 parent ec778a4 commit f278d9e

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

mpyq.py

+8
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,14 @@ def __init__(self, filename, listfile=True):
105105
else:
106106
self.files = None
107107

108+
def close(self):
109+
if self.file:
110+
self.file.close()
111+
self.file = None
112+
113+
def __del__(self):
114+
self.close()
115+
108116
def read_header(self):
109117
"""Read the header of a MPQ archive."""
110118

setup.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,9 @@
3030
'Topic :: Software Development :: Libraries',
3131
'Topic :: System :: Archiving',
3232
],
33-
install_requires=['argparse'] if float(sys.version[:3]) < 2.7 else [],
33+
install_requires=[
34+
'argparse; python_version < "2.7"',
35+
'mock; python_version < "3.3"',
36+
'six',
37+
]
3438
)

test/test_mpqarchive.py

+21-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@
22
# coding: utf-8
33

44
import os
5-
import sys
65
import unittest
6+
7+
try:
8+
from unittest import mock # Python 3+
9+
except ImportError:
10+
import mock # Python 2.7
11+
712
from mpyq import MPQArchive
13+
import six
814

915
TEST_DIR = os.path.realpath(os.path.dirname(__file__)) + '/'
1016

@@ -13,6 +19,10 @@ class TestMPQArchive(unittest.TestCase):
1319
def setUp(self):
1420
self.archive = MPQArchive(TEST_DIR + 'test.SC2Replay')
1521

22+
def tearDown(self):
23+
self.archive.close()
24+
self.archive = None
25+
1626
def test_init_with_file(self):
1727
self.archive = MPQArchive(open(TEST_DIR + 'test.SC2Replay', 'rb'))
1828

@@ -41,9 +51,10 @@ def test_files(self):
4151
b'replay.smartcam.events',
4252
b'replay.sync.events'])
4353

44-
def test_print_hash_table(self):
54+
@mock.patch('sys.stdout', new_callable=six.StringIO)
55+
def test_print_hash_table(self, mock_stdout):
4556
self.archive.print_hash_table()
46-
self.assertEqual(sys.stdout.getvalue(),
57+
self.assertEqual(mock_stdout.getvalue(),
4758
"MPQ archive hash table\n"
4859
"----------------------\n"
4960
" Hash A Hash B Locl Plat BlockIdx\n"
@@ -65,9 +76,10 @@ def test_print_hash_table(self):
6576
"31952289 6A5FFAA3 0000 0000 00000003\n"
6677
"\n")
6778

68-
def test_print_block_table(self):
79+
@mock.patch('sys.stdout', new_callable=six.StringIO)
80+
def test_print_block_table(self, mock_stdout):
6981
self.archive.print_block_table()
70-
self.assertEqual(sys.stdout.getvalue(),
82+
self.assertEqual(mock_stdout.getvalue(),
7183
"MPQ archive block table\n"
7284
"-----------------------\n"
7385
" Offset ArchSize RealSize Flags\n"
@@ -82,3 +94,7 @@ def test_print_block_table(self):
8294
"00031DDE 120 164 81000200\n"
8395
"00031E56 254 288 81000200\n"
8496
"\n")
97+
98+
99+
if __name__ == '__main__':
100+
unittest.main()

0 commit comments

Comments
 (0)