import subprocess
import tempfile
import os
-import StringIO
+import io
sys.path.append("..")
from gen_esp32part import *
binary_table = binary_table[0:len(binary_table)-32]
return binary_table
+class Py23TestCase(unittest.TestCase):
-class CSVParserTests(unittest.TestCase):
+ def __init__(self, *args, **kwargs):
+ super(Py23TestCase, self).__init__(*args, **kwargs)
+ try:
+ self.assertRaisesRegex
+ except AttributeError:
+ # assertRaisesRegexp is deprecated in Python3 but assertRaisesRegex doesn't exist in Python2
+ # This fix is used in order to avoid using the alias from the six library
+ self.assertRaisesRegex = self.assertRaisesRegexp
+
+class CSVParserTests(Py23TestCase):
def test_simple_partition(self):
table = PartitionTable.from_csv(SIMPLE_CSV)
# Name,Type, SubType,Offset,Size
ihavenotype,
"""
- with self.assertRaisesRegexp(InputError, "type"):
+ with self.assertRaisesRegex(InputError, "type"):
PartitionTable.from_csv(csv)
first, app, factory, 0x100000, 2M
second, app, ota_0, 0x200000, 1M
"""
- with self.assertRaisesRegexp(InputError, "overlap"):
+ with self.assertRaisesRegex(InputError, "overlap"):
t = PartitionTable.from_csv(csv)
t.verify()
first, app, factory, 0x100000, 1M
first, app, ota_0, 0x200000, 1M
"""
- with self.assertRaisesRegexp(InputError, "Partition names must be unique"):
+ with self.assertRaisesRegex(InputError, "Partition names must be unique"):
t = PartitionTable.from_csv(csv)
t.verify()
-class BinaryOutputTests(unittest.TestCase):
+class BinaryOutputTests(Py23TestCase):
def test_binary_entry(self):
csv = """
first, 0x30, 0xEE, 0x100400, 0x300000
self.assertTrue(tr[0].encrypted)
-class BinaryParserTests(unittest.TestCase):
+class BinaryParserTests(Py23TestCase):
def test_parse_one_entry(self):
# type 0x30, subtype 0xee,
# offset 1MB, size 2MB
b"\x00\x00\x20\x00" + \
b"0123456789abc\0\0\0" + \
b"\x00\x00\x00\x00"
- with self.assertRaisesRegexp(InputError, "Invalid magic bytes"):
+ with self.assertRaisesRegex(InputError, "Invalid magic bytes"):
PartitionTable.from_binary(bad_magic)
def test_bad_length(self):
b"\x00\x00\x10\x00" + \
b"\x00\x00\x20\x00" + \
b"0123456789"
- with self.assertRaisesRegexp(InputError, "32 bytes"):
+ with self.assertRaisesRegex(InputError, "32 bytes"):
PartitionTable.from_binary(bad_length)
-class CSVOutputTests(unittest.TestCase):
+class CSVOutputTests(Py23TestCase):
def _readcsv(self, source_str):
return list(csv.reader(source_str.split("\n")))
roundtrip = PartitionTable.from_csv(as_csv)
self.assertEqual(roundtrip, table)
-class CommandLineTests(unittest.TestCase):
+class CommandLineTests(Py23TestCase):
def test_basic_cmdline(self):
try:
output = subprocess.check_output([sys.executable, "../gen_esp32part.py",
binpath, csvpath], stderr=subprocess.STDOUT)
# reopen the CSV and check the generated binary is identical
- self.assertNotIn("WARNING", output)
+ self.assertNotIn(b"WARNING", output)
with open(csvpath, 'r') as f:
from_csv = PartitionTable.from_csv(f.read())
self.assertEqual(_strip_trailing_ffs(from_csv.to_binary()), LONGER_BINARY_TABLE)
# run gen_esp32part.py to conver the CSV to binary again
output = subprocess.check_output([sys.executable, "../gen_esp32part.py",
csvpath, binpath], stderr=subprocess.STDOUT)
- self.assertNotIn("WARNING", output)
+ self.assertNotIn(b"WARNING", output)
# assert that file reads back as identical
with open(binpath, 'rb') as f:
binary_readback = f.read()
pass
-class VerificationTests(unittest.TestCase):
+class VerificationTests(Py23TestCase):
def test_bad_alignment(self):
csv = """
# Name,Type, SubType,Offset,Size
app,app, factory, 32K, 1M
"""
- with self.assertRaisesRegexp(ValidationError,
+ with self.assertRaisesRegex(ValidationError,
r"Offset.+not aligned"):
t = PartitionTable.from_csv(csv)
t.verify()
def test_warnings(self):
try:
- sys.stderr = StringIO.StringIO() # capture stderr
+ sys.stderr = io.StringIO() # capture stderr
csv_1 = "app, 1, 2, 32K, 1M\n"
PartitionTable.from_csv(csv_1).verify()
self.assertIn("WARNING", sys.stderr.getvalue())
self.assertIn("partition type", sys.stderr.getvalue())
- sys.stderr = StringIO.StringIO()
+ sys.stderr = io.StringIO()
csv_2 = "ota_0, app, ota_1, , 1M\n"
PartitionTable.from_csv(csv_2).verify()
self.assertIn("WARNING", sys.stderr.getvalue())
finally:
sys.stderr = sys.__stderr__
-class PartToolTests(unittest.TestCase):
+class PartToolTests(Py23TestCase):
def _run_parttool(self, csvcontents, args):
csvpath = tempfile.mktemp()
try:
output = subprocess.check_output([sys.executable, "../parttool.py"] + args.split(" ") + [ csvpath ],
stderr=subprocess.STDOUT)
- self.assertNotIn("WARNING", output)
- m = re.search("0x[0-9a-fA-F]+", output)
+ self.assertNotIn(b"WARNING", output)
+ m = re.search(b"0x[0-9a-fA-F]+", output)
return m.group(0) if m else ""
finally:
os.remove(csvpath)
rpt = lambda args: self._run_parttool(csv, args)
self.assertEqual(
- rpt("--type data --subtype nvs --offset"), "0x9000")
+ rpt("--type data --subtype nvs --offset"), b"0x9000")
self.assertEqual(
- rpt("--type data --subtype nvs --size"), "0x4000")
+ rpt("--type data --subtype nvs --size"), b"0x4000")
self.assertEqual(
- rpt("--partition-name otadata --offset"), "0xd000")
+ rpt("--partition-name otadata --offset"), b"0xd000")
self.assertEqual(
- rpt("--default-boot-partition --offset"), "0x10000")
+ rpt("--default-boot-partition --offset"), b"0x10000")
def test_fallback(self):
csv = """
rpt = lambda args: self._run_parttool(csv, args)
self.assertEqual(
- rpt("--type app --subtype ota_1 --offset"), "0x130000")
+ rpt("--type app --subtype ota_1 --offset"), b"0x130000")
self.assertEqual(
- rpt("--default-boot-partition --offset"), "0x30000") # ota_0
+ rpt("--default-boot-partition --offset"), b"0x30000") # ota_0
csv_mod = csv.replace("ota_0", "ota_2")
self.assertEqual(
self._run_parttool(csv_mod, "--default-boot-partition --offset"),
- "0x130000") # now default is ota_1
+ b"0x130000") # now default is ota_1
if __name__ =="__main__":
unittest.main()