Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions lib/codex_notify/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,19 @@ def app_root
APP_ROOT
end

def resolve_env_path(path = DEFAULT_ENV_PATH)
def resolve_env_paths(path = DEFAULT_ENV_PATH)
env_path = Pathname(path)
return env_path if env_path.absolute?
return [env_path] if env_path.absolute?

[Pathname(Dir.pwd).join(env_path), app_root.join(env_path)].find(&:exist?)
[Pathname(Dir.pwd).join(env_path), app_root.join(env_path)].select(&:exist?).uniq
end

def load_env_file(path = DEFAULT_ENV_PATH, override: false)
env_path = resolve_env_path(path)
return unless env_path
return unless env_path.exist?
env_paths = resolve_env_paths(path)
return if env_paths.empty?

loader = override ? Dotenv.method(:overload) : Dotenv.method(:load)
loader.call(env_path.to_s)
loader.call(*env_paths.map(&:to_s))
end

def system_user_name
Expand Down
13 changes: 6 additions & 7 deletions lib/codex_notify/hook_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,19 @@ def app_root
APP_ROOT
end

def resolve_env_path(path = DEFAULT_ENV_PATH)
def resolve_env_paths(path = DEFAULT_ENV_PATH)
env_path = Pathname(path)
return env_path if env_path.absolute?
return [env_path] if env_path.absolute?

[Pathname(Dir.pwd).join(env_path), app_root.join(env_path)].find(&:exist?)
[Pathname(Dir.pwd).join(env_path), app_root.join(env_path)].select(&:exist?).uniq
end

def load_env_file(path = DEFAULT_ENV_PATH, override: false)
env_path = resolve_env_path(path)
return unless env_path
return unless env_path.exist?
env_paths = resolve_env_paths(path)
return if env_paths.empty?

loader = override ? Dotenv.method(:overload) : Dotenv.method(:load)
loader.call(env_path.to_s)
loader.call(*env_paths.map(&:to_s))
end

def system_user_name
Expand Down
28 changes: 28 additions & 0 deletions test/test_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,34 @@ def test_load_env_file_falls_back_to_app_root_for_relative_path
assert_equal 'CROOT', ENV['SLACK_CHANNEL']
end

def test_load_env_file_merges_cwd_and_app_root_relative_paths
with_tmpdir do |app_root|
app_root.join('.env').write("SLACK_BOT_TOKEN=xoxb-app-root\nSLACK_CHANNEL=CROOT\n")
ENV.delete('SLACK_BOT_TOKEN')
ENV.delete('SLACK_CHANNEL')

original = Config.method(:app_root)
Dir.mktmpdir do |cwd|
Pathname(cwd).join('.env').write("CODEX_NOTIFY_USER_NAME=cwd-user\n")

Dir.chdir(cwd) do
with_silenced_warnings do
Config.singleton_class.send(:define_method, :app_root) { app_root }
end

args = Config.parse_args([])
assert_equal 'xoxb-app-root', args.token
assert_equal 'CROOT', args.channel
assert_equal 'cwd-user', args.user_name
end
end
ensure
with_silenced_warnings do
Config.singleton_class.send(:define_method, :app_root, original)
end
end
end

def test_parse_args_prefers_cli_user_name_over_env
ENV['CODEX_NOTIFY_USER_NAME'] = 'env-user'

Expand Down
29 changes: 29 additions & 0 deletions test/test_hook_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,35 @@ def test_parse_args_falls_back_to_app_root_env_for_relative_path
end
end

def test_parse_args_merges_cwd_and_app_root_relative_paths
with_tmpdir do |app_root|
app_root.join('.env').write("SLACK_BOT_TOKEN=xoxb-hook\nSLACK_CHANNEL=CHOOK\n")
ENV.delete('SLACK_BOT_TOKEN')
ENV.delete('SLACK_CHANNEL')
ENV.delete('CODEX_NOTIFY_USER_NAME')

original = HookConfig.method(:app_root)
Dir.mktmpdir do |cwd|
Pathname(cwd).join('.env').write("CODEX_NOTIFY_USER_NAME=cwd-user\n")

Dir.chdir(cwd) do
with_silenced_warnings do
HookConfig.singleton_class.send(:define_method, :app_root) { app_root }
end

args = HookConfig.parse_args([])
assert_equal 'xoxb-hook', args.token
assert_equal 'CHOOK', args.channel
assert_equal 'cwd-user', args.user_name
end
end
ensure
with_silenced_warnings do
HookConfig.singleton_class.send(:define_method, :app_root, original)
end
end
end

private

def with_tmpdir
Expand Down
Loading