Skip to content
Browse files

A Hoe plugin that allows your gem to gain maximum benefit from http:/…

  • Loading branch information...
0 parents commit 830d4f1ffd9b3ad4788229dec7fbc14cff97b896 @drbrain committed Feb 15, 2012
Showing with 633 additions and 0 deletions.
  1. +8 −0 .autotest
  2. +5 −0 .gitignore
  3. +13 −0 .travis.yml
  4. +5 −0 History.txt
  5. +7 −0 Manifest.txt
  6. +108 −0 README.rdoc
  7. +21 −0 Rakefile
  8. +281 −0 lib/hoe/travis.rb
  9. +185 −0 test/test_hoe_travis.rb
8 .autotest
@@ -0,0 +1,8 @@
+# -*- ruby -*-
+
+require 'autotest/restart'
+
+Autotest.add_hook :initialize do |at|
+ at.testlib = 'minitest/autorun'
+end
+
5 .gitignore
@@ -0,0 +1,5 @@
+*.rbc
+*.swp
+/TAGS
+/doc
+/pkg
13 .travis.yml
@@ -0,0 +1,13 @@
+---
+before_script:
+- gem install hoe-travis --no-rdoc --no-ri
+- rake travis:before
+language: ruby
+notifications:
+ email:
+ - drbrain@segment7.net
+rvm:
+- 1.8.7
+- 1.9.2
+- 1.9.3
+script: rake travis
5 History.txt
@@ -0,0 +1,5 @@
+=== 1.0 / 2012-02-15
+
+* Major enhancements
+ * Birthday!
+
7 Manifest.txt
@@ -0,0 +1,7 @@
+.autotest
+History.txt
+Manifest.txt
+README.rdoc
+Rakefile
+lib/hoe/travis.rb
+test/test_hoe_travis.rb
108 README.rdoc
@@ -0,0 +1,108 @@
+= hoe-travis
+
+home :: https://github.com/drbrain/hoe-travis
+rdoc :: http://docs.seattlerb.org/hoe-travis
+bugs :: https://github.com/drbrain/hoe-travis/issues
+
+== Description
+
+hoe-travis is a Hoe plugin that allows your gem to gain maximum benefit from
+http://travis-ci.org. The plugin contains a <code>.travis.yml</code>
+generator and a pre-defined rake task which runs the tests and ensures your
+manifest file is correct.
+
+With hoe-travis, additional checks and setup can be added with
+locally-verifiable rake tasks instead of committing and pushing a change,
+waiting for travis to run your tests, then trying a new commit if you didn't
+fix the problem.
+
+== Features
+
+* .travis.yml generation task
+* Pre-defined rake tasks which are run by travis-ci
+* Easy to hook up rake tasks for additional travis-ci setup and checks
+
+== Install
+
+ sudo gem install hoe-travis
+
+== Synopsis
+
+Add hoe-travis as a plugin to your Rakefile:
+
+ Hoe.plugin :travis
+
+Then generate a .travis.yml
+
+ $ rake travis:generate
+
+This will bring up your EDITOR with your travis.yml for any desired tweaks.
+Save the file when you're done, then check in your .travis.yml. For further
+details of how the configuration is generated see Hoe::Travis@Config
+
+(If you don't have the EDITOR environment variable set to your favorite
+editor, please do so. Note that some editors may need extra flags to wait for
+files to be edited. For MacVIM, <code>export EDITOR="mvim
+--remote-wait"</code> will wait for the file to be closed before returning.)
+
+If you would like to make future changes to your .travis.yml you can run:
+
+ $ rake travis:edit
+
+Which, like <code>travis:generate</code>, will bring up your EDITOR with your
+.travis.yml. When you've saved the file the changes will be checked by
+travis-lint before writing back to .travis.yml and give you a chance to
+correct them.
+
+If you've edited your .travis.yml by hand you can run:
+
+ $ rake travis:check
+
+to check it.
+
+Testing your travis-ci setup is easy with hoe-travis. You can run:
+
+ $ rake travis
+
+to run the same checks travis-ci will. By default this includes running the
+tests and ensuring the Manifest.txt file is complete. There is also the
+before script:
+
+ $ rake travis:before
+
+Which will run the setup tasks needed for your project.
+
+== Developers
+
+After checking out the source, run:
+
+ $ rake newb
+
+This task will install any missing dependencies, run the tests/specs,
+and generate the RDoc.
+
+== License
+
+(The MIT License)
+
+Copyright (c) Eric Hodel
+
+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.
+
21 Rakefile
@@ -0,0 +1,21 @@
+# -*- ruby -*-
+
+require 'rubygems'
+require 'hoe'
+
+Hoe.plugin :minitest
+Hoe.plugin :git
+Hoe.plugin :travis
+
+Hoe.spec 'hoe-travis' do
+ developer 'Eric Hodel', 'drbrain@segment7.net'
+
+ rdoc_locations << 'docs.seattlerb.org:/data/www/docs.seattlerb.org/hoe-travis/'
+
+ self.readme_file = 'README.rdoc'
+ self.extra_rdoc_files += Dir['*.rdoc']
+
+ self.extra_deps << ['travis-lint', '~> 1.0']
+end
+
+# vim: syntax=ruby
281 lib/hoe/travis.rb
@@ -0,0 +1,281 @@
+require 'hoe'
+require 'tempfile'
+
+##
+# The travis plugin for Hoe manages your .travis.yml file for you in a clean
+# and extensible way you can use across projects or by through integration
+# with other Hoe plugins.
+#
+# == Tasks
+#
+# You can extend the following tasks in your Rakefile or Hoe plugins to add
+# extra checks to travis-ci.
+#
+# travis::
+# Run by travis-ci. Defaults to running your tests and checking your
+# manifest file. You can run this locally to check what travis-ci will do.
+#
+# travis:before::
+# Runs as the before_script on travis-ci. Defaults to installing your
+# development dependencies.
+#
+# travis:check::
+# Runs travis-lint against your .travis.yml.
+#
+# travis:edit::
+# Pulls up your .travis.yml in your EDITOR and runs travis-lint upon saving.
+# Does not allow you to save a bad .travis.yml.
+#
+# travis:generate::
+# Generates a .travis.yml based on your Hoe spec and .hoerc then brings it
+# up in your EDITOR and runs travis-lint upon saving. Does not allow you to
+# save a bad .travis.yml.
+#
+# == Config
+#
+# The configuration is only used to generate the .travis.yml. After you've
+# generated a .travis.yml you may any changes you wish to it and the following
+# defaults will not apply. If you have multiple projects, setting up a common
+# custom configuration in ~/.hoerc can save you time.
+#
+# The following default configuration options are provided under the "travis"
+# key of the Hoe configuration (accessible from Hoe#with_config):
+#
+# before_script::
+# Array of commands run before the test script. Defaults to installing
+# hoe-travis and its dependencies (rake and hoe) followed by running the
+# travis:before rake task.
+#
+# script::
+# Runs the travis rake task.
+#
+# versions::
+# The versions of ruby used to run your tests. Note that if you have
+# multiruby installed, your installed versions will be preferred over the
+# defaults of ruby 1.8.7, 1.9.2 and 1.9.3.
+#
+# In your .hoerc you may provide a "notifications" key such as:
+#
+# travis:
+# notifications:
+# irc: "irc.example#your_channel"
+#
+# Notifications specified in a .hoerc will override the default email
+# notifications created from the Hoe spec.
+
+module Hoe::Travis
+
+ ##
+ # This version of Hoe::Travis
+
+ VERSION = '1.0'
+
+ Hoe::DEFAULT_CONFIG['travis'] = {
+ 'before_script' => [
+ 'gem install hoe-travis --no-rdoc --no-ri',
+ 'rake travis:before',
+ ],
+ 'script' => 'rake travis',
+ 'versions' => %w[
+ 1.8.7
+ 1.9.2
+ 1.9.3
+ ],
+ }
+
+ ##
+ # Adds travis tasks to rake
+
+ def define_travis_tasks
+ desc "Runs your tests for travis"
+ task :travis => %w[test travis:fake_config check_manifest]
+
+ namespace :travis do
+ desc "Run by travis-ci before your running the default checks"
+ task :before => %w[
+ check_extra_deps
+ ]
+
+ desc "Runs travis-lint on your .travis.yml"
+ task :check do
+ abort unless check_travis_yml '.travis.yml'
+ end
+
+ desc "Brings .travis.yml up in your EDITOR then checks it on save"
+ task :edit do
+ Tempfile.open 'travis.yml' do |io|
+ io.write File.read '.travis.yml'
+ io.rewind
+
+ ok = travis_yml_edit io.path
+
+ travis_yml_write io.path if ok
+ end
+ end
+
+ task :fake_config do
+ travis_fake_config
+ end
+
+ desc "Generates a new .travis.yml and allows you to customize it with your EDITOR"
+ task :generate do
+ Tempfile.open 'travis.yml' do |io|
+ io.write travis_yml_generate
+ io.rewind
+
+ ok = travis_yml_edit io.path
+
+ travis_yml_write io.path if ok
+ end
+ end
+ end
+ end
+
+ def have_gem? name # :nodoc:
+ Gem::Specification.find_by_name name
+ rescue Gem::LoadError
+ return false
+ end
+
+ ##
+ # Extracts the travis before_script from your .hoerc
+
+ def travis_before_script
+ with_config { |config, _| config['travis']['before_script'] }
+ end
+
+ ##
+ # Creates a fake config file for use on travis-ci. Running this with a
+ # pre-existing .hoerc has no effect.
+
+ def travis_fake_config
+ fake_hoerc = File.expand_path '~/.hoerc'
+
+ return if File.exist? fake_hoerc
+
+ config = { 'exclude' => /\.(git|travis)/ }
+
+ open fake_hoerc, 'w' do |io|
+ YAML.dump config, io
+ end
+ end
+
+ ##
+ # Creates the travis notifications hash from the developers for your
+ # project. The developer will be merged with the travis notifications from
+ # your .hoerc.
+
+ def travis_notifications
+ email = @email.compact
+ email.delete ''
+
+ default_notifications = { 'email' => email }
+ notifications = with_config do |config, _|
+ config['travis']['notifications']
+ end || {}
+
+ default_notifications.merge notifications
+ end
+
+ ##
+ # Determines the travis versions from multiruby, if available, or your
+ # .hoerc.
+
+ def travis_versions
+ if have_gem? 'ZenTest' then
+ `multiruby -v` =~ /^Passed: (.*)/
+
+ $1.split(', ').map do |ruby_release|
+ ruby_release.sub(/-.*/, '')
+ end
+ else
+ with_config do |config, _|
+ config['travis']['versions']
+ end
+ end.sort
+ end
+
+ ##
+ # Runs travis-lint against the travis.yml in +path+. If the file is OK true
+ # is returned, otherwise the issues are displayed on $stderr and false is
+ # returned.
+
+ def travis_yml_check path
+ require 'travis/lint'
+
+ travis_yml = YAML.load_file path
+
+ issues = Travis::Lint::Linter.validate travis_yml
+
+ return true if issues.empty?
+
+ issues.each do |issue|
+ warn "There is an issue with the key #{issue[:key].inspect}:"
+ warn "\t#{issue[:issue]}"
+ end
+
+ false
+ rescue ArgumentError, Psych::SyntaxError => e
+ warn "invalid YAML in travis.yml file at #{path}: #{e.message}"
+
+ return false
+ end
+
+ ##
+ # Loads the travis.yml in +path+ in your EDITOR (or vi if unset). Upon
+ # saving the travis.yml is checked with travis-lint. If any problems are
+ # found you will be asked to retry the edit.
+ #
+ # If the edited travis.yml is OK true is returned, otherwise false.
+
+ def travis_yml_edit path
+ loop do
+ editor = ENV['EDITOR'] || 'vi'
+
+ system "#{editor} #{path}"
+
+ break true if travis_yml_check path
+
+ abort unless $stdout.tty?
+
+ print "\nRetry edit? [Yn]\n> "
+ $stdout.flush
+
+ break false if $stdin.gets =~ /\An/i
+ end
+ end
+
+ ##
+ # Generates a travis.yml from .hoerc, the Hoe spec and the default
+ # configuration.
+
+ def travis_yml_generate
+ travis_yml = {
+ 'before_script' => travis_before_script,
+ 'language' => 'ruby',
+ 'notifications' => travis_notifications,
+ 'rvm' => travis_versions,
+ 'script' => 'rake travis'
+ }
+
+ travis_yml.each do |key, value|
+ travis_yml.delete key unless value
+ end
+
+ YAML.dump travis_yml
+ end
+
+ ##
+ # Writes the travis.yml in +source_file+ to .travis.yml in the current
+ # directory. Overwrites an existing .travis.yml.
+
+ def travis_yml_write source_file
+ open source_file do |source_io|
+ open '.travis.yml', 'w' do |dest_io|
+ dest_io.write source_io.read
+ end
+ end
+ end
+
+end
+
185 test/test_hoe_travis.rb
@@ -0,0 +1,185 @@
+require 'minitest/autorun'
+require 'hoe/travis'
+require 'tmpdir'
+
+class TestHoeTravis < MiniTest::Unit::TestCase
+
+ def setup
+ @hoe = Hoe.spec "blah" do
+ developer 'author', 'email@example'
+ developer 'silent', ''
+
+ self.readme_file = 'README.rdoc'
+ end
+
+ @hoe.extend Hoe::Travis
+
+ @editor = ENV['EDITOR']
+ end
+
+ def teardown
+ ENV['EDITOR'] = @editor
+ end
+
+ def test_have_gem_eh
+ assert @hoe.have_gem? 'hoe'
+ refute @hoe.have_gem? 'nonexistent'
+ end
+
+ def test_travis_before_script
+ expected = @hoe.with_config do |config, _|
+ config['travis']['before_script']
+ end
+
+ assert_equal expected, @hoe.travis_before_script
+ end
+
+ def test_travis_fake_config
+ home = ENV['HOME']
+
+ Dir.mktmpdir do |path|
+ ENV['HOME'] = path
+
+ fake_config = File.expand_path '~/.hoerc'
+
+ @hoe.travis_fake_config
+
+ assert File.exist? fake_config
+
+ expected = {
+ 'exclude' => /\.(git|travis)/
+ }
+
+ assert_equal expected, YAML.load_file(fake_config)
+ end
+ ensure
+ ENV['HOME'] = home
+ end
+
+ def test_travis_notifications
+ expected = {
+ 'email' => %w[email@example]
+ }
+
+ assert_equal expected, @hoe.travis_notifications
+ end
+
+ def test_travis_notifications_config
+ Hoe::DEFAULT_CONFIG['travis']['notifications'] = {
+ 'email' => %w[other@example]
+ }
+
+ expected = {
+ 'email' => %w[other@example]
+ }
+
+ assert_equal expected, @hoe.travis_notifications
+ ensure
+ Hoe::DEFAULT_CONFIG['travis'].delete 'notifications'
+ end
+
+ def test_travis_versions
+ def @hoe.have_gem?(name) false end
+
+ assert_equal %w[1.8.7 1.9.2 1.9.3], @hoe.travis_versions
+ end
+
+ def test_travis_yml_check
+ Tempfile.open 'travis' do |io|
+ io.write "---\nlanguage: ruby\nrvm:\n - 1.8.7\n"
+ io.rewind
+
+ assert @hoe.travis_yml_check io.path
+ end
+ end
+
+ def test_travis_yml_check_invalid
+ Tempfile.open 'travis' do |io|
+ io.write "---\nlanguage: ruby\n"
+ io.rewind
+
+ out, err = capture_io do
+ refute @hoe.travis_yml_check io.path
+ end
+
+ assert_empty out
+ refute_empty err
+ end
+ end
+
+ def test_travis_yml_edit
+ Tempfile.open 'out' do |out_io|
+ ENV['EDITOR'] = "cat > #{out_io.path} < "
+
+ Tempfile.open 'travis' do |io|
+ io.write "---\nlanguage: ruby\nrvm:\n - 1.8.7\n"
+ io.rewind
+
+ @hoe.travis_yml_edit io.path
+ end
+
+ assert_equal "---\nlanguage: ruby\nrvm:\n - 1.8.7\n", out_io.read
+ end
+ end
+
+ def test_travis_yml_edit_bad
+ ENV['EDITOR'] = "cat > /dev/null < "
+
+ Tempfile.open 'travis' do |io|
+ io.write "travis"
+ io.rewind
+
+ e = assert_raises SystemExit do
+ capture_io do
+ @hoe.travis_yml_edit io.path
+ end
+ end
+
+ assert_equal 1, e.status
+ end
+ end
+
+ def test_travis_yml_generate
+ def @hoe.have_gem?(name) false end
+
+ Dir.mktmpdir do |path|
+ Dir.chdir path do
+ travis_yml = @hoe.travis_yml_generate
+
+ expected = <<-TRAVIS_YML
+---
+before_script:
+- gem install hoe-travis --no-rdoc --no-ri
+- rake travis:before
+language: ruby
+notifications:
+ email:
+ - email@example
+rvm:
+- 1.8.7
+- 1.9.2
+- 1.9.3
+script: rake travis
+ TRAVIS_YML
+
+ assert_equal expected, travis_yml
+ end
+ end
+ end
+
+ def test_travis_yml_write
+ Dir.mktmpdir do |path|
+ Dir.chdir path do
+ open 'travis', 'w' do |io| io.write 'travis' end
+
+ @hoe.travis_yml_write 'travis'
+
+ assert File.exist? '.travis.yml'
+
+ assert_equal 'travis', File.read('.travis.yml')
+ end
+ end
+ end
+
+end
+

0 comments on commit 830d4f1

Please sign in to comment.
Something went wrong with that request. Please try again.