File: core_spec.rb

package info (click to toggle)
ruby-aruba 2.1.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,972 kB
  • sloc: ruby: 7,151; javascript: 6,850; makefile: 5
file content (286 lines) | stat: -rw-r--r-- 8,448 bytes parent folder | download | duplicates (2)
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
require "spec_helper"
require "aruba/api"
require "fileutils"

RSpec.describe Aruba::Api::Core do
  include_context "uses aruba API"

  describe "#cd" do
    before do
      @directory_name = "test_dir"
      @directory_path = File.join(@aruba.aruba.current_directory, @directory_name)
    end

    context "with a block given" do
      it "runs the passed block in the given directory" do
        @aruba.create_directory @directory_name
        full_path = File.expand_path(@directory_path)
        @aruba.cd @directory_name do
          expect(Dir.pwd).to eq full_path
        end
        expect(Dir.pwd).not_to eq full_path
      end

      it "sets directory environment in the passed block" do
        @aruba.create_directory @directory_name
        old_pwd = Dir.pwd
        full_path = File.expand_path(@directory_path)
        @aruba.cd @directory_name do
          expect(ENV["PWD"]).to eq full_path
          expect(ENV["OLDPWD"]).to eq old_pwd
        end
      end

      it "sets aruba environment in the passed block" do
        @aruba.create_directory @directory_name
        @aruba.set_environment_variable("FOO", "bar")
        @aruba.cd @directory_name do
          expect(ENV["FOO"]).to eq "bar"
        end
      end

      it "does not touch other environment variables in the passed block" do
        keys = ENV.keys - %w(PWD OLDPWD)
        old_values = ENV.values_at(*keys)
        @aruba.create_directory @directory_name
        @aruba.cd @directory_name do
          expect(ENV.values_at(*keys)).to eq old_values
        end
      end

      it 'expands "~" to the aruba home directory' do
        full_path = aruba.config.home_directory
        @aruba.cd "~" do
          expect(Dir.pwd).to eq full_path
        end
        expect(Dir.pwd).not_to eq full_path
      end
    end

    context "with no block given" do
      it "sets aruba's current directory to the given directory" do
        @aruba.create_directory @directory_name
        full_path = File.expand_path(@directory_path)
        @aruba.cd @directory_name
        expect(File.expand_path(@aruba.aruba.current_directory)).to eq full_path
      end

      it 'expands "~" to the aruba home directory' do
        full_path = aruba.config.home_directory
        @aruba.cd "~"
        expect(File.expand_path(@aruba.aruba.current_directory)).to eq full_path
      end
    end
  end

  describe "#expand_path" do
    context "when file_name is given" do
      it { expect(@aruba.expand_path(@file_name)).to eq File.expand_path(@file_path) }
    end

    context "when an absolute file_path is given" do
      let(:logger) { @aruba.aruba.logger }

      before do
        allow(logger).to receive :warn
      end

      it "raises a UserError" do
        expect { @aruba.expand_path(@file_path) }.to raise_error Aruba::UserError
      end

      it "does not raise an error if told not to" do
        @aruba.aruba.config.allow_absolute_paths = true
        expect { @aruba.expand_path(@file_path) }.not_to raise_error
      end
    end

    context 'when path contains "."' do
      it { expect(@aruba.expand_path(".")).to eq File.expand_path(aruba.current_directory) }
    end

    context 'when path contains ".."' do
      it {
        expect(@aruba.expand_path("path/.."))
          .to eq File.expand_path(File.join(aruba.current_directory))
      }
    end

    context "when path is nil" do
      it { expect { @aruba.expand_path(nil) }.to raise_error ArgumentError }
    end

    context "when path is empty" do
      it { expect { @aruba.expand_path("") }.to raise_error ArgumentError }
    end

    context "when second argument is given" do
      it "behaves similar to File.expand_path" do
        expect(@aruba.expand_path(@file_name, "path"))
          .to eq File.expand_path(File.join(aruba.current_directory, "path", @file_name))
      end
    end

    context 'when file_name contains fixtures "%" string' do
      it "finds files in the fixtures directory" do
        expect(@aruba.expand_path("%/cli-app"))
          .to eq File.expand_path("cli-app", File.join(aruba.fixtures_directory))
      end
    end
  end

  describe "#in_current_directory" do
    let(:directory_path) { @aruba.aruba.current_directory }
    let!(:full_path) { File.expand_path(directory_path) }

    context "with a block given" do
      it "runs the passed block in the given directory" do
        @aruba.in_current_directory do
          expect(Dir.pwd).to eq full_path
        end
        expect(Dir.pwd).not_to eq full_path
      end

      it "sets directory environment in the passed block" do
        old_pwd = Dir.pwd
        @aruba.in_current_directory do
          expect(ENV["PWD"]).to eq full_path
          expect(ENV["OLDPWD"]).to eq old_pwd
        end
      end

      it "sets aruba environment in the passed block" do
        @aruba.set_environment_variable("FOO", "bar")
        @aruba.in_current_directory do
          expect(ENV["FOO"]).to eq "bar"
        end
      end

      it "does not touch other environment variables in the passed block" do
        keys = ENV.keys - %w(PWD OLDPWD)
        old_values = ENV.values_at(*keys)
        @aruba.in_current_directory do
          expect(ENV.values_at(*keys)).to eq old_values
        end
      end
    end
  end

  describe "#with_environment" do
    it "modifies env for block" do
      variable = "THIS_IS_A_ENV_VAR"
      ENV[variable] = "1"

      @aruba.with_environment variable => "0" do
        expect(ENV[variable]).to eq "0"
      end

      expect(ENV[variable]).to eq "1"
    end

    it "modifies env for recursive calls block" do
      variable = "THIS_IS_A_ENV_VAR"
      ENV[variable] = "1"

      @aruba.with_environment variable => "0" do
        @aruba.with_environment do
          expect(ENV[variable]).to eq "0"
        end
      end

      expect(ENV[variable]).to eq "1"
    end

    it "works together with #set_environment_variable" do
      variable = "THIS_IS_A_ENV_VAR"
      @aruba.set_environment_variable variable, "1"

      @aruba.with_environment do
        expect(ENV[variable]).to eq "1"
        @aruba.set_environment_variable variable, "0"
        @aruba.with_environment do
          expect(ENV[variable]).to eq "0"
        end
        expect(ENV[variable]).to eq "1"
      end
    end

    it "works with a mix of ENV and #set_environment_variable" do
      variable = "THIS_IS_A_ENV_VAR"
      @aruba.set_environment_variable variable, "1"
      ENV[variable] = "2"
      expect(ENV[variable]).to eq "2"

      @aruba.with_environment do
        expect(ENV[variable]).to eq "1"
        @aruba.set_environment_variable variable, "0"
        @aruba.with_environment do
          expect(ENV[variable]).to eq "0"
        end
        expect(ENV[variable]).to eq "1"
      end
      expect(ENV[variable]).to eq "2"
    end

    it "works with a mix of argument and #set_environment_variable" do
      variable = "THIS_IS_A_ENV_VAR"

      @aruba.set_environment_variable variable, "1"
      @aruba.with_environment variable => "2" do
        expect(ENV[variable]).to eq "2"
        @aruba.with_environment do
          expect(ENV[variable]).to eq "2"
        end
      end
    end

    it "works together with #delete_environment_variable" do
      variable = "THIS_IS_A_ENV_VAR"
      ENV[variable] = "2"
      @aruba.delete_environment_variable variable

      @aruba.with_environment do
        expect(ENV[variable]).to be_nil
      end
      expect(ENV[variable]).to eq "2"
    end

    it "works with #delete_environment_variable when called several times" do
      variable = "THIS_IS_A_ENV_VAR"
      ENV[variable] = "2"
      @aruba.delete_environment_variable variable

      @aruba.with_environment do
        expect(ENV[variable]).to be_nil
      end

      @aruba.with_environment do
        expect(ENV[variable]).to be_nil
      end
      expect(ENV[variable]).to eq "2"
    end

    it "forgets passed argument when called again" do
      variable = "THIS_IS_A_ENV_VAR"

      @aruba.with_environment variable => 2 do
        expect(ENV[variable]).to eq "2"
      end

      @aruba.with_environment do
        expect(ENV[variable]).to be_nil
      end
    end

    it "keeps values not set in argument" do
      variable = "THIS_IS_A_ENV_VAR"
      ENV[variable] = "2"
      expect(ENV[variable]).to eq "2"

      @aruba.with_environment do
        expect(ENV[variable]).to eq "2"
      end
      expect(ENV[variable]).to eq "2"
    end
  end
end