1 # Copyright 2008 Wincent Colaiuta
2 # This program is free software: you can redistribute it and/or modify
3 # it under the terms of the GNU General Public License as published by
4 # the Free Software Foundation, either version 3 of the License, or
5 # (at your option) any later version.
7 # This program is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 # GNU General Public License for more details.
12 # You should have received a copy of the GNU General Public License
13 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15 require File.join(File.dirname(__FILE__), 'spec_helper')
17 describe 'Dir.mkdtemp' do
19 if not File.exist? '/tmp'
20 pending "cannot run spec because '/tmp' does not exist"
21 elsif not File.directory? '/tmp'
22 pending "cannot run spec because '/tmp' is not a directory"
23 elsif not File.writable? '/tmp'
24 pending "cannot run spec because '/tmp' is not writeable"
28 it 'should return the modified template path' do
30 path.should be_kind_of(String)
31 path.should_not == '/tmp/temp.XXXXXX'
34 it 'should create the directory corresponding to the template' do
36 File.exist?(path).should == true
37 File.directory?(path).should == true
40 it 'should create the directory with write permissions' do
42 File.writable?(path).should == true
45 it 'should create the directory with read permissions' do
47 File.readable?(path).should == true
50 it 'should create the directory with secure ownership (same user as caller)' do
52 File.owned?(path).should == true
55 it 'should complain if passed a template that includes non-existentent parent directories' do
56 non_existent_dir = '/temp-which-does-not-exist'
57 if File.exist? non_existent_dir
58 pending "cannot run because '#{non_existent_dir}' exists"
60 lambda { Dir.mkdtemp "/#{non_existent_dir}/temp.XXXXXX" }.should raise_error(SystemCallError)
63 it 'should use "/tmp/temp.XXXXXX" as a template if passed nil' do
65 path.should match(%r{\A/tmp/temp\..{6}\z})
66 path.should_not == '/tmp/temp.XXXXXX'
69 it 'should use substitute random characters for the trailing "Xs" in the template' do
70 path = Dir.mkdtemp '/tmp/test.XXXXXX'
71 path.should_not == '/tmp/test.XXXXXX'
74 it 'should leave the prefix portion of the template unchanged' do
75 path = Dir.mkdtemp '/tmp/test.XXXXXX'
76 path.should match(%r{\A/tmp/test\..{6}\z})