aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/dev-cmd/update-test.rb
blob: 1f8d0510ba12a64a4213dd707046d2073865e356 (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
#: @hide_from_man_page
#:  * `update-test` [`--commit=<sha1>`] [`--before=<date>`] [`--keep-tmp`]:
#:    Runs a test of `brew update` with a new repository clone.
#:
#:    If no arguments are passed, use `origin/master` as the start commit.
#:
#:    If `--commit=<sha1>` is passed, use `<sha1>` as the start commit.
#:
#:    If `--before=<date>` is passed, use the commit at `<date>` as the
#:    start commit.
#:
#:    If `--keep-tmp` is passed, retain the temporary directory containing
#:    the new repository clone.

module Homebrew
  def update_test
    cd HOMEBREW_REPOSITORY
    start_sha1 = if commit = ARGV.value("commit")
      commit
    elsif date = ARGV.value("before")
      Utils.popen_read("git", "rev-list", "-n1", "--before=#{date}", "origin/master").chomp
    else
      Utils.popen_read("git", "rev-parse", "origin/master").chomp
    end
    start_sha1 = Utils.popen_read("git", "rev-parse", start_sha1).chomp
    end_sha1 = Utils.popen_read("git", "rev-parse", "HEAD").chomp

    puts "Start commit: #{start_sha1}"
    puts "End   commit: #{end_sha1}"

    mktemp("update-test") do |staging|
      staging.retain! if ARGV.keep_tmp?
      curdir = Pathname.new(Dir.pwd)

      oh1 "Setup test environment..."
      # copy Homebrew installation
      safe_system "git", "clone", "--local", "#{HOMEBREW_REPOSITORY}/.git", "."

      # set git origin to another copy
      safe_system "git", "clone", "--local", "--bare", "#{HOMEBREW_REPOSITORY}/.git", "remote.git"
      safe_system "git", "config", "remote.origin.url", "#{curdir}/remote.git"

      # force push origin to end_sha1
      safe_system "git", "checkout", "--force", "master"
      safe_system "git", "reset", "--hard", end_sha1
      safe_system "git", "push", "--force", "origin", "master"

      # set test copy to start_sha1
      safe_system "git", "reset", "--hard", start_sha1

      # update ENV["PATH"]
      ENV["PATH"] = "#{curdir}/bin:/usr/local/bin:/usr/bin:/bin"

      # run brew update
      oh1 "Running brew update..."
      safe_system "brew", "update", "--verbose"
      actual_end_sha1 = Utils.popen_read("git", "rev-parse", "master").chomp
      if start_sha1 != end_sha1 && start_sha1 == actual_end_sha1
        raise <<-EOS.undent
          brew update didn't update master!
          Start commit:        #{start_sha1}
          Expected end commit: #{end_sha1}
          Actual end commit:   #{actual_end_sha1}
        EOS
      end
    end
  end
end