Module: Ace::TestSupport::TestHelper
- Included in:
- BaseTestCase
- Defined in:
- lib/ace/test_support/test_helper.rb
Overview
Test helper methods for all ace-* gems
Instance Method Summary collapse
- #assert_directory_exists(path, message = nil) ⇒ Object
- #assert_file_content(path, expected_content, message = nil) ⇒ Object
- #assert_file_exists(path, message = nil) ⇒ Object
-
#capture_stderr ⇒ Object
Capture only stderr and return as string (convenience wrapper).
-
#capture_stdout ⇒ Object
Capture only stdout and return as string (convenience wrapper).
- #capture_subprocess_io ⇒ Object
-
#clear_project_root_cache ⇒ Object
Clear ProjectRootFinder cache (safe to call even if ace-support-fs not loaded).
- #create_config_file(path, content) ⇒ Object
- #with_temp_dir ⇒ Object
- #with_temp_file(content = "") ⇒ Object
Instance Method Details
#assert_directory_exists(path, message = nil) ⇒ Object
56 57 58 |
# File 'lib/ace/test_support/test_helper.rb', line 56 def assert_directory_exists(path, = nil) assert Dir.exist?(path), || "Expected directory #{path} to exist" end |
#assert_file_content(path, expected_content, message = nil) ⇒ Object
50 51 52 53 54 |
# File 'lib/ace/test_support/test_helper.rb', line 50 def assert_file_content(path, expected_content, = nil) assert_file_exists(path, ) actual = File.read(path) assert_equal expected_content, actual, || "File #{path} content mismatch" end |
#assert_file_exists(path, message = nil) ⇒ Object
46 47 48 |
# File 'lib/ace/test_support/test_helper.rb', line 46 def assert_file_exists(path, = nil) assert File.exist?(path), || "Expected file #{path} to exist" end |
#capture_stderr ⇒ Object
Capture only stderr and return as string (convenience wrapper)
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/ace/test_support/test_helper.rb', line 93 def capture_stderr require "stringio" original_stderr = $stderr $stderr = StringIO.new yield $stderr.string ensure $stderr = original_stderr end |
#capture_stdout ⇒ Object
Capture only stdout and return as string (convenience wrapper)
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/ace/test_support/test_helper.rb', line 81 def capture_stdout require "stringio" original_stdout = $stdout $stdout = StringIO.new yield $stdout.string ensure $stdout = original_stdout end |
#capture_subprocess_io ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/ace/test_support/test_helper.rb', line 60 def capture_subprocess_io require "stringio" captured_stdout = StringIO.new captured_stderr = StringIO.new orig_stdout = $stdout orig_stderr = $stderr $stdout = captured_stdout $stderr = captured_stderr yield [captured_stdout.string, captured_stderr.string] ensure $stdout = orig_stdout $stderr = orig_stderr end |
#clear_project_root_cache ⇒ Object
Clear ProjectRootFinder cache (safe to call even if ace-support-fs not loaded)
24 25 26 27 28 29 30 31 |
# File 'lib/ace/test_support/test_helper.rb', line 24 def clear_project_root_cache if defined?(Ace::Support::Fs::Molecules::ProjectRootFinder) Ace::Support::Fs::Molecules::ProjectRootFinder.clear_cache! end if defined?(Ace::Bundle) && Ace::Bundle.respond_to?(:reset_config!) Ace::Bundle.reset_config! end end |
#create_config_file(path, content) ⇒ Object
41 42 43 44 |
# File 'lib/ace/test_support/test_helper.rb', line 41 def create_config_file(path, content) FileUtils.mkdir_p(File.dirname(path)) File.write(path, content) end |
#with_temp_dir ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/ace/test_support/test_helper.rb', line 7 def with_temp_dir Dir.mktmpdir do |dir| original_pwd = Dir.pwd Dir.chdir(dir) # Clear ProjectRootFinder cache when changing directories # This ensures consistent behavior in tests clear_project_root_cache yield dir ensure Dir.chdir(original_pwd) clear_project_root_cache end end |
#with_temp_file(content = "") ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/ace/test_support/test_helper.rb', line 33 def with_temp_file(content = "") Tempfile.create do |file| file.write(content) file.flush yield file.path end end |