Skip to contentSkip to conversation

test-utils: handle import bb failing and skip the test

PATCH In review Ross Burton 3 participants 5 messages May 23, 2013 – May 30, 2013

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 test-utils: handle import bb failing and skip the test

Instead of reporting an error when bb cannot be imported, skip the test instead. This makes it a lot easier to iterate a test suite when we don't care about this particular test.

---
 meta/lib/oe/tests/test_utils.py |    6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
meta/lib/oe/tests/test_utils.py +5 -1
index 466c47e..779247a 100644
@@ -1,11 +1,15 @@
 import unittest
-import bb, oe.utils
+import oe.utils
 
 class TestPackagesFilterOutSystem(unittest.TestCase):
     def test_filter(self):
         """
         Test that oe.utils.packages_filter_out_system works.
         """
+        try:
+            import bb
+        except ImportError:
+            self.skipTest("Cannot import bb")
 
         d = bb.data_smart.DataSmart()
         d.setVar("PN", "foo")
Signature · 1 line
1.7.10.4
#1 lore raw

PATCH 2/3 test_utils: import functions directly for conciseness

---
 meta/lib/oe/tests/test_utils.py |   10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
meta/lib/oe/tests/test_utils.py +5 -5
index 779247a..8bb36f2 100644
@@ -1,5 +1,5 @@
 import unittest
-import oe.utils
+from oe.utils import packages_filter_out_system
 
 class TestPackagesFilterOutSystem(unittest.TestCase):
     def test_filter(self):
@@ -15,17 +15,17 @@ class TestPackagesFilterOutSystem(unittest.TestCase):
         d.setVar("PN", "foo")
 
         d.setVar("PACKAGES", "foo foo-doc foo-dev")
-        pkgs = oe.utils.packages_filter_out_system(d)
+        pkgs = packages_filter_out_system(d)
         self.assertEqual(pkgs, [])
 
         d.setVar("PACKAGES", "foo foo-doc foo-data foo-dev")
-        pkgs = oe.utils.packages_filter_out_system(d)
+        pkgs = packages_filter_out_system(d)
         self.assertEqual(pkgs, ["foo-data"])
 
         d.setVar("PACKAGES", "foo foo-locale-en-gb")
-        pkgs = oe.utils.packages_filter_out_system(d)
+        pkgs = packages_filter_out_system(d)
         self.assertEqual(pkgs, [])
 
         d.setVar("PACKAGES", "foo foo-data foo-locale-en-gb")
-        pkgs = oe.utils.packages_filter_out_system(d)
+        pkgs = packages_filter_out_system(d)
         self.assertEqual(pkgs, ["foo-data"])
Signature · 1 line
1.7.10.4
#2 lore raw

PATCH 3/3 utils: add trim_version() function

In reply to Ross Burton

Add a helper function that returns just the first <num_parts> of <version>, split by periods. For example, trim_version("1.2.3", 2) will return "1.2".

This should help reduce the spread of numerous copies of this idea across classes and recipes.

---
 meta/lib/oe/tests/test_utils.py |   20 ++++++++++++++++++++
 meta/lib/oe/utils.py            |   15 +++++++++++++++
 2 files changed, 35 insertions(+)
meta/lib/oe/tests/test_utils.py +20 -0
index 8bb36f2..5d9ac52 100644
@@ -29,3 +29,23 @@ class TestPackagesFilterOutSystem(unittest.TestCase):
         d.setVar("PACKAGES", "foo foo-data foo-locale-en-gb")
         pkgs = packages_filter_out_system(d)
         self.assertEqual(pkgs, ["foo-data"])
+
+
+class TestTrimVersion(unittest.TestCase):
+    def test_version_exception(self):
+        with self.assertRaises(TypeError):
+            trim_version(None, 2)
+        with self.assertRaises(TypeError):
+            trim_version((1, 2, 3), 2)
+
+    def test_num_exception(self):
+        with self.assertRaises(ValueError):
+            trim_version("1.2.3", 0)
+        with self.assertRaises(ValueError):
+            trim_version("1.2.3", -1)
+
+    def test_valid(self):
+        self.assertEqual(trim_version("1.2.3", 1), "1")
+        self.assertEqual(trim_version("1.2.3", 2), "1.2")
+        self.assertEqual(trim_version("1.2.3", 3), "1.2.3")
+        self.assertEqual(trim_version("1.2.3", 4), "1.2.3")
meta/lib/oe/utils.py +15 -0
index 0a2092b..82987e8 100644
@@ -135,3 +135,18 @@ def packages_filter_out_system(d):
 
 def getstatusoutput(cmd):
     return cmdstatus.getstatusoutput(cmd)
+
+
+def trim_version(version, num_parts=2):
+    """
+    Return just the first <num_parts> of <version>, split by periods.  For
+    example, trim_version("1.2.3", 2) will return "1.2".
+    """
+    if type(version) is not str:
+        raise TypeError("Version should be a string")
+    if num_parts < 1:
+        raise ValueError("Cannot split to parts < 1")
+
+    parts = version.split(".")
+    trimmed = ".".join(parts[:num_parts])
+    return trimmed
Signature · 1 line
1.7.10.4
#3 lore raw

Re: [PATCH 3/3] utils: add trim_version() function

Quoted 4 lines · Ross Burton
meta/lib/oe/utils.py +3 -0
+def trim_version(version, num_parts=2):
+    """
+    Return just the first <num_parts> of <version>, split by periods.  For

meta/lib/oe/utils.py def packages_filter_out_system(d): · line 152

Quoted 10 lines
meta/lib/oe/utils.py +10 -0
+    example, trim_version("1.2.3", 2) will return "1.2".
+    """
+    if type(version) is not str:
+        raise TypeError("Version should be a string")
+    if num_parts < 1:
+        raise ValueError("Cannot split to parts < 1")
+
+    parts = version.split(".")
+    trimmed = ".".join(parts[:num_parts])
+    return trimmed

Another option would be to do something like:

def trim_version(version, num_parts=2):
v = distutils.version.LooseVersion(version)
return v.version[:num_parts]

Signature · 5 lines
Christopher Larson
clarson at kergoth dot com
Founder - BitBake, OpenEmbedded, OpenZaurus
Maintainer - Tslib
Senior Software Engineer, Mentor Graphics
#4 lore raw
In reply to Ross Burton
Quoted 20 lines · Ross Burton

Instead of reporting an error when bb cannot be imported, skip the test instead. This makes it a lot easier to iterate a test suite when we don't care about this particular test.

---
 meta/lib/oe/tests/test_utils.py |    6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
meta/lib/oe/tests/test_utils.py +1 -1
index 466c47e..779247a 100644
@@ -1,11 +1,15 @@
 import unittest
-import bb, oe.utils
+import oe.utils
 
 class TestPackagesFilterOutSystem(unittest.TestCase):
     def test_filter(self):

meta/lib/oe/tests/test_utils.py line 15

Quoted 10 lines
meta/lib/oe/tests/test_utils.py +4 -0
         """
         Test that oe.utils.packages_filter_out_system works.
         """
+        try:
+            import bb
+        except ImportError:
+            self.skipTest("Cannot import bb")
 
         d = bb.data_smart.DataSmart()
         d.setVar("PN", "foo")

This a python 2.7ism :(

For various reasons we should really add unittest from python 2.7 into bitbake and solve this problem that way once and for all. I appreciate that adds a hard dependency on using bitbake but OE isn't much use without it...

Cheers,

Richard