Skip to content

Commit 58ce4a2

Browse files
committed
Added master and development branch name properties. Made specs run
1 parent f32d5af commit 58ce4a2

5 files changed

Lines changed: 127 additions & 22 deletions

File tree

‎lib/code/branch.rb‎

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@ class Branch
88
PrivateBranchError = Class.new(StandardError)
99
NoPRError = Class.new(StandardError)
1010

11-
12-
MASTER_BRANCH_NAME = 'master'
13-
DEVELOPMENT_BRANCH_NAME = 'development'
14-
PROTECTED_BRANCH_NAMES = %w{master development}
15-
1611
def self.all_names
1712
System.result('git branch').strip.lines.map { |line| line.gsub(/\s|\*/, '') }
1813
end
@@ -53,11 +48,11 @@ def self.current
5348
end
5449

5550
def self.development
56-
new DEVELOPMENT_BRANCH_NAME
51+
new Config.get_development_branch_name
5752
end
5853

5954
def self.master
60-
new MASTER_BRANCH_NAME
55+
new Config.get_master_branch_name
6156
end
6257

6358
def self.create(branch_name)
@@ -86,15 +81,18 @@ def exists?
8681
end
8782

8883
def development?
89-
name == DEVELOPMENT_BRANCH_NAME
84+
name == Config.get_development_branch_name
9085
end
9186

9287
def master?
93-
name == MASTER_BRANCH_NAME
88+
name == Config.get_master_branch_name
9489
end
9590

9691
def protected?
97-
PROTECTED_BRANCH_NAMES.include? name
92+
master_branch_name = Config.get_master_branch_name
93+
development_branch_name = Config.get_development_branch_name
94+
protected_branch_names = [ master_branch_name, development_branch_name ]
95+
protected_branch_names.include? name
9896
end
9997

10098
def feature?
@@ -149,7 +147,7 @@ def mark_prs_as_awaiting_review
149147
end
150148

151149
def mark_prs_as_hotfix
152-
label_prs('hotfix')
150+
label_prs("hotfix")
153151
end
154152

155153
def pull_request_url

‎lib/code/config.rb‎

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,58 @@
33
module Code
44

55
PROMPT_TEXTS = {
6+
master_branch: "You didn't set the name of the master branch. What is it ('master')?",
7+
development_branch: "You didn't set the name of the development branch. What is it ('development')?"
8+
}
69

10+
DEFAULTS = {
11+
master_branch: "master",
12+
development_branch: "development"
713
}
814

915
module Config
1016
extend self
1117

1218
def self.get(property)
1319
ensure_file_exists
20+
get_property_value(property)
21+
end
22+
23+
def self.get_master_branch_name
24+
self.get(:master_branch)
25+
end
26+
27+
def self.get_development_branch_name
28+
self.get(:development_branch)
29+
end
30+
31+
private
1432

33+
def ensure_file_exists
34+
FileUtils.touch ".codeconfig" unless File.exists? ".codeconfig"
35+
end
36+
37+
def get_property_value(property)
1538
config = ParseConfig.new ".codeconfig"
1639

17-
if config[property]
18-
property_value = config[property] if config[property]
40+
if config[property.to_s]
41+
property_value = config[property.to_s]
1942
else
2043
property_value = System.prompt(PROMPT_TEXTS[property] || "Unknown") unless config[property]
21-
config.add(property, property_value)
22-
file = File.open ".codeconfig", "w"
23-
config.write file
24-
file.close
44+
45+
store_property_to_config(property, property_value, config)
2546
end
47+
2648
property_value
2749
end
2850

29-
def self.ensure_file_exists
30-
FileUtils.touch ".codeconfig" unless File.exists? ".codeconfig"
51+
def store_property_to_config(property, property_value, config)
52+
config.add(property, property_value) if property_value
53+
config.add(property, DEFAULTS[property]) unless property_value
54+
55+
file = File.open ".codeconfig", "w"
56+
config.write file
57+
file.close
3158
end
3259
end
33-
end
60+
end

‎spec/code/branch_spec.rb‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require "code/branch"
22
require "code/system"
33
require "code/pull_request"
4+
require "code/config"
45

