[Piglit] [PATCH 02/32] framework/grouptools: fix commonprefix to handle non-overlapping values

Dylan Baker baker.dylan.c at gmail.com
Fri Sep 11 15:08:25 PDT 2015


Currently if commonprefix is passed ['foo', 'bar'] it will pass an empty
list to join, which requires at least one argument to work. This will
cause an exception to be raised. Rather than doing that, we should check
that there are common values, and just return '' if there isn't.

Signed-off-by: Dylan Baker <dylanx.c.baker at intel.com>
---
 framework/grouptools.py             | 6 +++++-
 framework/tests/grouptools_tests.py | 5 +++++
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/framework/grouptools.py b/framework/grouptools.py
index b223bf4..62d3f9c 100644
--- a/framework/grouptools.py
+++ b/framework/grouptools.py
@@ -94,7 +94,11 @@ def commonprefix(args):
         else:
             break
 
-    return join(*common)
+    # Join needs at least one element to join
+    if common:
+        return join(*common)
+    else:
+        return ''
 
 
 def join(first, *args):
diff --git a/framework/tests/grouptools_tests.py b/framework/tests/grouptools_tests.py
index f36e350..acab7ed 100644
--- a/framework/tests/grouptools_tests.py
+++ b/framework/tests/grouptools_tests.py
@@ -114,3 +114,8 @@ def test_join_empty():
     expected = 'spec'
     test = grouptools.join('', 'spec')
     nt.eq_(expected, test)
+
+
+def test_commonprefix_none():
+    """grouptools.commonprefix: returns '' when no values are the same"""
+    nt.eq_('', grouptools.commonprefix(['foo', 'bar']))
-- 
2.5.1



More information about the Piglit mailing list