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
|
abort if ARGV.include? "--skip-update"
require 'testing_env'
HOMEBREW_CELLAR.mkpath
require 'extend/ARGV' # needs to be after test/unit to avoid conflict with OptionsParser
ARGV.extend(HomebrewArgvExtension)
require 'formula'
require 'utils'
require 'cmd/update'
class RefreshBrewMock < RefreshBrew
def git_repo?
@git_repo
end
attr_writer :git_repo
def in_prefix_expect(cmd, output = '')
@outputs ||= Hash.new { |h,k| h[k] = [] }
@expected ||= []
@expected << cmd
@outputs[cmd] << output
end
def `(cmd)
if Dir.pwd == HOMEBREW_PREFIX.to_s and @expected.include?(cmd) and !@outputs[cmd].empty?
@called ||= []
@called << cmd
@outputs[cmd].shift
else
raise "#{inspect} Unexpectedly called backticks in pwd `#{HOMEBREW_PREFIX}' and command `#{cmd}'"
end
end
alias safe_system `
def expectations_met?
@expected == @called
end
def inspect
"#<#{self.class.name} #{object_id}>"
end
end
class UpdaterTests < Test::Unit::TestCase
OUTSIDE_PREFIX = '/tmp'
def outside_prefix
Dir.chdir(OUTSIDE_PREFIX) { yield }
end
def fixture(name)
self.class.fixture_data[name]
end
def self.fixture_data
unless @fixture_data
require 'yaml'
@fixture_data = YAML.load_file(Pathname.new(ABS__FILE__).parent.realpath + 'fixtures/updater_fixture.yaml')
end
@fixture_data
end
def test_init_homebrew
outside_prefix do
updater = RefreshBrewMock.new
updater.git_repo = false
updater.in_prefix_expect("git init")
updater.in_prefix_expect("git remote add origin #{RefreshBrewMock::REPOSITORY_URL}")
updater.in_prefix_expect("git fetch origin")
updater.in_prefix_expect("git reset --hard origin/master")
updater.in_prefix_expect("git pull origin refs/heads/master:refs/remotes/origin/master")
updater.in_prefix_expect("git rev-parse HEAD", "1234abcd")
assert_equal false, updater.update_from_masterbrew!
assert updater.expectations_met?
assert updater.updated_formulae.empty?
assert updater.added_formulae.empty?
end
end
def test_update_homebrew_without_any_changes
outside_prefix do
updater = RefreshBrewMock.new
updater.git_repo = true
updater.in_prefix_expect("git checkout -q master")
updater.in_prefix_expect("git rev-parse HEAD", "1234abcd")
updater.in_prefix_expect("git remote", "origin")
updater.in_prefix_expect("git pull origin refs/heads/master:refs/remotes/origin/master")
updater.in_prefix_expect("git rev-parse HEAD", "3456cdef")
updater.in_prefix_expect("git diff-tree -r --name-status -z 1234abcd 3456cdef", "")
assert_equal false, updater.update_from_masterbrew!
assert updater.expectations_met?
assert updater.updated_formulae.empty?
assert updater.added_formulae.empty?
end
end
def test_update_homebrew_without_formulae_changes
outside_prefix do
updater = RefreshBrewMock.new
updater.git_repo = true
diff_output = fixture('update_git_diff_output_without_formulae_changes')
updater.in_prefix_expect("git checkout -q master")
updater.in_prefix_expect("git rev-parse HEAD", "1234abcd")
updater.in_prefix_expect("git remote", "origin")
updater.in_prefix_expect("git pull origin refs/heads/master:refs/remotes/origin/master")
updater.in_prefix_expect("git rev-parse HEAD", "3456cdef")
updater.in_prefix_expect("git diff-tree -r --name-status -z 1234abcd 3456cdef", diff_output.gsub(/\s+/, "\0"))
assert_equal true, updater.update_from_masterbrew!
assert updater.expectations_met?
assert !updater.pending_formulae_changes?
assert updater.updated_formulae.empty?
assert updater.added_formulae.empty?
end
end
def test_update_homebrew_with_formulae_changes
outside_prefix do
updater = RefreshBrewMock.new
updater.git_repo = true
diff_output = fixture('update_git_diff_output_with_formulae_changes')
updater.in_prefix_expect("git checkout -q master")
updater.in_prefix_expect("git rev-parse HEAD", "1234abcd")
updater.in_prefix_expect("git remote", "origin")
updater.in_prefix_expect("git pull origin refs/heads/master:refs/remotes/origin/master")
updater.in_prefix_expect("git rev-parse HEAD", "3456cdef")
updater.in_prefix_expect("git diff-tree -r --name-status -z 1234abcd 3456cdef", diff_output.gsub(/\s+/, "\0"))
assert_equal true, updater.update_from_masterbrew!
assert updater.expectations_met?
assert updater.pending_formulae_changes?
assert_equal %w{ xar yajl }, updater.updated_formulae
assert_equal %w{ antiword bash-completion ddrescue dict lua }, updater.added_formulae
end
end
end
|