Patchwork [4,of,5,V5] testing: allow Hypothesis to enable extensions

login
register
mail settings
Submitter David MacIver
Date Feb. 24, 2016, 6:42 p.m.
Message ID <6847cd559c93ce671de9.1456339370@laser-shark>
Download mbox | patch
Permalink /patch/13360/
State Superseded
Headers show

Comments

David MacIver - Feb. 24, 2016, 6:42 p.m.
# HG changeset patch
# User David R. MacIver <david@drmaciver.com>
# Date 1456319490 0
#      Wed Feb 24 13:11:30 2016 +0000
# Node ID 6847cd559c93ce671de93fde7d10aa4da911bb13
# Parent  416381cb260e0a5c3c172ffd942e8f682885639a
testing: allow Hypothesis to enable extensions

This adds support for testing extensions, including both tests
that extensions don't change behaviour and test for specific
commands.

We use the precondition system to determine what commands are
available to us. If we never use any commands enabled by an
extension then that extension is *skippable* and should not
have changed the behaviour of the test. We thus rerun the test
with an environment variable which is designed to turn off the
extension.

Patch

diff -r 416381cb260e -r 6847cd559c93 tests/test-verify-repo-operations.py
--- a/tests/test-verify-repo-operations.py	Wed Feb 24 18:36:49 2016 +0000
+++ b/tests/test-verify-repo-operations.py	Wed Feb 24 13:11:30 2016 +0000
@@ -17,7 +17,8 @@ 
 
 from hypothesis.extra.datetime import datetimes
 from hypothesis.errors import HypothesisException
-from hypothesis.stateful import rule, RuleBasedStateMachine, Bundle
+from hypothesis.stateful import (
+    rule, RuleBasedStateMachine, Bundle, precondition)
 import hypothesis.strategies as st
 from hypothesis import settings, note
 from hypothesis.configuration import set_hypothesis_home_dir
@@ -136,6 +137,9 @@ 
         self.mkdirp("repo1")
         self.cd("repo1")
         self.hg("init")
+        self.extensions = {}
+        self.all_extensions = set()
+        self.non_skippable_extensions = set()
 
     def teardown(self):
         """On teardown we clean up after ourselves as usual, but we also
@@ -166,6 +170,17 @@ 
         e = None
         if not self.failed:
             try:
+                for ext in (
+                    self.all_extensions - self.non_skippable_extensions
+                ):
+                    try:
+                        os.environ["SKIP_EXTENSION"] = ext
+                        output = subprocess.check_output([
+                            runtests, path, "--local",
+                        ], stderr=subprocess.STDOUT)
+                        assert "Ran 1 test" in output, output
+                    finally:
+                        del os.environ["SKIP_EXTENSION"]
                 output = subprocess.check_output([
                     runtests, path, "--local", "--pure"
                 ], stderr=subprocess.STDOUT)
@@ -447,6 +462,56 @@ 
             else:
                 self.hg("update", "--", branch)
 
+    # Section: Extension management
+    def hasextension(self, extension):
+        repo = self.currentrepo
+        return repo in self.extensions and extension in self.extensions[repo]
+
+    def commandused(self, extension):
+        assert extension in self.all_extensions
+        self.non_skippable_extensions.add(extension)
+
+    @rule(extension=st.sampled_from((
+        'shelve', 'mq', 'blackbox', 'evolve'
+    )))
+    def addextension(self, extension):
+        self.all_extensions.add(extension)
+        extensions = self.extensions.setdefault(self.currentrepo, set())
+        if extension in extensions:
+            return
+        extensions.add(extension)
+        if not os.path.exists(hgrc):
+            self.command("touch", hgrc)
+        with open(hgrc, "r") as i:
+            existing = i.read()
+        if "[extensions]" not in existing:
+            with open(hgrc, "a") as o:
+                o.write("[extensions]\n")
+            self.log.append("$ echo '[extensions]' >> %s" % (hgrc,))
+        with open(hgrc, 'a') as o:
+            line = "%s=" % (extension,)
+            o.write(line)
+            o.write("\n")
+        self.log.append((
+            '$ if [[ "$SKIP_EXTENSION" == "%s" ]; '
+            'then  echo %s >> %s; fi') % (
+                extension, line, hgrc,))
+
+    # Section: Commands from the shelve extension
+    @rule()
+    @precondition(lambda self: self.hasextension("shelve"))
+    def shelve(self):
+        self.commandused("shelve")
+        with acceptableerrors("nothing changed"):
+            self.hg("shelve")
+
+    @rule()
+    @precondition(lambda self: self.hasextension("shelve"))
+    def unshelve(self):
+        self.commandused("shelve")
+        with acceptableerrors("no shelved changes to apply"):
+            self.hg("unshelve")
+
 settings.register_profile(
     'default',  settings(
         timeout=300,