Skip to content
This repository has been archived by the owner on May 10, 2021. It is now read-only.

Commit

Permalink
Tests, tweaks and unified simplecov
Browse files Browse the repository at this point in the history
  • Loading branch information
Joseph Henrich committed Jul 1, 2014
1 parent 6e0be9b commit 2f1f422
Show file tree
Hide file tree
Showing 16 changed files with 240 additions and 82 deletions.
19 changes: 19 additions & 0 deletions .simplecov
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'simplecov-rcov'
SimpleCov.profiles.define 'spec' do
add_group 'jenkins_pipeline_builder', '/lib/'
add_filter 'spec'
coverage_dir 'out/coverage'
formatter SimpleCov::Formatter::MultiFormatter[
SimpleCov::Formatter::Console,
SimpleCov::Formatter::RcovFormatter,
]
end

class SimpleCov::Formatter::Console
def format(result)
print "COVERAGE: #{result.covered_percent.round(2)}%\n"
end
end

SimpleCov.start 'spec' #if ENV["COVERAGE"]

1 change: 0 additions & 1 deletion bin/generate
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#

$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + '/../lib'))
require 'jenkins_pipeline_builder'
Expand Down
6 changes: 5 additions & 1 deletion lib/jenkins_pipeline_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,17 @@ def credentials=(creds)
@credentials
end

def logger
@_client.logger
end

def registry
generator.module_registry
end
end
end
JenkinsPipelineBuilder.generator
require 'jenkins_pipeline_builder/extensions'
require 'jenkins_pipeline_builder/builders'
require 'jenkins_pipeline_builder/job_builder'
require 'jenkins_pipeline_builder/wrappers'
Expand All @@ -64,4 +69,3 @@ def registry
require 'jenkins_pipeline_builder/cli/list'
require 'jenkins_pipeline_builder/cli/describe'
require 'jenkins_pipeline_builder/cli/base'
require 'jenkins_pipeline_builder/extensions'
2 changes: 0 additions & 2 deletions lib/jenkins_pipeline_builder/builders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
require 'jenkins_pipeline_builder/extensions'

builder do
name :multi_job
Expand Down
4 changes: 3 additions & 1 deletion lib/jenkins_pipeline_builder/cli/describe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ module CLI
klass = Class.new(Thor) do

extensions = JenkinsPipelineBuilder.registry.registry[:job][entry]
extensions.each do |key, ext|
extensions.each do |key, exts|
# TODO: don't just take the first
ext = exts.first
desc key, "Details for #{ext.name}"
define_method(ext.name) do
display_module(ext)
Expand Down
26 changes: 25 additions & 1 deletion lib/jenkins_pipeline_builder/extensions.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
#
# Copyright (c) 2014 Constant Contact
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

require 'jenkins_pipeline_builder'
JenkinsPipelineBuilder.registry.entries.each do |type, path|
singular_type = type.to_s.singularize
define_method singular_type do |&block|
ext = JenkinsPipelineBuilder::Extension.new
ext.instance_eval(&block)
ext.path path
ext.type singular_type
unless ext.valid?
name = ext.name || 'A plugin with no name provided'
puts "Encountered errors while registering #{name}"
Expand All @@ -19,6 +41,7 @@
def job_attribute(&block)
ext = JenkinsPipelineBuilder::Extension.new
ext.instance_eval(&block)
ext.type :job_attribute
unless ext.valid?
name = ext.name || 'A plugin with no name provided'
puts "Encountered errors while registering #{name}"
Expand All @@ -38,7 +61,8 @@ class Extension
jenkins_name: 'No jenkins display name set',
description: 'No description set',
path: false,
announced: true
announced: true,
type: false
}
DSL_METHODS.keys.each do |method_name|
define_method method_name do |value = nil|
Expand Down
68 changes: 37 additions & 31 deletions lib/jenkins_pipeline_builder/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ def initialize

def debug=(value)
@debug = value
@logger.level = (value) ? Logger::DEBUG : Logger::INFO
logger.level = (value) ? Logger::DEBUG : Logger::INFO
end

def logger
JenkinsPipelineBuilder.logger
end

