1 # Copyright 2008-2010 Wincent Colaiuta. All rights reserved.
3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions are met:
6 # 1. Redistributions of source code must retain the above copyright notice,
7 # this list of conditions and the following disclaimer.
8 # 2. Redistributions in binary form must reproduce the above copyright notice,
9 # this list of conditions and the following disclaimer in the documentation
10 # and/or other materials provided with the distribution.
12 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
13 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
15 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
16 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
17 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
18 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
19 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
20 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
21 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
22 # POSSIBILITY OF SUCH DAMAGE.
24 require File.join(File.dirname(__FILE__), 'spec_helper')
26 describe 'Dir.mkdtemp' do
28 if not File.exist? '/tmp'
29 pending "cannot run spec because '/tmp' does not exist"
30 elsif not File.directory? '/tmp'
31 pending "cannot run spec because '/tmp' is not a directory"
32 elsif not File.writable? '/tmp'
33 pending "cannot run spec because '/tmp' is not writeable"
37 it 'should return the modified template path' do
39 path.should be_kind_of(String)
40 path.should_not == '/tmp/temp.XXXXXX'
43 it 'should create the directory corresponding to the template' do
45 File.exist?(path).should == true
46 File.directory?(path).should == true
49 it 'should create the directory with write permissions' do
51 File.writable?(path).should == true
54 it 'should create the directory with read permissions' do
56 File.readable?(path).should == true
59 it 'should create the directory with secure ownership (same user as caller)' do
61 File.owned?(path).should == true
64 it 'should complain if passed a template that includes non-existentent parent directories' do
65 non_existent_dir = '/temp-which-does-not-exist'
66 if File.exist? non_existent_dir
67 pending "cannot run because '#{non_existent_dir}' exists"
69 lambda { Dir.mkdtemp "/#{non_existent_dir}/temp.XXXXXX" }.should raise_error(SystemCallError)
72 it 'should use "/tmp/temp.XXXXXX" as a template if passed nil' do
74 path.should match(%r{\A/tmp/temp\..{6}\z})
75 path.should_not == '/tmp/temp.XXXXXX'
78 it 'should use substitute random characters for the trailing "Xs" in the template' do
79 path = Dir.mkdtemp '/tmp/test.XXXXXX'
80 path.should_not == '/tmp/test.XXXXXX'
83 it 'should leave the prefix portion of the template unchanged' do
84 path = Dir.mkdtemp '/tmp/test.XXXXXX'
85 path.should match(%r{\A/tmp/test\..{6}\z})