summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2010-07-01 09:06:25 +1200
committerLars Wirzenius <liw@liw.fi>2010-07-01 09:06:25 +1200
commit492bb58a42e621645e560f0f4585e4110e600bf7 (patch)
treea3ac190a33c3a80ffc7e8726d525a2b71e0d8cac
parent8cb296a1ac5a3fc5750725c3f603e73c23db82b3 (diff)
parent9aee5a84d88e6f027c516311c763d1571e53317b (diff)
downloadpython-ttystatus-492bb58a42e621645e560f0f4585e4110e600bf7.tar.gz
Fix handling of bad values (zero divisions, non-numbers, etc).
-rw-r--r--ttystatus/percent_tests.py4
-rw-r--r--ttystatus/progressbar.py7
-rw-r--r--ttystatus/progressbar_tests.py8
-rw-r--r--ttystatus/remtime.py14
-rw-r--r--ttystatus/remtime_tests.py12
5 files changed, 38 insertions, 7 deletions
diff --git a/ttystatus/percent_tests.py b/ttystatus/percent_tests.py
index 441710d..9e29530 100644
--- a/ttystatus/percent_tests.py
+++ b/ttystatus/percent_tests.py
@@ -35,3 +35,7 @@ class PercentDoneTests(unittest.TestCase):
self.w.update({ 'done': '', 'total': '' }, 999)
self.assertEqual(str(self.w), '0.0 %')
+ def test_handles_zero_total(self):
+ self.w.update({ 'done': '', 'total': 0 }, 999)
+ self.assertEqual(str(self.w), '0.0 %')
+
diff --git a/ttystatus/progressbar.py b/ttystatus/progressbar.py
index 4ed9bf3..aca8ea3 100644
--- a/ttystatus/progressbar.py
+++ b/ttystatus/progressbar.py
@@ -27,8 +27,11 @@ class ProgressBar(ttystatus.Widget):
def update(self, master, width):
done = float(master.get(self.done_name, 0))
- total = float(master.get(self.total_name, 1))
- fraction = done / total
+ total = float(master.get(self.total_name, 1) or 0)
+ if total == 0:
+ fraction = 0
+ else:
+ fraction = done / total
n_stars = int(round(fraction * width))
n_dashes = int(width - n_stars)
self.value = ('#' * n_stars) + ('-' * n_dashes)
diff --git a/ttystatus/progressbar_tests.py b/ttystatus/progressbar_tests.py
index a84a1f0..0e3bf5a 100644
--- a/ttystatus/progressbar_tests.py
+++ b/ttystatus/progressbar_tests.py
@@ -27,6 +27,14 @@ class ProgressBarTests(unittest.TestCase):
def test_sets_initial_value_to_empty(self):
self.assertEqual(str(self.w), '')
+ def test_shows_zero_percent_for_empty_string_total(self):
+ self.w.update({ 'done': 1, 'total': '' }, 10)
+ self.assertEqual(str(self.w), '-' * 10)
+
+ def test_shows_zero_percent_for_zero_total(self):
+ self.w.update({ 'done': 1, 'total': 0 }, 10)
+ self.assertEqual(str(self.w), '-' * 10)
+
def test_shows_zero_percent_correctly(self):
self.w.update({ 'done': 0, 'total': 100 }, 10)
self.assertEqual(str(self.w), '-' * 10)
diff --git a/ttystatus/remtime.py b/ttystatus/remtime.py
index 98a8643..b1cffe5 100644
--- a/ttystatus/remtime.py
+++ b/ttystatus/remtime.py
@@ -27,7 +27,8 @@ class RemainingTime(ttystatus.Widget):
self.done_name = done_name
self.total_name = total_name
self.started = None
- self.value = '--h--m--s'
+ self.default = '--h--m--s'
+ self.value = self.default
def get_time(self): # pragma: no cover
'''Return current time.
@@ -51,9 +52,12 @@ class RemainingTime(ttystatus.Widget):
self.started = self.get_time()
duration = self.get_time() - self.started
if duration >= 1.0:
- done = float(master[self.done_name])
- total = float(master[self.total_name])
+ done = float(master.get(self.done_name, 0) or 0)
+ total = float(master.get(self.total_name, 0) or 0)
speed = done / duration
remaining = total - done
- secs = remaining / speed
- self.value = self.format(secs)
+ if speed > 0:
+ secs = remaining / speed
+ self.value = self.format(secs)
+ else:
+ self.value = self.default
diff --git a/ttystatus/remtime_tests.py b/ttystatus/remtime_tests.py
index 817048b..6c6f452 100644
--- a/ttystatus/remtime_tests.py
+++ b/ttystatus/remtime_tests.py
@@ -40,3 +40,15 @@ class RemainingTimeTests(unittest.TestCase):
self.w.update({ 'done': 80, 'total': 100 }, 999)
self.assertEqual(str(self.w), '00h00m05s')
+ def test_handles_zero_speed(self):
+ self.w.update({ 'done': 0, 'total': 100 }, 999)
+ self.w.get_time = lambda: 5.0
+ self.w.update({ 'done': 0, 'total': 100 }, 999)
+ self.assertEqual(str(self.w), '--h--m--s')
+
+ def test_handles_empty_strings_for_done_and_total(self):
+ self.w.update({ 'done': '', 'total': '' }, 999)
+ self.w.get_time = lambda: 5.0
+ self.w.update({ 'done': '', 'total': '' }, 999)
+ self.assertEqual(str(self.w), '--h--m--s')
+