Class: Homebrew::DevCmd::UpdateTest Private

Inherits:
AbstractCommand show all
Includes:
FileUtils
Defined in:
extend/os/linux/dev-cmd/update-test.rb,
dev-cmd/update-test.rb,
sorbet/rbi/dsl/homebrew/dev_cmd/update_test.rbi

This class is part of a private API. This class may only be used in the Homebrew/brew repository. Third parties should avoid using this class if possible, as it may be removed or changed without warning.

Defined Under Namespace

Classes: Args

Instance Method Summary collapse

Methods inherited from AbstractCommand

command, command_name, dev_cmd?, #initialize, parser, ruby_cmd?

Constructor Details

This class inherits a constructor from Homebrew::AbstractCommand

Instance Method Details

#argsHomebrew::DevCmd::UpdateTest::Args

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



10
# File 'sorbet/rbi/dsl/homebrew/dev_cmd/update_test.rbi', line 10

def args; end

#generic_git_tagsObject

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



7
# File 'extend/os/linux/dev-cmd/update-test.rb', line 7

alias generic_git_tags git_tags

#runvoid

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.



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
# File 'dev-cmd/update-test.rb', line 30

def run
  # Avoid `update-report.rb` tapping Homebrew/homebrew-core
  ENV["HOMEBREW_UPDATE_TEST"] = "1"

  # Avoid accidentally updating when we don't expect it.
  ENV["HOMEBREW_NO_AUTO_UPDATE"] = "1"

  # Use default behaviours
  ENV["HOMEBREW_AUTO_UPDATE_SECS"] = nil
  ENV["HOMEBREW_DEVELOPER"] = nil
  ENV["HOMEBREW_DEV_CMD_RUN"] = nil
  ENV["HOMEBREW_MERGE"] = nil
  ENV["HOMEBREW_NO_UPDATE_CLEANUP"] = nil
  ENV["HOMEBREW_UPDATE_TO_TAG"] = nil

  branch = if args.to_tag?
    ENV["HOMEBREW_UPDATE_TO_TAG"] = "1"
    "stable"
  else
    ENV["HOMEBREW_DEV_CMD_RUN"] = "1"
    "master"
  end

  # Utils.popen_read returns a String without a block argument, but that isn't easily typed. We thus label this
  # as untyped for now.
  start_commit = T.let("", T.untyped)
  end_commit = "HEAD"
  cd HOMEBREW_REPOSITORY do
    start_commit = if (commit = args.commit)
      commit
    elsif (date = args.before)
      Utils.popen_read("git", "rev-list", "-n1", "--before=#{date}", "origin/master").chomp
    elsif args.to_tag?
      tags = git_tags
      current_tag, previous_tag, = tags.lines
      current_tag = current_tag.to_s.chomp
      odie "Could not find current tag in:\n#{tags}" if current_tag.empty?
      # ^0 ensures this points to the commit rather than the tag object.
      end_commit = "#{current_tag}^0"

      previous_tag = previous_tag.to_s.chomp
      odie "Could not find previous tag in:\n#{tags}" if previous_tag.empty?
      # ^0 ensures this points to the commit rather than the tag object.
      "#{previous_tag}^0"
    else
      Utils.popen_read("git", "merge-base", "origin/master", end_commit).chomp
    end
    odie "Could not find start commit!" if start_commit.empty?

    start_commit = Utils.popen_read("git", "rev-parse", start_commit).chomp
    odie "Could not find start commit!" if start_commit.empty?

    end_commit = T.cast(Utils.popen_read("git", "rev-parse", end_commit).chomp, String)
    odie "Could not find end commit!" if end_commit.empty?

    if Utils.popen_read("git", "branch", "--list", "master").blank?
      safe_system "git", "branch", "master", "origin/master"
    end
  end

  puts <<~EOS
    Start commit: #{start_commit}
      End commit: #{end_commit}
  EOS

  mkdir "update-test"
  chdir "update-test" do
    curdir = Pathname.new(Dir.pwd)

    oh1 "Preparing test environment..."
    # copy Homebrew installation
    safe_system "git", "clone", "#{HOMEBREW_REPOSITORY}/.git", ".",
                "--branch", "master", "--single-branch"

    # set git origin to another copy
    safe_system "git", "clone", "#{HOMEBREW_REPOSITORY}/.git", "remote.git",
                "--bare", "--branch", "master", "--single-branch"
    safe_system "git", "config", "remote.origin.url", "#{curdir}/remote.git"
    ENV["HOMEBREW_BREW_GIT_REMOTE"] = "#{curdir}/remote.git"

    # force push origin to end_commit
    safe_system "git", "checkout", "-B", "master", end_commit
    safe_system "git", "push", "--force", "origin", "master"

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

    # update ENV["PATH"]
    ENV["PATH"] = PATH.new(ENV.fetch("PATH")).prepend(curdir/"bin").to_s

    # Run `brew help` to install `portable-ruby` (if needed).
    quiet_system "brew", "help"

    # run brew update
    oh1 "Running `brew update`..."
    safe_system "brew", "update", "--verbose", "--debug"
    actual_end_commit = Utils.popen_read("git", "rev-parse", branch).chomp
    if actual_end_commit != end_commit
      start_log = Utils.popen_read("git", "log", "-1", "--decorate", "--oneline", start_commit).chomp
      end_log = Utils.popen_read("git", "log", "-1", "--decorate", "--oneline", end_commit).chomp
      actual_log = Utils.popen_read("git", "log", "-1", "--decorate", "--oneline", actual_end_commit).chomp
      odie <<~EOS
        `brew update` didn't update #{branch}!
        Start commit:        #{start_log}
        Expected end commit: #{end_log}
        Actual end commit:   #{actual_log}
      EOS
    end
  end
ensure
  FileUtils.rm_rf "update-test" unless args.keep_tmp?
end