Module: GollyUtils::Testing::Helpers

Defined in:
lib/golly-utils/testing/helpers_base.rb,
lib/golly-utils/testing/rspec/within_time.rb,
lib/golly-utils/testing/file_helpers.rb

Defined Under Namespace

Modules: ClassMethods Classes: WithinTime

Instance Method Summary collapse

Instance Method Details

#get_dir_entries(dir = nil, &select_filter) ⇒ Array<String>

Returns a list of all files, directories, symlinks, etc in a directory tree.

Parameters:

  • dir (String, nil) (defaults to: nil)

    The directory to inspect, or nil to indicate the current directory.

  • select_filter

    An optional filter to be applied to each entry where negative calls result in the entry being discarded.

Returns:

  • (Array<String>)

    A sorted array of dir entries. Filenames will be relative to the provided directory. . and .. will never be returned.



106
107
108
109
110
111
112
113
114
115
# File 'lib/golly-utils/testing/file_helpers.rb', line 106

def get_dir_entries(dir=nil, &select_filter)
  if dir
    Dir.chdir(dir){ get_dir_entries &select_filter }
  else
    Dir.glob('**/*',File::FNM_DOTMATCH)
      .reject{|f| /(?:^|[\\\/]+)\.{1,2}$/ === f } # Ignore:  .  ..  dir/.  dir/..
      .select{|f| select_filter ? select_filter.call(f) : true }
      .sort
  end
end

#get_dirs(dir = nil) ⇒ Array<String>

Returns a list of all subdirectories in a directory.

Parameters:

  • dir (String, nil) (defaults to: nil)

    The directory to inspect, or nil to indicate the current directory.

Returns:

  • (Array<String>)

    A sorted array of dirs. Filenames will be relative to the provided directory. . and .. will never be returned.



95
96
97
# File 'lib/golly-utils/testing/file_helpers.rb', line 95

def get_dirs(dir=nil)
  get_dir_entries(dir){|f| File.directory? f }
end

#get_files(dir = nil) ⇒ Array<String>

Returns a list of all files in a directory tree.

Parameters:

  • dir (String, nil) (defaults to: nil)

    The directory to inspect, or nil to indicate the current directory.

Returns:

  • (Array<String>)

    A sorted array of files. Filenames will be relative to the provided directory.



86
87
88
# File 'lib/golly-utils/testing/file_helpers.rb', line 86

def get_files(dir=nil)
  get_dir_entries(dir){|f| File.file? f }
end

#in_tmp_dir?Boolean

Indicates whether the current directory is one made by #inside_empty_dir.

Returns:

  • (Boolean)


57
58
59
# File 'lib/golly-utils/testing/file_helpers.rb', line 57

def in_tmp_dir?
  @tmp_dir_stack && !@tmp_dir_stack.empty? or false
end

#inside_empty_dir(dir_name = nil) {|dir| ... } ⇒ Object #inside_empty_dir(dir_name = nil) ⇒ String

Creates an empty, temporary directory and changes the current directory into it.

Examples:

puts Dir.pwd        # => /home/david/my_project
inside_empty_dir {
  puts Dir.pwd      # => /tmp/abcdef123567
}
                    # Empty directory now removed
puts Dir.pwd        # => /home/david/my_project

Overloads:

  • #inside_empty_dir(dir_name = nil) {|dir| ... } ⇒ Object

    When a block is given, after yielding, the current directory is restored and the temp directory deleted.

    Yield Parameters:

    • dir (String)

      The newly-created temp dir.

    Returns:

    • The result of yielding.

  • #inside_empty_dir(dir_name = nil) ⇒ String

    When no block is given the current directory is not restored and the temp directory not deleted.

    Returns:

    • (String)

      The newly-created temp dir.

Parameters:

  • dir_name (nil|String) (defaults to: nil)

    If not nil, then the empty directory name will be set to the provided value.



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
# File 'lib/golly-utils/testing/file_helpers.rb', line 24

def inside_empty_dir(dir_name=nil)
  if block_given?
    Dir.mktmpdir {|dir|
      if dir_name
        dir= File.join dir, dir_name
        Dir.mkdir dir
      end
      Dir.chdir(dir) {
        (@tmp_dir_stack ||= [])<< :inside_empty_dir
        begin
          return yield dir
        ensure
          @tmp_dir_stack.pop
        end
      }
    }
  else
    x= {}
    x[:old_dir]= Dir.pwd
    x[:tmp_dir]= Dir.mktmpdir
    if dir_name
      x[:tmp_dir]= File.join x[:tmp_dir], dir_name
      Dir.mkdir x[:tmp_dir]
    end
    Dir.chdir x[:tmp_dir]
    (@tmp_dir_stack ||= [])<< x
    x[:tmp_dir]
  end
end

#step_out_of_tmp_dirnil

To be used in conjunction with #inside_empty_dir.

Examples:

inside_empty_dir
begin
  # Do stuff in empty dir
ensure
  step_out_of_tmp_dir
end

Returns:

  • (nil)


72
73
74
75
76
77
78
79
80
# File 'lib/golly-utils/testing/file_helpers.rb', line 72

def step_out_of_tmp_dir
  if @tmp_dir_stack
    x= @tmp_dir_stack.pop
    raise "You cannot call step_out_of_tmp_dir() within the yield block of #{x}()" if x.is_a?(Symbol)
    Dir.chdir x[:old_dir]
    FileUtils.rm_rf x[:tmp_dir]
  end
  nil
end

#within(timeout) ⇒ Object

Re-runs a given block until it:

  • indicates success by returning true
  • fails by not returning true within a given time period.

Examples:

within(5).seconds{ 'Gemfile'.should exist_as_a_file }

Parameters:

  • timeout (Numeric)

    The maximum number of time units (to be specified after a call to this) to wait for a condition to be met.

See Also:



16
17
18
# File 'lib/golly-utils/testing/rspec/within_time.rb', line 16

def within(timeout)
  WithinTime.new(timeout)
end