Commit c6150f57929ced71182741db125edc176f4742d8

Michael Crumm 2021-08-28T12:54:52

Check version on --if-missing, closes #5 Co-authored-by: José Valim <jose.valim@dashbit.co>

diff --git a/lib/mix/tasks/sass.install.ex b/lib/mix/tasks/sass.install.ex
index 073d7ca..d65c832 100644
--- a/lib/mix/tasks/sass.install.ex
+++ b/lib/mix/tasks/sass.install.ex
@@ -23,7 +23,7 @@ defmodule Mix.Tasks.Sass.Install do
   def run(args) do
     case OptionParser.parse_head!(args, strict: [if_missing: :boolean]) do
       {opts, []} ->
-        if opts[:if_missing] && DartSass.installed?() do
+        if opts[:if_missing] && latest_version?() do
           :ok
         else
           if Code.ensure_loaded?(Mix.Tasks.App.Config) do
@@ -42,4 +42,9 @@ defmodule Mix.Tasks.Sass.Install do
         """)
     end
   end
+
+  defp latest_version?() do
+    version = DartSass.configured_version()
+    match?({:ok, ^version}, DartSass.bin_version())
+  end
 end
diff --git a/test/dart_sass_test.exs b/test/dart_sass_test.exs
index c81f9ec..796296f 100644
--- a/test/dart_sass_test.exs
+++ b/test/dart_sass_test.exs
@@ -1,15 +1,35 @@
 defmodule DartSassTest do
   use ExUnit.Case, async: true
 
+  @version "1.38.1"
+
   test "run on default" do
     assert ExUnit.CaptureIO.capture_io(fn ->
              assert DartSass.run(:default, ["--version"]) == 0
-           end) =~ "1.38.1"
+           end) =~ @version
   end
 
   test "run on profile" do
     assert ExUnit.CaptureIO.capture_io(fn ->
              assert DartSass.run(:another, []) == 0
-           end) =~ "1.38.1"
+           end) =~ @version
+  end
+
+  test "updates on install" do
+    Application.put_env(:dart_sass, :version, "1.36.0")
+
+    Mix.Task.rerun("sass.install", ["--if-missing"])
+
+    assert ExUnit.CaptureIO.capture_io(fn ->
+      assert DartSass.run(:default, ["--version"]) == 0
+    end) =~ "1.36.0"
+
+    Application.delete_env(:dart_sass, :version)
+
+    Mix.Task.rerun("sass.install", ["--if-missing"])
+
+    assert ExUnit.CaptureIO.capture_io(fn ->
+      assert DartSass.run(:default, ["--version"]) == 0
+    end) =~ @version
   end
 end