Commit 9ce64d70 authored by Thong Kuah's avatar Thong Kuah 🌏

Add rspec tests for env vars

Moved env vars to GDK::Env as it's impossible to require lib/run without
side-effects (so I can test)

Fix editorconfig to ignore Gemfile.lock
parent d43194b2
......@@ -34,6 +34,13 @@ insert_final_newline = unset
indent_size = unset
indent_style = unset
# Gemfile.lock is auto-generated.
# It mostly uses 2 spaces but
# BUNDLED_WITH section has 3 spaces :(
[Gemfile.lock]
indent_size = unset
indent_style = unset
[**/.gitkeep]
end_of_line = unset
trim_trailing_whitespace = unset
......
......@@ -80,6 +80,14 @@ build:image:
paths:
- ./latest_image.tar
rspec:
image: ruby:2.5-alpine
stage: test
script:
- gem install bundler -N
- bundle install
- bundle exec rspec
test:install:
image: $TEST_IMAGE
stage: test
......
--require spec_helper
-I lib
# frozen_string_literal: true
source 'https://rubygems.org'
gem 'rspec'
GEM
remote: https://rubygems.org/
specs:
diff-lcs (1.3)
rspec (3.8.0)
rspec-core (~> 3.8.0)
rspec-expectations (~> 3.8.0)
rspec-mocks (~> 3.8.0)
rspec-core (3.8.0)
rspec-support (~> 3.8.0)
rspec-expectations (3.8.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.8.0)
rspec-mocks (3.8.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.8.0)
rspec-support (3.8.0)
PLATFORMS
ruby
DEPENDENCIES
rspec
BUNDLED WITH
2.0.1
......@@ -13,6 +13,22 @@ module GDK
end
end
def set_env_vars
# Try to read the gitlab-workhorse host:port from the environments
# Otherwise fallback to localhost:3000
unless ENV['host']
ENV['host'] = read_file('host') || 'localhost'
end
unless ENV['port']
ENV['port'] = read_file('port') || '3000'
end
unless ENV['relative_url_root']
ENV['relative_url_root'] = read_file('relative_url_root') || '/'
end
end
private
def print_env
......@@ -26,6 +42,12 @@ module GDK
Kernel::exec(env, *argv)
end
def read_file(filename)
File.read(filename).chomp if File.exist?(filename)
rescue Errno::ENOENT
# return nil
end
def env
case get_project
when 'gitaly'
......
def set_env_vars
# Try to read the gitlab-workhorse host:port from the environments
# Otherwise fallback to localhost:3000
unless ENV['host']
ENV['host'] = read_file('host') || 'localhost'
end
unless ENV['port']
ENV['port'] = read_file('port') || '3000'
end
unless ENV['relative_url_root']
ENV['relative_url_root'] = read_file('relative_url_root') || '/'
end
end
require_relative 'gdk/env'
def main(argv)
set_env_vars
GDK::Env.set_env_vars
case argv[0]
when 'db'
......@@ -50,12 +36,6 @@ def main(argv)
end
end
def read_file(filename)
File.read(filename).chomp if File.exist?(filename)
rescue Errno::ENOENT
# return nil
end
def foreman_exec(svcs = [], exclude: [])
args = %w[foreman start]
unless svcs.empty? && exclude.empty?
......
# frozen_string_literal: true
require 'spec_helper'
require 'gdk/env'
describe GDK::Env do
describe 'env vars' do
subject { described_class.set_env_vars }
shared_context 'file does not exist' do |filename|
before do
allow(File).to receive(:exist?).and_call_original
expect(File).to receive(:exist?).with(filename).and_return(false)
end
end
shared_context 'file exists' do |filename|
before do
allow(File).to receive(:exist?).and_call_original
allow(File).to receive(:read).and_call_original
expect(File).to receive(:exist?).with(filename).and_return(true)
expect(File).to receive(:read).with(filename).and_return('file-value')
end
end
before do
@original_env = ENV.to_hash
end
after do
ENV.replace(@original_env)
end
describe 'host' do
context 'file does not exist' do
include_context 'file does not exist', 'host'
it 'defaults to localhost' do
subject
expect(ENV['host']).to eq('localhost')
end
end
context 'file exists' do
include_context 'file exists', 'host'
it 'uses the value of the file' do
subject
expect(ENV['host']).to eq('file-value')
end
end
end
describe 'port' do
context 'file does not exist' do
include_context 'file does not exist', 'port'
it 'defaults to 3000' do
subject
expect(ENV['port']).to eq('3000')
end
end
context 'file exists' do
include_context 'file exists', 'port'
it 'uses the value of the file' do
subject
expect(ENV['port']).to eq('file-value')
end
end
end
describe 'relative_url_root' do
context 'file does not exist' do
include_context 'file does not exist', 'relative_url_root'
it 'defaults to /' do
subject
expect(ENV['relative_url_root']).to eq('/')
end
end
context 'file exists' do
include_context 'file exists', 'relative_url_root'
it 'uses the value of the file' do
subject
expect(ENV['relative_url_root']).to eq('file-value')
end
end
end
end
end
# Given that it is always loaded, you are encouraged to keep this file as
# light-weight as possible. Requiring heavyweight dependencies from this file
# will add to the boot time of your test suite on EVERY test run, even for an
# individual file that may not need all of that loaded. Instead, consider making
# a separate helper file that requires the additional dependencies and performs
# the additional setup, and require it from the spec files that actually need
# it.
#
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
config.expect_with :rspec do |expectations|
# This option will default to `true` in RSpec 4. It makes the `description`
# and `failure_message` of custom matchers include text for helper methods
# defined using `chain`, e.g.:
# be_bigger_than(2).and_smaller_than(4).description
# # => "be bigger than 2 and smaller than 4"
# ...rather than:
# # => "be bigger than 2"
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
# Prevents you from mocking or stubbing a method that does not exist on
# a real object. This is generally recommended, and will default to
# `true` in RSpec 4.
mocks.verify_partial_doubles = true
end
# This option will default to `:apply_to_host_groups` in RSpec 4 (and will
# have no way to turn it off -- the option exists only for backwards
# compatibility in RSpec 3). It causes shared context metadata to be
# inherited by the metadata hash of host groups and examples, rather than
# triggering implicit auto-inclusion in groups with matching metadata.
config.shared_context_metadata_behavior = :apply_to_host_groups
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment