From 51a54dff04c9ddae742c6b8f27b24d61fa8c7a80 Mon Sep 17 00:00:00 2001 From: Zachary York Date: Wed, 11 Nov 2015 15:32:27 -0800 Subject: [PATCH] [filebrowser] Update xxd_test to not fail for different offset lengths --- apps/filebrowser/src/filebrowser/lib/xxd_test.py | 29 +++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/apps/filebrowser/src/filebrowser/lib/xxd_test.py b/apps/filebrowser/src/filebrowser/lib/xxd_test.py index 30db3a3..2a62ff6 100644 --- a/apps/filebrowser/src/filebrowser/lib/xxd_test.py +++ b/apps/filebrowser/src/filebrowser/lib/xxd_test.py @@ -35,6 +35,33 @@ class XxdTest(unittest.TestCase): def test_mask_not_printable(self): self.assertEquals( (2, "..@"), xxd.mask_not_alphanumeric("\xff\x90\x40")) + def _get_offset_width(self, line): + offset, match, _ = line.partition(":") + if not match: + raise ValueError("No offset was found in the xxd output") + return len(offset) + + def _is_offset_width_same(self, expected, actual): + return self._get_offset_width(expected) == self._get_offset_width(actual) + + def _remove_padding(self, line): + for index, c in enumerate(line): + if c != '0': + return line[index:] + return '' + + def _standardize_xxd_output(self, xxd_output): + return "\n".join(self._remove_padding(line) for line in xxd_output.splitlines()) + + def _verify_content(self, expected, actual): + if self._is_offset_width_same(expected, actual): + self.assertEquals(expected, actual) + return + # Not all distributions have the same amount of bits in their 'Offset' + # This corrects for this to avoid having this test fail when that is the only problem + corrected_expected = self._standardize_xxd_output(expected) + corrected_actual = self._standardize_xxd_output(actual) + self.assertEquals(corrected_expected, corrected_actual) def test_compare_to_xxd(self): """ @@ -55,7 +82,7 @@ class XxdTest(unittest.TestCase): output = StringIO.StringIO() xxd.main(StringIO.StringIO(random_text), output) - self.assertEquals(stdin, output.getvalue()) + self._verify_content(stdin, output.getvalue()) if __name__ == "__main__": unittest.main() -- 1.7.9.5