def client
Expand All @@ -66,7 +70,7 @@ def view
def load_collection_from_path(path, recursively = false, remote = false)
path = File.expand_path(path, Dir.getwd)
if File.directory?(path)
@logger.info "Generating from folder #{path}"
logger.info "Generating from folder #{path}"
Dir[File.join(path, '/*.yaml'), File.join(path, '/*.yml')].each do |file|
if File.directory?(file) # TODO: This doesn't work unless the folder contains .yml or .yaml at the end
if recursively
Expand All @@ -75,12 +79,12 @@ def load_collection_from_path(path, recursively = false, remote = false)
next
end
end
@logger.info "Loading file #{file}"
logger.info "Loading file #{file}"
yaml = YAML.load_file(file)
load_job_collection(yaml, remote)
end
else
@logger.info "Loading file #{path}"
logger.info "Loading file #{path}"
yaml = YAML.load_file(path)
load_job_collection(yaml, remote)
end
Expand All @@ -92,14 +96,14 @@ def load_job_collection(yaml, remote = false)
key = section.keys.first
value = section[key]
if key == :dependencies
@logger.info 'Resolving Dependencies for remote project'
logger.info 'Resolving Dependencies for remote project'
load_remote_yaml(value)
next
end
name = value[:name]
if @job_collection.key?(name)
if remote
@logger.info "Duplicate item with name '#{name}' was detected from the remote folder."
logger.info "Duplicate item with name '#{name}' was detected from the remote folder."
else
fail "Duplicate item with name '#{name}' was detected."
end
Expand All @@ -118,10 +122,10 @@ def load_extensions(path)
path = File.expand_path(path, Dir.getwd)
return unless File.directory?(path)

@logger.info "Loading extensions from folder #{path}"
@logger.info Dir.glob("#{path}/*.rb").inspect
logger.info "Loading extensions from folder #{path}"
logger.info Dir.glob("#{path}/*.rb").inspect
Dir.glob("#{path}/*.rb").each do |file|
@logger.info "Loaded #{file}"
logger.info "Loaded #{file}"
require file
end
match_extension_versions
Expand All @@ -130,7 +134,7 @@ def load_extensions(path)
def match_extension_versions
registry = @module_registry.registry[:job]
installed_plugins = @debug ? nil : list_plugins # Only get plugins if not in debug mode
@logger.debug 'Loading newest version of all plugins since we are in debug mode.'
logger.debug 'Loading newest version of all plugins since we are in debug mode.'
registry.each do |registry_key, registry_value|
if registry_value[:registry]
registry_value[:registry].each do |extension_key, extension_value|
Expand Down Expand Up @@ -178,7 +182,7 @@ def load_template(path, template)
end

if File.directory?(path)
@logger.info "Loading from #{path}"
logger.info "Loading from #{path}"
load_collection_from_path(path, false, true)
true
else
Expand All @@ -188,15 +192,15 @@ def load_template(path, template)

def download_yaml(url, file)
@remote_depends[url] = file
@logger.info "Downloading #{url} to #{file}.tar"
logger.info "Downloading #{url} to #{file}.tar"
open("#{file}.tar", 'w') do |local_file|
open(url) do |remote_file|
local_file.write(Zlib::GzipReader.new(remote_file).read)
end
end

# Extract Tar.gz to 'remote' folder
@logger.info "Unpacking #{file}.tar to #{file} folder"
logger.info "Unpacking #{file}.tar to #{file} folder"
Archive::Tar::Minitar.unpack("#{file}.tar", file)
end

Expand All @@ -216,7 +220,7 @@ def load_remote_yaml(dependencies)
path = File.expand_path(file, Dir.getwd)
# Load templates recursively
unless source[:templates]
@logger.info 'No specific template specified'
logger.info 'No specific template specified'
# Try to load the folder or the pipeline folder
path = File.join(path, 'pipeline') if Dir.entries(path).include? 'pipeline'
return load_collection_from_path(path)
Expand All @@ -229,12 +233,12 @@ def load_remote_yaml(dependencies)
def load_templates(path, templates)
templates.each do |template|
version = template[:version] || 'newest'
@logger.info "Loading #{template[:name]} at version #{version}"
logger.info "Loading #{template[:name]} at version #{version}"
# Move into the remote folder and look for the template folder
remote = Dir.entries(path)
if remote.include? template[:name]
# We found the template name, load this path
@logger.info 'We found the template!'
logger.info 'We found the template!'
load_template(path, template)
else
# Many cases we must dig one layer deep
Expand Down Expand Up @@ -312,7 +316,7 @@ def resolve_project(project)
project_body = project[:value]

