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

Commit

Permalink
Fix all of the specs.
Browse files Browse the repository at this point in the history
  • Loading branch information
nathankleyn committed Oct 1, 2015
1 parent d225ced commit a0d2c02
Show file tree
Hide file tree
Showing 29 changed files with 438 additions and 401 deletions.
4 changes: 1 addition & 3 deletions lib/shanty/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
require 'shanty/info'
require 'shanty/task_set'
require 'shanty/env'
require 'shenanigans/hash/to_ostruct'
require 'deep_merge'

module Shanty
# Public: Handle the CLI interface between the user and the registered tasks
Expand Down Expand Up @@ -44,7 +42,7 @@ def run
def global_config
global_option('-c', '--config [CONFIG]', "Add config via command line in the format #{CONFIG_FORMAT}") do |config|
if (match = config.match(/(?<plugin>\S+):(?<key>\S+)\s+(?<value>\S+)/))
Env.config.merge!(match[:plugin] => { match[:key] => match[:value] })
@env.config[match[:plugin].to_sym][match[:key].to_sym] = match[:value]
else
fail(I18n.t('cli.invalid_config_param', actual: config, expected: CONFIG_FORMAT))
end
Expand Down
3 changes: 1 addition & 2 deletions lib/shanty/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
require 'shanty/plugins/shantyfile_plugin'
require 'shanty/task_set'
require 'shanty/task_sets/basic_task_set'
require 'shenanigans/hash/to_ostruct'
require 'yaml'

module Shanty
Expand Down Expand Up @@ -35,7 +34,7 @@ def projects
end

def config
@config ||= OpenStruct.new
@config ||= Hash.new { |h, k| h[k] = {} }
end

def require(*args)
Expand Down
4 changes: 4 additions & 0 deletions lib/shanty/logger.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
module Shanty
# Small mixin that gives the receiver class a logger method. This method simply wraps the Ruby Logger class with a
# nice consistent logging format.
module Logger
module_function

def logger
@logger ||= ::Logger.new($stdout).tap do |logger|
logger.formatter = proc do |_, datetime, _, msg|
Expand Down
47 changes: 16 additions & 31 deletions lib/shanty/plugin.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
require 'call_me_ruby'
require 'shanty/plugin_dsl'
require 'shanty/project'

module Shanty
# Some basic functionality for every plugin.
class Plugin
include CallMeRuby
extend PluginDsl

attr_reader :project, :env

Expand All @@ -13,8 +15,9 @@ def initialize(project, env)
@env = env
end

# External getters
def self.project_providers
@projects ||= []
@project_providers ||= []
end

def self.project_globs
Expand All @@ -25,45 +28,27 @@ def self.tags
@tags ||= [name]
end

# FIXME: Deal with this.
def self.options
config[name]
def self.name
to_s.split('::').last.downcase.gsub('plugin', '').to_sym
end

# Outside builders

def self.projects(env)
Set.new.tap do |s|
s.merge(project_providers.flat_map { |provider| send(provider, env) })
Set.new.tap do |projects|
projects.merge(project_providers.flat_map { |provider| send(provider, env) })
env.file_tree.glob(*project_globs).each do |path|
s << find_or_create_project(File.absolute_path(File.dirname(path)), env)
projects << find_or_create_project(File.dirname(path), env)
end
end
end

def self.option(option, default = nil)
# FIXME: How do we get env into here!?
env.config[name][option] = default if env.config[name][option].nil?
end

def self.provides_projects(*syms)
project_providers.concat(syms)
end

def self.provides_projects_containing(*globs)
project_globs.concat(globs)
end

def self.provides_tags(*args)
tags.concat(args.map(&:to_sym))
end

def self.name
to_s.split('::').last.downcase.gsub('plugin', '').to_sym
projects.each { |project| project.add_plugin(self) }
end
end

# Inner class methods
def self.find_or_create_project(path, env)
(env.projects[path] ||= Project.new(path, env)).tap do |project|
project.add_plugin(self)
end
path = File.absolute_path(path)
(env.projects[path] ||= Project.new(path, env))
end

def artifacts(_)
Expand Down
17 changes: 17 additions & 0 deletions lib/shanty/plugin_dsl.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Shanty
# Small mixin providing the DSL style class methods that plugin authors will rely on. This module is expected to be
# mixed into a Plugin class.
module PluginDsl
def provides_projects(*syms)
project_providers.concat(syms.map(&:to_sym))
end

