Skip to content
This repository has been archived by the owner on Dec 31, 2022. It is now read-only.

Commit

Permalink
Added code quality tools
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Rzegocki committed Apr 9, 2016
1 parent c57f71e commit 730857f
Show file tree
Hide file tree
Showing 34 changed files with 163 additions and 82 deletions.
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# EditorConfig is awesome: http://EditorConfig.org

# top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending every file
[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 2

1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ Berksfile.lock

# Bundler
Gemfile.lock
bin/*
.bundle/*

.kitchen/
Expand Down
49 changes: 49 additions & 0 deletions .overcommit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
PreCommit:
ALL:
on_warn: fail # Treat all warnings as failures
BrokenSymlinks:
enabled: true
BundleAudit:
enabled: true
description: 'Run bundle-audit'
required_executable: './bin/bundle-audit'
flags: ['check', '--update']
install_command: 'gem install bundler-audit'
include:
- 'Gemfile'
- 'Gemfile.lock'
BundleCheck:
enabled: true
CaseConflicts:
enabled: true
Foodcritic:
enabled: true
description: 'Run foodcritic'
require_files: false
required_executable: './bin/foodcritic'
install_command: 'gem install foodcritic'
flags: ['-f', 'any', '-B', '.']
HardTabs:
enabled: true
JsonSyntax:
enabled: true
LocalPathsInGemfile:
enabled: true
Mdl:
enabled: true
MergeConflicts:
enabled: true
RuboCop:
enabled: true
flags: ['--format=emacs', '--force-exclusion', '--display-cop-names']
ShellCheck:
enabled: true
TrailingWhitespace:
enabled: true
XmlLint:
enabled: true
XmlSyntax:
enabled: true
YamlSyntax:
enabled: true
3 changes: 3 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-r 'spec_helper'
--color
--format=documentation
10 changes: 10 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
Metrics/LineLength:
Max: 120

Style/Documentation:
Enabled: false

AllCops:
TargetRubyVersion: 2.3
DisplayCopNames: true
1 change: 1 addition & 0 deletions .ruby-gemset
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
opsworks_ruby
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ruby-2.3.0
1 change: 1 addition & 0 deletions Berksfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
source 'https://supermarket.chef.io'

metadata
20 changes: 20 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true
source 'https://rubygems.org/'

group :lint do
gem 'brakeman'
gem 'bundler-audit'
gem 'foodcritic'
gem 'mdl'
gem 'overcommit'
gem 'rubocop'
end

group :development do
gem 'rspec'
gem 'chefspec'
end

gem 'test-kitchen'
gem 'kitchen-vagrant'
gem 'berkshelf'
3 changes: 3 additions & 0 deletions bin/bundle-audit
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

bundle exec bundle-audit "$@"
3 changes: 3 additions & 0 deletions bin/foodcritic
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

bundle exec foodcritic "$@"
1 change: 1 addition & 0 deletions libraries/all.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
libdir = File.expand_path('..', __FILE__)
require File.join(libdir, 'core_ext')
Dir[File.join(libdir, 'drivers', 'dsl', '**', '*.rb')].each { |f| require f }
Expand Down
11 changes: 6 additions & 5 deletions libraries/core_ext.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true
class Object
def try(*a, &b)
try!(*a, &b) if a.empty? || respond_to?(a.first)
try!(*a, &b) if a.empty? || respond_to?(a.first)
end

def try!(*a, &b)
Expand All @@ -16,28 +17,28 @@ def try!(*a, &b)
end

def blank?
respond_to?(:empty?) ? !!empty? : !self
respond_to?(:empty?) ? empty? : !self
end

def present?
!blank?
end

def self.descendants
ObjectSpace.each_object(Class).select { |klass| klass < self }
ObjectSpace.each_object(Class).select { |klass| klass < self }
end
end

class String
def constantize
split('::').inject(Object) {|o,c| o.const_get c}
split('::').inject(Object) { |a, e| a.const_get(e) }
end

def classify
gsub(/(?:^.|_.)/) { |s| s[-1].upcase }
end

def underscore
gsub(/[A-Z]/) { |s| "_#{s.downcase}" }.sub(/^_/, '')
gsub(/[A-Z]/) { |s| "_#{s.downcase}" }.sub(/^_/, '')
end
end
1 change: 1 addition & 0 deletions libraries/drivers/base.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
module Drivers
class Base
def initialize(app, node, options = {})
Expand Down
21 changes: 11 additions & 10 deletions libraries/drivers/db/base.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
module Drivers
module Db
class Base < Drivers::Base
Expand All @@ -10,6 +11,7 @@ def initialize(app, node, options = {})
@connection_data_source = validate_engine
end

# rubocop:disable Metrics/AbcSize
def out
deploy_db_data = JSON.parse(node['deploy'][app['shortname']]['database'].to_json, symbolize_names: true) || {}
if @connection_data_source == :adapter
Expand All @@ -19,20 +21,16 @@ def out
end

deploy_db_data.merge(
adapter: adapter,
username: options[:rds]['db_user'],
password: options[:rds]['db_password'],
host: options[:rds]['address'],
database: app['data_sources'].first['database_name']
adapter: adapter, username: options[:rds]['db_user'], password: options[:rds]['db_password'],
host: options[:rds]['address'], database: app['data_sources'].first['database_name']
)
end
# rubocop:enable Metrics/AbcSize

def configure(context)
handle_packages(context)
end

protected

def self.allowed_engines(*engines)
@allowed_engines = engines.map(&:to_s) if engines.present?
@allowed_engines || []
Expand All @@ -43,6 +41,8 @@ def self.adapter(adapter = nil)
(@adapter || self.class.name.underscore).to_s
end

protected

def allowed_engines
self.class.allowed_engines
end
Expand All @@ -64,9 +64,6 @@ def validate_engine
end

def validate_adapter
connection_data = node['deploy'][app['shortname']]['database']
adapter_engine = connection_data.try(:[], 'adapter')

raise ArgumentError, "Missing :rds engine, expected #{allowed_engines.inspect}." if adapter_engine.blank?
unless allowed_engines.include?(adapter_engine)
raise ArgumentError,
Expand All @@ -75,6 +72,10 @@ def validate_adapter

:adapter
end

def adapter_engine
node['deploy'][app['shortname']]['database'].try(:[], 'adapter')
end
end
end
end
12 changes: 9 additions & 3 deletions libraries/drivers/db/factory.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
# frozen_string_literal: true
module Drivers
module Db
class Factory
def self.build(app, node, options = {})
raise ArgumentError, ':rds option is not set.' unless options[:rds]
engine = Drivers::Db::Base.descendants.detect do |db_driver|

engine = detect_engine(app, node, options)
raise StandardError, 'There is no supported Db driver for given configuration.' if engine.blank?
engine.new(app, node, options)
end

def self.detect_engine(app, node, options)
Drivers::Db::Base.descendants.detect do |db_driver|
db_driver.allowed_engines.include?(
options[:rds]['engine'] || node['deploy'][app['shortname']]['database']['adapter']
)
end
raise StandardError, 'There is no supported Db driver for given configuration.' if engine.blank?
engine.new(app, node, options)
end
end
end
Expand Down
1 change: 1 addition & 0 deletions libraries/drivers/db/postgresql.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
module Drivers
module Db
class Postgresql < Base
Expand Down
21 changes: 0 additions & 21 deletions libraries/drivers/dsl/base.rb

This file was deleted.

1 change: 1 addition & 0 deletions libraries/drivers/dsl/packages.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
module Drivers
module Dsl
module Packages
Expand Down
3 changes: 3 additions & 0 deletions metadata.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
name 'opsworks_ruby'
maintainer 'The Authors'
maintainer_email 'you@example.com'
Expand All @@ -8,3 +9,5 @@

depends 'packages'

source_url 'https://github.com/ajgon/opsworks_ruby'
issues_url 'https://github.com/ajgon/opsworks_ruby/issues'
5 changes: 5 additions & 0 deletions recipes/configure.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
# frozen_string_literal: true
#
# Cookbook Name:: opsworks_ruby
# Recipe:: configure
#

if Chef::Config[:solo]
Chef::Log.warn('This recipe uses search. Chef Solo does not support search.')
end

app = search(:aws_opsworks_app).first
rds = search(:aws_opsworks_rds_db_instance).first

Expand Down
1 change: 1 addition & 0 deletions recipes/default.rb
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# frozen_string_literal: true
include_recipe 'opsworks_ruby::deploy'
7 changes: 5 additions & 2 deletions spec/fixtures/aws_opsworks_app.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true
# rubocop:disable Metrics/MethodLength
def aws_opsworks_app(override = {})
item = {
app_id: '3aef37c1-7e2b-4255-bbf1-03e06f07701a',
Expand All @@ -18,7 +20,7 @@ def aws_opsworks_app(override = {})
data_sources: [
{ arn: 'arn:aws:rds:us-west-2:850906259207:db:dummy-project', type: 'RdsDbInstance', database_name: 'dummydb' }
],
domains: [ 'dummy-project.example.com', 'dummy_project' ],
domains: ['dummy-project.example.com', 'dummy_project'],
enable_ssl: true,
environment: {
'ENV_VAR1' => 'test',
Expand All @@ -29,7 +31,7 @@ def aws_opsworks_app(override = {})
ssl_configuration: {
certificate: '--- SSL CERTIFICATE ---',
private_key: '--- SSL PRICATE KEY ---',
chain: '--- SSL CERTIFICATE CHAIN ---',
chain: '--- SSL CERTIFICATE CHAIN ---'
},
type: 'other',
deploy: true,
Expand All @@ -38,3 +40,4 @@ def aws_opsworks_app(override = {})

JSON.parse(item.to_json)
end
# rubocop:enable Metrics/MethodLength
3 changes: 3 additions & 0 deletions spec/fixtures/aws_opsworks_rds_db_instance.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true
# rubocop:disable Metrics/MethodLength
def aws_opsworks_rds_db_instance(override = {})
item = {
rds_db_instance_arn: 'arn:aws:rds:us-west-2:850906259207:db:dummy-project',
Expand All @@ -13,3 +15,4 @@ def aws_opsworks_rds_db_instance(override = {})

JSON.parse(item.to_json)
end
# rubocop:enable Metrics/MethodLength
3 changes: 3 additions & 0 deletions spec/fixtures/node.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true
# rubocop:disable Metrics/MethodLength
def node(override = {})
item = {
deploy: {
Expand All @@ -16,3 +18,4 @@ def node(override = {})

JSON.parse(item.to_json)
end
# rubocop:enable Metrics/MethodLength
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'chefspec'
require 'chefspec/berkshelf'

Expand Down
3 changes: 2 additions & 1 deletion spec/unit/libraries/core_ext_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'spec_helper'

describe 'Core extensions' do
Expand Down Expand Up @@ -25,7 +26,7 @@
end

it 'Drivers::Dsl::Basic' do
expect('Drivers::Dsl::Basic'.constantize).to eq Drivers::Dsl::Basic
expect('Drivers::Dsl::Packages'.constantize).to eq Drivers::Dsl::Packages
end
end

Expand Down
Loading

0 comments on commit 730857f

Please sign in to comment.