[gnome-builder] maven: Add Test provider



commit a84886e271a2a202fc55fa25ac9830daeb0d6eb7
Author: Alberto Fanjul <albertofanjul gmail com>
Date:   Thu May 31 07:54:36 2018 +0200

    maven: Add Test provider
    
    Using a example project https://gitlab.gnome.org/albfan/pressme
    here is all the commands you need to run to find the available
    tests, run them one by one, and parse its results

 src/plugins/maven/maven_plugin.py | 132 +++++++++++++++++++++++++++++++++++++-
 1 file changed, 130 insertions(+), 2 deletions(-)
---
diff --git a/src/plugins/maven/maven_plugin.py b/src/plugins/maven/maven_plugin.py
index 7d276c0e6..cdb0eb27a 100755
--- a/src/plugins/maven/maven_plugin.py
+++ b/src/plugins/maven/maven_plugin.py
@@ -32,6 +32,11 @@ from gi.repository import Ide
 
 _ = Ide.gettext
 
+_ATTRIBUTES = ",".join([
+    Gio.FILE_ATTRIBUTE_STANDARD_NAME,
+    Gio.FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME,
+    Gio.FILE_ATTRIBUTE_STANDARD_SYMBOLIC_ICON,
+])
 
 class MavenBuildSystem(Ide.Object, Ide.BuildSystem, Gio.AsyncInitable):
     project_file = GObject.Property(type=Gio.File)
@@ -43,7 +48,7 @@ class MavenBuildSystem(Ide.Object, Ide.BuildSystem, Gio.AsyncInitable):
         return 'Maven'
 
     def do_init_async(self, io_priority, cancellable, callback, data):
-        task = Gio.Task.new(self, cancellable, callback)
+        task = Ide.Task.new(self, cancellable, callback)
 
         try:
             # Maybe this is a pom.xml
@@ -147,7 +152,7 @@ class MavenBuildTarget(Ide.Object, Ide.BuildTarget):
 class MavenBuildTargetProvider(Ide.Object, Ide.BuildTargetProvider):
 
     def do_get_targets_async(self, cancellable, callback, data):
-        task = Gio.Task.new(self, cancellable, callback)
+        task = Ide.Task.new(self, cancellable, callback)
         task.set_priority(GLib.PRIORITY_LOW)
 
         context = self.get_context()
@@ -165,3 +170,126 @@ class MavenBuildTargetProvider(Ide.Object, Ide.BuildTargetProvider):
     def do_get_targets_finish(self, result):
         if result.propagate_boolean():
             return result.targets
+
+class MavenIdeTestProvider(Ide.TestProvider):
+
+    def do_run_async(self, test, pipeline, cancellable, callback, data):
+        task = Ide.Task.new(self, cancellable, callback)
+        task.set_priority(GLib.PRIORITY_LOW)
+
+        context = self.get_context()
+        build_system = context.get_build_system()
+
+        if type(build_system) != MavenBuildSystem:
+            task.return_error(GLib.Error('Not maven build system',
+                                         domain=GLib.quark_to_string(Gio.io_error_quark()),
+                                         code=Gio.IOErrorEnum.NOT_SUPPORTED))
+            return
+
+        task.targets = [MavenBuildTarget(context=self.get_context())]
+
+        try:
+            runtime = pipeline.get_runtime()
+            runner = runtime.create_runner()
+            if not runner:
+               task.return_error(Ide.NotSupportedError())
+
+            runner.set_flags(Gio.SubprocessFlags.STDOUT_PIPE | Gio.SubprocessFlags.STDERR_PIPE)
+
+            srcdir = pipeline.get_srcdir()
+            runner.set_cwd(srcdir)
+
+            commands = test.get_command()
+
+            for command in commands:
+               runner.append_argv(command)
+
+            test.set_status(Ide.TestStatus.RUNNING)
+
+            def run_test_cb(runner, result, data):
+                try:
+                    runner.run_finish(result)
+                    test.set_status(Ide.TestStatus.SUCCESS)
+                except:
+                    test.set_status(Ide.TestStatus.FAILED)
+                finally:
+                    task.return_boolean(True)
+
+            runner.run_async(cancellable, run_test_cb, task)
+        except Exception as ex:
+            task.return_error(ex)
+
+    def do_run_finish(self, result):
+        if result.propagate_boolean():
+            return result.targets
+
+    def do_reload(self):
+
+        context = self.get_context()
+        build_system = context.get_build_system()
+
+        if type(build_system) != MavenBuildSystem:
+            task.return_error(GLib.Error('Not maven build system',
+                                         domain=GLib.quark_to_string(Gio.io_error_quark()),
+                                         code=Gio.IOErrorEnum.NOT_SUPPORTED))
+            return
+
+        # find all files in test directory
+        # http://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html
+        build_manager = context.get_build_manager()
+        pipeline = build_manager.get_pipeline()
+        srcdir = pipeline.get_srcdir()
+        test_suite = Gio.File.new_for_path(os.path.join(srcdir, 'src/test/java'))
+        test_suite.enumerate_children_async(_ATTRIBUTES,
+                                            Gio.FileQueryInfoFlags.NONE,
+                                            GLib.PRIORITY_LOW,
+                                            None,
+                                            self.on_enumerator_loaded,
+                                            None)
+
+    def on_enumerator_loaded(self, parent, result, data):
+        try:
+            enumerator = parent.enumerate_children_finish(result)
+            info = enumerator.next_file(None)
+
+            while info is not None:
+                name = info.get_name()
+                gfile = parent.get_child(name)
+                if info.get_file_type() == Gio.FileType.DIRECTORY:
+                    gfile.enumerate_children_async(_ATTRIBUTES,
+                                                    Gio.FileQueryInfoFlags.NONE,
+                                                    GLib.PRIORITY_LOW,
+                                                    None,
+                                                    self.on_enumerator_loaded,
+                                                    None)
+                else:
+                    #TODO Ask java through introspection for classes with TestCase and its public void 
methods 
+                    # or Annotation @Test methods
+                    result, contents, etag = gfile.load_contents()
+                    tests = [x for x in str(contents).split('\\n') if 'public void' in x]
+                    tests = [v.replace("()", "").replace("public void","").strip() for v in tests]
+                    classname=name.replace(".java", "")
+
+                    for testname in tests:
+                        # http://maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html
+                        # it has to be junit 4.x
+                        command = ["mvn", "-Dtest={}#{}".format(classname, testname), "test"]
+
+                        test = MavenTest(group=classname, id=classname+testname, display_name=testname)
+                        test.set_command(command)
+                        self.add(test)
+
+                info = enumerator.next_file(None)
+
+            enumerator.close()
+
+        except Exception as ex:
+            Ide.warning(repr(ex))
+
+class MavenTest(Ide.Test):
+
+    def get_command(self):
+        return self.command
+
+    def set_command(self, command):
+        self.command = command


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]