def provides_projects_containing(*globs)
project_globs.concat(globs.map(&:to_s))
end

def provides_tags(*args)
tags.concat(args.map(&:to_sym))
end
end
end
6 changes: 3 additions & 3 deletions lib/shanty/plugins/rspec_plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ module Shanty
module Plugins
# Public: Rspec plugin for testing ruby projects.
class RspecPlugin < Plugin
# By default, we'll detect RSpec in a project if has a dependency on it in a Gemfile or *.gemspec file. If you don't
# use these files, you'll need to import the plugin manually using a Shantyfile as we can't tell if RSpec is being
# used from the presence of a spec directory alone (this can be many other testing frameworks!)
# By default, we'll detect RSpec in a project if has a dependency on it in a Gemfile or *.gemspec file. If you
# don't use these files, you'll need to import the plugin manually using a Shantyfile as we can't tell if RSpec is
# being used from the presence of a spec directory alone (this can be many other testing frameworks!)
provides_projects :rspec_projects
subscribe :test, :rspec

Expand Down
4 changes: 2 additions & 2 deletions lib/shanty/plugins/rubygem_plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class RubygemPlugin < Plugin
subscribe :build, :build_gem

def build_gem
gemspec_files(project).each do |file|
gemspec_files.each do |file|
system "gem build #{file}"
end
end
Expand All @@ -31,7 +31,7 @@ def artifacts
private

def gemspec_files
@gemspec_files ||= project_tree.glob(File.join(project.path, '*.gemspec'))
@gemspec_files ||= env.file_tree.glob(File.join(project.path, '*.gemspec'))
end
end
end
Expand Down
11 changes: 5 additions & 6 deletions lib/shanty/project.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
require 'acts_as_graph_vertex'
require 'call_me_ruby'
require 'pathname'
require 'shanty/logger'

module Shanty
Expand All @@ -16,13 +15,13 @@ class Project
# path - The path to the project.
# env - The instance of the environment this project should have access to.
def initialize(path, env)
pathname = Pathname.new(File.expand_path(path, env.root))
fail('Path to project must be a directory.') unless pathname.directory?
full_path = File.expand_path(path, env.root)
fail('Path to project must be a directory.') unless File.directory?(full_path)

@path = path
@path = full_path
@env = env

@name = File.basename(path)
@name = File.basename(full_path)
# FIXME: When changed is implemented properly, redefine this to default
# to false.
@changed = true
Expand Down Expand Up @@ -97,7 +96,7 @@ def inspect
name: @name,
path: @path,
tags: all_tags,
options: @options,
config: @config,
parents: parents.map(&:path)
}.inspect
end
Expand Down
2 changes: 0 additions & 2 deletions shanty.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ Gem::Specification.new do |gem|
gem.add_dependency 'bundler', '~>1.10'
gem.add_dependency 'call_me_ruby', '~>1.1'
gem.add_dependency 'commander', '~>4.3'
gem.add_dependency 'deep_merge', '~>1.0'
gem.add_dependency 'gitignore_rb', '~>0.2.2'
gem.add_dependency 'i18n', '~>0.7'
gem.add_dependency 'shenanigans', '~>1.0'

gem.add_development_dependency 'coveralls', '~>0.8'
gem.add_development_dependency 'cucumber', '~>2.1'
Expand Down
50 changes: 40 additions & 10 deletions spec/lib/shanty/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,42 @@
require 'shanty/cli'
require 'shanty/info'
require 'shanty/env'
require 'shenanigans/hash/to_ostruct'
require_fixture 'test_task_set'
require 'shanty/task_set'

RSpec.describe(Shanty::Cli) do
let(:task_sets) { [TestTaskSet] }
subject { described_class.new(task_sets) }
subject { described_class.new([task_set], env, graph) }
let(:env) { double('env') }
let(:graph) { double('graph') }
let(:config) { Hash.new { |h, k| h[k] = {} } }
let(:task_set) do
double('task set').tap do |d|
allow(d).to receive(:tasks).and_return(
foo: {
klass: d,
options: {
cat: {
desc: 'test.options.cat'
},
dog: {
type: :boolean,
desc: 'test.options.dog'
},
catweasel: {
required: true,
type: :string,
desc: 'test.options.catweasel'
}
},
syntax: 'foo [--cat CAT] [--dog] --catweasel CATWEASEL',
desc: 'test.foo.desc'
}
)
end
end

