summaryrefslogtreecommitdiff
path: root/spec/ruby/core/dir/mkdir_spec.rb
blob: d0bf32c143b8ea6177d6ecf1c2d73315186f9441 (plain)
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
require_relative '../../spec_helper'
require_relative 'fixtures/common'

describe "Dir.mkdir" do
  before :all do
    DirSpecs.create_mock_dirs
  end

  after :all do
    DirSpecs.delete_mock_dirs
  end

  it "creates the named directory with the given permissions" do
    DirSpecs.clear_dirs

    begin
      File.exist?('nonexisting').should == false
      Dir.mkdir 'nonexisting'
      File.exist?('nonexisting').should == true
      platform_is_not :windows do
        Dir.mkdir 'default_perms'
        a = File.stat('default_perms').mode
        Dir.mkdir 'reduced', (a - 1)
        File.stat('reduced').mode.should_not == a
      end
      platform_is :windows do
        Dir.mkdir 'default_perms', 0666
        a = File.stat('default_perms').mode
        Dir.mkdir 'reduced', 0444
        File.stat('reduced').mode.should_not == a
      end

      Dir.mkdir('always_returns_0').should == 0
      platform_is_not(:windows) do
        File.chmod(0777, "nonexisting","default_perms","reduced","always_returns_0")
      end
      platform_is_not(:windows) do
        File.chmod(0644, "nonexisting","default_perms","reduced","always_returns_0")
      end
    ensure
      DirSpecs.clear_dirs
    end
  end

  it "calls #to_path on non-String arguments" do
    DirSpecs.clear_dirs
    p = mock('path')
    p.should_receive(:to_path).and_return('nonexisting')
    Dir.mkdir(p)
    DirSpecs.clear_dirs
  end

  it "raises a SystemCallError if any of the directories in the path before the last does not exist" do
    lambda { Dir.mkdir "#{DirSpecs.nonexistent}/subdir" }.should raise_error(SystemCallError)
  end

  it "raises Errno::EEXIST if the specified directory already exists" do
    lambda { Dir.mkdir("#{DirSpecs.mock_dir}/dir") }.should raise_error(Errno::EEXIST)
  end

  it "raises Errno::EEXIST if the argument points to the existing file" do
    lambda { Dir.mkdir("#{DirSpecs.mock_dir}/file_one.ext") }.should raise_error(Errno::EEXIST)
  end
end

# The permissions flag are not supported on Windows as stated in documentation:
# The permissions may be modified by the value of File.umask, and are ignored on NT.
platform_is_not :windows do
  as_user do
    describe "Dir.mkdir" do
      before :each do
        @dir = tmp "noperms"
      end

      after :each do
        File.chmod 0777, @dir
        rm_r @dir
      end

      it "raises a SystemCallError when lacking adequate permissions in the parent dir" do
        Dir.mkdir @dir, 0000

        lambda { Dir.mkdir "#{@dir}/subdir" }.should raise_error(SystemCallError)
      end
    end
  end
end