2
2
require "minitest/mock"
3
3
4
4
class Tailwindcss ::CommandsTest < ActiveSupport ::TestCase
5
- test ".platform is a string containing just the cpu and os (not the version)" do
6
- expected = "#{ Gem ::Platform . local . cpu } -#{ Gem ::Platform . local . os } "
7
- assert_equal ( expected , Tailwindcss ::Commands . platform )
5
+ setup do
6
+ @orig_path = ENV [ "PATH" ]
7
+ ENV [ "PATH" ] = ""
8
+ end
9
+
10
+ teardown do
11
+ ENV [ "PATH" ] = @orig_path
8
12
end
9
13
10
14
def mock_exe_directory ( platform )
@@ -18,6 +22,25 @@ def mock_exe_directory(platform)
18
22
end
19
23
end
20
24
25
+ def mock_path_binary ( shim : false )
26
+ Dir . mktmpdir do |dir |
27
+ orig_path = ENV [ "PATH" ]
28
+ ENV [ "PATH" ] = [ dir , ENV [ "PATH" ] ] . join ( File ::PATH_SEPARATOR )
29
+ filepath = File . join ( dir , "tailwindcss" )
30
+ FileUtils . touch ( filepath )
31
+ FileUtils . chmod ( "ugo+x" , filepath )
32
+ File . write ( filepath , "#!/usr/bin/env ruby" ) if shim
33
+ yield ( dir )
34
+ ensure
35
+ ENV [ "PATH" ] = orig_path
36
+ end
37
+ end
38
+
39
+ test ".platform is a string containing just the cpu and os (not the version)" do
40
+ expected = "#{ Gem ::Platform . local . cpu } -#{ Gem ::Platform . local . os } "
41
+ assert_equal ( expected , Tailwindcss ::Commands . platform )
42
+ end
43
+
21
44
test ".executable returns the absolute path to the binary" do
22
45
mock_exe_directory ( "sparc-solaris2.8" ) do |dir , executable |
23
46
expected = File . expand_path ( File . join ( dir , "sparc-solaris2.8" , "tailwindcss" ) )
@@ -26,6 +49,31 @@ def mock_exe_directory(platform)
26
49
end
27
50
end
28
51
52
+ test "when a packaged exe is not found, .executable returns the absolute path to `tailwindcss` that is in PATH" do
53
+ mock_path_binary do |installed |
54
+ expected = File . join ( installed , "tailwindcss" )
55
+ assert_equal ( expected , Tailwindcss ::Commands . executable ( exe_path : "/blurgh" ) )
56
+ end
57
+ end
58
+
59
+ test "when a packaged exe is not found, .executable ignores shims found in PATH" do
60
+ mock_path_binary ( shim : true ) do |installed |
61
+ assert_raises ( Tailwindcss ::Commands ::ExecutableNotFoundException ) do
62
+ Tailwindcss ::Commands . executable ( exe_path : "/blurgh" )
63
+ end
64
+ end
65
+ end
66
+
67
+ test "when a packaged exe is found, .executable ignores what's in PATH" do
68
+ mock_exe_directory ( "sparc-solaris2.8" ) do |dir , executable |
69
+ mock_path_binary do |installed |
70
+ expected = File . expand_path ( File . join ( dir , "sparc-solaris2.8" , "tailwindcss" ) )
71
+ assert_equal ( expected , executable , "assert on setup" )
72
+ assert_equal ( expected , Tailwindcss ::Commands . executable ( exe_path : dir ) )
73
+ end
74
+ end
75
+ end
76
+
29
77
test ".executable raises UnsupportedPlatformException when we're not on a supported platform" do
30
78
Gem ::Platform . stub ( :match , false ) do # nothing is supported
31
79
assert_raises ( Tailwindcss ::Commands ::UnsupportedPlatformException ) do
0 commit comments