before do
allow(env).to receive(:config).and_return(config)
end

describe('#tasks') do
it('returns a list of tasks from all given task sets') do
Expand All @@ -18,7 +48,7 @@
command = subject.tasks[:foo]
expect(command[:syntax]).to eql('foo [--cat CAT] [--dog] --catweasel CATWEASEL')
expect(command[:desc]).to eql('test.foo.desc')
expect(command[:klass]).to equal(TestTaskSet)
expect(command[:klass]).to equal(task_set)

options = command[:options]
expect(options.keys).to contain_exactly(:cat, :dog, :catweasel)
Expand Down Expand Up @@ -48,14 +78,14 @@

it('sets the version of the program') do
allow(subject).to receive(:run!)
expect(subject).to receive(:program).with(:version, Info::VERSION)
expect(subject).to receive(:program).with(:version, Shanty::Info::VERSION)

subject.run
end

it('sets the description of the program') do
allow(subject).to receive(:run!)
expect(subject).to receive(:program).with(:version, Info::VERSION)
expect(subject).to receive(:program).with(:version, Shanty::Info::VERSION)

subject.run
end
Expand Down Expand Up @@ -122,23 +152,23 @@

expect do
subject.run
end.to raise_error(I18n.t('cli.invalid_config_param', actual: 'nic', expected: Cli::CONFIG_FORMAT))
end.to raise_error(I18n.t('cli.invalid_config_param', actual: 'nic', expected: Shanty::Cli::CONFIG_FORMAT))
end

it('runs a command with a config option') do
ARGV.concat(['-c nic:kim cage'])

subject.run

expect(Shanty::Env.config.nic).to eql({ kim: 'cage' }.to_ostruct)
expect(config[:nic]).to eql(kim: 'cage')
end

it('runs a command with multiple config options') do
ARGV.concat(['-c nic:kim cage', '-c nic:copolla cage'])

subject.run

expect(Shanty::Env.config.nic).to eql({ kim: 'cage', copolla: 'cage' }.to_ostruct)
expect(config[:nic]).to eql(kim: 'cage', copolla: 'cage')
end
end
end
35 changes: 6 additions & 29 deletions spec/lib/shanty/env_spec.rb
Original file line number Diff line number Diff line change
@@ -1,56 +1,33 @@
require 'spec_helper'
require 'fileutils'
require 'i18n'
require 'tmpdir'
require 'shanty/env'
require 'shenanigans/hash/to_ostruct'
require 'deep_merge'
require 'spec_helper'
require 'tmpdir'

RSpec.describe(Shanty::Env) do
around do |example|
Dir.mktmpdir('shanty-tests') do |tmp_path|
Dir.chdir(tmp_path) do
FileUtils.touch('.shanty.yml')
FileUtils.touch('Shantyconfig')
example.run
end
end
end

describe('#logger') do
it('returns a logger object') do
expect(subject.logger).to be_a(Logger)
end
end

describe('#root') do
it('returns the path to the closest ancestor folder with a .shanty.yml file in it') do
expect(subject.root).to eql(Dir.pwd)
end

it('throws an exception if no ancestor folders have a .shanty.yml file in them') do
FileUtils.rm('.shanty.yml')
FileUtils.rm('Shantyconfig')
expect { subject.root }.to raise_error(I18n.t('missing_root', config_file: described_class::CONFIG_FILE))
end
end

describe('#config') do
before do
ENV['SHANTY_ENV'] = 'stray_cats'
end

it('handles no config file existing') do
allow(File).to receive(:exist?) { false }
expect(subject.config.nic).to eql(OpenStruct.new)
end

it('handles no data in the config file') do
allow(YAML).to receive(:load_file) { false }
expect(subject.config)
end

it('returns nothing if .shanty.yml does not exist') do
FileUtils.rm('.shanty.yml')
expect(subject.config.nic).to eql(OpenStruct.new)
it('returns an empty hash for any missing keys') do
expect(subject.config[:foo]).to eql({})
end
end
end
Loading

0 comments on commit a0d2c02

Please sign in to comment.