File: find_executable_spec.cr

package info (click to toggle)
crystal 1.14.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 24,384 kB
  • sloc: javascript: 6,400; sh: 695; makefile: 269; ansic: 121; python: 105; cpp: 77; xml: 32
file content (141 lines) | stat: -rw-r--r-- 3,928 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
require "spec"
require "../../support/tempfile"

{% if flag?(:win32) %}
  FIND_EXECUTABLE_TEST_FILES = {
    [
      "inbase.exe",
      "not_exe",
      ".exe",
      "inboth.exe",

      "inbasebat.bat",
      "inbase.foo.exe",
      ".inbase.exe",

      "sub/insub.exe",
      "sub/not_exe",
      "sub/.exe",

      "../path/inpath.exe",
      "../path/not_exe",
      "../path/.exe",
      "../path/inboth.exe",
    ], [] of String,
  }

  def find_executable_test_cases(pwd)
    pwd_nodrive = "\\#{pwd.relative_to(pwd.anchor.not_nil!)}"
    {
      "inbase.exe"     => "inbase.exe",
      "inbase"         => "inbase.exe",
      "sub\\insub.exe" => "sub/insub.exe",
      "sub/insub"      => "sub/insub.exe",
      "inpath.exe"     => "../path/inpath.exe",
      "inpath"         => "../path/inpath.exe",
      "sub/.exe"       => "sub/.exe",
      "sub\\"          => "sub/.exe",
      "sub/"           => "sub/.exe",
      ".exe"           => ".exe",
      "not_exe"        => nil,
      "sub\\not_exe"   => nil,
      "inbasebat"      => nil,
      "inbase.foo.exe" => "inbase.foo.exe",
      "inbase.foo"     => nil,
      ".inbase.exe"    => ".inbase.exe",
      ".inbase"        => nil,
      ""               => nil,
      "."              => nil,
      "inboth.exe"     => "inboth.exe",
      "inboth"         => "inboth.exe",
      "./inbase"       => "inbase.exe",
      "../base/inbase" => "inbase.exe",
      "./inpath"       => nil,
      "sub"            => nil,
      "#{pwd}\\sub"    => nil,
      "#{pwd}\\sub\\"  => nil,
      # 'C:\Temp\base\inbase', 'C:\Temp\base\.exe', 'C:\Temp\base\'
      "#{pwd}\\inbase" => "inbase.exe",
      "#{pwd}\\.exe"   => ".exe",
      "#{pwd}\\"       => nil,
      # 'C:inbase', 'C:.exe', 'C:'
      "#{pwd.drive}inbase" => "inbase.exe",
      "#{pwd.drive}.exe"   => ".exe",
      "#{pwd.drive}"       => nil,
      "#{pwd.drive}sub\\"  => nil,
      # '\Temp\base\inbase', '\Temp\base\.exe', '\Temp\base\'
      "#{pwd_nodrive}\\inbase" => "inbase.exe",
      "#{pwd_nodrive}\\.exe"   => ".exe",
      "#{pwd_nodrive}\\"       => nil,
    }
  end
{% else %}
  FIND_EXECUTABLE_TEST_FILES = {
    [
      "inbase",
      "sub/insub",
      "../path/inpath",
    ], [
      "not_exe",
      "sub/not_exe",
      "../path/not_exe",
    ],
  }

  def find_executable_test_cases(pwd)
    {
      "./inbase"       => "inbase",
      "../base/inbase" => "inbase",
      "inbase"         => nil,
      "sub/insub"      => "sub/insub",
      "inpath"         => "../path/inpath",
      "./inpath"       => nil,
      "inbase/"        => nil,
      "sub/insub/"     => nil,
      "./not_exe"      => nil,
      "not_exe"        => nil,
      "sub/not_exe"    => nil,
      ""               => nil,
      "."              => nil,
      "#{pwd}/inbase"  => "inbase",
      "#{pwd}/inbase/" => nil,
      "#{pwd}/sub"     => nil,
      "./sub"          => nil,
      "sub"            => nil,
    }
  end
{% end %}

describe "Process.find_executable" do
  test_dir = Path[SPEC_TEMPFILE_PATH] / "find_executable"
  base_dir = Path[test_dir] / "base"
  path_dir = Path[test_dir] / "path"

  around_all do |all|
    exe_names, non_exe_names = FIND_EXECUTABLE_TEST_FILES
    (exe_names + non_exe_names).each do |name|
      Dir.mkdir_p((base_dir / name).parent)
      File.write(base_dir / name, "")
    end
    exe_names.each do |name|
      File.chmod(base_dir / name, 0o755)
    end

    all.run

    FileUtils.rm_r(test_dir.to_s)
  end

  find_executable_test_cases(base_dir).each do |(command, exp)|
    if exp
      exp_path = File.expand_path(exp, base_dir)
      it "finds '#{command}' as '#{exp}'" do
        Process.find_executable(command, path: path_dir.to_s, pwd: base_dir).should eq exp_path
      end
    else
      it "fails to find '#{command}'" do
        Process.find_executable(command, path: path_dir.to_s, pwd: base_dir).should be_nil
      end
    end
  end
end