Repository: coala/coala

Issue:
WIP: JuliaRequirement: Add installation methods



Discussion:
Comment on cd98f26deb79fa00c78907c9809e8d0917b64ef2.

Shortlog of HEAD commit is 10 character(s) longer than the limit (60 > 50).

_GitCommitBear, severity NORMAL, section `commit`._

but it will be merged eventually. so please fix the coala problems and stuff, and if you dont want it to be reviewed by other people, mark the Pull Request title starting with `WIP:`

Comment on cd98f26deb79fa00c78907c9809e8d0917b64ef2.

Shortlog of HEAD commit is 10 character(s) longer than the limit (60 > 50).

_GitCommitBear, severity NORMAL, section `commit`._


Resolving patch:
diff --git a/coalib/bears/requirements/JuliaRequirement.py b/coalib/bears/requirements/JuliaRequirement.py
--- a/coalib/bears/requirements/JuliaRequirement.py
+++ b/coalib/bears/requirements/JuliaRequirement.py
@@ -1,4 +1,9 @@
+import shlex
+
 from coalib.bears.requirements.PackageRequirement import PackageRequirement
+from coalib.misc.Shell import call_without_output
+
+from coala_utils.string_processing import escape
 
 
 class JuliaRequirement(PackageRequirement):
@@ -24,3 +29,31 @@ def __init__(self, package, version=""):
         :param version: A version string. Leave empty to specify latest version.
         """
         PackageRequirement.__init__(self, 'julia', package, version)
+
+    def install_command(self):
+        """
+        Creates the installation command for the instance of the class.
+
+        >>> JuliaRequirement('Lint').install_command()
+        'julia -e \\'Pkg.add("Lint")\\''
+
+        :return: A string with the installation command.
+        """
+        code = 'Pkg.add("{}")'.format(escape(self.package, '\\"'))
+        args = ('julia', '-e', shlex.quote(code))
+        return ' '.join(args)
+
+    def is_installed(self):
+        """
+        Checks if the dependency is installed.
+
+        :return: ``True`` if dependency is installed, ``False`` otherwise.
+        """
+        # We need to check explicitly if `nothing` is returned, as this happens
+        # when the package is *registered*, but *not installed*. If it's not
+        # even registered, julia will throw an exception which lets julia exit
+        # with an error code different from 0.
+        code = 'Pkg.installed("{}")==nothing?exit(1):exit(0)'.format(
+            escape(self.package, '\\"'))
+        args = ('julia', '-e', code)
+        return not call_without_output(args)


Test patch:
diff --git a/tests/bears/requirements/JuliaRequirementTest.py b/tests/bears/requirements/JuliaRequirementTest.py
new file mode 100644
--- /dev/null
+++ b/tests/bears/requirements/JuliaRequirementTest.py
@@ -0,0 +1,19 @@
+import shutil
+import unittest
+
+from coalib.bears.requirements.JuliaRequirement import JuliaRequirement
+
+
+@unittest.skipIf(shutil.which('julia') is None, 'Julia is not installed.')
+class JuliaRequirementTestCase(unittest.TestCase):
+
+    def test_installed_requirement(self):
+        self.assertTrue(JuliaRequirement("Lint").is_installed())
+
+    def test_not_registered_requirement(self):
+        self.assertFalse(JuliaRequirement("some_bad_package").is_installed())
+
+    def test_registered_but_not_installed_requirement(self):
+        # FIXME Make this test more reliable, because `ACME` can be an
+        # FIXME installed package.
+        self.assertFalse(JuliaRequirement("ACME").is_installed())