|
1 | 1 | # Helper indices for commits:
|
2 |
| -# git_repo.commits[0] => 'add railties dir' (initial commit) |
3 |
| -# git_repo.commits[1] => 'commit1' |
4 |
| -# git_repo.commits.last => 'commit2' |
| 2 | +# @git_repo.commits[0] => 'add railties dir' (initial commit) |
| 3 | +# @git_repo.commits[1] => 'commit1' |
| 4 | +# @git_repo.commits.last => 'commit2' |
5 | 5 |
|
6 | 6 | require "rails/diff/rails_repo"
|
7 | 7 | require "fileutils"
|
|
11 | 11 | let(:logger) { spy }
|
12 | 12 | let(:cache_dir) { Dir.mktmpdir }
|
13 | 13 | let(:rails_path) { File.join(cache_dir, "rails") }
|
14 |
| - let(:git_repo) do |
15 |
| - GitRepo.new.tap do |repo| |
| 14 | + |
| 15 | + before(:all) do |
| 16 | + @git_repo = GitRepo.new.tap do |repo| |
16 | 17 | repo.add_commit("commit1")
|
17 | 18 | repo.add_commit("commit2")
|
18 | 19 | end
|
19 | 20 | end
|
20 | 21 |
|
21 | 22 | after do
|
22 | 23 | FileUtils.rm_rf(cache_dir)
|
23 |
| - git_repo.cleanup |
| 24 | + @git_repo.cleanup |
24 | 25 | end
|
25 | 26 |
|
26 | 27 | describe "#up_to_date?" do
|
27 | 28 | it "returns false when repo is not cloned yet" do
|
28 |
| - repo = described_class.new(logger:, cache_dir:, rails_repo: git_repo.remote_repo) |
| 29 | + repo = described_class.new(logger:, cache_dir:, rails_repo: @git_repo.remote_repo) |
29 | 30 |
|
30 | 31 | result = repo.up_to_date?
|
31 | 32 |
|
32 | 33 | expect(result).to eq false
|
33 | 34 | end
|
34 | 35 |
|
35 | 36 | it "returns true when repo is on the latest commit" do
|
36 |
| - git_repo.clone_at_commit(git_repo.commits.last, rails_path) |
37 |
| - repo = described_class.new(logger:, cache_dir:, rails_repo: git_repo.remote_repo) |
| 37 | + @git_repo.clone_at_commit(@git_repo.commits.last, rails_path) |
| 38 | + repo = described_class.new(logger:, cache_dir:, rails_repo: @git_repo.remote_repo) |
38 | 39 |
|
39 | 40 | result = repo.up_to_date?
|
40 | 41 |
|
41 | 42 | expect(result).to eq true
|
42 | 43 | end
|
43 | 44 |
|
44 | 45 | it "returns false and removes the repo when repo is on an old commit" do
|
45 |
| - git_repo.clone_at_commit(git_repo.commits.first, rails_path) |
46 |
| - repo = described_class.new(logger:, cache_dir:, rails_repo: git_repo.remote_repo) |
| 46 | + @git_repo.clone_at_commit(@git_repo.commits.first, rails_path) |
| 47 | + repo = described_class.new(logger:, cache_dir:, rails_repo: @git_repo.remote_repo) |
47 | 48 |
|
48 | 49 | result = repo.up_to_date?
|
49 | 50 |
|
|
54 | 55 |
|
55 | 56 | describe "#latest_commit" do
|
56 | 57 | it "returns the latest commit SHA from the remote repo" do
|
57 |
| - repo = described_class.new(logger:, cache_dir:, rails_repo: git_repo.remote_repo) |
| 58 | + repo = described_class.new(logger:, cache_dir:, rails_repo: @git_repo.remote_repo) |
58 | 59 |
|
59 | 60 | latest = repo.latest_commit
|
60 | 61 |
|
61 |
| - expect(latest).to eq git_repo.commits.last |
| 62 | + expect(latest).to eq @git_repo.commits.last |
62 | 63 | end
|
63 | 64 |
|
64 | 65 | it "returns the new latest commit after a new commit is pushed" do
|
65 |
| - new_commit = git_repo.add_commit("commit3") |
66 |
| - repo = described_class.new(logger:, cache_dir:, rails_repo: git_repo.remote_repo) |
| 66 | + new_commit = @git_repo.add_commit("commit3") |
| 67 | + repo = described_class.new(logger:, cache_dir:, rails_repo: @git_repo.remote_repo) |
67 | 68 |
|
68 | 69 | latest = repo.latest_commit
|
69 | 70 |
|
|
73 | 74 |
|
74 | 75 | describe "#checkout" do
|
75 | 76 | it "checks out the given commit in the repo" do
|
76 |
| - git_repo.clone_at_commit(git_repo.commits.last, rails_path) |
77 |
| - repo = described_class.new(logger:, cache_dir:, rails_repo: git_repo.remote_repo) |
| 77 | + @git_repo.clone_at_commit(@git_repo.commits.last, rails_path) |
| 78 | + repo = described_class.new(logger:, cache_dir:, rails_repo: @git_repo.remote_repo) |
78 | 79 |
|
79 | 80 | # Checkout the first file commit (not the initial railties commit)
|
80 |
| - repo.checkout(git_repo.commits[1]) |
| 81 | + repo.checkout(@git_repo.commits[1]) |
81 | 82 |
|
82 | 83 | # Verify HEAD is now at the first file commit
|
83 | 84 | current = Dir.chdir(rails_path) { `git rev-parse HEAD`.strip }
|
84 |
| - expect(current).to eq git_repo.commits[1] |
| 85 | + expect(current).to eq @git_repo.commits[1] |
85 | 86 | end
|
86 | 87 |
|
87 | 88 | it "logs the checkout info message" do
|
88 |
| - git_repo.clone_at_commit(git_repo.commits.last, rails_path) |
89 |
| - repo = described_class.new(logger:, cache_dir:, rails_repo: git_repo.remote_repo) |
| 89 | + @git_repo.clone_at_commit(@git_repo.commits.last, rails_path) |
| 90 | + repo = described_class.new(logger:, cache_dir:, rails_repo: @git_repo.remote_repo) |
90 | 91 |
|
91 |
| - repo.checkout(git_repo.commits[1]) |
| 92 | + repo.checkout(@git_repo.commits[1]) |
92 | 93 |
|
93 | 94 | expect(logger).to have_received(:info)
|
94 |
| - .with(/Checking out Rails \(at commit #{git_repo.commits[1][0..6]}\)/) |
| 95 | + .with(/Checking out Rails \(at commit #{@git_repo.commits[1][0..6]}\)/) |
95 | 96 | end
|
96 | 97 | end
|
97 | 98 |
|
98 | 99 | describe "#install_dependencies" do
|
99 | 100 | it "runs bundle check and bundle install if needed, and logs appropriately" do
|
100 |
| - git_repo.clone_at_commit(git_repo.commits[0], rails_path) |
101 |
| - repo = described_class.new(logger:, cache_dir:, rails_repo: git_repo.remote_repo) |
| 101 | + @git_repo.clone_at_commit(@git_repo.commits[0], rails_path) |
| 102 | + repo = described_class.new(logger:, cache_dir:, rails_repo: @git_repo.remote_repo) |
102 | 103 |
|
103 | 104 | # Simulate bundle check failing, so bundle install is needed
|
104 | 105 | allow(Rails::Diff).to receive(:system!).with("bundle check", abort: false, logger: logger).and_return(false)
|
|
111 | 112 | end
|
112 | 113 |
|
113 | 114 | it "does not run bundle install if bundle check passes" do
|
114 |
| - git_repo.clone_at_commit(git_repo.commits[0], rails_path) |
115 |
| - repo = described_class.new(logger:, cache_dir:, rails_repo: git_repo.remote_repo) |
| 115 | + @git_repo.clone_at_commit(@git_repo.commits[0], rails_path) |
| 116 | + repo = described_class.new(logger:, cache_dir:, rails_repo: @git_repo.remote_repo) |
116 | 117 |
|
117 | 118 | # Simulate bundle check passing
|
118 | 119 | allow(Rails::Diff).to receive(:system!).with("bundle check", abort: false, logger: logger).and_return(true)
|
|
125 | 126 |
|
126 | 127 | describe "#new_app" do
|
127 | 128 | it "runs the rails new command with the correct arguments and logs the command" do
|
128 |
| - git_repo.clone_at_commit(git_repo.commits.last, rails_path) |
129 |
| - repo = described_class.new(logger:, cache_dir:, rails_repo: git_repo.remote_repo) |
| 129 | + @git_repo.clone_at_commit(@git_repo.commits.last, rails_path) |
| 130 | + repo = described_class.new(logger:, cache_dir:, rails_repo: @git_repo.remote_repo) |
130 | 131 |
|
131 | 132 | allow(Rails::Diff).to receive(:system!).and_return(true)
|
132 | 133 | app_name = "myapp"
|
|
0 commit comments