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

Commit

Permalink
Changes for updated shanty core
Browse files Browse the repository at this point in the history
  • Loading branch information
janstenpickle committed Oct 6, 2015
1 parent faadbdf commit e7d2fb2
Show file tree
Hide file tree
Showing 10 changed files with 206 additions and 188 deletions.
Empty file removed .shanty.yml
Empty file.
14 changes: 13 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,16 @@ sudo: false
language: ruby
rvm:
- 2.2.3
script: shanty test

# We have to install Rust manually, as Travis does not yet allow multiligual
# builds. We install the latest stable Rust build.
before_install:
- mkdir ~/rust-installer
- curl -sL https://static.rust-lang.org/rustup.sh -o ~/rust-installer/rustup.sh
- sh ~/rust-installer/rustup.sh --prefix=~/rust --spec=stable -y --disable-sudo 2> /dev/null
- export PATH=~/rust/bin:$PATH
- export LD_LIBRARY_PATH=~/rust/lib:$LD_LIBRARY_PATH
- rustc --version
- cargo --version

script: bundle exec shanty test
4 changes: 4 additions & 0 deletions Shantyconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'shanty/plugins/bundler_plugin'
require 'shanty/plugins/rspec_plugin'
require 'shanty/plugins/rubocop_plugin'
require 'shanty/plugins/rubygem_plugin'
44 changes: 24 additions & 20 deletions lib/shanty_template_plugin/template_plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,36 @@ class TemplatePlugin < Shanty::Plugin
PROJECT_EXTENSION = 'erb'
DS_EXTENSTION = 'syaml'

option :datasource, 'plain'
option :output_dir, 'build'
subscribe :build, :on_build
projects "**/*.#{PROJECT_EXTENSION}"
provides_config :datasource, 'plain'
provides_config :output_dir, 'build'
provides_projects_containing "**/*.#{PROJECT_EXTENSION}"

def on_build(project)
load_data(project).each do |env, data|
project_files(project).each do |template|
output = output_filename(project, template, env)
def on_build
load_data.each do |template_env, data|
project_files.each do |template|
output = output_filename(template, template_env)
FileUtils.mkdir_p(File.dirname(output))
File.write(output, Erubis::Eruby.new(File.read(template)).result(data: data))
end
end
end

def artifacts(project)
load_data(project).keys.flat_map do |env|
project_files(project).map do |template|
filename = output_filename(project, template, env)
def artifacts
load_data.keys.flat_map do |template_env|
project_files.map do |template|
filename = output_filename(template, template_env)
Shanty::Artifact.new(File.extname(filename), self.class.name, URI("file://#{filename}"))
end
end
end

private

def output_filename(project, template, env)
def output_filename(template, env)
file = File.basename(template, PROJECT_EXTENSION).split('.')

File.join(project.path, self.class.options.output_dir, filename(file, env))
File.join(project.path, config[:output_dir], filename(file, env))
end

def filename(file, env)
Expand All @@ -55,12 +55,12 @@ def filename(file, env)
"#{filename}.#{file_extension}"
end

def project_files(project)
@project_files ||= project_tree.glob("#{project.path}/*.#{PROJECT_EXTENSION}")
def project_files
@project_files ||= env.file_tree.glob("#{project.path}/*.#{PROJECT_EXTENSION}")
end

def load_data(project)
@data ||= artifact_paths(project).each_with_object({}) do |a, acc|
def load_data
@data ||= artifact_paths.each_with_object({}) do |a, acc|
data = YAML.load_file(a)
validate!(a, data)
acc.deep_merge!(data)
Expand All @@ -73,9 +73,13 @@ def validate!(file, data)
end
end

def artifact_paths(project)
(project.parents.flat_map(&:all_artifacts).concat(project.all_artifacts)).keep_if do |a|
a.local? && File.basename(a.uri.path) == "#{self.class.options.datasource}.#{DS_EXTENSTION}"
def all_artifacts
project.parents.flat_map(&:all_artifacts).concat(project.all_artifacts)
end

def artifact_paths
all_artifacts.keep_if do |a|
a.local? && File.basename(a.uri.path) == "#{config[:datasource]}.#{DS_EXTENSTION}"
end.map(&:to_local_path)
end
end
Expand Down
23 changes: 0 additions & 23 deletions spec/fixtures/test_project.rb

