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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
|
# encoding: UTF-8
require 'spec_helper'
require 'puppet/module_tool'
describe Puppet::ModuleTool do
describe '.is_module_root?' do
it 'should return true if directory has a metadata.json file' do
expect(FileTest).to receive(:file?).with(have_attributes(to_s: '/a/b/c/metadata.json')).and_return(true)
expect(subject.is_module_root?(Pathname.new('/a/b/c'))).to be_truthy
end
it 'should return false if directory does not have a metadata.json file' do
expect(FileTest).to receive(:file?).with(have_attributes(to_s: '/a/b/c/metadata.json')).and_return(false)
expect(subject.is_module_root?(Pathname.new('/a/b/c'))).to be_falsey
end
end
describe '.find_module_root' do
let(:sample_path) { Pathname.new('/a/b/c').expand_path }
it 'should return the first path as a pathname when it contains a module file' do
expect(Puppet::ModuleTool).to receive(:is_module_root?).with(sample_path).
and_return(true)
expect(subject.find_module_root(sample_path)).to eq(sample_path)
end
it 'should return a parent path as a pathname when it contains a module file' do
expect(Puppet::ModuleTool).to receive(:is_module_root?).with(have_attributes(to_s: File.expand_path('/a/b/c'))).and_return(false)
expect(Puppet::ModuleTool).to receive(:is_module_root?).with(have_attributes(to_s: File.expand_path('/a/b'))).and_return(true)
expect(subject.find_module_root(sample_path)).to eq(Pathname.new('/a/b').expand_path)
end
it 'should return nil when no module root can be found' do
expect(Puppet::ModuleTool).to receive(:is_module_root?).at_least(:once).and_return(false)
expect(subject.find_module_root(sample_path)).to be_nil
end
end
describe '.format_tree' do
it 'should return an empty tree when given an empty list' do
expect(subject.format_tree([])).to eq('')
end
it 'should return a shallow when given a list without dependencies' do
list = [ { :text => 'first' }, { :text => 'second' }, { :text => 'third' } ]
expect(subject.format_tree(list)).to eq <<-TREE
├── first
├── second
└── third
TREE
end
it 'should return a deeply nested tree when given a list with deep dependencies' do
list = [
{
:text => 'first',
:dependencies => [
{
:text => 'second',
:dependencies => [
{ :text => 'third' }
]
}
]
},
]
expect(subject.format_tree(list)).to eq <<-TREE
└─┬ first
└─┬ second
└── third
TREE
end
it 'should show connectors when deep dependencies are not on the last node of the top level' do
list = [
{
:text => 'first',
:dependencies => [
{
:text => 'second',
:dependencies => [
{ :text => 'third' }
]
}
]
},
{ :text => 'fourth' }
]
expect(subject.format_tree(list)).to eq <<-TREE
├─┬ first
│ └─┬ second
│ └── third
└── fourth
TREE
end
it 'should show connectors when deep dependencies are not on the last node of any level' do
list = [
{
:text => 'first',
:dependencies => [
{
:text => 'second',
:dependencies => [
{ :text => 'third' }
]
},
{ :text => 'fourth' }
]
}
]
expect(subject.format_tree(list)).to eq <<-TREE
└─┬ first
├─┬ second
│ └── third
└── fourth
TREE
end
it 'should show connectors in every case when deep dependencies are not on the last node' do
list = [
{
:text => 'first',
:dependencies => [
{
:text => 'second',
:dependencies => [
{ :text => 'third' }
]
},
{ :text => 'fourth' }
]
},
{ :text => 'fifth' }
]
expect(subject.format_tree(list)).to eq <<-TREE
├─┬ first
│ ├─┬ second
│ │ └── third
│ └── fourth
└── fifth
TREE
end
end
describe '.set_option_defaults' do
let(:options) { {} }
let(:modulepath) { ['/env/module/path', '/global/module/path'] }
let(:environment_name) { :current_environment }
let(:environment) { Puppet::Node::Environment.create(environment_name, modulepath) }
subject do
described_class.set_option_defaults(options)
options
end
around do |example|
envs = Puppet::Environments::Static.new(environment)
Puppet.override(:environments => envs) do
example.run
end
end
describe ':environment' do
context 'as String' do
let(:options) { { :environment => "#{environment_name}" } }
it 'assigns the environment with the given name to :environment_instance' do
expect(subject).to include :environment_instance => environment
end
end
context 'as Symbol' do
let(:options) { { :environment => :"#{environment_name}" } }
it 'assigns the environment with the given name to :environment_instance' do
expect(subject).to include :environment_instance => environment
end
end
context 'as Puppet::Node::Environment' do
let(:env) { Puppet::Node::Environment.create('anonymous', []) }
let(:options) { { :environment => env } }
it 'assigns the given environment to :environment_instance' do
expect(subject).to include :environment_instance => env
end
end
end
describe ':modulepath' do
let(:options) do
{ :modulepath => %w[bar foo baz].join(File::PATH_SEPARATOR) }
end
let(:paths) { options[:modulepath].split(File::PATH_SEPARATOR).map { |dir| File.expand_path(dir) } }
it 'is expanded to an absolute path' do
expect(subject[:environment_instance].full_modulepath).to eql paths
end
it 'is used to compute :target_dir' do
expect(subject).to include :target_dir => paths.first
end
context 'conflicts with :environment' do
let(:options) do
{ :modulepath => %w[bar foo baz].join(File::PATH_SEPARATOR), :environment => environment_name }
end
it 'replaces the modulepath of the :environment_instance' do
expect(subject[:environment_instance].full_modulepath).to eql paths
end
it 'is used to compute :target_dir' do
expect(subject).to include :target_dir => paths.first
end
end
end
describe ':strict_semver' do
context 'when set' do
let(:options) do
{ :strict_semver => true }
end
it 'is not overridden by default' do
expect(subject).to include :strict_semver => true
end
end
context 'when unset' do
let(:options) do
{ }
end
it 'defaults to false' do
expect(subject).to include :strict_semver => false
end
end
end
describe ':target_dir' do
let(:options) do
{ :target_dir => 'foo' }
end
let(:target) { File.expand_path(options[:target_dir]) }
it 'is expanded to an absolute path' do
expect(subject).to include :target_dir => target
end
it 'is prepended to the modulepath of the :environment_instance' do
expect(subject[:environment_instance].full_modulepath.first).to eql target
end
context 'conflicts with :modulepath' do
let(:options) do
{ :target_dir => 'foo', :modulepath => %w[bar foo baz].join(File::PATH_SEPARATOR) }
end
it 'is prepended to the modulepath of the :environment_instance' do
expect(subject[:environment_instance].full_modulepath.first).to eql target
end
it 'shares the provided :modulepath via the :environment_instance' do
paths = %w[foo] + options[:modulepath].split(File::PATH_SEPARATOR)
paths.map! { |dir| File.expand_path(dir) }
expect(subject[:environment_instance].full_modulepath).to eql paths
end
end
context 'conflicts with :environment' do
let(:options) do
{ :target_dir => 'foo', :environment => environment_name }
end
it 'is prepended to the modulepath of the :environment_instance' do
expect(subject[:environment_instance].full_modulepath.first).to eql target
end
it 'shares the provided :modulepath via the :environment_instance' do
paths = %w[foo] + environment.full_modulepath
paths.map! { |dir| File.expand_path(dir) }
expect(subject[:environment_instance].full_modulepath).to eql paths
end
end
context 'when not passed' do
it 'is populated with the first component of the modulepath' do
expect(subject).to include :target_dir => subject[:environment_instance].full_modulepath.first
end
end
end
end
describe '.parse_module_dependency' do
it 'parses a dependency without a version range expression' do
name, range, expr = subject.parse_module_dependency('source', 'name' => 'foo-bar')
expect(name).to eql('foo-bar')
expect(range).to eql(SemanticPuppet::VersionRange.parse('>= 0.0.0'))
expect(expr).to eql('>= 0.0.0')
end
it 'parses a dependency with a version range expression' do
name, range, expr = subject.parse_module_dependency('source', 'name' => 'foo-bar', 'version_requirement' => '1.2.x')
expect(name).to eql('foo-bar')
expect(range).to eql(SemanticPuppet::VersionRange.parse('1.2.x'))
expect(expr).to eql('1.2.x')
end
it 'does not raise an error on invalid version range expressions' do
name, range, expr = subject.parse_module_dependency('source', 'name' => 'foo-bar', 'version_requirement' => 'nope')
expect(name).to eql('foo-bar')
expect(range).to eql(SemanticPuppet::VersionRange::EMPTY_RANGE)
expect(expr).to eql('nope')
end
end
end
|