aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/test/test_updater.rb
blob: ec48851ae1ab5802acba7ac2275ac7d783c346c8 (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
require 'testing_env'
require 'cmd/update'
require 'yaml'

class UpdaterTests < Homebrew::TestCase
  class UpdaterMock < ::Updater
    attr_accessor :diff

    def initialize(*)
      super
      @outputs = Hash.new { |h, k| h[k] = [] }
      @expected = []
      @called = []
    end

    def in_repo_expect(cmd, output = '')
      @expected << cmd
      @outputs[cmd] << output
    end

    def `(*args)
      cmd = args.join(" ")
      if @expected.include?(cmd) and !@outputs[cmd].empty?
        @called << cmd
        @outputs[cmd].shift
      else
        raise "#{inspect} unexpectedly called backticks: `#{cmd}`"
      end
    end
    alias_method :safe_system, :`

    def expectations_met?
      @expected == @called
    end

    def inspect
      "#<#{self.class.name}>"
    end
  end

  def fixture(name)
    self.class.fixture_data[name] || ""
  end

  def self.fixture_data
    @fixture_data ||= YAML.load_file("#{TEST_DIRECTORY}/fixtures/updater_fixture.yaml")
  end

  def setup
    @updater = UpdaterMock.new(HOMEBREW_REPOSITORY)
    @report = Report.new
  end

  def teardown
    FileUtils.rm_rf HOMEBREW_LIBRARY.join("Taps")
  end

  def perform_update(fixture_name="")
    @updater.diff = fixture(fixture_name)
    @updater.in_repo_expect("git checkout -q master")
    @updater.in_repo_expect("git rev-parse -q --verify HEAD", "1234abcd")
    @updater.in_repo_expect("git config core.autocrlf false")
    @updater.in_repo_expect("git pull -q origin refs/heads/master:refs/remotes/origin/master")
    @updater.in_repo_expect("git rev-parse -q --verify HEAD", "3456cdef")
    @updater.pull!
    @report.update(@updater.report)
    assert_predicate @updater, :expectations_met?
  end

  def test_update_homebrew_without_any_changes
    perform_update
    assert_empty @report
  end

  def test_update_homebrew_without_formulae_changes
    perform_update("update_git_diff_output_without_formulae_changes")
    assert_empty @report.select_formula(:M)
    assert_empty @report.select_formula(:A)
    assert_empty @report.select_formula(:D)
  end

  def test_update_homebrew_with_formulae_changes
    perform_update("update_git_diff_output_with_formulae_changes")
    assert_equal %w{ xar yajl }, @report.select_formula(:M)
    assert_equal %w{ antiword bash-completion ddrescue dict lua }, @report.select_formula(:A)
  end

  def test_update_homebrew_with_removed_formulae
    perform_update("update_git_diff_output_with_removed_formulae")
    assert_equal %w{libgsasl}, @report.select_formula(:D)
  end

  def test_update_homebrew_with_changed_filetype
    perform_update("update_git_diff_output_with_changed_filetype")
  end

  def test_update_homebrew_with_restructured_tap
    repo = HOMEBREW_LIBRARY.join("Taps", "foo", "bar")
    @updater = UpdaterMock.new(repo)
    repo.join("Formula").mkpath

    perform_update("update_git_diff_output_with_restructured_tap")

    assert_equal %w{foo/bar/git foo/bar/lua}, @report.select_formula(:A)
    assert_empty @report.select_formula(:D)
  end

  def test_update_homebrew_simulate_homebrew_php_restructuring
    repo = HOMEBREW_LIBRARY.join("Taps", "foo", "bar")
    @updater = UpdaterMock.new(repo)
    repo.join("Formula").mkpath

    perform_update("update_git_diff_simulate_homebrew_php_restructuring")

    assert_empty @report.select_formula(:A)
    assert_equal %w{foo/bar/git foo/bar/lua}, @report.select_formula(:D)
  end

  def test_update_homebrew_with_tap_formulae_changes
    repo = HOMEBREW_LIBRARY.join("Taps", "foo", "bar")
    @updater = UpdaterMock.new(repo)
    repo.join("Formula").mkpath

    perform_update("update_git_diff_output_with_tap_formulae_changes")

    assert_equal %w{foo/bar/lua}, @report.select_formula(:A)
    assert_equal %w{foo/bar/git}, @report.select_formula(:M)
    assert_empty @report.select_formula(:D)

    assert_empty @report.removed_tapped_formula
    assert_equal [repo.join("Formula", "lua.rb")],
      @report.new_tapped_formula
    assert_equal [repo.join("Formula", "git.rb")],
      @report.tapped_formula_for(:M)
  end
end