aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarkus Reiter2017-02-15 02:02:25 +0100
committerMarkus Reiter2017-02-23 05:40:31 +0100
commit85ff9add185fc24d99b296cf9cca3071b6d865c0 (patch)
treeb8ecad7437012256a998e275a03e8de62c675029
parent76ca97b4e73bf54dc98439ac3921cf27383722e2 (diff)
downloadbrew-85ff9add185fc24d99b296cf9cca3071b6d865c0.tar.bz2
Convert Pathname test to spec.
-rw-r--r--Library/Homebrew/test/pathname_spec.rb300
-rw-r--r--Library/Homebrew/test/pathname_test.rb263
2 files changed, 300 insertions, 263 deletions
diff --git a/Library/Homebrew/test/pathname_spec.rb b/Library/Homebrew/test/pathname_spec.rb
new file mode 100644
index 000000000..da12d4347
--- /dev/null
+++ b/Library/Homebrew/test/pathname_spec.rb
@@ -0,0 +1,300 @@
+require "tmpdir"
+require "extend/pathname"
+require "install_renamed"
+
+describe Pathname do
+ include FileUtils
+
+ let(:src) { Pathname.new(Dir.mktmpdir) }
+ let(:dst) { Pathname.new(Dir.mktmpdir) }
+ let(:file) { src/"foo" }
+ let(:dir) { src/"bar" }
+
+ after(:each) { rm_rf [src, dst] }
+
+ describe DiskUsageExtension do
+ before(:each) do
+ mkdir_p dir/"a-directory"
+ touch [dir/".DS_Store", dir/"a-file"]
+ File.truncate(dir/"a-file", 1_048_576)
+ ln_s dir/"a-file", dir/"a-symlink"
+ ln dir/"a-file", dir/"a-hardlink"
+ end
+
+ describe "#file_count" do
+ it "returns the number of files in a directory" do
+ expect(dir.file_count).to eq(3)
+ end
+ end
+
+ describe "#abv" do
+ context "when called on a directory" do
+ it "returns a string with the file count and disk usage" do
+ expect(dir.abv).to eq("3 files, 1M")
+ end
+ end
+
+ context "when called on a file" do
+ it "returns the disk usage" do
+ expect((dir/"a-file").abv).to eq("1M")
+ end
+ end
+ end
+ end
+
+ describe "#rmdir_if_possible" do
+ before(:each) { mkdir_p dir }
+
+ it "returns true and removes a directory if it doesn't contain files" do
+ expect(dir.rmdir_if_possible).to be true
+ expect(dir).not_to exist
+ end
+
+ it "returns false and doesn't delete a directory if it contains files" do
+ touch dir/"foo"
+ expect(dir.rmdir_if_possible).to be false
+ expect(dir).to be_a_directory
+ end
+
+ it "ignores .DS_Store files" do
+ touch dir/".DS_Store"
+ expect(dir.rmdir_if_possible).to be true
+ expect(dir).not_to exist
+ end
+ end
+
+ describe "#write" do
+ it "creates a file and writes to it" do
+ expect(file).not_to exist
+ file.write("CONTENT")
+ expect(File.read(file)).to eq("CONTENT")
+ end
+
+ it "raises an error if the file already exists" do
+ touch file
+ expect { file.write("CONTENT") }.to raise_error(RuntimeError)
+ end
+ end
+
+ describe "#append_lines" do
+ it "appends lines to a file" do
+ touch file
+
+ file.append_lines("CONTENT")
+ expect(File.read(file)).to eq <<-EOS.undent
+ CONTENT
+ EOS
+
+ file.append_lines("CONTENTS")
+ expect(File.read(file)).to eq <<-EOS.undent
+ CONTENT
+ CONTENTS
+ EOS
+ end
+
+ it "raises an error if the file does not exist" do
+ expect(file).not_to exist
+ expect { file.append_lines("CONTENT") }.to raise_error(RuntimeError)
+ end
+ end
+
+ describe "#atomic_write" do
+ it "atomically replaces a file" do
+ touch file
+ file.atomic_write("CONTENT")
+ expect(File.read(file)).to eq("CONTENT")
+ end
+
+ it "preserves permissions" do
+ File.open(file, "w", 0100777).close
+ file.atomic_write("CONTENT")
+ expect(file.stat.mode).to eq(0100777 & ~File.umask)
+ end
+
+ it "preserves default permissions" do
+ file.atomic_write("CONTENT")
+ sentinel = file.parent.join("sentinel")
+ touch sentinel
+ expect(file.stat.mode).to eq(sentinel.stat.mode)
+ end
+ end
+
+ describe "#ensure_writable" do
+ it "makes a file writable and restores permissions afterwards" do
+ touch file
+ chmod 0555, file
+ expect(file).not_to be_writable
+ file.ensure_writable do
+ expect(file).to be_writable
+ end
+ expect(file).not_to be_writable
+ end
+ end
+
+ describe "#extname" do
+ it "supports common multi-level archives" do
+ expect(Pathname.new("foo-0.1.tar.gz").extname).to eq(".tar.gz")
+ expect(Pathname.new("foo-0.1.cpio.gz").extname).to eq(".cpio.gz")
+ end
+ end
+
+ describe "#stem" do
+ it "returns the basename without double extensions" do
+ expect(Pathname("foo-0.1.tar.gz").stem).to eq("foo-0.1")
+ expect(Pathname("foo-0.1.cpio.gz").stem).to eq("foo-0.1")
+ end
+ end
+
+ describe "#install" do
+ before(:each) do
+ (src/"a.txt").write "This is sample file a."
+ (src/"b.txt").write "This is sample file b."
+ end
+
+ it "raises an error if the file doesn't exist" do
+ expect { dst.install "non_existent_file" }.to raise_error(Errno::ENOENT)
+ end
+
+ it "installs a file to a directory with its basename" do
+ touch file
+ dst.install(file)
+ expect(dst/file.basename).to exist
+ expect(file).not_to exist
+ end
+
+ it "creates intermediate directories" do
+ touch file
+ expect(dir).not_to be_a_directory
+ dir.install(file)
+ expect(dir).to be_a_directory
+ end
+
+ it "can install a file" do
+ dst.install src/"a.txt"
+ expect(dst/"a.txt").to exist, "a.txt was not installed"
+ expect(dst/"b.txt").not_to exist, "b.txt was installed."
+ end
+
+ it "can install an array of files" do
+ dst.install [src/"a.txt", src/"b.txt"]
+
+ expect(dst/"a.txt").to exist, "a.txt was not installed"
+ expect(dst/"b.txt").to exist, "b.txt was not installed"
+ end
+
+ it "can install a directory" do
+ bin = src/"bin"
+ bin.mkpath
+ mv Dir[src/"*.txt"], bin
+ dst.install bin
+
+ expect(dst/"bin/a.txt").to exist, "a.txt was not installed"
+ expect(dst/"bin/b.txt").to exist, "b.txt was not installed"
+ end
+
+ it "supports renaming files" do
+ dst.install src/"a.txt" => "c.txt"
+
+ expect(dst/"c.txt").to exist, "c.txt was not installed"
+ expect(dst/"a.txt").not_to exist, "a.txt was installed but not renamed"
+ expect(dst/"b.txt").not_to exist, "b.txt was installed"
+ end
+
+ it "supports renaming multiple files" do
+ dst.install(src/"a.txt" => "c.txt", src/"b.txt" => "d.txt")
+
+ expect(dst/"c.txt").to exist, "c.txt was not installed"
+ expect(dst/"d.txt").to exist, "d.txt was not installed"
+ expect(dst/"a.txt").not_to exist, "a.txt was installed but not renamed"
+ expect(dst/"b.txt").not_to exist, "b.txt was installed but not renamed"
+ end
+
+ it "supports renaming directories" do
+ bin = src/"bin"
+ bin.mkpath
+ mv Dir[src/"*.txt"], bin
+ dst.install bin => "libexec"
+
+ expect(dst/"bin").not_to exist, "bin was installed but not renamed"
+ expect(dst/"libexec/a.txt").to exist, "a.txt was not installed"
+ expect(dst/"libexec/b.txt").to exist, "b.txt was not installed"
+ end
+
+ it "can install directories as relative symlinks" do
+ bin = src/"bin"
+ bin.mkpath
+ mv Dir[src/"*.txt"], bin
+ dst.install_symlink bin
+
+ expect(dst/"bin").to be_a_symlink
+ expect(dst/"bin").to be_a_directory
+ expect(dst/"bin/a.txt").to exist
+ expect(dst/"bin/b.txt").to exist
+ expect((dst/"bin").readlink).to be_relative
+ end
+
+ it "can install relative paths as symlinks" do
+ dst.install_symlink "foo" => "bar"
+ expect((dst/"bar").readlink).to eq(Pathname.new("foo"))
+ end
+ end
+
+ describe InstallRenamed do
+ before(:each) do
+ dst.extend(InstallRenamed)
+ end
+
+ it "renames the installed file if it already exists" do
+ file.write "a"
+ dst.install file
+
+ file.write "b"
+ dst.install file
+
+ expect(File.read(dst/file.basename)).to eq("a")
+ expect(File.read(dst/"#{file.basename}.default")).to eq("b")
+ end
+
+ it "renames the installed directory" do
+ file.write "a"
+ dst.install src
+ expect(File.read(dst/src.basename/file.basename)).to eq("a")
+ end
+
+ it "recursively renames directories" do
+ (dst/dir.basename).mkpath
+ (dst/dir.basename/"another_file").write "a"
+ dir.mkpath
+ (dir/"another_file").write "b"
+ dst.install dir
+ expect(File.read(dst/dir.basename/"another_file.default")).to eq("b")
+ end
+ end
+
+ describe "#cp_path_sub" do
+ it "copies a file and replaces the given pattern" do
+ file.write "a"
+ file.cp_path_sub src, dst
+ expect(File.read(dst/file.basename)).to eq("a")
+ end
+
+ it "copies a directory and replaces the given pattern" do
+ dir.mkpath
+ dir.cp_path_sub src, dst
+ expect(dst/dir.basename).to be_a_directory
+ end
+ end
+end
+
+describe FileUtils do
+ let(:dst) { Pathname.new(Dir.mktmpdir) }
+
+ describe "#mkdir" do
+ it "creates indermediate directories" do
+ described_class.mkdir dst/"foo/bar/baz" do
+ expect(dst/"foo/bar/baz").to exist, "foo/bar/baz was not created"
+ expect(dst/"foo/bar/baz").to be_a_directory, "foo/bar/baz was not a directory structure"
+ end
+ end
+ end
+end
diff --git a/Library/Homebrew/test/pathname_test.rb b/Library/Homebrew/test/pathname_test.rb
deleted file mode 100644
index b48a26fbd..000000000
--- a/Library/Homebrew/test/pathname_test.rb
+++ /dev/null
@@ -1,263 +0,0 @@
-require "testing_env"
-require "tmpdir"
-require "extend/pathname"
-require "install_renamed"
-
-module PathnameTestExtension
- include FileUtils
-
- def setup
- super
- @src = Pathname.new(mktmpdir)
- @dst = Pathname.new(mktmpdir)
- @file = @src/"foo"
- @dir = @src/"bar"
- end
-end
-
-class PathnameTests < Homebrew::TestCase
- include PathnameTestExtension
-
- def test_disk_usage_extension
- mkdir_p @dir/"a-directory"
- touch @dir/".DS_Store"
- touch @dir/"a-file"
- File.truncate(@dir/"a-file", 1_048_576)
- ln_s @dir/"a-file", @dir/"a-symlink"
- ln @dir/"a-file", @dir/"a-hardlink"
- assert_equal 3, @dir.file_count
- assert_equal "3 files, 1M", @dir.abv
- assert_equal "1M", (@dir/"a-file").abv
- end
-
- def test_rmdir_if_possible
- mkdir_p @dir
- touch @dir/"foo"
-
- assert !@dir.rmdir_if_possible
- assert_predicate @dir, :directory?
-
- rm_f @dir/"foo"
- assert @dir.rmdir_if_possible
- refute_predicate @dir, :exist?
- end
-
- def test_rmdir_if_possible_ignore_ds_store
- mkdir_p @dir
- touch @dir/".DS_Store"
- assert @dir.rmdir_if_possible
- refute_predicate @dir, :exist?
- end
-
- def test_write
- @file.write("CONTENT")
- assert_equal "CONTENT", File.read(@file)
- end
-
- def test_write_does_not_overwrite
- touch @file
- assert_raises(RuntimeError) { @file.write("CONTENT") }
- end
-
- def test_append_lines
- touch @file
- @file.append_lines("CONTENT")
- assert_equal "CONTENT\n", File.read(@file)
- @file.append_lines("CONTENTS")
- assert_equal "CONTENT\nCONTENTS\n", File.read(@file)
- end
-
- def test_append_lines_does_not_create
- assert_raises(RuntimeError) { @file.append_lines("CONTENT") }
- end
-
- def test_atomic_write
- touch @file
- @file.atomic_write("CONTENT")
- assert_equal "CONTENT", File.read(@file)
- end
-
- def test_atomic_write_preserves_permissions
- File.open(@file, "w", 0100777) {}
- @file.atomic_write("CONTENT")
- assert_equal 0100777 & ~File.umask, @file.stat.mode
- end
-
- def test_atomic_write_preserves_default_permissions
- @file.atomic_write("CONTENT")
- sentinel = @file.parent.join("sentinel")
- touch sentinel
- assert_equal sentinel.stat.mode, @file.stat.mode
- end
-
- def test_ensure_writable
- touch @file
- chmod 0555, @file
- @file.ensure_writable { assert_predicate @file, :writable? }
- refute_predicate @file, :writable?
- end
-
- def test_extname
- assert_equal ".tar.gz", Pathname("foo-0.1.tar.gz").extname
- assert_equal ".cpio.gz", Pathname("foo-0.1.cpio.gz").extname
- end
-
- def test_stem
- assert_equal "foo-0.1", Pathname("foo-0.1.tar.gz").stem
- assert_equal "foo-0.1", Pathname("foo-0.1.cpio.gz").stem
- end
-
- def test_install_missing_file
- assert_raises(Errno::ENOENT) { @dst.install "non_existent_file" }
- end
-
- def test_install_removes_original
- touch @file
- @dst.install(@file)
-
- assert_predicate @dst/@file.basename, :exist?
- refute_predicate @file, :exist?
- end
-
- def test_install_creates_intermediate_directories
- touch @file
- refute_predicate @dir, :directory?
- @dir.install(@file)
- assert_predicate @dir, :directory?
- end
-
- def test_install_renamed
- @dst.extend(InstallRenamed)
-
- @file.write "a"
- @dst.install @file
- @file.write "b"
- @dst.install @file
-
- assert_equal "a", File.read(@dst/@file.basename)
- assert_equal "b", File.read(@dst/"#{@file.basename}.default")
- end
-
- def test_install_renamed_directory
- @dst.extend(InstallRenamed)
- @file.write "a"
- @dst.install @src
- assert_equal "a", File.read(@dst/@src.basename/@file.basename)
- end
-
- def test_install_renamed_directory_recursive
- @dst.extend(InstallRenamed)
- (@dst/@dir.basename).mkpath
- (@dst/@dir.basename/"another_file").write "a"
- @dir.mkpath
- (@dir/"another_file").write "b"
- @dst.install @dir
- assert_equal "b", File.read(@dst/@dir.basename/"another_file.default")
- end
-
- def test_cp_path_sub_file
- @file.write "a"
- @file.cp_path_sub @src, @dst
- assert_equal "a", File.read(@dst/"foo")
- end
-
- def test_cp_path_sub_directory
- @dir.mkpath
- @dir.cp_path_sub @src, @dst
- assert_predicate @dst/@dir.basename, :directory?
- end
-end
-
-class PathnameInstallTests < Homebrew::TestCase
- include PathnameTestExtension
-
- def setup
- super
- (@src/"a.txt").write "This is sample file a."
- (@src/"b.txt").write "This is sample file b."
- end
-
- def test_install
- @dst.install @src/"a.txt"
-
- assert_predicate @dst/"a.txt", :exist?, "a.txt was not installed"
- refute_predicate @dst/"b.txt", :exist?, "b.txt was installed."
- end
-
- def test_install_list
- @dst.install [@src/"a.txt", @src/"b.txt"]
-
- assert_predicate @dst/"a.txt", :exist?, "a.txt was not installed"
- assert_predicate @dst/"b.txt", :exist?, "b.txt was not installed"
- end
-
- def test_install_glob
- @dst.install Dir[@src/"*.txt"]
-
- assert_predicate @dst/"a.txt", :exist?, "a.txt was not installed"
- assert_predicate @dst/"b.txt", :exist?, "b.txt was not installed"
- end
-
- def test_install_directory
- bin = @src/"bin"
- bin.mkpath
- mv Dir[@src/"*.txt"], bin
- @dst.install bin
-
- assert_predicate @dst/"bin/a.txt", :exist?, "a.txt was not installed"
- assert_predicate @dst/"bin/b.txt", :exist?, "b.txt was not installed"
- end
-
- def test_install_rename
- @dst.install @src/"a.txt" => "c.txt"
-
- assert_predicate @dst/"c.txt", :exist?, "c.txt was not installed"
- refute_predicate @dst/"a.txt", :exist?, "a.txt was installed but not renamed"
- refute_predicate @dst/"b.txt", :exist?, "b.txt was installed"
- end
-
- def test_install_rename_more
- @dst.install(@src/"a.txt" => "c.txt", @src/"b.txt" => "d.txt")
-
- assert_predicate @dst/"c.txt", :exist?, "c.txt was not installed"
- assert_predicate @dst/"d.txt", :exist?, "d.txt was not installed"
- refute_predicate @dst/"a.txt", :exist?, "a.txt was installed but not renamed"
- refute_predicate @dst/"b.txt", :exist?, "b.txt was installed but not renamed"
- end
-
- def test_install_rename_directory
- bin = @src/"bin"
- bin.mkpath
- mv Dir[@src/"*.txt"], bin
- @dst.install bin => "libexec"
-
- refute_predicate @dst/"bin", :exist?, "bin was installed but not renamed"
- assert_predicate @dst/"libexec/a.txt", :exist?, "a.txt was not installed"
- assert_predicate @dst/"libexec/b.txt", :exist?, "b.txt was not installed"
- end
-
- def test_install_symlink
- bin = @src/"bin"
- bin.mkpath
- mv Dir[@src/"*.txt"], bin
- @dst.install_symlink bin
-
- assert_predicate @dst/"bin", :symlink?
- assert_predicate @dst/"bin", :directory?
- assert_predicate @dst/"bin/a.txt", :exist?
- assert_predicate @dst/"bin/b.txt", :exist?
- assert_predicate((@dst/"bin").readlink, :relative?)
- end
-
- def test_install_relative_symlink
- @dst.install_symlink "foo" => "bar"
- assert_equal Pathname.new("foo"), (@dst/"bar").readlink
- end
-
- def test_mkdir_creates_intermediate_directories
- mkdir @dst/"foo/bar/baz" do
- assert_predicate @dst/"foo/bar/baz", :exist?, "foo/bar/baz was not created"
- assert_predicate @dst/"foo/bar/baz", :directory?, "foo/bar/baz was not a directory structure"
- end
- end
-end