]> git.wincent.com - mkdtemp.git/commitdiff
Add specs 1.0
authorWincent Colaiuta <win@wincent.com>
Sun, 28 Sep 2008 21:50:45 +0000 (23:50 +0200)
committerWincent Colaiuta <win@wincent.com>
Sun, 28 Sep 2008 21:50:45 +0000 (23:50 +0200)
Signed-off-by: Wincent Colaiuta <win@wincent.com>
spec/mkdtemp_spec.rb [new file with mode: 0644]
spec/spec_helper.rb [new file with mode: 0644]

diff --git a/spec/mkdtemp_spec.rb b/spec/mkdtemp_spec.rb
new file mode 100644 (file)
index 0000000..0ef60c4
--- /dev/null
@@ -0,0 +1,78 @@
+# Copyright 2008 Wincent Colaiuta
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+require File.join(File.dirname(__FILE__), 'spec_helper')
+
+describe 'Dir.mkdtemp' do
+  before do
+    if not File.exist? '/tmp'
+      pending "cannot run spec because '/tmp' does not exist"
+    elsif not File.directory? '/tmp'
+      pending "cannot run spec because '/tmp' is not a directory"
+    elsif not File.writable? '/tmp'
+      pending "cannot run spec because '/tmp' is not writeable"
+    end
+  end
+
+  it 'should return the modified template path' do
+    path = Dir.mkdtemp
+    path.should be_kind_of(String)
+    path.should_not == '/tmp/temp.XXXXXX'
+  end
+
+  it 'should create the directory corresponding to the template' do
+    path = Dir.mkdtemp
+    File.exist?(path).should == true
+    File.directory?(path).should == true
+  end
+
+  it 'should create the directory with write permissions' do
+    path = Dir.mkdtemp
+    File.writable?(path).should == true
+  end
+
+  it 'should create the directory with read permissions' do
+    path = Dir.mkdtemp
+    File.readable?(path).should == true
+  end
+
+  it 'should create the directory with secure ownership (same user as caller)' do
+    path = Dir.mkdtemp
+    File.owned?(path).should == true
+  end
+
+  it 'should complain if passed a template that includes non-existentent parent directories' do
+    non_existent_dir = '/temp-which-does-not-exist'
+    if File.exist? non_existent_dir
+      pending "cannot run because '#{non_existent_dir}' exists"
+    end
+    lambda { Dir.mkdtemp "/#{non_existent_dir}/temp.XXXXXX" }.should raise_error(SystemCallError)
+  end
+
+  it 'should use "/tmp/temp.XXXXXX" as a template if passed nil' do
+    path = Dir.mkdtemp
+    path.should match(%r{\A/tmp/temp\..{6}\z})
+    path.should_not == '/tmp/temp.XXXXXX'
+  end
+
+  it 'should use substitute random characters for the trailing "Xs" in the template' do
+    path = Dir.mkdtemp '/tmp/test.XXXXXX'
+    path.should_not == '/tmp/test.XXXXXX'
+  end
+
+  it 'should leave the prefix portion of the template unchanged' do
+    path = Dir.mkdtemp '/tmp/test.XXXXXX'
+    path.should match(%r{\A/tmp/test\..{6}\z})
+  end
+end
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
new file mode 100644 (file)
index 0000000..ce5895e
--- /dev/null
@@ -0,0 +1,18 @@
+# Copyright 2008 Wincent Colaiuta
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+require 'pathname'
+require 'rubygems'
+require 'spec'
+require File.join(File.dirname(__FILE__), '..', 'ext', 'mkdtemp')