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 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
|
require (File.dirname(File.realdirpath(__FILE__)) + '/test_helper.rb')
class TestConstructTest < Minitest::Test
include TestConstruct::Helpers
def teardown
Dir.chdir File.expand_path("../..", __FILE__)
TestConstruct.destroy_all!
end
testing 'using within_construct explicitly' do
test 'creates construct' do
num = rand(1_000_000_000)
TestConstruct.stubs(:rand).returns(num)
directory = false
TestConstruct::within_construct do |construct|
directory = File.directory?(File.join(TestConstruct.tmpdir, "construct_container-#{$PROCESS_ID}-#{num}"))
end
assert directory
directory = false
TestConstruct.within_construct do |construct|
directory = File.directory?(File.join(TestConstruct.tmpdir, "construct_container-#{$PROCESS_ID}-#{num}"))
end
assert directory
end
end
testing 'creating a construct container' do
test 'exists' do
num = rand(1_000_000_000)
self.stubs(:rand).returns(num)
within_construct do |construct|
assert File.directory?(File.join(TestConstruct.tmpdir, "construct_container-#{$PROCESS_ID}-#{num}"))
end
end
test 'yields to its block' do
sensor = 'no yield'
within_construct do
sensor = 'yielded'
end
assert_equal 'yielded', sensor
end
test 'block argument is container directory Pathname' do
num = rand(1_000_000_000)
self.stubs(:rand).returns(num)
within_construct do |container_path|
expected_path = (Pathname(TestConstruct.tmpdir) +
"construct_container-#{$PROCESS_ID}-#{num}")
assert_equal(expected_path, container_path)
end
end
test 'does not exist afterwards' do
path = nil
within_construct do |container_path|
path = container_path
end
assert !path.exist?
end
test 'removes entire tree afterwards' do
path = nil
within_construct do |container_path|
path = container_path
(container_path + 'foo').mkdir
end
assert !path.exist?
end
test 'removes dir if block raises exception' do
path = nil
begin
within_construct do |container_path|
path = container_path
raise 'something bad happens here'
end
rescue
end
assert !path.exist?
end
test 'does not capture exceptions raised in block' do
err = RuntimeError.new('an error')
begin
within_construct do
raise err
end
rescue RuntimeError => e
assert_same err, e
end
end
end
testing 'creating a file in a container' do
test 'exists while in construct block' do
within_construct do |construct|
construct.file('foo.txt')
assert File.exists?(construct + 'foo.txt')
end
end
test 'does not exist after construct block' do
filepath = 'unset'
within_construct do |construct|
filepath = construct.file('foo.txt')
end
assert !File.exists?(filepath)
end
test 'has empty file contents by default' do
within_construct do |construct|
construct.file('foo.txt')
assert_equal '', File.read(construct + 'foo.txt')
end
end
test 'writes contents to file' do
within_construct do |construct|
construct.file('foo.txt','abcxyz')
assert_equal 'abcxyz', File.read(construct+'foo.txt')
end
end
test 'contents can be given in a block' do
within_construct do |construct|
construct.file('foo.txt') do
<<-EOS
File
Contents
EOS
end
assert_equal "File\nContents\n", File.read(construct+'foo.txt')
end
end
test 'contents block overwrites contents argument' do
within_construct do |construct|
construct.file('foo.txt','abc') do
'xyz'
end
assert_equal 'xyz', File.read(construct+'foo.txt')
end
end
test 'block is passed File object' do
within_construct do |construct|
construct.file('foo.txt') do |file|
assert_equal((construct+'foo.txt').to_s, file.path)
end
end
end
test 'writes to File object passed to block' do
within_construct do |construct|
construct.file('foo.txt') do |file|
file << 'abc'
end
assert_equal 'abc', File.read(construct+'foo.txt')
end
end
test 'closes file after block ends' do
within_construct do |construct|
construct_file = nil
construct.file('foo.txt') do |file|
construct_file = file
end
assert construct_file.closed?
end
end
test 'block return value not used as content if passed File object' do
within_construct do |construct|
construct.file('foo.txt') do |file|
file << 'abc'
'xyz'
end
assert_equal 'abc', File.read(construct+'foo.txt')
end
end
test 'contents argument is ignored if block takes File arg' do
within_construct do |construct|
construct.file('foo.txt','xyz') do |file|
file << 'abc'
end
assert_equal 'abc', File.read(construct+'foo.txt')
end
end
test 'returns file path' do
within_construct do |construct|
assert_equal(construct+'foo.txt', construct.file('foo.txt'))
end
end
test 'creates file including path in one call' do
within_construct do |construct|
construct.file('foo/bar/baz.txt')
assert (construct+'foo/bar/baz.txt').exist?
end
end
test 'creates file including path in one call when directories exists' do
within_construct do |construct|
construct.directory('foo/bar')
construct.file('foo/bar/baz.txt')
assert (construct+'foo/bar/baz.txt').exist?
end
end
test 'creates file including path with chained calls' do
within_construct do |construct|
construct.directory('foo').directory('bar').file('baz.txt')
assert (construct+'foo/bar/baz.txt').exist?
end
end
end
testing 'creating a subdirectory in container' do
test 'exists while in construct block' do
within_construct do |construct|
construct.directory 'foo'
assert (construct+'foo').directory?
end
end
test 'does not exist after construct block' do
subdir = 'unset'
within_construct do |construct|
construct.directory 'foo'
subdir = construct + 'foo'
end
assert !subdir.directory?
end
test 'returns the new path name' do
within_construct do |construct|
assert_equal((construct+'foo'), construct.directory('foo'))
end
end
test 'yield to block' do
sensor = 'unset'
within_construct do |construct|
construct.directory('bar') do
sensor = 'yielded'
end
end
assert_equal 'yielded', sensor
end
test 'block argument is subdirectory path' do
within_construct do |construct|
construct.directory('baz') do |dir|
assert_equal((construct+'baz'),dir)
end
end
end
test 'creates nested directory in one call' do
within_construct do |construct|
construct.directory('foo/bar')
assert (construct+'foo/bar').directory?
end
end
test 'creates a nested directory in two calls' do
within_construct do |construct|
construct.directory('foo').directory('bar')
assert (construct+'foo/bar').directory?
end
end
end
testing "subdirectories changing the working directory" do
test 'forces directory stays the same' do
within_construct do |construct|
old_pwd = Dir.pwd
construct.directory('foo', :chdir => false) do
assert_equal old_pwd, Dir.pwd
end
end
end
test 'defaults chdir setting from construct' do
within_construct(:chdir => false) do |construct|
old_pwd = Dir.pwd
construct.directory('foo') do
assert_equal old_pwd, Dir.pwd
end
end
end
test 'overrides construct default' do
within_construct(:chdir => false) do |construct|
old_pwd = Dir.pwd
construct.directory('foo', :chdir => true) do |dir|
assert_equal dir.to_s, Dir.pwd
end
end
end
test 'current working directory is within subdirectory' do
within_construct do |construct|
construct.directory('foo') do |dir|
assert_equal dir.to_s, Dir.pwd
end
end
end
test 'current working directory is unchanged outside of subdirectory' do
within_construct do |construct|
old_pwd = Dir.pwd
construct.directory('foo')
assert_equal old_pwd, Dir.pwd
end
end
test 'current working directory is unchanged after exception' do
within_construct do |construct|
old_pwd = Dir.pwd
begin
construct.directory('foo') do
raise 'something bad happens here'
end
rescue
end
assert_equal old_pwd, Dir.pwd
end
end
test 'captures exceptions raised in block' do
within_construct do |construct|
error = assert_raises RuntimeError do
construct.directory('foo') do
raise 'fail!'
end
end
assert_equal 'fail!', error.message
end
end
test 'checking for a file is relative to subdirectory' do
within_construct do |construct|
construct.directory('bar') do |dir|
dir.file('foo.txt')
assert File.exists?('foo.txt')
end
end
end
test 'checking for a directory is relative to subdirectory' do
within_construct do |construct|
construct.directory('foo') do |dir|
dir.directory('mydir')
assert File.directory?('mydir')
end
end
end
end
testing "changing the working directory" do
test 'forces directory stays the same' do
old_pwd = Dir.pwd
within_construct(:chdir => false) do |construct|
assert_equal old_pwd, Dir.pwd
end
end
test 'current working directory is within construct' do
within_construct do |construct|
assert_equal construct.to_s, Dir.pwd
end
end
test 'current working directory is unchanged outside of construct' do
old_pwd = Dir.pwd
within_construct do |construct|
end
assert_equal old_pwd, Dir.pwd
end
test 'current working directory is unchanged after exception' do
old_pwd = Dir.pwd
begin
within_construct do |construct|
raise 'something bad happens here'
end
rescue
end
assert_equal old_pwd, Dir.pwd
end
test 'does not capture exceptions raised in block' do
error = assert_raises RuntimeError do
within_construct do
raise 'fail!'
end
end
assert_equal 'fail!', error.message
end
test 'checking for a file is relative to container' do
within_construct do |construct|
construct.file('foo.txt')
assert File.exists?('foo.txt')
end
end
test 'checking for a directory is relative to container' do
within_construct do |construct|
construct.directory('mydir')
assert File.directory?('mydir')
end
end
end
testing "#create_construct" do
test "returns a working Construct" do
it = create_construct
it.directory "foo"
it.file "bar", "CONTENTS"
assert (it + "foo").directory?
assert_equal "CONTENTS", (it + "bar").read
end
end
testing "#chdir" do
test "executes its block in the context of the construct" do
it = create_construct
refute_equal it.to_s, Dir.pwd
sensor = :unset
it.chdir do
sensor = Dir.pwd
end
assert_equal it.to_s, sensor
end
test "leaves construct directory on block exit" do
it = create_construct
it.chdir do
# NOOP
end
refute_equal it.to_s, Dir.pwd
end
end
testing "#destroy!" do
test "removes the construct container" do
it = create_construct
it.destroy!
assert !File.exist?(it.to_s)
end
end
testing "#finalize" do
test "removes the construct container" do
it = create_construct
it.finalize
assert !File.exist?(it.to_s)
end
test "leaves the container if keep is flagged" do
it = create_construct
it.keep
it.finalize
assert File.exist?(it.to_s)
end
test "leaves the container if keep is flagged in a subdir" do
it = create_construct
subdir = it.directory "subdir"
subdir.keep
it.finalize
assert File.exist?(it.to_s)
end
end
testing "keep_on_error = true" do
test 'keeps dir when block raises exception' do
path = nil
begin
within_construct(keep_on_error: true) do |container_path|
path = container_path
raise 'something bad happens here'
end
rescue
end
assert path.exist?
end
test 'updates exception message to include location of files' do
path = nil
begin
within_construct(keep_on_error: true) do |container_path|
path = container_path
raise 'bad stuff'
end
rescue => e
error = e
end
assert_equal "bad stuff\nTestConstruct files kept at: #{path}", e.message
end
end
testing 'base_dir option' do
test 'determines the location of construct dirs' do
base_dir = File.expand_path("../temp", __FILE__)
within_construct(base_dir: base_dir) do |container|
assert_equal base_dir, container.dirname.to_s
end
end
end
testing 'name option' do
test 'used in generation of the directory name' do
within_construct(name: "My best test ever!") do |container|
assert_match /my-best-test-ever-$/, container.basename.to_s
end
end
end
end
|