This file was deleted.

187 changes: 102 additions & 85 deletions spec/lib/shanty_template_plugin/template_plugin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,125 +3,142 @@
require 'shanty_template_plugin/template_plugin'
require 'yaml'

# Tests for template plugin
module ShantyTemplatePlugin
RSpec.describe(TemplatePlugin) do
include_context('graph')
RSpec.describe(ShantyTemplatePlugin::TemplatePlugin) do
include_context('plugin')

def test_output(data, expected_files, output_dir = 'build')
setup_project(data)
def test_output(data, expected_files, output_dir = 'build')
setup_project(data)

expect_artifacts(full_paths(expected_files, output_dir))
expect_contents(full_paths(expected_files, output_dir))
end
expect_artifacts(full_paths(expected_files, output_dir))
expect_contents(full_paths(expected_files, output_dir))
end

def test_artifacts(data, expected_files, output_dir = 'build')
setup_project(data)
def test_artifacts(data, expected_files, output_dir = 'build')
setup_project(data)

expect_artifacts(full_paths(expected_files, output_dir))
end

def setup_project(data)
project.add_parent(parent)
expect_artifacts(full_paths(expected_files, output_dir))
end

File.open(parent_artifact, 'wb') do |f|
f.write(data.to_yaml)
end
def setup_project(data)
allow(project).to receive(:parents).and_return([parent])

subject.on_build(project)
File.open(parent_artifact, 'wb') do |f|
f.write(data.to_yaml)
end

def expect_artifacts(expected_files)
expect(subject.artifacts(project).map(&:to_local_path)).to match_array(expected_files.keys)
end
subject.on_build
end

def expect_contents(expected_files)
expected_files.each do |f, exp|
expect(File.open(f, 'rb').read.strip).to eql(exp)
end
end
def expect_artifacts(expected_files)
expect(subject.artifacts.map(&:to_local_path)).to match_array(expected_files.keys)
end

def full_paths(expected_files, output_dir)
expected_files.each_with_object({}) do |v, acc|
acc[File.join(root, 'one', output_dir, v.first)] = v.last
end
def expect_contents(expected_files)
expected_files.each do |f, exp|
expect(File.open(f, 'rb').read.strip).to eql(exp)
end
end

before(:each) do
File.write(File.join(root, 'one', 'test.erb'), '<%= data[\'nic\'] %>')
FileUtils.mkdir_p(File.dirname(parent_artifact))
def full_paths(expected_files, output_dir)
expected_files.each_with_object({}) do |v, acc|
acc[File.join(root, 'one', output_dir, v.first)] = v.last
end
end

let(:parent_dir) { File.join(root, 'two') }
let(:parent_artifact) { File.join(parent_dir, 'build', 'plain.syaml') }
let(:parent) { TestProject.new(parent_dir, parent_artifact) }
before(:each) do
File.write(File.join(project_path, 'test.erb'), '<%= data[\'nic\'] %>')
FileUtils.mkdir_p(File.dirname(parent_artifact))
allow(project).to receive(:path).and_return(project_path)
allow(project).to receive(:parents).and_return([])
allow(project).to receive(:config).and_return({})
allow(project).to receive(:all_artifacts).and_return([])
allow(file_tree).to receive(:glob).and_return(
[File.join(project_path, 'test.erb')]
)
allow(parent_project).to receive(:path).and_return(parent_dir)
allow(parent_project).to receive(:all_artifacts).and_return(
[Shanty::Artifact.new(File.extname(parent_artifact), 'test', URI("file://#{parent_artifact}"))]
)
allow(parent_project).to receive(:parents).and_return([])
end

it('adds the template tag automatically') do
expect(described_class.tags).to match_array([:template])
end
let(:parent_dir) { File.join(root, 'two') }
let(:parent_artifact) { File.join(parent_dir, 'build', 'plain.syaml') }
let(:parent) { parent_project }
let(:parent_project) { double('parent_project') }

it('adds option for the output dir') do
expect(described_class).to add_option(:output_dir, 'build')
end
it('adds the template tag automatically') do
expect(described_class.tags).to match_array([:template])
end

it('finds projects that have erb files') do
expect(described_class).to define_projects.with('**/*.erb')
end
it('adds option for the output dir') do
expect(described_class).to add_config(:output_dir, 'build')
end

