File: isolated_home_directory.rb

package info (click to toggle)
ruby-rspec 3.8.0c0e1m0s0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 6,640 kB
  • sloc: ruby: 65,844; sh: 807; makefile: 99
file content (37 lines) | stat: -rw-r--r-- 1,077 bytes parent folder | download | duplicates (4)
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
require 'tmpdir'
require 'fileutils'
require 'pathname'

RSpec.shared_context "isolated home directory" do
  around do |ex|
    Dir.mktmpdir do |tmp_dir|
      # If user running this test suite has a custom $XDG_CONFIG_HOME, also
      # clear that out when changing $HOME so tests don't touch the user's real
      # configuration files.
      without_env_vars "XDG_CONFIG_HOME" do
        with_env_vars "HOME" => tmp_dir do
          ex.call
        end
      end
    end
  end
end

module HomeFixtureHelpers
  def create_fixture_file(file_name, contents)
    path = Pathname.new(file_name).expand_path
    if !path.exist?
      path.dirname.mkpath
      # Pathname#write does not exist in all supported Ruby versions
      File.open(path.to_s, 'w') { |file| file << contents }
    else
      # Abort just in case we're about to destroy something important.
      raise "File at #{path} already exists!"
    end
  end
end

RSpec.configure do |c|
  c.include_context "isolated home directory", :isolated_home => true
  c.include HomeFixtureHelpers, :isolated_home => true
end