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 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
|
require 'date'
require 'fileutils'
require 'spec_helper'
describe FileUtils do
before :each do
MemFs::File.umask(0020)
MemFs.activate!
described_class.mkdir '/test'
end
after :each do
MemFs.deactivate!
end
describe '.cd' do
it 'changes the current working directory' do
described_class.cd '/test'
expect(described_class.pwd).to eq('/test')
end
it 'returns nil' do
expect(described_class.cd('/test')).to be_nil
end
it "raises an error when the given path doesn't exist" do
expect { described_class.cd('/nowhere') }.to raise_specific_error(Errno::ENOENT)
end
it 'raises an error when the given path is not a directory' do
described_class.touch('/test-file')
expect { described_class.cd('/test-file') }.to raise_specific_error(Errno::ENOTDIR)
end
context 'when called with a block' do
it 'changes current working directory for the block execution' do
described_class.cd '/test' do
expect(described_class.pwd).to eq('/test')
end
end
it 'resumes to the old working directory after the block execution finished' do
described_class.cd '/'
expect { described_class.cd('/test') {} }.to_not change { described_class.pwd }
end
end
context 'when the destination is a symlink' do
before :each do
described_class.symlink('/test', '/test-link')
end
it 'changes directory to the last target of the link chain' do
described_class.cd('/test-link')
expect(described_class.pwd).to eq('/test')
end
it "raises an error if the last target of the link chain doesn't exist" do
expect { described_class.cd('/nowhere-link') }.to raise_specific_error(Errno::ENOENT)
end
end
end
describe '.chmod' do
it 'changes permission bits on the named file to the bit pattern represented by mode' do
described_class.touch '/test-file'
described_class.chmod 0777, '/test-file'
expect(File.stat('/test-file').mode).to eq(0100777)
end
it 'changes permission bits on the named files (in list) to the bit pattern represented by mode' do
described_class.touch ['/test-file', '/test-file2']
described_class.chmod 0777, ['/test-file', '/test-file2']
expect(File.stat('/test-file2').mode).to eq(0100777)
end
it 'returns an array containing the file names' do
file_names = %w[/test-file /test-file2]
described_class.touch file_names
expect(described_class.chmod(0777, file_names)).to eq(file_names)
end
it 'raises an error if an entry does not exist' do
expect { described_class.chmod(0777, '/test-file') }.to raise_specific_error(Errno::ENOENT)
end
context 'when the named file is a symlink' do
before :each do
described_class.touch '/test-file'
described_class.symlink '/test-file', '/test-link'
end
context 'when File responds to lchmod' do
it 'changes the mode on the link' do
described_class.chmod(0777, '/test-link')
expect(File.lstat('/test-link').mode).to eq(0100777)
end
it "doesn't change the mode of the link's target" do
mode = File.lstat('/test-file').mode
described_class.chmod(0777, '/test-link')
expect(File.lstat('/test-file').mode).to eq(mode)
end
end
context "when File doesn't respond to lchmod" do
it 'does nothing' do
allow_any_instance_of(described_class::Entry_).to \
receive_messages(have_lchmod?: false)
mode = File.lstat('/test-link').mode
described_class.chmod(0777, '/test-link')
expect(File.lstat('/test-link').mode).to eq(mode)
end
end
end
end
describe '.chmod_R' do
before :each do
described_class.touch '/test/test-file'
end
it 'changes the permission bits on the named entry' do
described_class.chmod_R(0777, '/test')
expect(File.stat('/test').mode).to eq(0100777)
end
it 'changes the permission bits on any sub-directory of the named entry' do
described_class.chmod_R(0777, '/')
expect(File.stat('/test').mode).to eq(0100777)
end
it 'changes the permission bits on any descendant file of the named entry' do
described_class.chmod_R(0777, '/')
expect(File.stat('/test/test-file').mode).to eq(0100777)
end
end
describe '.chown' do
it 'changes owner on the named file' do
described_class.chown(42, nil, '/test')
expect(File.stat('/test').uid).to eq(42)
end
it 'changes owner on the named files (in list)' do
described_class.touch('/test-file')
described_class.chown(42, nil, ['/test', '/test-file'])
expect(File.stat('/test-file').uid).to eq(42)
end
it 'changes group on the named entry' do
described_class.chown(nil, 42, '/test')
expect(File.stat('/test').gid).to eq(42)
end
it 'changes group on the named entries in list' do
described_class.touch('/test-file')
described_class.chown(nil, 42, ['/test', '/test-file'])
expect(File.stat('/test-file').gid).to eq(42)
end
it "doesn't change user if user is nil" do
described_class.chown(nil, 42, '/test')
expect(File.stat('/test').uid).not_to be(42)
end
it "doesn't change group if group is nil" do
described_class.chown(42, nil, '/test')
expect(File.stat('/test').gid).not_to be(42)
end
context 'when the name entry is a symlink' do
before :each do
described_class.touch '/test-file'
described_class.symlink '/test-file', '/test-link'
end
it 'changes the owner on the last target of the link chain' do
described_class.chown(42, nil, '/test-link')
expect(File.stat('/test-file').uid).to eq(42)
end
it 'changes the group on the last target of the link chain' do
described_class.chown(nil, 42, '/test-link')
expect(File.stat('/test-file').gid).to eq(42)
end
it "doesn't change the owner of the symlink" do
described_class.chown(42, nil, '/test-link')
expect(File.lstat('/test-link').uid).not_to be(42)
end
it "doesn't change the group of the symlink" do
described_class.chown(nil, 42, '/test-link')
expect(File.lstat('/test-link').gid).not_to be(42)
end
end
end
describe '.chown_R' do
before :each do
described_class.touch '/test/test-file'
end
it 'changes the owner on the named entry' do
described_class.chown_R(42, nil, '/test')
expect(File.stat('/test').uid).to eq(42)
end
it 'changes the group on the named entry' do
described_class.chown_R(nil, 42, '/test')
expect(File.stat('/test').gid).to eq(42)
end
it 'changes the owner on any sub-directory of the named entry' do
described_class.chown_R(42, nil, '/')
expect(File.stat('/test').uid).to eq(42)
end
it 'changes the group on any sub-directory of the named entry' do
described_class.chown_R(nil, 42, '/')
expect(File.stat('/test').gid).to eq(42)
end
it 'changes the owner on any descendant file of the named entry' do
described_class.chown_R(42, nil, '/')
expect(File.stat('/test/test-file').uid).to eq(42)
end
it 'changes the group on any descendant file of the named entry' do
described_class.chown_R(nil, 42, '/')
expect(File.stat('/test/test-file').gid).to eq(42)
end
end
describe '.cmp' do
it_behaves_like 'aliased method', :cmp, :compare_file
end
describe '.compare_file' do
it 'returns true if the contents of a file A and a file B are identical' do
File.open('/test-file', 'w') { |f| f.puts 'this is a test' }
File.open('/test-file2', 'w') { |f| f.puts 'this is a test' }
expect(described_class.compare_file('/test-file', '/test-file2')).to be true
end
it 'returns false if the contents of a file A and a file B are not identical' do
File.open('/test-file', 'w') { |f| f.puts 'this is a test' }
File.open('/test-file2', 'w') { |f| f.puts 'this is not a test' }
expect(described_class.compare_file('/test-file', '/test-file2')).to be false
end
end
describe '.compare_stream' do
it 'returns true if the contents of a stream A and stream B are identical' do
File.open('/test-file', 'w') { |f| f.puts 'this is a test' }
File.open('/test-file2', 'w') { |f| f.puts 'this is a test' }
file1 = File.open('/test-file')
file2 = File.open('/test-file2')
expect(described_class.compare_stream(file1, file2)).to be true
end
it 'returns false if the contents of a stream A and stream B are not identical' do
File.open('/test-file', 'w') { |f| f.puts 'this is a test' }
File.open('/test-file2', 'w') { |f| f.puts 'this is not a test' }
file1 = File.open('/test-file')
file2 = File.open('/test-file2')
expect(described_class.compare_stream(file1, file2)).to be false
end
end
describe '.copy' do
it_behaves_like 'aliased method', :copy, :cp
end
describe '.copy_entry' do
it 'copies a file system entry +src+ to +dest+' do
File.open('/test-file', 'w') { |f| f.puts 'test' }
described_class.copy_entry('/test-file', '/test-copy')
expect(File.read('/test-copy')).to eq("test\n")
end
it 'preserves file types' do
described_class.touch('/test-file')
described_class.symlink('/test-file', '/test-link')
described_class.copy_entry('/test-link', '/test-copy')
expect(File.symlink?('/test-copy')).to be true
end
context 'when +src+ does not exist' do
it 'raises an exception' do
expect {
described_class.copy_entry('/test-file', '/test-copy')
}.to raise_specific_error(RuntimeError)
end
end
context 'when +preserve+ is true' do
let(:time) { Date.parse('2013-01-01') }
before :each do
described_class.touch('/test-file')
described_class.chown(1042, 1042, '/test-file')
described_class.chmod(0777, '/test-file')
_fs.find('/test-file').mtime = time
described_class.copy_entry('/test-file', '/test-copy', true)
end
it 'preserves owner' do
expect(File.stat('/test-copy').uid).to eq(1042)
end
it 'preserves group' do
expect(File.stat('/test-copy').gid).to eq(1042)
end
it 'preserves permissions' do
expect(File.stat('/test-copy').mode).to eq(0100777)
end
it 'preserves modified time' do
expect(File.stat('/test-copy').mtime).to eq(time)
end
end
context 'when +dest+ already exists' do
it 'overwrite it' do
File.open('/test-file', 'w') { |f| f.puts 'test' }
described_class.touch('/test-copy')
described_class.copy_entry('/test-file', '/test-copy')
expect(File.read('/test-copy')).to eq("test\n")
end
end
context 'when +remove_destination+ is true' do
it 'removes each destination file before copy' do
described_class.touch(['/test-file', '/test-copy'])
expect(File).to receive(:unlink).with('/test-copy')
described_class.copy_entry('/test-file', '/test-copy', false, false, true)
end
end
context 'when +src+ is a directory' do
it 'copies its contents recursively' do
described_class.mkdir_p('/test-dir/test-sub-dir')
described_class.copy_entry('/test-dir', '/test-copy')
expect(Dir.exist?('/test-copy/test-sub-dir')).to be true
end
end
end
describe '.copy_file' do
it 'copies file contents of src to dest' do
File.open('/test-file', 'w') { |f| f.puts 'test' }
described_class.copy_file('/test-file', '/test-file2')
expect(File.read('/test-file2')).to eq("test\n")
end
end
describe '.copy_stream' do
# This method is not implemented since it is delegated to the IO class.
end
describe '.cp' do
before :each do
File.open('/test-file', 'w') { |f| f.puts 'test' }
end
it 'copies a file content +src+ to +dest+' do
described_class.cp('/test-file', '/copy-file')
expect(File.read('/copy-file')).to eq("test\n")
end
context 'when +src+ and +dest+ are the same file' do
it 'raises an error' do
expect {
described_class.cp('/test-file', '/test-file')
}.to raise_specific_error(ArgumentError)
end
end
context 'when +dest+ is a directory' do
it 'copies +src+ to +dest/src+' do
described_class.mkdir('/dest')
described_class.cp('/test-file', '/dest/copy-file')
expect(File.read('/dest/copy-file')).to eq("test\n")
end
end
context 'when src is a list of files' do
context 'when +dest+ is not a directory' do
it 'raises an error' do
described_class.touch(['/dest', '/test-file2'])
expect {
described_class.cp(['/test-file', '/test-file2'], '/dest')
}.to raise_specific_error(Errno::ENOTDIR)
end
end
end
end
describe '.cp_r' do
it 'copies +src+ to +dest+' do
File.open('/test-file', 'w') { |f| f.puts 'test' }
described_class.cp_r('/test-file', '/copy-file')
expect(File.read('/copy-file')).to eq("test\n")
end
context 'when +src+ is a directory' do
it 'copies all its contents recursively' do
described_class.mkdir('/test/dir')
described_class.touch('/test/dir/file')
described_class.cp_r('/test', '/dest')
expect(File.exist?('/dest/dir/file')).to be true
end
end
context 'when +dest+ is a directory' do
it 'copies +src+ to +dest/src+' do
described_class.mkdir(['/test/dir', '/dest'])
described_class.touch('/test/dir/file')
described_class.cp_r('/test', '/dest')
expect(File.exist?('/dest/test/dir/file')).to be true
end
end
context 'when +src+ is a list of files' do
it 'copies each of them in +dest+' do
described_class.mkdir(['/test/dir', '/test/dir2', '/dest'])
described_class.touch(['/test/dir/file', '/test/dir2/file'])
described_class.cp_r(['/test/dir', '/test/dir2'], '/dest')
expect(File.exist?('/dest/dir2/file')).to be true
end
end
end
describe '.getwd' do
it_behaves_like 'aliased method', :getwd, :pwd
end
describe '.identical?' do
it_behaves_like 'aliased method', :identical?, :compare_file
end
describe '.install' do
before :each do
File.open('/test-file', 'w') { |f| f.puts 'test' }
end
it 'copies +src+ to +dest+' do
described_class.install('/test-file', '/test-file2')
expect(File.read('/test-file2')).to eq("test\n")
end
context 'when +:mode+ is set' do
it 'changes the permission mode to +mode+' do
expect(File).to receive(:chmod).with(0777, '/test-file2')
described_class.install('/test-file', '/test-file2', mode: 0777)
end
end
context 'when +src+ and +dest+ are the same file' do
it 'raises an exception' do
expect {
described_class.install('/test-file', '/test-file')
}.to raise_exception(ArgumentError)
end
end
context 'when +dest+ already exists' do
it 'removes destination before copy' do
expect(File).to receive(:unlink).with('/test-file2')
described_class.install('/test-file', '/test-file2')
end
context 'and +dest+ is a directory' do
it 'installs +src+ in dest/src' do
described_class.mkdir('/test-dir')
described_class.install('/test-file', '/test-dir')
expect(File.read('/test-dir/test-file')).to eq("test\n")
end
end
end
end
describe '.link' do
it_behaves_like 'aliased method', :link, :ln
end
describe '.ln' do
before :each do
File.open('/test-file', 'w') { |f| f.puts 'test' }
end
it 'creates a hard link +dest+ which points to +src+' do
described_class.ln('/test-file', '/test-file2')
expect(File.read('/test-file2')).to eq(File.read('/test-file'))
end
it 'creates a hard link, not a symlink' do
described_class.ln('/test-file', '/test-file2')
expect(File.symlink?('/test-file2')).to be false
end
context 'when +dest+ already exists' do
context 'and is a directory' do
it 'creates a link dest/src' do
described_class.mkdir('/test-dir')
described_class.ln('/test-file', '/test-dir')
expect(File.read('/test-dir/test-file')).to eq(File.read('/test-file'))
end
end
context 'and it is not a directory' do
it 'raises an exception' do
described_class.touch('/test-file2')
expect {
described_class.ln('/test-file', '/test-file2')
}.to raise_specific_error(SystemCallError)
end
context 'and +:force+ is set' do
it 'overwrites +dest+' do
described_class.touch('/test-file2')
described_class.ln('/test-file', '/test-file2', force: true)
expect(File.read('/test-file2')).to eq(File.read('/test-file'))
end
end
end
end
context 'when passing a list of paths' do
it 'creates a link for each path in +destdir+' do
described_class.touch('/test-file2')
described_class.mkdir('/test-dir')
described_class.ln(['/test-file', '/test-file2'], '/test-dir')
end
context 'and +destdir+ is not a directory' do
it 'raises an exception' do
described_class.touch(['/test-file2', '/not-a-dir'])
expect {
described_class.ln(['/test-file', '/test-file2'], '/not-a-dir')
}.to raise_specific_error(Errno::ENOTDIR)
end
end
end
end
describe '.ln_s' do
before :each do
File.open('/test-file', 'w') { |f| f.puts 'test' }
described_class.touch('/not-a-dir')
described_class.mkdir('/test-dir')
end
it 'creates a symbolic link +new+' do
described_class.ln_s('/test-file', '/test-link')
expect(File.symlink?('/test-link')).to be true
end
it 'creates a symbolic link which points to +old+' do
described_class.ln_s('/test-file', '/test-link')
expect(File.read('/test-link')).to eq(File.read('/test-file'))
end
context 'when +new+ already exists' do
context 'and it is a directory' do
it 'creates a symbolic link +new/old+' do
described_class.ln_s('/test-file', '/test-dir')
expect(File.symlink?('/test-dir/test-file')).to be true
end
end
context 'and it is not a directory' do
it 'raises an exeption' do
expect {
described_class.ln_s('/test-file', '/not-a-dir')
}.to raise_specific_error(Errno::EEXIST)
end
context 'and +:force+ is set' do
it 'overwrites +new+' do
described_class.ln_s('/test-file', '/not-a-dir', force: true)
expect(File.symlink?('/not-a-dir')).to be true
end
end
end
end
context 'when passing a list of paths' do
before :each do
File.open('/test-file2', 'w') { |f| f.puts 'test2' }
end
it 'creates several symbolic links in +destdir+' do
described_class.ln_s(['/test-file', '/test-file2'], '/test-dir')
expect(File.exist?('/test-dir/test-file2')).to be true
end
it 'creates symbolic links pointing to each item in the list' do
described_class.ln_s(['/test-file', '/test-file2'], '/test-dir')
expect(File.read('/test-dir/test-file2')).to eq(File.read('/test-file2'))
end
context 'when +destdir+ is not a directory' do
it 'raises an error' do
expect {
described_class.ln_s(['/test-file', '/test-file2'], '/not-a-dir')
}.to raise_specific_error(Errno::ENOTDIR)
end
end
end
end
describe '.ln_sf' do
it 'calls ln_s with +:force+ set to true' do
File.open('/test-file', 'w') { |f| f.puts 'test' }
File.open('/test-file2', 'w') { |f| f.puts 'test2' }
described_class.ln_sf('/test-file', '/test-file2')
expect(File.read('/test-file2')).to eq(File.read('/test-file'))
end
end
describe '.makedirs' do
it_behaves_like 'aliased method', :makedirs, :mkdir_p
end
describe '.mkdir' do
it 'creates one directory' do
described_class.mkdir('/test-dir')
expect(File.directory?('/test-dir')).to be true
end
context 'when passing a list of paths' do
it 'creates several directories' do
described_class.mkdir(['/test-dir', '/test-dir2'])
expect(File.directory?('/test-dir2')).to be true
end
end
context 'when passing options' do
context 'when passing mode parameter' do
it 'creates directory with specified permissions' do
described_class.mkdir('/test-dir', mode: 0654)
expect(File.exist?('/test-dir')).to be true
expect(File.stat('/test-dir').mode).to eq(0100654)
end
end
context 'when passing noop parameter' do
it 'does not create any directories' do
described_class.mkdir(['/test-dir', '/another-dir'], noop: true)
expect(File.directory?('/test-dir')).to be false
expect(File.directory?('/another-dir')).to be false
end
end
end
end
describe '.mkdir_p' do
it 'creates a directory' do
described_class.mkdir_p('/test-dir')
expect(File.directory?('/test-dir')).to be true
end
it 'creates all the parent directories' do
described_class.mkdir_p('/path/to/some/test-dir')
expect(File.directory?('/path/to/some')).to be true
end
context 'when passing a list of paths' do
it 'creates each directory' do
described_class.mkdir_p(['/test-dir', '/test-dir'])
expect(File.directory?('/test-dir')).to be true
end
it "creates each directory's parents" do
described_class.mkdir_p(['/test-dir', '/path/to/some/test-dir'])
expect(File.directory?('/path/to/some')).to be true
end
end
context 'when passing options' do
context 'when passing mode parameter' do
it 'creates directory with specified permissions' do
described_class.mkdir_p('/test-dir', mode: 0654)
expect(File.exist?('/test-dir')).to be true
expect(File.stat('/test-dir').mode).to eq(0100654)
end
end
context 'when passing noop parameter' do
it 'does not create any directories' do
described_class.mkdir_p(['/test-dir', '/another-dir'], noop: true)
expect(File.directory?('/test-dir')).to be false
expect(File.directory?('/another-dir')).to be false
end
end
end
end
describe '.mkpath' do
it_behaves_like 'aliased method', :mkpath, :mkdir_p
end
describe '.move' do
it_behaves_like 'aliased method', :move, :mv
end
describe '.mv' do
it 'moves +src+ to +dest+' do
described_class.touch('/test-file')
described_class.mv('/test-file', '/test-file2')
expect(File.exist?('/test-file2')).to be true
end
it 'removes +src+' do
described_class.touch('/test-file')
described_class.mv('/test-file', '/test-file2')
expect(File.exist?('/test-file')).to be false
end
context 'when +dest+ already exists' do
context 'and is a directory' do
it 'moves +src+ to dest/src' do
described_class.touch('/test-file')
described_class.mkdir('/test-dir')
described_class.mv('/test-file', '/test-dir')
expect(File.exist?('/test-dir/test-file')).to be true
end
end
context 'and +dest+ is not a directory' do
it 'it overwrites +dest+' do
File.open('/test-file', 'w') { |f| f.puts 'test' }
described_class.touch('/test-file2')
described_class.mv('/test-file', '/test-file2')
expect(File.read('/test-file2')).to eq("test\n")
end
end
end
end
describe '.pwd' do
it 'returns the name of the current directory' do
described_class.cd '/test'
expect(described_class.pwd).to eq('/test')
end
end
describe '.remove' do
it_behaves_like 'aliased method', :remove, :rm
end
describe '.remove_dir' do
it 'removes the given directory +dir+' do
described_class.mkdir('/test-dir')
described_class.remove_dir('/test-dir')
expect(File.exist?('/test-dir')).to be false
end
it 'removes the contents of the given directory +dir+' do
described_class.mkdir_p('/test-dir/test-sub-dir')
described_class.remove_dir('/test-dir')
expect(File.exist?('/test-dir/test-sub-dir')).to be false
end
context 'when +force+ is set' do
it 'ignores standard errors' do
expect { described_class.remove_dir('/test-dir', true) }.not_to raise_error
end
end
end
describe '.remove_entry' do
it 'removes a file system entry +path+' do
described_class.touch('/test-file')
described_class.remove_entry('/test-file')
expect(File.exist?('/test-file')).to be false
end
context 'when +path+ is a directory' do
it 'removes it recursively' do
described_class.mkdir_p('/test-dir/test-sub-dir')
described_class.remove_entry('/test-dir')
expect(Dir.exist?('/test-dir')).to be false
end
end
end
describe '.remove_entry_secure' do
before :each do
described_class.mkdir_p('/test-dir/test-sub-dir')
end
it 'removes a file system entry +path+' do
described_class.chmod(0755, '/')
described_class.remove_entry_secure('/test-dir')
expect(Dir.exist?('/test-dir')).to be false
end
context 'when +path+ is a directory' do
it 'removes it recursively' do
described_class.chmod(0755, '/')
described_class.remove_entry_secure('/test-dir')
expect(Dir.exist?('/test-dir/test-sub-dir')).to be false
end
context 'and is word writable' do
it 'calls chown(2) on it' do
described_class.chmod(01777, '/')
directory = _fs.find('/test-dir')
expect(directory).to receive(:uid=).at_least(:once)
described_class.remove_entry_secure('/test-dir')
end
it 'calls chmod(2) on all sub directories' do
described_class.chmod(01777, '/')
directory = _fs.find('/test-dir')
expect(directory).to receive(:mode=).at_least(:once)
described_class.remove_entry_secure('/test-dir')
end
end
end
end
describe '.remove_file' do
it 'removes a file path' do
described_class.touch('/test-file')
described_class.remove_file('/test-file')
expect(File.exist?('/test-file')).to be false
end
context 'when +force+ is set' do
it 'ignores StandardError' do
expect { described_class.remove_file('/no-file', true) }.not_to raise_error
end
end
end
describe '.rm' do
it 'removes the specified file' do
described_class.touch('/test-file')
described_class.rm('/test-file')
expect(File.exist?('/test-file')).to be false
end
it 'removes files specified in list' do
described_class.touch(['/test-file', '/test-file2'])
described_class.rm(['/test-file', '/test-file2'])
expect(File.exist?('/test-file2')).to be false
end
it 'cannot remove a directory' do
described_class.mkdir('/test-dir')
expect { described_class.rm('/test-dir') }.to raise_specific_error(Errno::EPERM)
end
context 'when +:force+ is set' do
it 'ignores StandardError' do
described_class.mkdir('/test-dir')
expect {
described_class.rm('/test-dir', force: true)
}.not_to raise_error
end
end
end
describe '.rm_f' do
it 'calls rm with +:force+ set to true' do
expect(described_class).to receive(:rm).with('test', force: true)
described_class.rm_f('test')
end
end
describe '.rm_r' do
before :each do
described_class.touch(['/test-file', '/test-file2'])
end
it 'removes a list of files' do
described_class.rm_r(['/test-file', '/test-file2'])
expect(File.exist?('/test-file2')).to be false
end
context 'when an item of the list is a directory' do
it 'removes all its contents recursively' do
described_class.mkdir('/test-dir')
described_class.touch('/test-dir/test-file')
described_class.rm_r(['/test-file', '/test-file2', '/test-dir'])
expect(File.exist?('/test-dir/test-file')).to be false
end
end
context 'when +:force+ is set' do
it 'ignores StandardError' do
expect {
described_class.rm_r(['/no-file'], force: true)
}.not_to raise_error
end
end
end
describe '.rm_rf' do
it 'calls rm with +:force+ set to true' do
expect(described_class).to receive(:rm_r).with('test', force: true)
described_class.rm_rf('test')
end
end
describe '.rmdir' do
it 'Removes a directory' do
described_class.mkdir('/test-dir')
described_class.rmdir('/test-dir')
expect(Dir.exist?('/test-dir')).to be false
end
it 'Removes a list of directories' do
described_class.mkdir('/test-dir')
described_class.mkdir('/test-dir2')
described_class.rmdir(['/test-dir', '/test-dir2'])
expect(Dir.exist?('/test-dir2')).to be false
end
context 'when a directory is not empty' do
before :each do
described_class.mkdir('/test-dir')
described_class.touch('/test-dir/test-file')
end
it 'ignores errors' do
expect { described_class.rmdir('/test-dir') }.not_to raise_error
end
it "doesn't remove the directory" do
described_class.rmdir('/test-dir')
expect(Dir.exist?('/test-dir')).to be true
end
end
end
describe '.rmtree' do
it_behaves_like 'aliased method', :rmtree, :rm_rf
end
describe '.safe_unlink' do
it_behaves_like 'aliased method', :safe_unlink, :rm_f
end
describe '.symlink' do
it_behaves_like 'aliased method', :symlink, :ln_s
end
describe '.touch' do
it "creates a file if it doesn't exist" do
described_class.touch('/test-file')
expect(_fs.find('/test-file')).not_to be_nil
end
it "creates a list of files if they don't exist" do
described_class.touch(['/test-file', '/test-file2'])
expect(_fs.find('/test-file2')).not_to be_nil
end
end
describe '.uptodate?' do
before :each do
described_class.touch('/test-file')
described_class.touch('/old-file')
_fs.find!('/old-file').mtime = Time.now - 3600
end
it 'returns true if +newer+ is newer than all +old_list+' do
expect(described_class.uptodate?('/test-file', ['/old-file'])).to be true
end
context 'when +newer+ does not exist' do
it 'consideres it as older' do
expect(described_class.uptodate?('/no-file', ['/old-file'])).to be false
end
end
context 'when a item of +old_list+ does not exist' do
it 'consideres it as older than +newer+' do
uptodate = described_class.uptodate?('/test-file', ['/old-file', '/no-file'])
expect(uptodate).to be true
end
end
end
end
|