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
-
#get_dir_entries(dir = nil, &select_filter) ⇒ Array<String>
Returns a list of all files, directories, symlinks, etc in a directory tree.
-
#get_dirs(dir = nil) ⇒ Array<String>
Returns a list of all subdirectories in a directory.
-
#get_files(dir = nil) ⇒ Array<String>
Returns a list of all files in a directory tree.
-
#in_tmp_dir? ⇒ Boolean
Indicates whether the current directory is one made by #inside_empty_dir.
-
#inside_empty_dir(dir_name = nil) ⇒ Object
Creates an empty, temporary directory and changes the current directory into it.
-
#step_out_of_tmp_dir ⇒ nil
To be used in conjunction with #inside_empty_dir.
-
#within(timeout) ⇒ Object
Re-runs a given block until it:.
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.
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.
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.
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.
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.
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_dir ⇒ nil
To be used in conjunction with #inside_empty_dir.
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
truewithin a given time period.
16 17 18 |
# File 'lib/golly-utils/testing/rspec/within_time.rb', line 16 def within(timeout) WithinTime.new(timeout) end |