File: git_spec.rb

package info (click to toggle)
ruby-librarian 1.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 624 kB
  • sloc: ruby: 6,109; makefile: 11
file content (174 lines) | stat: -rw-r--r-- 4,973 bytes parent folder | download
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
require "fileutils"
require "pathname"
require "securerandom"

require "librarian/error"
require "librarian/posix"
require "librarian/source/git"
require "librarian/source/git/repository"
require "librarian/mock/environment"

require "support/project_path_macro"

describe Librarian::Source::Git do
  include Support::ProjectPathMacro

  let(:tmp_path) { project_path + "tmp/spec/functional/source/git" }
  after { tmp_path.rmtree if tmp_path && tmp_path.exist? }
  let(:env_project_path) { tmp_path + "project" }

  def cmd!(command)
    Librarian::Posix.run! command
  end

  def git!(command)
    cmd!([Librarian::Source::Git::Repository.bin] + command)
  end

  def new_env
    Librarian::Mock::Environment.new(:project_path => env_project_path)
  end

  context "when the remote is bad" do
    let(:remote) { tmp_path.join(SecureRandom.hex(8)).to_s }
    let(:env) { new_env }
    let(:source) { described_class.new(env, remote, {}) }

    it "fails when caching" do
      expect { source.cache! }.to raise_error Librarian::Error,
        /^fatal: repository .+ does not exist$/ # from git
    end
  end

  context "when the remote has a repo" do
    let(:remote) { tmp_path.join(SecureRandom.hex(8)).to_s }
    let(:git_source_path) { Pathname.new(remote) }
    let(:env) { new_env }
    let(:source) { described_class.new(env, remote, {}) }

    before do
      git_source_path.mkpath
      Dir.chdir(git_source_path) do
        git! %W[init]
        git! %W[config user.name Simba]
        git! %W[config user.email simba@savannah-pride.gov]
        FileUtils.touch "butter.txt"
        git! %W[add butter.txt]
        git! %W[commit -m #{"Initial Commit"} --no-gpg-sign]
      end
    end

    let(:sha) do
      Dir.chdir(git_source_path) do
        git!(%W[rev-parse master]).strip
      end
    end

    context "when caching once" do
      it "has the expected sha" do
        expect{source.cache!}.to change{source.sha}.from(nil).to(sha)
      end

      it "records the history" do
        expect{source.cache!}.to change{source.git_ops_count}.from(0).to(9)
      end
    end

    context "when caching twice" do
      before { source.cache! }

      it "keeps the expected sha" do
        expect{source.cache!}.to_not change{source.sha}
      end

      it "runs git commands once" do
        expect{source.cache!}.to_not change{source.git_ops_count}
      end
    end

    context "when caching twice from different sources" do
      let(:other_source) { described_class.new(env, remote, {}) }
      before { other_source.cache! }

      it "has the expected sha" do
        expect{source.cache!}.to change{source.sha}.from(nil).to(sha)
      end

      it "records the history" do
        expect{source.cache!}.to change{source.git_ops_count}.from(0).to(1)
      end
    end

    context "when caching twice from different sources, second time with sha" do
      let(:other_source) { described_class.new(env, remote, {}) }
      before { other_source.cache! }

      let(:source) { described_class.new(env, remote, {:sha => sha}) }

      it "has the expected sha" do
        expect{source.cache!}.to_not change{source.sha}
      end

      it "records the history" do
        expect{source.cache!}.to change{source.git_ops_count}.from(0).to(1)
      end
    end

    context "when caching twice from different environments" do
      let(:other_source) { described_class.new(new_env, remote, {}) }
      before { other_source.cache! }

      it "has the expected sha" do
        expect{source.cache!}.to change{source.sha}.from(nil).to(sha)
      end

      it "records the history" do
        expect{source.cache!}.to change{source.git_ops_count}.from(0).to(8)
      end
    end

    context "when caching twice from different environments, second time with sha" do
      let(:other_source) { described_class.new(new_env, remote, {}) }
      before { other_source.cache! }

      let(:source) { described_class.new(env, remote, {:sha => sha}) }

      it "has the expected sha" do
        expect{source.cache!}.to_not change{source.sha}
      end

      it "records the history" do
        expect{source.cache!}.to change{source.git_ops_count}.from(0).to(3)
      end
    end

    context "when the sha is missing from a cached repo" do
      let(:other_source) { described_class.new(new_env, remote, {}) }
      before { other_source.cache! }

      before do
        Dir.chdir(git_source_path) do
          FileUtils.touch "jam.txt"
          git! %w[add jam.txt]
          git! %W[commit -m #{"Some Jam"} --no-gpg-sign]
        end
      end

      let(:source) { described_class.new(env, remote, {:sha => sha}) }

      it "has a new remote sha" do
        expect(sha).to_not eq(other_source.sha)
      end

      it "has the expected sha" do
        expect{source.cache!}.to_not change{source.sha}
      end

      it "records the history" do
        expect{source.cache!}.to change{source.git_ops_count}.from(0).to(8)
      end
    end

  end

end