Class: Ace::TestSupport::TestEnvironment
- Inherits:
-
Object
- Object
- Ace::TestSupport::TestEnvironment
- Defined in:
- lib/ace/test_support/test_environment.rb
Overview
Provides isolated test environment for integration testing across all ace-* gems
Instance Attribute Summary collapse
-
#gem_dir ⇒ Object
readonly
Returns the value of attribute gem_dir.
-
#home_dir ⇒ Object
readonly
Returns the value of attribute home_dir.
-
#project_dir ⇒ Object
readonly
Returns the value of attribute project_dir.
-
#temp_dir ⇒ Object
readonly
Returns the value of attribute temp_dir.
Instance Method Summary collapse
-
#chdir(subdir = nil) ⇒ Object
Change to subdirectory.
-
#config_path(type) ⇒ Object
Get config path for type.
-
#create_config_dirs ⇒ Object
Create config directory structure.
-
#create_gem_config_dir ⇒ Object
Create gem config directory.
-
#create_home_config_dir ⇒ Object
Create home config directory.
-
#create_project_config_dir ⇒ Object
Create project config directory.
-
#create_sample_file(path, content = "Sample content") ⇒ Object
Utility method for creating sample files.
-
#create_subdirectory(name) ⇒ Object
Create a subdirectory in project.
-
#initialize(gem_name = "core") ⇒ TestEnvironment
constructor
A new instance of TestEnvironment.
-
#setup ⇒ Object
Set up isolated test environment.
-
#teardown ⇒ Object
Tear down test environment.
-
#verify_structure ⇒ Object
Verify directory structure.
-
#write_config(type, filename, content) ⇒ Object
Write config file to specified location.
-
#write_env_file(filename = ".env", content = "") ⇒ Object
Write .env file to project directory.
Constructor Details
#initialize(gem_name = "core") ⇒ TestEnvironment
Returns a new instance of TestEnvironment.
12 13 14 15 16 |
# File 'lib/ace/test_support/test_environment.rb', line 12 def initialize(gem_name = "core") @gem_name = gem_name @original_env = {} @original_pwd = nil end |
Instance Attribute Details
#gem_dir ⇒ Object (readonly)
Returns the value of attribute gem_dir.
10 11 12 |
# File 'lib/ace/test_support/test_environment.rb', line 10 def gem_dir @gem_dir end |
#home_dir ⇒ Object (readonly)
Returns the value of attribute home_dir.
10 11 12 |
# File 'lib/ace/test_support/test_environment.rb', line 10 def home_dir @home_dir end |
#project_dir ⇒ Object (readonly)
Returns the value of attribute project_dir.
10 11 12 |
# File 'lib/ace/test_support/test_environment.rb', line 10 def project_dir @project_dir end |
#temp_dir ⇒ Object (readonly)
Returns the value of attribute temp_dir.
10 11 12 |
# File 'lib/ace/test_support/test_environment.rb', line 10 def temp_dir @temp_dir end |
Instance Method Details
#chdir(subdir = nil) ⇒ Object
Change to subdirectory
111 112 113 114 115 116 117 |
# File 'lib/ace/test_support/test_environment.rb', line 111 def chdir(subdir = nil) if subdir Dir.chdir(File.join(@project_dir, subdir)) else Dir.chdir(@project_dir) end end |
#config_path(type) ⇒ Object
Get config path for type
120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/ace/test_support/test_environment.rb', line 120 def config_path(type) case type when :project File.join(@project_dir, ".ace", @gem_name) when :home File.join(@home_dir, ".ace", @gem_name) when :gem File.join(@gem_dir, "config", "ace", @gem_name) else raise ArgumentError, "Unknown config type: #{type}" end end |
#create_config_dirs ⇒ Object
Create config directory structure
52 53 54 55 56 |
# File 'lib/ace/test_support/test_environment.rb', line 52 def create_config_dirs create_project_config_dir create_home_config_dir create_gem_config_dir end |
#create_gem_config_dir ⇒ Object
Create gem config directory
73 74 75 76 77 |
# File 'lib/ace/test_support/test_environment.rb', line 73 def create_gem_config_dir config_dir = File.join(@gem_dir, "config", "ace", @gem_name) FileUtils.mkdir_p(config_dir) config_dir end |
#create_home_config_dir ⇒ Object
Create home config directory
66 67 68 69 70 |
# File 'lib/ace/test_support/test_environment.rb', line 66 def create_home_config_dir config_dir = File.join(@home_dir, ".ace", @gem_name) FileUtils.mkdir_p(config_dir) config_dir end |
#create_project_config_dir ⇒ Object
Create project config directory
59 60 61 62 63 |
# File 'lib/ace/test_support/test_environment.rb', line 59 def create_project_config_dir config_dir = File.join(@project_dir, ".ace", @gem_name) FileUtils.mkdir_p(config_dir) config_dir end |
#create_sample_file(path, content = "Sample content") ⇒ Object
Utility method for creating sample files
144 145 146 147 148 149 |
# File 'lib/ace/test_support/test_environment.rb', line 144 def create_sample_file(path, content = "Sample content") full_path = File.join(@project_dir, path) FileUtils.mkdir_p(File.dirname(full_path)) File.write(full_path, content) full_path end |
#create_subdirectory(name) ⇒ Object
Create a subdirectory in project
104 105 106 107 108 |
# File 'lib/ace/test_support/test_environment.rb', line 104 def create_subdirectory(name) path = File.join(@project_dir, name) FileUtils.mkdir_p(path) path end |
#setup ⇒ Object
Set up isolated test environment
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/ace/test_support/test_environment.rb', line 19 def setup @temp_dir = Dir.mktmpdir("ace-#{@gem_name}-test") @home_dir = File.join(@temp_dir, "home") @project_dir = File.join(@temp_dir, "project") @gem_dir = File.join(@temp_dir, "gem") # Create directory structure Dir.mkdir(@home_dir) Dir.mkdir(@project_dir) Dir.mkdir(@gem_dir) # Store original environment @original_env["HOME"] = ENV["HOME"] @original_env["ACE_CONFIG_PATH"] = ENV["ACE_CONFIG_PATH"] @original_pwd = Dir.pwd # Set test environment ENV["HOME"] = @home_dir ENV["ACE_CONFIG_PATH"] = nil Dir.chdir(@project_dir) end |
#teardown ⇒ Object
Tear down test environment
42 43 44 45 46 47 48 49 |
# File 'lib/ace/test_support/test_environment.rb', line 42 def teardown # Restore original environment Dir.chdir(@original_pwd) if @original_pwd && Dir.exist?(@original_pwd) @original_env.each { |k, v| ENV[k] = v } # Clean up temp directory FileUtils.rm_rf(@temp_dir) if @temp_dir && Dir.exist?(@temp_dir) end |
#verify_structure ⇒ Object
Verify directory structure
134 135 136 137 138 139 140 141 |
# File 'lib/ace/test_support/test_environment.rb', line 134 def verify_structure { temp: @temp_dir && Dir.exist?(@temp_dir), home: @home_dir && Dir.exist?(@home_dir), project: @project_dir && Dir.exist?(@project_dir), gem: @gem_dir && Dir.exist?(@gem_dir) } end |
#write_config(type, filename, content) ⇒ Object
Write config file to specified location
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/ace/test_support/test_environment.rb', line 80 def write_config(type, filename, content) path = case type when :project File.join(create_project_config_dir, filename) when :home File.join(create_home_config_dir, filename) when :gem File.join(create_gem_config_dir, filename) else raise ArgumentError, "Unknown config type: #{type}" end File.write(path, content) path end |
#write_env_file(filename = ".env", content = "") ⇒ Object
Write .env file to project directory
97 98 99 100 101 |
# File 'lib/ace/test_support/test_environment.rb', line 97 def write_env_file(filename = ".env", content = "") path = File.join(@project_dir, filename) File.write(path, content) path end |