Module: PuppetlabsSpecHelper::Tasks::FixtureHelpers
- Defined in:
- lib/puppetlabs_spec_helper/tasks/fixtures.rb
Instance Method Summary collapse
- #auto_symlink ⇒ Object
- #clone_repo(scm, remote, target, _subdir = nil, ref = nil, branch = nil, flags = nil) ⇒ Object
-
#current_thread_count(items) ⇒ Object
returns the current thread count that is currently active a status of false or nil means the thread completed so when anything else we count that as a active thread.
- #fixtures(category) ⇒ Object
- #git_remote_url(target) ⇒ Object
- #include_repo?(version_range) ⇒ Boolean
-
#logger ⇒ Object
creates a logger so we can log events with certain levels.
-
#max_thread_limit ⇒ Object
returns the max_thread_count because we may want to limit ssh or https connections.
- #module_name ⇒ Object
- #module_working_directory ⇒ Object
- #remove_subdirectory(target, subdir) ⇒ Object
-
#repositories ⇒ Object
cache the repositories and return a hash object.
- #revision(scm, target, ref) ⇒ Object
- #shallow_git_repo? ⇒ Boolean
-
#source_dir ⇒ Object
This is a helper for the self-symlink entry of fixtures.yml.
- #update_repo(scm, target) ⇒ Object
- #valid_repo?(scm, target, remote) ⇒ Boolean
Instance Method Details
#auto_symlink ⇒ Object
34 35 36 |
# File 'lib/puppetlabs_spec_helper/tasks/fixtures.rb', line 34 def auto_symlink { module_name => '#{source_dir}' } end |
#clone_repo(scm, remote, target, _subdir = nil, ref = nil, branch = nil, flags = nil) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/puppetlabs_spec_helper/tasks/fixtures.rb', line 131 def clone_repo(scm, remote, target, _subdir = nil, ref = nil, branch = nil, flags = nil) args = [] case scm when 'hg' args.push('clone') args.push('-b', branch) if branch args.push(flags) if flags args.push(remote, target) when 'git' args.push('clone') args.push('--depth 1') unless ref args.push('-b', branch) if branch args.push(flags) if flags args.push(remote, target) else raise "Unfortunately #{scm} is not supported yet" end result = system("#{scm} #{args.flatten.join ' '}") unless File.exist?(target) raise "Failed to clone #{scm} repository #{remote} into #{target}" end result end |
#current_thread_count(items) ⇒ Object
returns the current thread count that is currently active a status of false or nil means the thread completed so when anything else we count that as a active thread
238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/puppetlabs_spec_helper/tasks/fixtures.rb', line 238 def current_thread_count(items) active_threads = items.find_all do |_item, opts| if opts[:thread] opts[:thread].status else false end end logger.debug "Current thread count #{active_threads.count}" active_threads.count end |
#fixtures(category) ⇒ Object
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 |
# File 'lib/puppetlabs_spec_helper/tasks/fixtures.rb', line 38 def fixtures(category) fixtures_yaml = if ENV['FIXTURES_YML'] ENV['FIXTURES_YML'] elsif File.exist?('.fixtures.yml') '.fixtures.yml' elsif File.exist?('.fixtures.yaml') '.fixtures.yaml' else false end begin fixtures = if fixtures_yaml YAML.load_file(fixtures_yaml) || { 'fixtures' => {} } else { 'fixtures' => {} } end rescue Errno::ENOENT raise("Fixtures file not found: '#{fixtures_yaml}'") rescue Psych::SyntaxError => e raise("Found malformed YAML in '#{fixtures_yaml}' on line #{e.line} column #{e.column}: #{e.problem}") end unless fixtures.include?('fixtures') # File is non-empty, but does not specify fixtures raise("No 'fixtures' entries found in '#{fixtures_yaml}'; required") end fixture_defaults = if fixtures.include? 'defaults' fixtures['defaults'] else {} end fixtures = fixtures['fixtures'] if fixtures['symlinks'].nil? fixtures['symlinks'] = auto_symlink end result = {} if fixtures.include?(category) && !fixtures[category].nil? defaults = { 'target' => 'spec/fixtures/modules' } # load defaults from the `.fixtures.yml` `defaults` section # for the requested category and merge them into my defaults if fixture_defaults.include? category defaults = defaults.merge(fixture_defaults[category]) end fixtures[category].each do |fixture, opts| # convert a simple string fixture to a hash, by # using the string fixture as the `repo` option of the hash. if opts.instance_of?(String) opts = { 'repo' => opts } end # there should be a warning or something if it's not a hash... next unless opts.instance_of?(Hash) # merge our options into the defaults to get the # final option list opts = defaults.merge(opts) next unless include_repo?(opts['puppet_version']) real_target = eval('"' + opts['target'] + '"') real_source = eval('"' + opts['repo'] + '"') result[real_source] = { 'target' => File.join(real_target, fixture), 'ref' => opts['ref'], 'branch' => opts['branch'], 'scm' => opts['scm'], 'flags' => opts['flags'], 'subdir' => opts['subdir'], } end end result end |
#git_remote_url(target) ⇒ Object
198 199 200 201 |
# File 'lib/puppetlabs_spec_helper/tasks/fixtures.rb', line 198 def git_remote_url(target) output, status = Open3.capture2e('git', '-C', target, 'remote', 'get-url', 'origin') status.success? ? output.strip : nil end |
#include_repo?(version_range) ⇒ Boolean
119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/puppetlabs_spec_helper/tasks/fixtures.rb', line 119 def include_repo?(version_range) if version_range && defined?(SemanticPuppet) puppet_spec = Gem::Specification.find_by_name('puppet') puppet_version = SemanticPuppet::Version.parse(puppet_spec.version.to_s) constraint = SemanticPuppet::VersionRange.parse(version_range) constraint.include?(puppet_version) else true end end |
#logger ⇒ Object
creates a logger so we can log events with certain levels
214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/puppetlabs_spec_helper/tasks/fixtures.rb', line 214 def logger unless @logger require 'logger' level = if ENV['ENABLE_LOGGER'] Logger::DEBUG else Logger::INFO end @logger = Logger.new(STDERR) @logger.level = level end @logger end |
#max_thread_limit ⇒ Object
returns the max_thread_count because we may want to limit ssh or https connections
252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/puppetlabs_spec_helper/tasks/fixtures.rb', line 252 def max_thread_limit unless @max_thread_limit # the default thread count is 10 but can be # raised by using environment variable MAX_FIXTURE_THREAD_COUNT @max_thread_limit = if ENV['MAX_FIXTURE_THREAD_COUNT'].to_i > 0 ENV['MAX_FIXTURE_THREAD_COUNT'].to_i else 10 # the default end end @max_thread_limit end |
#module_name ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/puppetlabs_spec_helper/tasks/fixtures.rb', line 13 def module_name raise ArgumentError unless File.file?('metadata.json') && File.readable?('metadata.json') = JSON.parse(File.read('metadata.json')) = .fetch('name', nil) || '' raise ArgumentError if .empty? .split('-').last rescue JSON::ParserError, ArgumentError File.basename(Dir.pwd).split('-').last end |
#module_working_directory ⇒ Object
228 229 230 231 232 233 |
# File 'lib/puppetlabs_spec_helper/tasks/fixtures.rb', line 228 def module_working_directory # The problem with the relative path is that PMT doesn't expand the path properly and so passing in a relative path here # becomes something like C:\somewhere\backslashes/spec/fixtures/work-dir on Windows, and then PMT barfs itself. # This has been reported as https://tickets.puppetlabs.com/browse/PUP-4884 File.((ENV['MODULE_WORKING_DIR']) ? ENV['MODULE_WORKING_DIR'] : 'spec/fixtures/work-dir') end |
#remove_subdirectory(target, subdir) ⇒ Object
203 204 205 206 207 208 209 210 211 |
# File 'lib/puppetlabs_spec_helper/tasks/fixtures.rb', line 203 def remove_subdirectory(target, subdir) unless subdir.nil? Dir.mktmpdir do |tmpdir| FileUtils.mv(Dir.glob("#{target}/#{subdir}/{.[^\.]*,*}"), tmpdir) FileUtils.rm_rf("#{target}/#{subdir}") FileUtils.mv(Dir.glob("#{tmpdir}/{.[^\.]*,*}"), target.to_s) end end end |
#repositories ⇒ Object
cache the repositories and return a hash object
27 28 29 30 31 32 |
# File 'lib/puppetlabs_spec_helper/tasks/fixtures.rb', line 27 def repositories unless @repositories @repositories = fixtures('repositories') end @repositories end |
#revision(scm, target, ref) ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/puppetlabs_spec_helper/tasks/fixtures.rb', line 173 def revision(scm, target, ref) args = [] case scm when 'hg' args.push('update', '--clean', '-r', ref) when 'git' args.push('reset', '--hard', ref) else raise "Unfortunately #{scm} is not supported yet" end result = system("#{scm} #{args.flatten.join ' '}", chdir: target) raise "Invalid ref #{ref} for #{target}" unless result end |
#shallow_git_repo? ⇒ Boolean
169 170 171 |
# File 'lib/puppetlabs_spec_helper/tasks/fixtures.rb', line 169 def shallow_git_repo? File.file?(File.join('.git', 'shallow')) end |
#source_dir ⇒ Object
This is a helper for the self-symlink entry of fixtures.yml
9 10 11 |
# File 'lib/puppetlabs_spec_helper/tasks/fixtures.rb', line 9 def source_dir Dir.pwd end |
#update_repo(scm, target) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/puppetlabs_spec_helper/tasks/fixtures.rb', line 155 def update_repo(scm, target) args = case scm when 'hg' ['pull'] when 'git' ['fetch'].tap do |git_args| git_args << '--unshallow' if shallow_git_repo? end else raise "Unfortunately #{scm} is not supported yet" end system("#{scm} #{args.flatten.join(' ')}", chdir: target) end |
#valid_repo?(scm, target, remote) ⇒ Boolean
187 188 189 190 191 192 193 194 195 196 |
# File 'lib/puppetlabs_spec_helper/tasks/fixtures.rb', line 187 def valid_repo?(scm, target, remote) return false unless File.directory?(target) return true if scm == 'hg' return true if git_remote_url(target) == remote warn "Git remote for #{target} has changed, recloning repository" FileUtils.rm_rf(target) false end |