it('subscribes to the build event') do
expect(described_class).to subscribe_to(:build).with(:on_build)
end
it('adds option for the datasource name') do
expect(described_class).to add_config(:datasource, 'plain')
end

describe('#on_build') do
it('does not create any artifacts when no parents exist') do
expect(subject.artifacts(project)).to be_empty
end
it('finds projects that have erb files') do
expect(described_class).to provide_projects_containing('**/*.erb')
end

it('populates a template when using a single environment') do
test_output({ 'test1' => { 'nic' => 'cage' } }, 'test.test1' => 'cage')
end
it('subscribes to the build event') do
expect(described_class).to subscribe_to(:build).with(:on_build)
end

it('can cope with templates with file extensions') do
FileUtils.rm_f(File.join(root, 'one', 'test.erb'))
File.write(File.join(root, 'one', 'test.txt.erb'), '<%= data[\'nic\'] %>')
describe('#on_build') do
it('does not create any artifacts when no parents exist') do
expect(subject.artifacts).to be_empty
end

test_output({ 'test1' => { 'nic' => 'cage' } }, 'test.test1.txt' => 'cage')
end
it('populates a template when using a single environment') do
test_output({ 'test1' => { 'nic' => 'cage' } }, 'test.test1' => 'cage')
end

it('can cope with multiple environments') do
test_output({ 'test1' => { 'nic' => 'cage' }, 'test2' => { 'nic' => 'copolla' } },
'test.test1' => 'cage', 'test.test2' => 'copolla')
end
it('can cope with templates with file extensions') do
FileUtils.rm_f(File.join(root, 'one', 'test.erb'))
File.write(File.join(root, 'one', 'test.txt.erb'), '<%= data[\'nic\'] %>')
allow(file_tree).to receive(:glob).and_return(
[File.join(project_path, 'test.txt.erb')]
)

test_output({ 'test1' => { 'nic' => 'cage' } }, 'test.test1.txt' => 'cage')
end

describe('#artifacts') do
it('fails to load data when input is not a hash') do
project.add_parent(parent)
it('can cope with multiple environments') do
test_output({ 'test1' => { 'nic' => 'cage' }, 'test2' => { 'nic' => 'copolla' } },
'test.test1' => 'cage', 'test.test2' => 'copolla')
end
end

File.open(parent_artifact, 'wb') do |f|
f.write({ 'test1' => 'derp' }.to_yaml)
end
describe('#artifacts') do
it('fails to load data when input is not a hash') do
allow(project).to receive(:parents).and_return([parent])

expect do
subject.artifacts(project)
end.to raise_error("File #{parent_artifact} is not valid for this plugin, each top level value must be a hash")
File.open(parent_artifact, 'wb') do |f|
f.write({ 'test1' => 'derp' }.to_yaml)
end

it('write artifacts to a different output directory') do
allow(YAML).to receive(:load_file) { { 'local' => { 'template' => { 'output_dir' => 'nic_cage' } } } }
expect do
subject.artifacts
end.to raise_error("File #{parent_artifact} is not valid for this plugin, each top level value must be a hash")
end

test_artifacts({ 'test1' => { 'nic' => 'cage' } }, { 'test.local' => 'cage' }, 'nic_cage')
end
it('write artifacts to a different output directory') do
allow(env).to receive(:config).and_return(template: { output_dir: 'nic_cage' })

test_artifacts({ 'test1' => { 'nic' => 'cage' } }, { 'test.test1' => 'cage' }, 'nic_cage')
end
end

describe('#artifacts') do
let(:parent_artifact) { File.join(parent_dir, 'build', 'encrypted.syaml') }
describe('#artifacts') do
let(:parent_artifact) { File.join(parent_dir, 'build', 'encrypted.syaml') }

it('can read data from a different data source') do
allow(YAML).to receive(:load_file) { { 'local' => { 'template' => { 'datasource' => 'encrypted' } } } }
it('can read data from a different data source') do
allow(env).to receive(:config).and_return(template: { datasource: 'encrypted' })

test_artifacts({ 'test1' => { 'nic' => 'cage' } }, 'test.local' => 'cage')
end
test_artifacts({ 'test1' => { 'nic' => 'cage' } }, 'test.test1' => 'cage')
end
end
end
Loading

0 comments on commit e7d2fb2

Please sign in to comment.