aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/test/diagnostic_spec.rb
blob: f347010a93c246e248b9ec65e1a3a36a1db10a86 (plain)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
require "diagnostic"

describe Homebrew::Diagnostic::Checks do
  specify "#inject_file_list" do
    expect(subject.inject_file_list([], "foo:\n")).to eq("foo:\n")
    expect(subject.inject_file_list(%w[/a /b], "foo:\n")).to eq("foo:\n  /a\n  /b\n")
  end

  specify "#check_path_for_trailing_slashes" do
    ENV["HOMEBREW_PATH"] += File::PATH_SEPARATOR + "/foo/bar/"
    expect(subject.check_path_for_trailing_slashes)
      .to match("Some directories in your path end in a slash")
  end

  specify "#check_build_from_source" do
    ENV["HOMEBREW_BUILD_FROM_SOURCE"] = "1"
    expect(subject.check_build_from_source)
      .to match("You have HOMEBREW_BUILD_FROM_SOURCE set.")
  end

  specify "#check_for_anaconda" do
    mktmpdir do |path|
      anaconda = "#{path}/anaconda"
      python = "#{path}/python"
      FileUtils.touch anaconda
      File.open(python, "w") do |file|
        file.write("#! #{`which bash`}\necho -n '#{python}'\n")
      end
      FileUtils.chmod 0755, anaconda
      FileUtils.chmod 0755, python

      ENV["PATH"] = "#{path}#{File::PATH_SEPARATOR}#{ENV["PATH"]}"

      expect(subject.check_for_anaconda).to match("Anaconda")
    end
  end

  specify "#check_access_homebrew_repository" do
    begin
      mode = HOMEBREW_REPOSITORY.stat.mode & 0777
      HOMEBREW_REPOSITORY.chmod 0555

      expect(subject.check_access_homebrew_repository)
        .to match("#{HOMEBREW_REPOSITORY} is not writable.")
    ensure
      HOMEBREW_REPOSITORY.chmod mode
    end
  end

  specify "#check_access_lock_dir" do
    begin
      prev_mode = HOMEBREW_LOCK_DIR.stat.mode
      mode = HOMEBREW_LOCK_DIR.stat.mode & 0777
      HOMEBREW_LOCK_DIR.chmod 0555
      expect(HOMEBREW_LOCK_DIR.stat.mode).not_to eq(prev_mode)

      expect(subject.check_access_lock_dir)
        .to match("#{HOMEBREW_LOCK_DIR} isn't writable.")
    ensure
      HOMEBREW_LOCK_DIR.chmod mode
    end
  end

  specify "#check_access_logs" do
    begin
      mode = HOMEBREW_LOGS.stat.mode & 0777
      HOMEBREW_LOGS.chmod 0555

      expect(subject.check_access_logs)
        .to match("#{HOMEBREW_LOGS} isn't writable.")
    ensure
      HOMEBREW_LOGS.chmod mode
    end
  end

  specify "#check_access_cache" do
    begin
      mode = HOMEBREW_CACHE.stat.mode & 0777
      HOMEBREW_CACHE.chmod 0555
      expect(subject.check_access_cache)
        .to match("#{HOMEBREW_CACHE} isn't writable.")
    ensure
      HOMEBREW_CACHE.chmod mode
    end
  end

  specify "#check_access_cellar" do
    begin
      mode = HOMEBREW_CELLAR.stat.mode & 0777
      HOMEBREW_CELLAR.chmod 0555

      expect(subject.check_access_cellar)
        .to match("#{HOMEBREW_CELLAR} isn't writable.")
    ensure
      HOMEBREW_CELLAR.chmod mode
    end
  end

  specify "#check_user_path_1" do
    bin = HOMEBREW_PREFIX/"bin"
    sep = File::PATH_SEPARATOR
    # ensure /usr/bin is before HOMEBREW_PREFIX/bin in the PATH
    ENV["PATH"] = "/usr/bin#{sep}#{bin}#{sep}" +
                  ENV["PATH"].gsub(%r{(?:^|#{sep})(?:/usr/bin|#{bin})}, "")

    # ensure there's at least one file with the same name in both /usr/bin/ and
    # HOMEBREW_PREFIX/bin/
    (bin/File.basename(Dir["/usr/bin/*"].first)).mkpath

    expect(subject.check_user_path_1)
      .to match("/usr/bin occurs before #{HOMEBREW_PREFIX}/bin")
  end

  specify "#check_user_path_2" do
    ENV["PATH"] = ENV["PATH"].gsub \
      %r{(?:^|#{File::PATH_SEPARATOR})#{HOMEBREW_PREFIX}/bin}, ""

    expect(subject.check_user_path_1).to be nil
    expect(subject.check_user_path_2)
      .to match("Homebrew's bin was not found in your PATH.")
  end

  specify "#check_user_path_3" do
    begin
      sbin = HOMEBREW_PREFIX/"sbin"
      ENV["HOMEBREW_PATH"] =
        "#{HOMEBREW_PREFIX}/bin#{File::PATH_SEPARATOR}" +
        ENV["HOMEBREW_PATH"].gsub(/(?:^|#{Regexp.escape(File::PATH_SEPARATOR)})#{Regexp.escape(sbin)}/, "")
      (sbin/"something").mkpath

      expect(subject.check_user_path_1).to be nil
      expect(subject.check_user_path_2).to be nil
      expect(subject.check_user_path_3)
        .to match("Homebrew's sbin was not found in your PATH")
    ensure
      sbin.rmtree
    end
  end

  specify "#check_user_curlrc" do
    mktmpdir do |path|
      FileUtils.touch "#{path}/.curlrc"
      ENV["CURL_HOME"] = path

      expect(subject.check_user_curlrc).to match("You have a curlrc file")
    end
  end

  specify "#check_for_config_scripts" do
    mktmpdir do |path|
      file = "#{path}/foo-config"
      FileUtils.touch file
      FileUtils.chmod 0755, file
      ENV["HOMEBREW_PATH"] =
        ENV["PATH"] =
          "#{path}#{File::PATH_SEPARATOR}#{ENV["PATH"]}"

      expect(subject.check_for_config_scripts)
        .to match('"config" scripts exist')
    end
  end

  specify "#check_dyld_vars" do
    ENV["DYLD_INSERT_LIBRARIES"] = "foo"
    expect(subject.check_dyld_vars).to match("Setting DYLD_INSERT_LIBRARIES")
  end

  specify "#check_for_symlinked_cellar" do
    begin
      HOMEBREW_CELLAR.rmtree

      mktmpdir do |path|
        FileUtils.ln_s path, HOMEBREW_CELLAR

        expect(subject.check_for_symlinked_cellar).to match(path)
      end
    ensure
      HOMEBREW_CELLAR.unlink
      HOMEBREW_CELLAR.mkpath
    end
  end

  specify "#check_tmpdir" do
    ENV["TMPDIR"] = "/i/don/t/exis/t"
    expect(subject.check_tmpdir).to match("doesn't exist")
  end

  specify "#check_for_external_cmd_name_conflict" do
    mktmpdir do |path1|
      mktmpdir do |path2|
        [path1, path2].each do |path|
          cmd = "#{path}/brew-foo"
          FileUtils.touch cmd
          FileUtils.chmod 0755, cmd
        end

        allow(Tap).to receive(:cmd_directories).and_return([path1, path2])

        expect(subject.check_for_external_cmd_name_conflict)
          .to match("brew-foo")
      end
    end
  end
end