jobs = prepare_jobs(project_body[:jobs]) if project_body[:jobs]
@logger.info project
logger.info project
process_job_changes(jobs)
errors = process_jobs(jobs, project)
errors = process_views(project_body[:views], project, errors) if project_body[:views]
Expand All @@ -333,7 +337,7 @@ def resolve_job_by_name(name, settings = {})
job = get_item(name)
fail "Failed to locate job by name '#{name}'" if job.nil?
job_value = job[:value]
@logger.debug "Compiling job #{name}"
logger.debug "Compiling job #{name}"
success, payload = Compiler.compile(job_value, settings, @job_collection)
[success, payload]
end
Expand Down Expand Up @@ -395,9 +399,9 @@ def publish_jobs(jobs, errors = {})
end

def bootstrap(path, project_name)
@logger.info "Bootstrapping pipeline from path #{path}"
logger.info "Bootstrapping pipeline from path #{path}"
load_collection_from_path(path)
@logger.info @job_collection
logger.info @job_collection
cleanup_temp_remote
load_extensions(path)
errors = {}
Expand All @@ -408,23 +412,23 @@ def bootstrap(path, project_name)
errors = publish_project(project_name)
end
errors.each do |k, v|
@logger.error "Encountered errors compiling: #{k}:"
@logger.error v
logger.error "Encountered errors compiling: #{k}:"
logger.error v
end
end

def pull_request(path, project_name)
@logger.info "Pull Request Generator Running from path #{path}"
logger.info "Pull Request Generator Running from path #{path}"
load_collection_from_path(path)
cleanup_temp_remote
load_extensions(path)
jobs = {}
@logger.info "Project: #{projects}"
logger.info "Project: #{projects}"
projects.each do |project|
if project[:name] == project_name || project_name.nil?
project_body = project[:value]
project_jobs = project_body[:jobs] || []
@logger.info "Using Project #{project}"
logger.info "Using Project #{project}"
pull_job = nil
project_jobs.each do |job|
job = job.keys.first if job.is_a? Hash
Expand Down Expand Up @@ -463,17 +467,17 @@ def pull_request(path, project_name)
end

def dump(job_name)
@logger.info "Debug #{@debug}"
@logger.info "Dumping #{job_name} into #{job_name}.xml"
logger.info "Debug #{@debug}"
logger.info "Dumping #{job_name} into #{job_name}.xml"
xml = client.job.get_config(job_name)
File.open(job_name + '.xml', 'w') { |f| f.write xml }
end

def create_or_update(job, xml)
job_name = job[:name]
if @debug
@logger.info "Will create job #{job}"
@logger.info "#{xml}"
logger.info "Will create job #{job}"
logger.info "#{xml}"
File.open(job_name + '.xml', 'w') { |f| f.write xml }
return
end
Expand All @@ -488,7 +492,7 @@ def create_or_update(job, xml)
def compile_job_to_xml(job)
fail 'Job name is not specified' unless job[:name]

@logger.info "Creating Yaml Job #{job}"
logger.info "Creating Yaml Job #{job}"
job[:job_type] = 'free_style' unless job[:job_type]
case job[:job_type]
when 'job_dsl'
Expand Down Expand Up @@ -530,7 +534,9 @@ def compile_freestyle_job_to_xml(params)
xml = client.job.build_freestyle_config(params)
n_xml = Nokogiri::XML(xml)

logger.debug 'Loading the required modules'
@module_registry.traverse_registry_path('job', params, n_xml)
logger.debug 'Module loading complete'

n_xml.to_xml
end
Expand All @@ -555,7 +561,7 @@ def update_job_dsl(job, xml)
end

def generate_job_dsl_body(params)
@logger.info 'Generating pipeline'
logger.info 'Generating pipeline'

xml = client.job.build_freestyle_config(params)

Expand Down
Loading

0 comments on commit 2f1f422

Please sign in to comment.