|
| 1 | +require "utils/git" |
| 2 | + |
| 3 | +describe Git do |
| 4 | + before(:each) do |
| 5 | + git = HOMEBREW_SHIMS_PATH/"scm/git" |
| 6 | + |
| 7 | + HOMEBREW_CACHE.cd do |
| 8 | + system git, "init" |
| 9 | + |
| 10 | + File.open(file, "w") { |f| f.write("blah") } |
| 11 | + system git, "add", HOMEBREW_CACHE/file |
| 12 | + system git, "commit", "-m", "'File added'" |
| 13 | + @h1 = `git rev-parse HEAD` |
| 14 | + |
| 15 | + File.open(file, "w") { |f| f.write("brew") } |
| 16 | + system git, "add", HOMEBREW_CACHE/file |
| 17 | + system git, "commit", "-m", "'written to File'" |
| 18 | + @h2 = `git rev-parse HEAD` |
| 19 | + end |
| 20 | + end |
| 21 | + |
| 22 | + let(:file) { "blah.rb" } |
| 23 | + let(:hash1) { @h1[0..6] } |
| 24 | + let(:hash2) { @h2[0..6] } |
| 25 | + |
| 26 | + describe "#last_revision_commit_of_file" do |
| 27 | + it "gives last revision commit when before_commit is nil" do |
| 28 | + expect( |
| 29 | + described_class.last_revision_commit_of_file(HOMEBREW_CACHE, file), |
| 30 | + ).to eq(hash1) |
| 31 | + end |
| 32 | + |
| 33 | + it "gives revision commit based on before_commit when it is not nil" do |
| 34 | + expect( |
| 35 | + described_class.last_revision_commit_of_file(HOMEBREW_CACHE, |
| 36 | + file, |
| 37 | + before_commit: hash2), |
| 38 | + ).to eq(hash2) |
| 39 | + end |
| 40 | + end |
| 41 | + |
| 42 | + describe "#last_revision_of_file" do |
| 43 | + it "returns last revision of file" do |
| 44 | + expect( |
| 45 | + described_class.last_revision_of_file(HOMEBREW_CACHE, |
| 46 | + HOMEBREW_CACHE/file), |
| 47 | + ).to eq("blah") |
| 48 | + end |
| 49 | + |
| 50 | + it "returns last revision of file based on before_commit" do |
| 51 | + expect( |
| 52 | + described_class.last_revision_of_file(HOMEBREW_CACHE, HOMEBREW_CACHE/file, |
| 53 | + before_commit: "0..3"), |
| 54 | + ).to eq("brew") |
| 55 | + end |
| 56 | + end |
| 57 | +end |
| 58 | + |
| 59 | +describe Utils do |
| 60 | + before(:each) do |
| 61 | + described_class.clear_git_available_cache |
| 62 | + end |
| 63 | + |
| 64 | + describe "::git_available?" do |
| 65 | + it "returns true if git --version command succeeds" do |
| 66 | + expect(described_class.git_available?).to be_truthy |
| 67 | + end |
| 68 | + |
| 69 | + it "returns false if git --version command does not succeed" do |
| 70 | + stub_const("HOMEBREW_SHIMS_PATH", HOMEBREW_PREFIX/"bin/shim") |
| 71 | + expect(described_class.git_available?).to be_falsey |
| 72 | + end |
| 73 | + end |
| 74 | + |
| 75 | + describe "::git_path" do |
| 76 | + it "returns nil when git is not available" do |
| 77 | + stub_const("HOMEBREW_SHIMS_PATH", HOMEBREW_PREFIX/"bin/shim") |
| 78 | + expect(described_class.git_path).to eq(nil) |
| 79 | + end |
| 80 | + |
| 81 | + it "returns path of git when git is available" do |
| 82 | + expect(described_class.git_path).to end_with("git") |
| 83 | + end |
| 84 | + end |
| 85 | + |
| 86 | + describe "::git_version" do |
| 87 | + it "returns nil when git is not available" do |
| 88 | + stub_const("HOMEBREW_SHIMS_PATH", HOMEBREW_PREFIX/"bin/shim") |
| 89 | + expect(described_class.git_path).to eq(nil) |
| 90 | + end |
| 91 | + |
| 92 | + it "returns version of git when git is available" do |
| 93 | + expect(described_class.git_version).not_to be_nil |
| 94 | + end |
| 95 | + end |
| 96 | + |
| 97 | + describe "::ensure_git_installed!" do |
| 98 | + it "returns nil if git already available" do |
| 99 | + expect(described_class.ensure_git_installed!).to be_nil |
| 100 | + end |
| 101 | + |
| 102 | + context "when git is not already available" do |
| 103 | + before do |
| 104 | + stub_const("HOMEBREW_SHIMS_PATH", HOMEBREW_PREFIX/"bin/shim") |
| 105 | + end |
| 106 | + |
| 107 | + it "can't install brewed git if homebrew/core is unavailable" do |
| 108 | + allow_any_instance_of(Pathname).to receive(:directory?).and_return(false) |
| 109 | + expect { described_class.ensure_git_installed! }.to raise_error("Git is unavailable") |
| 110 | + end |
| 111 | + |
| 112 | + it "raises error if can't install git" do |
| 113 | + stub_const("HOMEBREW_BREW_FILE", HOMEBREW_PREFIX/"bin/brew") |
| 114 | + expect { described_class.ensure_git_installed! }.to raise_error("Git is unavailable") |
| 115 | + end |
| 116 | + |
| 117 | + it "installs git" do |
| 118 | + allow(Homebrew).to receive(:_system).with(any_args).and_return(true) |
| 119 | + described_class.ensure_git_installed! |
| 120 | + end |
| 121 | + end |
| 122 | + end |
| 123 | + |
| 124 | + describe "::git_remote_exists" do |
| 125 | + it "returns true when git is not available" do |
| 126 | + stub_const("HOMEBREW_SHIMS_PATH", HOMEBREW_PREFIX/"bin/shim") |
| 127 | + expect(described_class.git_remote_exists("blah")).to be_truthy |
| 128 | + end |
| 129 | + |
| 130 | + context "when git is available" do |
| 131 | + it "returns true when git remote exists", :needs_network do |
| 132 | + git = HOMEBREW_SHIMS_PATH/"scm/git" |
| 133 | + url = "http://github.com/Homebrew/homebrew.github.io" |
| 134 | + repo = HOMEBREW_CACHE/"hey" |
| 135 | + repo.mkpath |
| 136 | + |
| 137 | + repo.cd do |
| 138 | + system git, "init" |
| 139 | + system git, "remote", "add", "origin", url |
| 140 | + end |
| 141 | + |
| 142 | + expect(described_class.git_remote_exists(url)).to be_truthy |
| 143 | + end |
| 144 | + |
| 145 | + it "returns false when git remote does not exist" do |
| 146 | + expect(described_class.git_remote_exists("blah")).to be_falsey |
| 147 | + end |
| 148 | + end |
| 149 | + end |
| 150 | +end |
0 commit comments