56
module Code
67
describe Branch do
@@ -10,6 +11,8 @@ module Code
1011
end
1112

1213
before do
14+
allow(Config).to receive(:get_master_branch_name).and_return "master"
15+
allow(Config).to receive(:get_development_branch_name).and_return "development"
1316
allow(System).to receive(:puts)
1417
Branch.setup_test_repo
1518
end

‎spec/code/config_spec.rb‎

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ module Code
4242
end
4343

4444
context "when config property does exist" do
45-
with_file_and_content(".codeconfig", "property = value")
45+
with_file_and_content(".codeconfig", "property = value\n")
4646

4747
it "should not prompt the user for the property value" do
4848
expect(System).not_to receive(:prompt)
@@ -54,7 +54,80 @@ module Code
5454
end
5555
end
5656
end
57+
end
58+
59+
describe "get_master_branch_name" do
60+
context "when there is no previously stored property value" do
61+
with_file_and_content(".codeconfig", "")
62+
let(:prompt_text) { "You didn't set the name of the master branch. What is it ('master')?" }
63+
64+
it "asks the user for the property value" do
65+
expect(System).to receive(:prompt).with prompt_text
66+
Config.get_master_branch_name
67+
end
68+
69+
it "should return the value the user has provided" do
70+
allow(System).to receive(:prompt).with(prompt_text).and_return("test-value")
71+
72+
expect(Config.get_master_branch_name).to eq ("test-value")
73+
end
74+
75+
it "should store the provided value into the file" do
76+
allow(System).to receive(:prompt).with(prompt_text).and_return("test-value")
77+
Config.get_master_branch_name
78+
file_content = File.read ".codeconfig"
79+
expect(file_content.strip).to eq "master_branch = \"test-value\""
80+
end
81+
end
82+
83+
context "when there is a previously stored property value" do
84+
with_file_and_content(".codeconfig", "master_branch = \"value\"\n")
85+
it "should not prompt the user for the property value" do
86+
expect(System).not_to receive(:prompt)
87+
Config.get_master_branch_name
88+
end
5789

90+
it "should return the stored property value" do
91+
expect(Config.get_master_branch_name).to eq "value"
92+
end
93+
end
94+
end
95+
96+
describe "get_development_branch_name" do
97+
context "when there is no previously stored property value" do
98+
with_file_and_content(".codeconfig", "")
99+
let(:prompt_text) { "You didn't set the name of the development branch. What is it ('development')?" }
100+
101+
it "asks the user for the property value" do
102+
expect(System).to receive(:prompt).with prompt_text
103+
Config.get_development_branch_name
104+
end
105+
106+
it "should return the value the user has provided" do
107+
allow(System).to receive(:prompt).with(prompt_text).and_return("test-value")
108+
109+
expect(Config.get_development_branch_name).to eq ("test-value")
110+
end
111+
112+
it "should store the provided value into the file" do
113+
allow(System).to receive(:prompt).with(prompt_text).and_return("test-value")
114+
Config.get_development_branch_name
115+
file_content = File.read ".codeconfig"
116+
expect(file_content.strip).to eq "development_branch = \"test-value\""
117+
end
118+
end
119+
120+
context "when there is a previously stored property value" do
121+
with_file_and_content(".codeconfig", "development_branch = \"value\"\n")
122+
it "should not prompt the user for the property value" do
123+
expect(System).not_to receive(:prompt)
124+
Config.get_development_branch_name
125+
end
126+
127+
it "should return the stored property value" do
128+
expect(Config.get_development_branch_name).to eq "value"
129+
end
130+
end
58131
end
59132
end
60-
end
133+
end

‎spec/code/git_spec.rb‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require 'code/git'
22
require 'code/branch'
33
require 'code/system'
4+
require "code/config"
45

56
require_relative '../support/git_extras'
67

@@ -17,6 +18,9 @@ module Code
1718
end
1819

1920
before do
21+
allow(Config).to receive(:get_master_branch_name).and_return "master"
22+
allow(Config).to receive(:get_development_branch_name).and_return "development"
23+
2024
allow(System).to receive(:puts)
2125
Git.setup_test_repo
2226
end

0 commit comments

Comments
 (0)