File: empty_directory_spec.rb

package info (click to toggle)
ruby-thor 1.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 924 kB
  • sloc: ruby: 9,491; makefile: 8; sh: 1
file content (129 lines) | stat: -rw-r--r-- 3,667 bytes parent folder | download | duplicates (3)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
require "helper"
require "thor/actions"

describe Thor::Actions::EmptyDirectory do
  before do
    ::FileUtils.rm_rf(destination_root)
  end

  def empty_directory(destination, options = {})
    @action = Thor::Actions::EmptyDirectory.new(base, destination)
  end

  def invoke!
    capture(:stdout) { @action.invoke! }
  end

  def revoke!
    capture(:stdout) { @action.revoke! }
  end

  def base
    @base ||= MyCounter.new([1, 2], {}, destination_root: destination_root)
  end

  describe "#destination" do
    it "returns the full destination with the destination_root" do
      expect(empty_directory("doc").destination).to eq(File.join(destination_root, "doc"))
    end

    it "takes relative root into account" do
      base.inside("doc") do
        expect(empty_directory("contents").destination).to eq(File.join(destination_root, "doc", "contents"))
      end
    end
  end

  describe "#relative_destination" do
    it "returns the relative destination to the original destination root" do
      base.inside("doc") do
        expect(empty_directory("contents").relative_destination).to eq("doc/contents")
      end
    end
  end

  describe "#given_destination" do
    it "returns the destination supplied by the user" do
      base.inside("doc") do
        expect(empty_directory("contents").given_destination).to eq("contents")
      end
    end
  end

  describe "#invoke!" do
    it "copies the file to the specified destination" do
      empty_directory("doc")
      invoke!
      expect(File.exist?(File.join(destination_root, "doc"))).to be true
    end

    it "shows created status to the user" do
      empty_directory("doc")
      expect(invoke!).to eq("      create  doc\n")
    end

    it "does not create a directory if pretending" do
      base.inside("foo", pretend: true) do
        empty_directory("ghost")
      end
      expect(File.exist?(File.join(base.destination_root, "ghost"))).to be false
    end

    describe "when directory exists" do
      it "shows exist status" do
        empty_directory("doc")
        invoke!
        expect(invoke!).to eq("       exist  doc\n")
      end
    end
  end

  describe "#revoke!" do
    it "removes the destination file" do
      empty_directory("doc")
      invoke!
      revoke!
      expect(File.exist?(@action.destination)).to be false
    end
  end

  describe "#exists?" do
    it "returns true if the destination file exists" do
      empty_directory("doc")
      expect(@action.exists?).to be false
      invoke!
      expect(@action.exists?).to be true
    end
  end

  context "protected methods" do
    describe "#convert_encoded_instructions" do
      before do
        empty_directory("test_dir")
        allow(@action.base).to receive(:file_name).and_return("expected")
      end

      it "accepts and executes a 'legal' %\w+% encoded instruction" do
        expect(@action.send(:convert_encoded_instructions, "%file_name%.txt")).to eq("expected.txt")
      end

      it "accepts and executes a private %\w+% encoded instruction" do
        @action.base.extend Module.new {
          def private_file_name
            "expected"
          end
          private :private_file_name
        }
        expect(@action.send(:convert_encoded_instructions, "%private_file_name%.txt")).to eq("expected.txt")
      end

      it "ignores an 'illegal' %\w+% encoded instruction" do
        expect(@action.send(:convert_encoded_instructions, "%some_name%.txt")).to eq("%some_name%.txt")
      end

      it "ignores incorrectly encoded instruction" do
        expect(@action.send(:convert_encoded_instructions, "%some.name%.txt")).to eq("%some.name%.txt")
      end
    end
  end
end