-
Notifications
You must be signed in to change notification settings - Fork 352
/
name_spec.rb
163 lines (140 loc) · 4.97 KB
/
name_spec.rb
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
require 'spec_helper'
require 'r10k/environment/name'
describe R10K::Environment::Name do
describe "strip_component" do
it "does not modify the given name when no strip_component is given" do
bn = described_class.new('myenv', source: 'source', prefix: false)
expect(bn.dirname).to eq 'myenv'
end
it "removes the first occurance of a regex match when a regex is given" do
bn = described_class.new('myenv', source: 'source', prefix: false, strip_component: '/env/')
expect(bn.dirname).to eq 'my'
end
it "does not modify the given name when there is no regex match" do
bn = described_class.new('myenv', source: 'source', prefix: false, strip_component: '/bar/')
expect(bn.dirname).to eq 'myenv'
end
it "removes the given name's prefix when it matches strip_component" do
bn = described_class.new('env/prod', source: 'source', prefix: false, strip_component: 'env/')
expect(bn.dirname).to eq 'prod'
end
it "raises an error when given an integer" do
expect {
described_class.new('env/prod', source: 'source', prefix: false, strip_component: 4)
}.to raise_error(%r{Improper.*"4"})
end
end
describe "prefixing" do
it "uses the branch name as the dirname when prefixing is off" do
bn = described_class.new('mybranch', :source => 'source', :prefix => false)
expect(bn.dirname).to eq 'mybranch'
end
it "prepends the source name when prefixing is on" do
bn = described_class.new('mybranch', :source => 'source', :prefix => true)
expect(bn.dirname).to eq 'source_mybranch'
end
it "prepends the prefix name when prefixing is overridden" do
bn = described_class.new('mybranch', {:prefix => "bar", :sourcename => 'foo'})
expect(bn.dirname).to eq 'bar_mybranch'
end
it "uses the branch name as the dirname when prefixing is nil" do
bn = described_class.new('mybranch', {:prefix => nil, :sourcename => 'foo'})
expect(bn.dirname).to eq 'mybranch'
end
end
describe "determining the validate behavior with :invalid" do
[
['correct_and_warn', {:validate => true, :correct => true}],
['correct', {:validate => false, :correct => true}],
['error', {:validate => true, :correct => false}],
].each do |(setting, outcome)|
it "treats #{setting} as #{outcome.inspect}" do
bn = described_class.new('mybranch', :source => 'source', :invalid => setting)
expect(bn.validate?).to eq outcome[:validate]
expect(bn.correct?).to eq outcome[:correct]
end
end
end
describe "determining if a branch is a valid environment name" do
invalid_cases = [
'hyphenated-branch',
'dotted.branch',
'slashed/branch',
'at@branch',
'http://branch'
]
valid_cases = [
'my_branchname',
'my_issue_346',
]
describe "and validate is false" do
invalid_cases.each do |branch|
it "is valid if the branch is #{branch}" do
bn = described_class.new(branch, {:validate => false})
expect(bn).to be_valid
end
end
valid_cases.each do |branch|
it "is valid if the branch is #{branch}" do
bn = described_class.new(branch, {:validate => false})
expect(bn).to be_valid
end
end
end
describe "and validate is true" do
invalid_cases.each do |branch|
it "is invalid if the branch is #{branch}" do
bn = described_class.new(branch, {:validate => true})
expect(bn).to_not be_valid
end
end
valid_cases.each do |branch|
it "is valid if the branch is #{branch}" do
bn = described_class.new(branch, {:validate => true})
expect(bn).to be_valid
end
end
end
end
describe "correcting branch names" do
invalid_cases = [
'hyphenated-branch',
'dotted.branch',
'slashed/branch',
'at@branch',
'http://branch'
]
valid_cases = [
'my_branchname',
'my_issue_346',
]
describe "and correct is false" do
invalid_cases.each do |branch|
it "doesn't modify #{branch}" do
bn = described_class.new(branch.dup, {:correct => false})
expect(bn.dirname).to eq branch
end
end
valid_cases.each do |branch|
it "doesn't modify #{branch}" do
bn = described_class.new(branch.dup, {:correct => false})
expect(bn.dirname).to eq branch
end
end
end
describe "and correct is true" do
invalid_cases.each do |branch|
it "replaces invalid characters in #{branch} with underscores" do
bn = described_class.new(branch.dup, {:correct => true})
expect(bn.dirname).to eq branch.gsub(/\W/, '_')
end
end
valid_cases.each do |branch|
it "doesn't modify #{branch}" do
bn = described_class.new(branch.dup, {:correct => true})
expect(bn.dirname).to eq branch
end
end
end
end
end