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
|
# frozen_string_literal: true
require 'spec_helper'
require 'fileutils'
require 'tmpdir'
# rubocop:disable RSpec/MultipleMemoizedHelpers
RSpec.describe ::Gitlab::Housekeeper::Git do
let(:logger) { instance_double(Logger, info: nil) }
let(:git) { described_class.new(logger: logger) }
let(:repository_path) { Pathname(Dir.mktmpdir) }
let(:test_branch_name) { 'gitlab-housekeeper--test-branch' }
let(:file_in_master) { 'file_in_master.txt' }
let(:file_in_another_branch) { 'file_in_another_branch.txt' }
def setup_master_branch
File.write(file_in_master, 'File already in master!')
::Gitlab::Housekeeper::Shell.execute('git', 'init')
::Gitlab::Housekeeper::Shell.execute('git', 'checkout', '-b', 'master')
::Gitlab::Housekeeper::Shell.execute('git', 'add', file_in_master)
::Gitlab::Housekeeper::Shell.execute('git', 'commit', '-m', 'Initial commit!')
end
def setup_and_checkout_another_branch
::Gitlab::Housekeeper::Shell.execute('git', 'checkout', '-b', 'another-branch')
File.write(file_in_another_branch, 'File in another unrelated branch should not be in new branch!')
::Gitlab::Housekeeper::Shell.execute('git', 'add', file_in_another_branch)
::Gitlab::Housekeeper::Shell.execute('git', 'commit', '-m', 'Commit in unrelated branch should not be included')
end
before do
@previous_dir = Dir.pwd
Dir.chdir(repository_path)
# Make sure there is a master branch with something to branch from
setup_master_branch
setup_and_checkout_another_branch
end
after do
Dir.chdir(@previous_dir) if @previous_dir # rubocop:disable RSpec/InstanceVariable -- let not suitable for before/after cleanup
FileUtils.rm_rf(repository_path)
end
describe '#with_branch_from_branch and #commit_in_branch' do
let(:file_not_to_commit) { repository_path.join('test_file_not_to_commit.txt') }
let(:test_file1) { 'test_file1.txt' }
let(:test_file2) { 'files/test_file2.txt' }
it 'commits the given change details to the given branch name' do
title = "The commit title"
description = <<~COMMIT
The commit description can be
split over multiple lines!
COMMIT
identifiers = %w[GitlabHousekeeper TestBranch]
Dir.mkdir('files')
File.write(test_file1, "Content in file 1!")
File.write(test_file2, "Other content in file 2!")
File.write(file_not_to_commit, 'Do not commit!')
changed_files = [test_file1, test_file2]
change = ::Gitlab::Housekeeper::Change.new(
identifiers,
title,
description,
changed_files
)
branch_name = nil
git.with_branch_from_branch do
branch_name = git.commit_in_branch(change)
end
expect(branch_name).to eq(test_branch_name)
branches = ::Gitlab::Housekeeper::Shell.execute('git', 'branch')
expect(branches).to include(branch_name)
current_commit_on_another_branch = ::Gitlab::Housekeeper::Shell.execute('git', 'show')
expect(current_commit_on_another_branch).to include('Commit in unrelated branch should not be included')
expected = <<~COMMIT
The commit title
The commit description can be
split over multiple lines!
This commit was generated by
[gitlab-housekeeper](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/139492).
Changelog: other
diff --git a/files/test_file2.txt b/files/test_file2.txt
new file mode 100644
index 0000000..ff205e0
--- /dev/null
+++ b/files/test_file2.txt
@@ -0,0 +1 @@
+Other content in file 2!
\\ No newline at end of file
diff --git a/test_file1.txt b/test_file1.txt
new file mode 100644
index 0000000..8dd3371
--- /dev/null
+++ b/test_file1.txt
@@ -0,0 +1 @@
+Content in file 1!
\\ No newline at end of file
COMMIT
commit = ::Gitlab::Housekeeper::Shell.execute('git', 'show', branch_name).gsub(/\s/, '')
expected_without_whitespace = expected.gsub(/\s/, '')
expect(commit).to include(expected_without_whitespace)
::Gitlab::Housekeeper::Shell.execute('git', 'checkout', branch_name)
expect(File).to exist(file_in_master)
expect(File).not_to exist(file_in_another_branch)
end
end
end
# rubocop:enable RSpec/MultipleMemoizedHelpers
|