[8/27] framework/profile: add a copy method to profile

Submitted by Dylan Baker on Oct. 24, 2016, 7:54 p.m.

Details

Message ID ecc914b906c74abbe291ecfaaf9d34ad507fd135.1477338764.git-series.dylan@pnwbakers.com
State New
Headers show
Series "Various cleanups for profiles" ( rev: 1 ) in Piglit

Not browsing as part of any series.

Commit Message

Dylan Baker Oct. 24, 2016, 7:54 p.m.
This will allow a profile to be copied and "subclassed" without
affecting the original profile. This will allow a long-standing bug that
made it impossible to run two subclasses of all.py (say shader.py and
glslparser.py) at the same time, since they would both try to modify the
all.py profile in incompatible ways.

Signed-off-by: Dylan Baker <dylanx.c.baker@intel.com>
---
 framework/profile.py                | 16 +++++++++-
 unittests/framework/test_profile.py | 52 ++++++++++++++++++++++++++++++-
 2 files changed, 68 insertions(+), 0 deletions(-)

Patch hide | download patch | download mbox

diff --git a/framework/profile.py b/framework/profile.py
index 954bf25..73a8a96 100644
--- a/framework/profile.py
+++ b/framework/profile.py
@@ -31,6 +31,7 @@  from __future__ import (
 )
 import collections
 import contextlib
+import copy
 import importlib
 import itertools
 import multiprocessing
@@ -404,6 +405,21 @@  class TestProfile(object):
         with self.test_list.allow_reassignment:
             yield
 
+    def copy(self):
+        """Create a copy of the TestProfile.
+
+        This method creates a copy with references to the original instance
+        (using copy.copy), except for the test_list attribute, which is copied
+        using copy.deepcopy, which is necessary to ensure that filter_tests
+        only affects the right instance. This allows profiles to be
+        "subclassed" by other profiles, without modifying the original.
+        """
+        new = copy.copy(self)
+        new.test_list = copy.deepcopy(self.test_list)
+        new.forced_test_list = copy.copy(self.forced_test_list)
+        new.filters = copy.copy(self.filters)
+        return new
+
 
 def load_test_profile(filename):
     """Load a python module and return it's profile attribute.
diff --git a/unittests/framework/test_profile.py b/unittests/framework/test_profile.py
index 6671349..5ef95e4 100644
--- a/unittests/framework/test_profile.py
+++ b/unittests/framework/test_profile.py
@@ -285,6 +285,58 @@  class TestTestProfile(object):
 
             assert grouptools.join('foo', 'abc') in self.profile.test_list
 
+    class TestCopy(object):
+        """Tests for the copy method."""
+
+        @pytest.fixture
+        def fixture(self):
+            orig = profile.TestProfile()
+            orig.test_list['foo'] = utils.Test(['foo'])
+            orig.test_list['bar'] = utils.Test(['bar'])
+            orig.filters = [lambda name, _: name != 'bar']
+            orig.forced_test_list = ['foo']
+            return orig
+
+        def test_filters(self, fixture):
+            """The filters attribute is copied correctly."""
+            new = fixture.copy()
+
+            # Assert that the fixtures are equivalent, but not the same
+            assert fixture.filters == new.filters
+            assert fixture.filters is not new.filters
+
+            # And double check by modifying one of them and asserting that the
+            # other has not changed.
+            new.filters.append(lambda name, _: name != 'oink')
+            assert len(fixture.filters) == 1
+
+        def test_forced_test_list(self, fixture):
+            """The forced_test_list attribute is copied correctly."""
+            new = fixture.copy()
+
+            # Assert that the fixtures are equivalent, but not the same
+            assert fixture.forced_test_list == new.forced_test_list
+            assert fixture.forced_test_list is not new.forced_test_list
+
+            # And double check by modifying one of them and asserting that the
+            # other has not changed.
+            del new.forced_test_list[0]
+            assert fixture.forced_test_list[0] == 'foo'
+
+        def test_test_list(self, fixture):
+            """The test_list attribute is copied correctly."""
+            new = fixture.copy()
+
+            # Assert that the fixtures are equivalent, but not the same
+            assert fixture.test_list == new.test_list
+            assert fixture.test_list is not new.test_list
+
+        def test_prepare_test_list(self, fixture):
+            """The prepare_test_list method doesn't affect both."""
+            new = fixture.copy()
+            new.prepare_test_list()
+            assert new.test_list != fixture.test_list
+
 
 class TestTestDict(object):
     """Tests for the TestDict object."""