forked from puppetlabs/pdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspec_helper.rb
126 lines (102 loc) · 3.16 KB
/
spec_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
if ENV['COVERAGE'] == 'yes'
require 'coveralls'
require 'simplecov'
require 'simplecov-console'
SimpleCov.formatters = [
SimpleCov::Formatter::HTMLFormatter,
SimpleCov::Formatter::Console,
Coveralls::SimpleCov::Formatter,
]
SimpleCov.start do
track_files 'lib/**/*.rb'
add_filter '/spec'
# do not track vendored files
add_filter '/lib/pdk/util/windows'
add_filter '/vendor'
add_filter '/.vendor'
# do not track gitignored files
# this adds about 4 seconds to the coverage check
# this could definitely be optimized
add_filter do |f|
# system returns true if exit status is 0, which with git-check-ignore means file is ignored
system("git check-ignore --quiet #{f.filename}")
end
end
end
$LOAD_PATH.unshift File.expand_path('../lib', __dir__)
require 'pdk'
require 'pdk/cli'
require 'tempfile'
require 'json'
# automatically load any shared examples or contexts
Dir['./spec/support/**/*.rb'].sort.each { |f| require f }
analytics_config = nil
FIXTURES_DIR = File.join(__dir__, 'fixtures')
RSpec.shared_context :stubbed_logger do
let(:logger) { instance_double('PDK::Logger').as_null_object }
before(:each) do |example|
allow(PDK).to receive(:logger).and_return(logger) if example.metadata[:use_stubbed_logger]
end
end
RSpec.shared_context :stubbed_analytics do
let(:analytics) { PDK::Analytics::Client::Noop.new(logger: logger) }
before(:each) do |example|
allow(PDK).to receive(:analytics).and_return(analytics) if example.metadata[:use_stubbed_analytics]
end
end
RSpec.configure do |c|
c.define_derived_metadata do |metadata|
metadata[:use_stubbed_logger] = true unless metadata.key?(:use_stubbed_logger)
metadata[:use_stubbed_analytics] = true unless metadata.key?(:use_stubbed_analytics)
end
c.include_context :stubbed_logger
c.include_context :stubbed_analytics
c.before(:suite) do
analytics_config = Tempfile.new('analytics.yml')
analytics_config.write(YAML.dump(disabled: true))
analytics_config.close
ENV['PDK_ANALYTICS_CONFIG'] = analytics_config.path
end
c.after(:suite) do
analytics_config.unlink
end
# This should catch any tests where we are not mocking out the actual calls to Rubygems.org
c.before(:each) do
allow(Gem::SpecFetcher).to receive(:fetcher).and_raise('Unmocked call to Gem::SpecFetcher.fetcher!')
ENV['PDK_DISABLE_ANALYTICS'] = 'true'
end
c.add_setting :root
c.root = File.dirname(__FILE__)
end
RSpec.shared_context :validators do
let(:validators) do
[
PDK::Validate::MetadataValidator,
PDK::Validate::YAMLValidator,
PDK::Validate::PuppetValidator,
PDK::Validate::RubyValidator,
PDK::Validate::TasksValidator,
]
end
end
# Add method to StringIO needed for TTY::TestPrompt to work on tty-prompt >=
# 0.19 (see https://github.com/piotrmurach/tty-prompt/issues/104)
class StringIO
def wait_readable(*)
true
end
end
module OS
def self.windows?
(%r{cygwin|mswin|mingw|bccwin|wince|emx} =~ RUBY_PLATFORM) != nil
end
def self.mac?
(%r{darwin} =~ RUBY_PLATFORM) != nil
end
def self.unix?
!OS.windows?
end
def self.linux?
OS.unix? && !OS.mac?
end
end