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 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
|
require "spec_helper"
require "aruba/api"
RSpec.describe Aruba::Api::Filesystem do
include_context "uses aruba API"
let(:name) { @file_name }
let(:path) { @file_path }
let(:dir_name) { "test.d" }
let(:dir_path) { @aruba.expand_path(dir_name) }
describe "#append_lines_to_file" do
it "inserts a newline if existing file does not end in one" do
Aruba.platform.write_file(path, "foo\nbar")
append_lines_to_file(name, "baz")
expect(File.read(path)).to eq "foo\nbar\nbaz"
end
it "does not insert a newline if the existing file ends in one" do
Aruba.platform.write_file(path, "foo\nbar\n")
append_lines_to_file(name, "baz")
expect(File.read(path)).to eq "foo\nbar\nbaz"
end
end
describe "#all_paths" do
context "when file exists" do
before do
Aruba.platform.write_file(path, "")
end
it { expect(all_paths).to include expand_path(name) }
end
context "when directory exists" do
let(:name) { "test_dir" }
let(:path) { File.join(@aruba.aruba.current_directory, name) }
before do
Aruba.platform.mkdir(path)
end
it { expect(all_paths).to include expand_path(name) }
end
context "when nothing exists" do
it { expect(all_paths).to eq [] }
end
end
describe "#all_files" do
context "when file exists" do
before do
Aruba.platform.write_file(path, "")
end
it { expect(all_files).to include expand_path(name) }
end
context "when directory exists" do
let(:name) { "test_dir" }
let(:path) { File.join(@aruba.aruba.current_directory, name) }
before do
Aruba.platform.mkdir(path)
end
it { expect(all_files).to eq [] }
end
context "when nothing exists" do
it { expect(all_files).to eq [] }
end
end
describe "#all_directories" do
context "when file exists" do
before do
Aruba.platform.write_file(path, "")
end
it { expect(all_directories).to eq [] }
end
context "when directory exists" do
let(:name) { "test_dir" }
let(:path) { File.join(@aruba.aruba.current_directory, name) }
before do
Aruba.platform.mkdir(path)
end
it { expect(all_directories).to include expand_path(name) }
end
context "when nothing exists" do
it { expect(all_directories).to eq [] }
end
end
describe "#file_size" do
let(:size) { file_size(name) }
context "when file exists" do
before do
File.open(path, "w") { |f| f.print "a" }
end
it { expect(size).to eq 1 }
end
context "when file does not exist" do
let(:name) { "non_existing_file" }
it { expect { size }.to raise_error RSpec::Expectations::ExpectationNotMetError }
end
end
describe "#touch" do
let(:options) { {} }
before do
@aruba.set_environment_variable "HOME", File.expand_path(@aruba.aruba.current_directory)
end
context "when touching a file that does not exist" do
before do
@aruba.touch(name, options)
end
context "and should be created in an existing directory" do
it { expect(File.size(path)).to eq 0 }
it_behaves_like "an existing file"
end
context "and should be created in a non-existing directory" do
let(:name) { "directory/test" }
let(:path) { File.join(@aruba.aruba.current_directory, "directory/test") }
it_behaves_like "an existing file"
end
context "and path includes ~" do
let(:string) { random_string }
let(:name) { File.join("~", string) }
let(:path) { File.join(@aruba.aruba.current_directory, string) }
it_behaves_like "an existing file"
end
context "and the mtime should be set statically" do
let(:time) { Time.parse("2014-01-01 10:00:00") }
let(:options) { { mtime: Time.parse("2014-01-01 10:00:00") } }
it_behaves_like "an existing file"
it { expect(File.mtime(path)).to eq time }
end
context "and multiple file names are given" do
let(:name) { %w(file1 file2 file3) }
let(:path) do
%w(file1 file2 file3).map { |p| File.join(@aruba.aruba.current_directory, p) }
end
it_behaves_like "an existing file"
end
end
context "when touching an existing directory" do
let(:name) { %w(directory1) }
let(:path) { Array(name).map { |p| File.join(@aruba.aruba.current_directory, p) } }
before do
Array(path).each { |p| Aruba.platform.mkdir p }
@aruba.touch(name, options)
end
context "and the mtime should be set statically" do
let(:time) { Time.parse("2014-01-01 10:00:00") }
let(:options) { { mtime: Time.parse("2014-01-01 10:00:00") } }
it_behaves_like "an existing directory"
it { Array(path).each { |p| expect(File.mtime(p)).to eq time } }
end
end
end
describe "#absolute?" do
context "when is absolute path" do
it { expect(@aruba).to be_absolute(path) }
end
context "when is relative path" do
it { expect(@aruba).not_to be_absolute(name) }
end
end
describe "#relative?" do
context "when given an absolute path" do
it { expect(@aruba).not_to be_relative(path) }
end
context "when given a relative path" do
it { expect(@aruba).to be_relative(name) }
end
end
describe "#exist?" do
context "when given a file" do
context "when it exists" do
before do
Aruba.platform.write_file(path, "")
end
it { expect(@aruba).to be_exist(name) }
end
context "when it does not exist" do
it { expect(@aruba).not_to be_exist(name) }
end
end
context "when given a directory" do
context "when it exists" do
before do
Aruba.platform.mkdir(dir_path)
end
it { expect(@aruba).to be_exist(dir_name) }
end
context "when it does not exist" do
it { expect(@aruba).not_to be_exist(dir_name) }
end
end
end
describe "#file?" do
context "when given a file" do
context "when it exists" do
before do
Aruba.platform.write_file(path, "")
end
it { expect(@aruba).to be_file(name) }
end
context "when does not exist" do
it { expect(@aruba).not_to be_file(name) }
end
end
context "when given a directory" do
let(:name) { "test.d" }
let(:path) { File.join(@aruba.aruba.current_directory, name) }
context "when it exists" do
before do
Aruba.platform.mkdir(path)
end
it { expect(@aruba).not_to be_file(name) }
end
context "when does not exist" do
it { expect(@aruba).not_to be_file(name) }
end
end
end
describe "#directory?" do
context "when given a file" do
context "when it exists" do
before do
Aruba.platform.write_file(path, "")
end
it { expect(@aruba).not_to be_directory(name) }
end
context "when does not exist" do
it { expect(@aruba).not_to be_directory(name) }
end
end
context "when given a directory" do
context "when it exists" do
before do
Aruba.platform.mkdir(dir_path)
end
it { expect(@aruba).to be_directory(dir_name) }
end
context "when does not exist" do
it { expect(@aruba).not_to be_directory(dir_name) }
end
end
end
describe "#copy" do
let(:source) { "file.txt" }
let(:destination) { "file1.txt" }
context "when source is existing" do
context "when destination is non-existing" do
context "when source is file" do
before do
create_test_files(source)
@aruba.copy source, destination
end
context "when source is plain file" do
it { expect(destination).to be_an_existing_file }
end
context 'when source is contains "~" in path' do
let(:source) { "~/file.txt" }
it { expect(destination).to be_an_existing_file }
end
context "when source is fixture" do
let(:source) { "%/copy/file.txt" }
let(:destination) { "file.txt" }
it { expect(destination).to be_an_existing_file }
end
context "when source is list of files" do
let(:source) { %w(file1.txt file2.txt file3.txt) }
let(:destination) { "file.d" }
let(:destination_files) { source.map { |s| File.join(destination, s) } }
it { expect(destination_files).to all be_an_existing_file }
end
end
context "when source is directory" do
let(:source) { "src.d" }
let(:destination) { "dst.d" }
before do
Aruba.platform.mkdir(File.join(@aruba.aruba.current_directory, source))
@aruba.copy source, destination
end
context "when source is single directory" do
it { expect(destination).to be_an_existing_directory }
end
context "when source is nested directory" do
let(:source) { "src.d/subdir.d" }
let(:destination) { "dst.d/" }
it { expect(destination).to be_an_existing_directory }
end
end
end
context "when destination is existing" do
context "when source is list of files" do
before { create_test_files(source) }
context "when destination is directory" do
let(:source) { %w(file1.txt file2.txt file3.txt) }
let(:destination) { "file.d" }
let(:destination_files) { source.map { |s| File.join(destination, s) } }
before do
Aruba.platform.mkdir(File.join(@aruba.aruba.current_directory, destination))
@aruba.copy source, destination
end
it { expect(destination_files).to all be_an_existing_file }
end
context "when destination is not a directory" do
let(:source) { %w(file1.txt file2.txt file3.txt) }
let(:destination) { "file.txt" }
let(:error_message) { "Multiples sources can only be copied to a directory" }
before { create_test_files(destination) }
it "raises an appropriate error" do
expect { @aruba.copy source, destination }
.to raise_error ArgumentError, error_message
end
end
context "when a source is the same like destination" do
let(:source) { "file1.txt" }
let(:destination) { "file1.txt" }
before { create_test_files(source) }
it "raises an appropriate error" do
src_path = File.expand_path(File.join(@aruba.aruba.current_directory, source))
dest_path = File.expand_path(File.join(@aruba.aruba.current_directory,
destination))
expected_message = %(same file: #{src_path} and #{dest_path})
expect { @aruba.copy source, destination }
.to raise_error ArgumentError, expected_message
end
end
context "when a fixture is destination" do
let(:source) { "%/copy/file.txt" }
let(:destination) { "%/copy/file.txt" }
let(:error_message) do
"Using a fixture as destination (#{destination}) is not supported"
end
it "raises an appropriate error" do
expect { @aruba.copy source, destination }
.to raise_error ArgumentError, error_message
end
end
end
end
context "when source is non-existing" do
it { expect { @aruba.copy source, destination }.to raise_error ArgumentError }
end
end
end
describe "#write_file" do
it "writes file" do
@aruba.write_file(name, "")
expect(File.exist?(path)).to be true
end
end
describe "#write_fixed_size_file" do
let(:file_size) { @file_size }
it "writes a fixed sized file" do
@aruba.write_fixed_size_file(name, file_size)
expect(File.exist?(path)).to be true
expect(File.size(path)).to eq file_size
end
it "works with ~ in path name" do
file_path = File.join("~", random_string)
@aruba.with_environment "HOME" => File.expand_path(aruba.current_directory) do
@aruba.write_fixed_size_file(file_path, file_size)
expect(File.exist?(File.expand_path(file_path))).to be true
expect(File.size(File.expand_path(file_path))).to eq file_size
end
end
end
describe "#chmod" do
def actual_permissions
format("%o", File::Stat.new(path).mode)[-4, 4]
end
let(:permissions) { "0644" }
before do
@aruba.set_environment_variable "HOME", File.expand_path(@aruba.aruba.current_directory)
File.open(path, "w") { |f| f << "" }
@aruba.chmod(permissions, name)
end
context "when file exists" do
context "and permissions are given as string" do
it { expect(actual_permissions).to eq("0644") }
end
context "and permissions are given as octal number" do
let(:permissions) { 0o644 }
it { expect(actual_permissions).to eq("0644") }
end
context "and path has ~ in it" do
let(:basename) { random_string }
let(:name) { File.join("~", basename) }
let(:path) { File.join(@aruba.aruba.current_directory, basename) }
it { expect(actual_permissions).to eq("0644") }
end
end
end
describe "#with_file_content" do
before do
@aruba.write_file(name, "foo bar baz")
end
it "checks the given file's full content against the expectations in the passed block" do
@aruba.with_file_content name do |full_content|
expect(full_content).to eq "foo bar baz"
end
end
it "works with ~ in path name" do
file_path = File.join("~", random_string)
@aruba.with_environment "HOME" => File.expand_path(aruba.current_directory) do
@aruba.write_file(file_path, "foo bar baz")
@aruba.with_file_content file_path do |full_content|
expect(full_content).to eq "foo bar baz"
end
end
end
context "checking the file's content against the expectations in the block" do
it "is successful when the inner expectations match" do
expect do
@aruba.with_file_content name do |full_content|
expect(full_content).to match(/foo/)
expect(full_content).not_to match(/zoo/)
end
end.not_to raise_error
end
it "raises ExpectationNotMetError when the inner expectations don't match" do
expect do
@aruba.with_file_content name do |full_content|
expect(full_content).to match(/zoo/)
expect(full_content).not_to match(/foo/)
end
end.to raise_error RSpec::Expectations::ExpectationNotMetError
end
end
end
describe "#create_directory" do
before do
@directory_name = "test_dir"
@directory_path = File.join(@aruba.aruba.current_directory, @directory_name)
end
it "creates a directory" do
@aruba.create_directory @directory_name
expect(File).to exist(File.expand_path(@directory_path))
end
end
describe "#read" do
let(:name) { "test.txt" }
let(:path) { File.join(@aruba.aruba.current_directory, name) }
let(:content) { "asdf" }
before do
@aruba.set_environment_variable "HOME", File.expand_path(@aruba.aruba.current_directory)
end
context "when does not exist" do
it { expect { @aruba.read(name) }.to raise_error ArgumentError }
end
context "when it exists" do
context "when file" do
before do
File.open(File.expand_path(path), "w") { |f| f << content }
end
context "when normal file" do
it { expect(@aruba.read(name)).to eq [content] }
end
context "when binary file" do
let(:content) { "\u0000" }
it { expect(@aruba.read(name)).to eq [content] }
end
context "when is empty file" do
let(:content) { "" }
it { expect(@aruba.read(name)).to eq [] }
end
context "when path contains ~" do
let(:string) { random_string }
let(:name) { File.join("~", string) }
let(:path) { File.join(@aruba.aruba.current_directory, string) }
it { expect(@aruba.read(name)).to eq [content] }
end
end
context "when directory" do
let(:name) { "test.d" }
before do
Aruba.platform.mkdir path
end
it { expect { @aruba.read(name) }.to raise_error ArgumentError }
end
end
end
describe "#list" do
let(:name) { "test.d" }
let(:content) { %w(subdir.1.d subdir.2.d) }
let(:path) { File.join(@aruba.aruba.current_directory, name) }
before do
@aruba.set_environment_variable "HOME", File.expand_path(@aruba.aruba.current_directory)
end
context "when does not exist" do
it { expect { @aruba.list(name) }.to raise_error ArgumentError }
end
context "when it exists" do
context "when file" do
let(:name) { "test.txt" }
before do
File.open(File.expand_path(path), "w") { |f| f << content }
end
context "when normal file" do
it { expect { @aruba.list(name) }.to raise_error ArgumentError }
end
end
context "when directory" do
before do
Aruba.platform.mkdir path
Array(content).each { |it| Aruba.platform.mkdir File.join(path, it) }
end
context "when has subdirectories" do
context "when is simple path" do
let(:existing_files) { @aruba.list(name) }
let(:expected_files) { content.map { |c| File.join(name, c) }.sort }
it { expect(expected_files - existing_files).to be_empty }
end
context "when path contains ~" do
let(:string) { random_string }
let(:name) { File.join("~", string) }
let(:path) { File.join(@aruba.aruba.current_directory, string) }
let(:existing_files) { @aruba.list(name) }
let(:expected_files) { content.map { |c| File.join(string, c) } }
it { expect(expected_files - existing_files).to be_empty }
end
end
context "when has no subdirectories" do
let(:content) { [] }
it { expect(@aruba.list(name)).to eq [] }
end
end
end
end
describe "#remove" do
let(:name) { "test.txt" }
let(:path) { File.join(@aruba.aruba.current_directory, name) }
let(:options) { {} }
before do
@aruba.set_environment_variable "HOME", File.expand_path(@aruba.aruba.current_directory)
end
context "when given a file" do
context "when it exists" do
before do
Array(path).each { |it| File.open(File.expand_path(it), "w") { |f| f << "" } }
@aruba.remove(name, options)
end
context "when is a single file" do
it_behaves_like "a non-existing file"
end
context "when are multiple files" do
let(:name) { %w(file1 file2 file3) }
let(:path) { name.map { |it| File.join(@aruba.aruba.current_directory, it) } }
it_behaves_like "a non-existing file"
end
context "when path contains ~" do
let(:string) { random_string }
let(:name) { File.join("~", string) }
let(:path) { File.join(@aruba.aruba.current_directory, string) }
it_behaves_like "a non-existing file"
end
end
context "when it does not exist" do
before do
@aruba.remove(name, options)
end
context "when forced to delete the file" do
let(:options) { { force: true } }
it_behaves_like "a non-existing file"
end
end
end
context "when given a directory" do
let(:name) { "test.d" }
context "when it exists" do
before do
Array(path).each { |it| Aruba.platform.mkdir it }
@aruba.remove(name, options)
end
context "when is a single directory" do
it_behaves_like "a non-existing directory"
end
context "when are multiple directorys" do
let(:name) { %w(directory1 directory2 directory3) }
let(:path) { name.map { |it| File.join(@aruba.aruba.current_directory, it) } }
it_behaves_like "a non-existing directory"
end
context "when path contains ~" do
let(:string) { random_string }
let(:name) { File.join("~", string) }
let(:path) { File.join(@aruba.aruba.current_directory, string) }
it_behaves_like "a non-existing directory"
end
end
context "when it does not exist" do
before do
@aruba.remove(name, options)
end
context "when forced to delete the directory" do
let(:options) { { force: true } }
it_behaves_like "a non-existing directory"
end
end
end
end
end
|