From 70b7507734a17d2f8927769351dfb131a07ec46a Mon Sep 17 00:00:00 2001 From: Thom May Date: Fri, 2 Sep 2016 09:23:45 +0100 Subject: [PATCH 1/2] Enable ChefStyle and Travis testing Signed-off-by: Thom May --- .travis.yml | 11 +++++++++++ Gemfile | 3 ++- Rakefile | 17 +++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..9df4ff8 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,11 @@ +branches: + only: + master + +language: ruby +rvm: + - 2.1 + - 2.2 + - 2.3.1 +before_install: gem install bundler -v 1.11.2 +script: bundle exec rake ci diff --git a/Gemfile b/Gemfile index 075ede0..8aad981 100644 --- a/Gemfile +++ b/Gemfile @@ -2,6 +2,7 @@ source 'https://rubygems.org' gemspec -group :test do +group :development, :test do gem 'minitar' + gem 'chefstyle' end diff --git a/Rakefile b/Rakefile index 809eb56..4140e14 100644 --- a/Rakefile +++ b/Rakefile @@ -1,2 +1,19 @@ require "bundler/gem_tasks" +require "rspec/core/rake_task" +RSpec::Core::RakeTask.new(:spec) + +task :default => :spec + +desc "Run tests for Travis CI" +task ci: [:style, :spec] + +begin + require "chefstyle" + require "rubocop/rake_task" + RuboCop::RakeTask.new(:style) do |task| + task.options += ["--display-cop-names", "--no-color"] + end +rescue LoadError + puts "chefstyle/rubocop is not available. gem install chefstyle to do style checking." +end From 374fc13a47f2749ce0ede15d196819a85ee5518e Mon Sep 17 00:00:00 2001 From: Thom May Date: Fri, 2 Sep 2016 09:29:28 +0100 Subject: [PATCH 2/2] ChefStyle Signed-off-by: Thom May --- Gemfile | 6 +- cookbook-omnifetch.gemspec | 32 +-- lib/cookbook-omnifetch.rb | 5 +- lib/cookbook-omnifetch/artifactserver.rb | 27 +-- lib/cookbook-omnifetch/base.rb | 2 +- lib/cookbook-omnifetch/chef_server.rb | 6 +- lib/cookbook-omnifetch/exceptions.rb | 6 +- lib/cookbook-omnifetch/git.rb | 42 ++-- lib/cookbook-omnifetch/integration.rb | 3 +- lib/cookbook-omnifetch/path.rb | 6 +- .../example_cookbook-0.5.0/metadata.rb | 6 +- .../cookbooks/example_cookbook/metadata.rb | 6 +- spec/spec_helper.rb | 9 +- spec/unit/artifactserver_spec.rb | 21 +- spec/unit/base_spec.rb | 70 +++---- spec/unit/chef_server_spec.rb | 12 +- spec/unit/exceptions_spec.rb | 4 +- spec/unit/git_spec.rb | 195 +++++++++--------- spec/unit/path_spec.rb | 84 ++++---- 19 files changed, 263 insertions(+), 279 deletions(-) diff --git a/Gemfile b/Gemfile index 8aad981..7843bf7 100644 --- a/Gemfile +++ b/Gemfile @@ -1,8 +1,8 @@ -source 'https://rubygems.org' +source "https://rubygems.org" gemspec group :development, :test do - gem 'minitar' - gem 'chefstyle' + gem "minitar" + gem "chefstyle" end diff --git a/cookbook-omnifetch.gemspec b/cookbook-omnifetch.gemspec index 985886a..452bc61 100644 --- a/cookbook-omnifetch.gemspec +++ b/cookbook-omnifetch.gemspec @@ -1,26 +1,26 @@ # coding: utf-8 -lib = File.expand_path('../lib', __FILE__) +lib = File.expand_path("../lib", __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) -require 'cookbook-omnifetch/version' +require "cookbook-omnifetch/version" Gem::Specification.new do |spec| spec.name = "cookbook-omnifetch" spec.version = CookbookOmnifetch::VERSION spec.authors = [ - 'Jamie Winsor', - 'Josiah Kiehl', - 'Michael Ivey', - 'Justin Campbell', - 'Seth Vargo', - 'Daniel DeLeo' + "Jamie Winsor", + "Josiah Kiehl", + "Michael Ivey", + "Justin Campbell", + "Seth Vargo", + "Daniel DeLeo", ] - spec.email = [ - 'jamie@vialstudios.com', - 'jkiehl@riotgames.com', - 'michael.ivey@riotgames.com', - 'justin@justincampbell.me', - 'sethvargo@gmail.com', - 'dan@getchef.com' + spec.email = [ + "jamie@vialstudios.com", + "jkiehl@riotgames.com", + "michael.ivey@riotgames.com", + "justin@justincampbell.me", + "sethvargo@gmail.com", + "dan@getchef.com", ] spec.summary = %q{Library code to fetch Chef cookbooks from a variety of sources to a local cache} spec.homepage = "http://www.getchef.com/" @@ -34,6 +34,6 @@ Gem::Specification.new do |spec| spec.add_dependency "mixlib-archive", "~> 0.2" spec.add_development_dependency "rake", "~> 10.3" - spec.add_development_dependency 'rspec','~> 3.0' + spec.add_development_dependency "rspec", "~> 3.0" end diff --git a/lib/cookbook-omnifetch.rb b/lib/cookbook-omnifetch.rb index 1305933..8d6086e 100644 --- a/lib/cookbook-omnifetch.rb +++ b/lib/cookbook-omnifetch.rb @@ -7,7 +7,6 @@ require "cookbook-omnifetch/path" require "cookbook-omnifetch/artifactserver" - module CookbookOmnifetch # Create a new instance of a Location class given dependency and options. @@ -42,8 +41,8 @@ def self.init(dependency, options = {}) def self.which(executable) if File.file?(executable) && File.executable?(executable) executable - elsif ENV['PATH'] - path = ENV['PATH'].split(File::PATH_SEPARATOR).find do |p| + elsif ENV["PATH"] + path = ENV["PATH"].split(File::PATH_SEPARATOR).find do |p| File.executable?(File.join(p, executable)) end path && File.expand_path(executable, path) diff --git a/lib/cookbook-omnifetch/artifactserver.rb b/lib/cookbook-omnifetch/artifactserver.rb index 205ff0d..4ddb7d3 100644 --- a/lib/cookbook-omnifetch/artifactserver.rb +++ b/lib/cookbook-omnifetch/artifactserver.rb @@ -1,7 +1,7 @@ -require 'cookbook-omnifetch/base' +require "cookbook-omnifetch/base" -require 'mixlib/archive' -require 'tmpdir' +require "mixlib/archive" +require "tmpdir" module CookbookOmnifetch @@ -47,7 +47,7 @@ def install FileUtils.mkdir_p(staging_root) unless staging_root.exist? Dir.mktmpdir(nil, staging_root) do |staging_dir| Mixlib::Archive.new(cache_path).extract(staging_dir, perms: false, - ignore: /^\./) + ignore: /^\./) staged_cookbook_path = File.join(staging_dir, cookbook_name) validate_cached!(staged_cookbook_path) FileUtils.mv(staged_cookbook_path, install_path) @@ -85,8 +85,8 @@ def cached_cookbook def lock_data out = {} - out['artifactserver'] = uri - out['version'] = cookbook_version + out["artifactserver"] = uri + out["version"] = cookbook_version out end @@ -98,24 +98,13 @@ def to_lock "#to_lock must be implemented on #{self.class.name}!" end - - def ==(other) - raise 'TODO' - other.is_a?(GitLocation) && - other.uri == uri && - other.branch == branch && - other.tag == tag && - other.shortref == shortref && - other.rel == rel - end - # The path where all pristine tarballs from an artifactserver are held. # Tarballs are moved/swapped into this location once they have been staged # in a co-located staging directory. # # @return [Pathname] def cache_root - Pathname.new(CookbookOmnifetch.cache_path).join('.cache', 'artifactserver') + Pathname.new(CookbookOmnifetch.cache_path).join(".cache", "artifactserver") end # The path where tarballs are downloaded to and unzipped. On certain platforms @@ -129,7 +118,7 @@ def cache_root # # @return [Pathname] def staging_root - Pathname.new(CookbookOmnifetch.cache_path).join('.cache_tmp', 'artifactserver') + Pathname.new(CookbookOmnifetch.cache_path).join(".cache_tmp", "artifactserver") end # The path where the pristine tarball is cached diff --git a/lib/cookbook-omnifetch/base.rb b/lib/cookbook-omnifetch/base.rb index 56d8677..fd3d418 100644 --- a/lib/cookbook-omnifetch/base.rb +++ b/lib/cookbook-omnifetch/base.rb @@ -1,4 +1,4 @@ -require 'cookbook-omnifetch/exceptions' +require "cookbook-omnifetch/exceptions" module CookbookOmnifetch class BaseLocation diff --git a/lib/cookbook-omnifetch/chef_server.rb b/lib/cookbook-omnifetch/chef_server.rb index c8f0716..bc3f347 100644 --- a/lib/cookbook-omnifetch/chef_server.rb +++ b/lib/cookbook-omnifetch/chef_server.rb @@ -1,4 +1,4 @@ -require 'cookbook-omnifetch/base' +require "cookbook-omnifetch/base" module CookbookOmnifetch class CookbookMetadata @@ -12,7 +12,7 @@ class CookbookMetadata :attributes, :files, :templates, - :root_files + :root_files, ].freeze def initialize(metadata) @@ -98,7 +98,7 @@ def cache_key # # @return [Pathname] def staging_root - Pathname.new(CookbookOmnifetch.cache_path).join('.cache_tmp', 'artifactserver') + Pathname.new(CookbookOmnifetch.cache_path).join(".cache_tmp", "artifactserver") end def staging_path diff --git a/lib/cookbook-omnifetch/exceptions.rb b/lib/cookbook-omnifetch/exceptions.rb index cc08d3e..2ed705e 100644 --- a/lib/cookbook-omnifetch/exceptions.rb +++ b/lib/cookbook-omnifetch/exceptions.rb @@ -20,9 +20,9 @@ class GitError < OmnifetchError class GitNotInstalled < GitError def initialize - super 'You need to install Git before you can download ' \ - 'cookbooks from git repositories. For more information, please ' \ - 'see the Git docs: http://git-scm.org.' + super "You need to install Git before you can download " \ + "cookbooks from git repositories. For more information, please " \ + "see the Git docs: http://git-scm.org." end end diff --git a/lib/cookbook-omnifetch/git.rb b/lib/cookbook-omnifetch/git.rb index 1578c56..972d5c5 100644 --- a/lib/cookbook-omnifetch/git.rb +++ b/lib/cookbook-omnifetch/git.rb @@ -1,7 +1,7 @@ -require 'tmpdir' -require 'cookbook-omnifetch' -require 'cookbook-omnifetch/base' -require 'cookbook-omnifetch/exceptions' +require "tmpdir" +require "cookbook-omnifetch" +require "cookbook-omnifetch/base" +require "cookbook-omnifetch/exceptions" module CookbookOmnifetch class GitLocation < BaseLocation @@ -23,7 +23,7 @@ def initialize(dependency, options = {}) @rel = options[:rel] # The revision to parse - @rev_parse = options[:ref] || options[:branch] || options[:tag] || 'master' + @rev_parse = options[:ref] || options[:branch] || options[:tag] || "master" end # @see BaseLoation#installed? @@ -41,26 +41,26 @@ def install if cached? Dir.chdir(cache_path) do - git %|fetch --force --tags #{uri} "refs/heads/*:refs/heads/*"| + git %{fetch --force --tags #{uri} "refs/heads/*:refs/heads/*"} end else - git %|clone #{uri} "#{cache_path}" --bare --no-hardlinks| + git %{clone #{uri} "#{cache_path}" --bare --no-hardlinks} end Dir.chdir(cache_path) do - @revision ||= git %|rev-parse #{@rev_parse}| + @revision ||= git %{rev-parse #{@rev_parse}} end # Clone into a scratch directory for validations - git %|clone --no-checkout "#{cache_path}" "#{scratch_path}"| + git %{clone --no-checkout "#{cache_path}" "#{scratch_path}"} # Make sure the scratch directory is up-to-date and account for rel paths Dir.chdir(scratch_path) do - git %|fetch --force --tags "#{cache_path}"| - git %|reset --hard #{@revision}| + git %{fetch --force --tags "#{cache_path}"} + git %{reset --hard #{@revision}} if rel - git %|filter-branch --subdirectory-filter "#{rel}" --force| + git %{filter-branch --subdirectory-filter "#{rel}" --force} end end @@ -72,7 +72,7 @@ def install FileUtils.cp_r(scratch_path, install_path) # Remove the git history - FileUtils.rm_rf(File.join(install_path, '.git')) + FileUtils.rm_rf(File.join(install_path, ".git")) install_path.chmod(0777 & ~File.umask) ensure @@ -91,11 +91,11 @@ def cached_cookbook def ==(other) other.is_a?(GitLocation) && - other.uri == uri && - other.branch == branch && - other.tag == tag && - other.shortref == shortref && - other.rel == rel + other.uri == uri && + other.branch == branch && + other.tag == tag && + other.shortref == shortref && + other.rel == rel end def to_s @@ -163,11 +163,11 @@ def shortref # @raise [String] # the +$stdout+ from the command def git(command, error = true) - unless CookbookOmnifetch.which('git') || CookbookOmnifetch.which('git.exe') || CookbookOmnifetch.which('git.bat') + unless CookbookOmnifetch.which("git") || CookbookOmnifetch.which("git.exe") || CookbookOmnifetch.which("git.bat") raise GitNotInstalled.new end - response = CookbookOmnifetch.shell_out_class.shell_out(%|git #{command}|) + response = CookbookOmnifetch.shell_out_class.shell_out(%{git #{command}}) if error && !response.success? raise GitCommandError.new(command, cache_path, response.stderr) @@ -188,7 +188,7 @@ def cached? # @return [Pathname] def cache_path Pathname.new(CookbookOmnifetch.cache_path) - .join('.cache', 'git', Digest::SHA1.hexdigest(uri)) + .join(".cache", "git", Digest::SHA1.hexdigest(uri)) end end end diff --git a/lib/cookbook-omnifetch/integration.rb b/lib/cookbook-omnifetch/integration.rb index 7c2940c..4fa5815 100644 --- a/lib/cookbook-omnifetch/integration.rb +++ b/lib/cookbook-omnifetch/integration.rb @@ -1,4 +1,4 @@ -require 'cookbook-omnifetch/exceptions' +require "cookbook-omnifetch/exceptions" module CookbookOmnifetch @@ -28,7 +28,6 @@ def self.configurable(name) value end end - end configurable :cache_path diff --git a/lib/cookbook-omnifetch/path.rb b/lib/cookbook-omnifetch/path.rb index 947b246..2c527ed 100644 --- a/lib/cookbook-omnifetch/path.rb +++ b/lib/cookbook-omnifetch/path.rb @@ -1,4 +1,4 @@ -require 'cookbook-omnifetch/base' +require "cookbook-omnifetch/base" module CookbookOmnifetch class PathLocation < BaseLocation @@ -65,8 +65,8 @@ def expanded_path def ==(other) other.is_a?(PathLocation) && - other.metadata? == metadata? && - other.relative_path == relative_path + other.metadata? == metadata? && + other.relative_path == relative_path end def lock_data diff --git a/spec/fixtures/cookbooks/example_cookbook-0.5.0/metadata.rb b/spec/fixtures/cookbooks/example_cookbook-0.5.0/metadata.rb index f0fa5ad..87b3898 100644 --- a/spec/fixtures/cookbooks/example_cookbook-0.5.0/metadata.rb +++ b/spec/fixtures/cookbooks/example_cookbook-0.5.0/metadata.rb @@ -1,3 +1,3 @@ -name 'example_cookbook' -maintainer 'Berkshelf Core' -version '0.5.0' +name "example_cookbook" +maintainer "Berkshelf Core" +version "0.5.0" diff --git a/spec/fixtures/cookbooks/example_cookbook/metadata.rb b/spec/fixtures/cookbooks/example_cookbook/metadata.rb index f0fa5ad..87b3898 100644 --- a/spec/fixtures/cookbooks/example_cookbook/metadata.rb +++ b/spec/fixtures/cookbooks/example_cookbook/metadata.rb @@ -1,3 +1,3 @@ -name 'example_cookbook' -maintainer 'Berkshelf Core' -version '0.5.0' +name "example_cookbook" +maintainer "Berkshelf Core" +version "0.5.0" diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 4fd6deb..c8d0fa3 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,9 +1,9 @@ -require 'cookbook-omnifetch' +require "cookbook-omnifetch" module Fixtures def fixtures_path - spec_root.join('fixtures') + spec_root.join("fixtures") end def spec_root @@ -34,11 +34,10 @@ module MockCachedCookbook; end config.before(:suite) do CookbookOmnifetch.configure do |c| - c.cache_path = File.expand_path('~/.berkshelf') - c.storage_path = Pathname.new(File.expand_path('~/.berkshelf/cookbooks')) + c.cache_path = File.expand_path("~/.berkshelf") + c.storage_path = Pathname.new(File.expand_path("~/.berkshelf/cookbooks")) c.shell_out_class = MockShellOut c.cached_cookbook_class = MockCachedCookbook end end end - diff --git a/spec/unit/artifactserver_spec.rb b/spec/unit/artifactserver_spec.rb index 710a964..8fb0ab4 100644 --- a/spec/unit/artifactserver_spec.rb +++ b/spec/unit/artifactserver_spec.rb @@ -1,7 +1,7 @@ -require 'spec_helper' -require 'cookbook-omnifetch/artifactserver' -require 'zlib' -require 'archive/tar/minitar' +require "spec_helper" +require "cookbook-omnifetch/artifactserver" +require "zlib" +require "archive/tar/minitar" module CookbookOmnifetch describe ArtifactserverLocation do @@ -16,7 +16,7 @@ module CookbookOmnifetch let(:url) { "https://supermarket.getchef.com/api/v1/cookbooks/nginx/versions/1.5.23/download" } - let(:options) { {artifactserver: url, version: cookbook_version } } + let(:options) { { artifactserver: url, version: cookbook_version } } subject(:public_repo_location) { described_class.new(dependency, options) } @@ -37,7 +37,7 @@ module CookbookOmnifetch end it "sets the install location as the cache path plus cache key" do - expected_install_path = Pathname.new('~/.berkshelf/cookbooks').expand_path.join("nginx-1.5.23-supermarket.getchef.com") + expected_install_path = Pathname.new("~/.berkshelf/cookbooks").expand_path.join("nginx-1.5.23-supermarket.getchef.com") expect(public_repo_location.install_path).to eq(expected_install_path) end @@ -54,7 +54,7 @@ module CookbookOmnifetch it "provides lock data as a Hash" do expected_data = { "artifactserver" => url, - "version" => "1.5.23" + "version" => "1.5.23", } expect(public_repo_location.lock_data).to eq(expected_data) end @@ -69,14 +69,14 @@ module CookbookOmnifetch let(:cache_path) { File.join(test_root, "cache") } - let(:cookbook_fixtures_path) { fixtures_path.join('cookbooks') } + let(:cookbook_fixtures_path) { fixtures_path.join("cookbooks") } let(:cookbook_name) { "example_cookbook" } let(:cookbook_version) { "0.5.0" } let(:cookbook_tarball_handle) do - gz_file_name = File.join(test_root, 'input.gz') + gz_file_name = File.join(test_root, "input.gz") Zlib::GzipWriter.open(gz_file_name) do |gz| # Minitar writes the full paths provided and doesn't seem to have a way to # remove prefixes. So we chdir like barbarians. @@ -87,7 +87,7 @@ module CookbookOmnifetch File.open(gz_file_name) end - let(:cookbook_files) { %w". .. .gitignore .kitchen.yml Berksfile Berksfile.lock metadata.rb README.md recipes" } + let(:cookbook_files) { %w{. .. .gitignore .kitchen.yml Berksfile Berksfile.lock metadata.rb README.md recipes} } before do allow(CookbookOmnifetch).to receive(:storage_path).and_return(Pathname.new(storage_path)) @@ -114,4 +114,3 @@ module CookbookOmnifetch end end end - diff --git a/spec/unit/base_spec.rb b/spec/unit/base_spec.rb index 1c10869..511b5cd 100644 --- a/spec/unit/base_spec.rb +++ b/spec/unit/base_spec.rb @@ -1,59 +1,59 @@ -require 'spec_helper' -require 'cookbook-omnifetch/base' +require "spec_helper" +require "cookbook-omnifetch/base" module CookbookOmnifetch describe BaseLocation do - let(:constraint) { double('constraint') } - let(:dependency) { double('dependency', name: 'cookbook', version_constraint: constraint) } + let(:constraint) { double("constraint") } + let(:dependency) { double("dependency", name: "cookbook", version_constraint: constraint) } subject { described_class.new(dependency) } - describe '#installed?' do - it 'is an abstract function' do + describe "#installed?" do + it "is an abstract function" do expect { subject.installed? }.to raise_error(AbstractFunction) end end - describe '#install' do - it 'is an abstract function' do + describe "#install" do + it "is an abstract function" do expect { subject.install }.to raise_error(AbstractFunction) end end - describe '#cached_cookbook' do - it 'is an abstract function' do + describe "#cached_cookbook" do + it "is an abstract function" do expect { subject.cached_cookbook }.to raise_error(AbstractFunction) end end - describe '#to_lock' do - it 'is an abstract function' do + describe "#to_lock" do + it "is an abstract function" do expect { subject.to_lock }.to raise_error(AbstractFunction) end end - describe '#lock_data' do - it 'is an abstract function' do + describe "#lock_data" do + it "is an abstract function" do expect { subject.lock_data }.to raise_error(AbstractFunction) end end - describe '#validate_cached!' do - context 'when the path is not a cookbook' do + describe "#validate_cached!" do + context "when the path is not a cookbook" do before { CookbookOmnifetch.stub(:cookbook?).and_return(false) } - it 'raises an error' do - expect { - subject.validate_cached!('/foo/bar') - }.to raise_error(NotACookbook) + it "raises an error" do + expect do + subject.validate_cached!("/foo/bar") + end.to raise_error(NotACookbook) end end - context 'when the path is a cookbook' do + context "when the path is a cookbook" do let(:cookbook) do - double('cookbook', - cookbook_name: 'cookbook', - version: '0.1.0', + double("cookbook", + cookbook_name: "cookbook", + version: "0.1.0" ) end @@ -62,23 +62,23 @@ module CookbookOmnifetch MockCachedCookbook.stub(:from_path).and_return(cookbook) end - it 'raises an error if the constraint does not satisfy' do - constraint.stub(:satisfies?).with('0.1.0').and_return(false) - expect { + it "raises an error if the constraint does not satisfy" do + constraint.stub(:satisfies?).with("0.1.0").and_return(false) + expect do subject.validate_cached!(cookbook) - }.to raise_error(CookbookValidationFailure) + end.to raise_error(CookbookValidationFailure) end - it 'raises an error if the names do not match' do - constraint.stub(:satisfies?).with('0.1.0').and_return(true) - cookbook.stub(:cookbook_name).and_return('different_name') - expect { + it "raises an error if the names do not match" do + constraint.stub(:satisfies?).with("0.1.0").and_return(true) + cookbook.stub(:cookbook_name).and_return("different_name") + expect do subject.validate_cached!(cookbook) - }.to raise_error(MismatchedCookbookName) + end.to raise_error(MismatchedCookbookName) end - it 'returns true when the validation succeeds' do - constraint.stub(:satisfies?).with('0.1.0').and_return(true) + it "returns true when the validation succeeds" do + constraint.stub(:satisfies?).with("0.1.0").and_return(true) expect(subject.validate_cached!(cookbook)).to be true end end diff --git a/spec/unit/chef_server_spec.rb b/spec/unit/chef_server_spec.rb index b8e3a25..5507060 100644 --- a/spec/unit/chef_server_spec.rb +++ b/spec/unit/chef_server_spec.rb @@ -4,10 +4,10 @@ module CookbookOmnifetch METADATA = { "recipes" => [ - {"name"=>"default.rb", "path"=>"recipes/default.rb", "checksum"=>"a6be794cdd2eb44d38fdf17f792a0d0d", "specificity"=>"default", "url"=>"https://example.com/recipes/default.rb"}, + { "name" => "default.rb", "path" => "recipes/default.rb", "checksum" => "a6be794cdd2eb44d38fdf17f792a0d0d", "specificity" => "default", "url" => "https://example.com/recipes/default.rb" }, ], - "root_files"=>[ - {"name"=>"metadata.rb", "path"=>"metadata.rb", "checksum"=>"5b346119e5e41ab99500608decac8dca", "specificity"=>"default", "url"=>"https://example.com/metadata.rb"}, + "root_files" => [ + { "name" => "metadata.rb", "path" => "metadata.rb", "checksum" => "5b346119e5e41ab99500608decac8dca", "specificity" => "default", "url" => "https://example.com/metadata.rb" }, ], } @@ -41,9 +41,9 @@ module CookbookOmnifetch let(:cookbook_fixture_path) { fixtures_path.join("cookbooks/example_cookbook") } let(:remote_path) { File.join(test_root, "remote") } - let(:options) { {version: cookbook_version, http_client: http_client } } + let(:options) { { version: cookbook_version, http_client: http_client } } - let(:cookbook_files) { %w". .. metadata.rb recipes" } + let(:cookbook_files) { %w{. .. metadata.rb recipes} } subject(:chef_server_location) { described_class.new(dependency, options) } before do @@ -70,6 +70,6 @@ module CookbookOmnifetch expect(Dir).to exist(chef_server_location.install_path) expect(Dir.entries(chef_server_location.install_path)).to match_array(cookbook_files) - end + end end end diff --git a/spec/unit/exceptions_spec.rb b/spec/unit/exceptions_spec.rb index aa5853a..3e8e4e3 100644 --- a/spec/unit/exceptions_spec.rb +++ b/spec/unit/exceptions_spec.rb @@ -1,5 +1,5 @@ -require 'spec_helper' -require 'cookbook-omnifetch/exceptions' +require "spec_helper" +require "cookbook-omnifetch/exceptions" module CookbookOmnifetch describe "Exceptions" do diff --git a/spec/unit/git_spec.rb b/spec/unit/git_spec.rb index 7e4a820..a80c0fb 100644 --- a/spec/unit/git_spec.rb +++ b/spec/unit/git_spec.rb @@ -1,77 +1,77 @@ -require 'spec_helper' -require 'cookbook-omnifetch/git' +require "spec_helper" +require "cookbook-omnifetch/git" module CookbookOmnifetch describe GitLocation do - let(:dependency) { double(name: 'bacon') } + let(:dependency) { double(name: "bacon") } subject do - described_class.new(dependency, git: 'https://repo.com', branch: 'ham', - tag: 'v1.2.3', ref: 'abc123', revision: 'defjkl123456', rel: 'hi') + described_class.new(dependency, git: "https://repo.com", branch: "ham", + tag: "v1.2.3", ref: "abc123", revision: "defjkl123456", rel: "hi") end - describe '.initialize' do - it 'sets the uri' do - instance = described_class.new(dependency, git: 'https://repo.com') - expect(instance.uri).to eq('https://repo.com') + describe ".initialize" do + it "sets the uri" do + instance = described_class.new(dependency, git: "https://repo.com") + expect(instance.uri).to eq("https://repo.com") end - it 'sets the branch' do + it "sets the branch" do instance = described_class.new(dependency, - git: 'https://repo.com', branch: 'magic_new_feature') - expect(instance.branch).to eq('magic_new_feature') + git: "https://repo.com", branch: "magic_new_feature") + expect(instance.branch).to eq("magic_new_feature") end - it 'sets the tag' do + it "sets the tag" do instance = described_class.new(dependency, - git: 'https://repo.com', tag: 'v1.2.3') - expect(instance.tag).to eq('v1.2.3') + git: "https://repo.com", tag: "v1.2.3") + expect(instance.tag).to eq("v1.2.3") end - it 'adds the ref' do + it "adds the ref" do instance = described_class.new(dependency, - git: 'https://repo.com', ref: 'abc123') - expect(instance.ref).to eq('abc123') + git: "https://repo.com", ref: "abc123") + expect(instance.ref).to eq("abc123") end - it 'sets the revision' do + it "sets the revision" do instance = described_class.new(dependency, - git: 'https://repo.com', revision: 'abcde12345') - expect(instance.revision).to eq('abcde12345') + git: "https://repo.com", revision: "abcde12345") + expect(instance.revision).to eq("abcde12345") end - it 'sets the rel' do + it "sets the rel" do instance = described_class.new(dependency, - git: 'https://repo.com', rel: 'internal/path') - expect(instance.rel).to eq('internal/path') + git: "https://repo.com", rel: "internal/path") + expect(instance.rel).to eq("internal/path") end - context 'rev_parse' do + context "rev_parse" do def rev_parse(instance) instance.instance_variable_get(:@rev_parse) end - it 'uses the :ref option with priority' do + it "uses the :ref option with priority" do instance = described_class.new(dependency, - git: 'https://repo.com', ref: 'abc123', branch: 'magic_new_feature') - expect(rev_parse(instance)).to eq('abc123') + git: "https://repo.com", ref: "abc123", branch: "magic_new_feature") + expect(rev_parse(instance)).to eq("abc123") end - it 'uses the :branch option with priority' do + it "uses the :branch option with priority" do instance = described_class.new(dependency, - git: 'https://repo.com', branch: 'magic_new_feature', tag: 'v1.2.3') - expect(rev_parse(instance)).to eq('magic_new_feature') + git: "https://repo.com", branch: "magic_new_feature", tag: "v1.2.3") + expect(rev_parse(instance)).to eq("magic_new_feature") end - it 'uses the :tag option' do + it "uses the :tag option" do instance = described_class.new(dependency, - git: 'https://repo.com', tag: 'v1.2.3') - expect(rev_parse(instance)).to eq('v1.2.3') + git: "https://repo.com", tag: "v1.2.3") + expect(rev_parse(instance)).to eq("v1.2.3") end it 'uses "master" when none is given' do - instance = described_class.new(dependency, git: 'https://repo.com') - expect(rev_parse(instance)).to eq('master') + instance = described_class.new(dependency, git: "https://repo.com") + expect(rev_parse(instance)).to eq("master") end end end @@ -84,26 +84,26 @@ def rev_parse(instance) end end - describe '#installed?' do - it 'returns false when there is no revision' do + describe "#installed?" do + it "returns false when there is no revision" do subject.stub(:revision).and_return(nil) expect(subject.installed?).to be false end - it 'returns false when the install_path does not exist' do - subject.stub(:revision).and_return('abcd1234') + it "returns false when the install_path does not exist" do + subject.stub(:revision).and_return("abcd1234") subject.stub(:install_path).and_return(double(exist?: false)) expect(subject.installed?).to be false end - it 'returns true when the location is installed' do - subject.stub(:revision).and_return('abcd1234') + it "returns true when the location is installed" do + subject.stub(:revision).and_return("abcd1234") subject.stub(:install_path).and_return(double(exist?: true)) expect(subject.installed?).to be true end end - describe '#install' do + describe "#install" do before do File.stub(:chmod) FileUtils.stub(:cp_r) @@ -111,8 +111,8 @@ def rev_parse(instance) subject.stub(:git) end - context 'when the repository is cached' do - it 'pulls a new version' do + context "when the repository is cached" do + it "pulls a new version" do Dir.stub(:chdir) { |args, &b| b.call } # Force eval the chdir block subject.stub(:cached?).and_return(true) @@ -123,27 +123,27 @@ def rev_parse(instance) end end - context 'when the revision is not cached' do - it 'clones the repository' do + context "when the revision is not cached" do + it "clones the repository" do Dir.stub(:chdir) { |args, &b| b.call } # Force eval the chdir block cache_path = subject.send(:cache_path) subject.stub(:cached?).and_return(false) expect(subject).to receive(:git).with( - %|clone https://repo.com "#{cache_path}" --bare --no-hardlinks| + %{clone https://repo.com "#{cache_path}" --bare --no-hardlinks} ) subject.install end end end - describe '#cached_cookbook' do - it 'returns nil if the cookbook is not installed' do + describe "#cached_cookbook" do + it "returns nil if the cookbook is not installed" do subject.stub(:installed?).and_return(false) expect(subject.cached_cookbook).to be_nil end - it 'returns the cookbook at the install_path' do + it "returns the cookbook at the install_path" do subject.stub(:installed?).and_return(true) MockCachedCookbook.stub(:from_path) @@ -152,69 +152,69 @@ def rev_parse(instance) end end - describe '#==' do + describe "#==" do let(:other) { subject.dup } - it 'returns true when everything matches' do + it "returns true when everything matches" do expect(subject).to eq(other) end - it 'returns false when the other location is not an GitLocation' do + it "returns false when the other location is not an GitLocation" do other.stub(:is_a?).and_return(false) expect(subject).to_not eq(other) end - it 'returns false when the uri is different' do - other.stub(:uri).and_return('different') + it "returns false when the uri is different" do + other.stub(:uri).and_return("different") expect(subject).to_not eq(other) end - it 'returns false when the branch is different' do - other.stub(:branch).and_return('different') + it "returns false when the branch is different" do + other.stub(:branch).and_return("different") expect(subject).to_not eq(other) end - it 'returns false when the tag is different' do - other.stub(:tag).and_return('different') + it "returns false when the tag is different" do + other.stub(:tag).and_return("different") expect(subject).to_not eq(other) end - it 'returns false when the ref is different' do - other.stub(:ref).and_return('different') + it "returns false when the ref is different" do + other.stub(:ref).and_return("different") expect(subject).to_not eq(other) end - it 'returns false when the rel is different' do - other.stub(:rel).and_return('different') + it "returns false when the rel is different" do + other.stub(:rel).and_return("different") expect(subject).to_not eq(other) end end - describe '#to_s' do - it 'prefers the tag' do - expect(subject.to_s).to eq('https://repo.com (at v1.2.3/hi)') + describe "#to_s" do + it "prefers the tag" do + expect(subject.to_s).to eq("https://repo.com (at v1.2.3/hi)") end - it 'prefers the branch' do + it "prefers the branch" do subject.stub(:tag).and_return(nil) - expect(subject.to_s).to eq('https://repo.com (at ham/hi)') + expect(subject.to_s).to eq("https://repo.com (at ham/hi)") end - it 'falls back to the ref' do + it "falls back to the ref" do subject.stub(:tag).and_return(nil) subject.stub(:branch).and_return(nil) - expect(subject.to_s).to eq('https://repo.com (at abc123/hi)') + expect(subject.to_s).to eq("https://repo.com (at abc123/hi)") end - it 'does not use the rel if missing' do + it "does not use the rel if missing" do subject.stub(:rel).and_return(nil) - expect(subject.to_s).to eq('https://repo.com (at v1.2.3)') + expect(subject.to_s).to eq("https://repo.com (at v1.2.3)") end end - describe '#to_lock' do - it 'includes all the information' do - expect(subject.to_lock).to eq <<-EOH.gsub(/^ {8}/, '') + describe "#to_lock" do + it "includes all the information" do + expect(subject.to_lock).to eq <<-EOH.gsub(/^ {8}/, "") git: https://repo.com revision: defjkl123456 ref: abc123 @@ -224,23 +224,23 @@ def rev_parse(instance) EOH end - it 'does not include the branch if missing' do + it "does not include the branch if missing" do subject.stub(:branch).and_return(nil) - expect(subject.to_lock).to_not include('branch') + expect(subject.to_lock).to_not include("branch") end - it 'does not include the tag if missing' do + it "does not include the tag if missing" do subject.stub(:tag).and_return(nil) - expect(subject.to_lock).to_not include('tag') + expect(subject.to_lock).to_not include("tag") end - it 'does not include the rel if missing' do + it "does not include the rel if missing" do subject.stub(:rel).and_return(nil) - expect(subject.to_lock).to_not include('rel') + expect(subject.to_lock).to_not include("rel") end end - describe '#lock_data' do + describe "#lock_data" do let(:full_lock_data) do { "git" => "https://repo.com", @@ -248,43 +248,42 @@ def rev_parse(instance) "ref" => "abc123", "branch" => "ham", "tag" => "v1.2.3", - "rel" => "hi" + "rel" => "hi", } end - it 'includes all the information' do + it "includes all the information" do expect(subject.lock_data).to eq(full_lock_data) end - it 'does not include the branch if missing' do + it "does not include the branch if missing" do subject.stub(:branch).and_return(nil) - expect(subject.lock_data).to_not have_key('branch') + expect(subject.lock_data).to_not have_key("branch") end - it 'does not include the tag if missing' do + it "does not include the tag if missing" do subject.stub(:tag).and_return(nil) - expect(subject.lock_data).to_not have_key('tag') + expect(subject.lock_data).to_not have_key("tag") end - it 'does not include the rel if missing' do + it "does not include the rel if missing" do subject.stub(:rel).and_return(nil) - expect(subject.lock_data).to_not have_key('rel') + expect(subject.lock_data).to_not have_key("rel") end end - - describe '#git' do + describe "#git" do before { described_class.send(:public, :git) } - it 'raises an error if Git is not installed' do + it "raises an error if Git is not installed" do CookbookOmnifetch.stub(:which).and_return(false) - expect { subject.git('foo') }.to raise_error(GitNotInstalled) + expect { subject.git("foo") }.to raise_error(GitNotInstalled) end - it 'raises an error if the command fails' do - shell_out = double('shell_out', success?: false, stderr: nil) + it "raises an error if the command fails" do + shell_out = double("shell_out", success?: false, stderr: nil) MockShellOut.stub(:shell_out).and_return(shell_out) - expect { subject.git('foo') }.to raise_error(GitCommandError) + expect { subject.git("foo") }.to raise_error(GitCommandError) end end end diff --git a/spec/unit/path_spec.rb b/spec/unit/path_spec.rb index 6b9cf18..efe752b 100644 --- a/spec/unit/path_spec.rb +++ b/spec/unit/path_spec.rb @@ -1,116 +1,116 @@ -require 'spec_helper' -require 'cookbook-omnifetch/path' +require "spec_helper" +require "cookbook-omnifetch/path" module CookbookOmnifetch describe PathLocation do let(:relative_paths_root) { File.dirname(__FILE__) } - let(:constraint) { double('constraint', satisfies?: true) } + let(:constraint) { double("constraint", satisfies?: true) } let(:dependency) do - double('dependency', - name: 'nginx', + double("dependency", + name: "nginx", version_constraint: constraint, relative_paths_root: relative_paths_root ) end - let(:path) { fixtures_path.join('cookbooks', 'example_cookbook') } - let(:relative_path) { Pathname.new('../fixtures/cookbooks/example_cookbook') } + let(:path) { fixtures_path.join("cookbooks", "example_cookbook") } + let(:relative_path) { Pathname.new("../fixtures/cookbooks/example_cookbook") } subject { described_class.new(dependency, path: path) } - describe '#installed?' do - it 'returns false' do + describe "#installed?" do + it "returns false" do expect(subject.installed?).to be false end end - describe '#install' do - it 'validates the cached cookbook' do + describe "#install" do + it "validates the cached cookbook" do expect(subject).to receive(:validate_cached!).with(path) subject.install end end - describe '#cached_cookbook' do - it 'loads the cached cookbook at the path' do + describe "#cached_cookbook" do + it "loads the cached cookbook at the path" do expect(MockCachedCookbook).to receive(:from_path).with(path) subject.cached_cookbook end end - describe '#relative_path' do - it 'returns the path to the Berksfile' do + describe "#relative_path" do + it "returns the path to the Berksfile" do expect(subject.relative_path).to eq(relative_path) end end - describe '#expanded_path' do - it 'returns the expanded path, relative to the Berksfile' do + describe "#expanded_path" do + it "returns the expanded path, relative to the Berksfile" do absolute_path = Pathname.new(File.expand_path(relative_path, relative_paths_root)) expect(subject.expanded_path).to eq(absolute_path) end end - describe '#==' do - it 'is false when compared with a non-PathLocation' do - this = PathLocation.new(dependency, path: '.') - that = 'A string' + describe "#==" do + it "is false when compared with a non-PathLocation" do + this = PathLocation.new(dependency, path: ".") + that = "A string" expect(this).to_not eq(that) end - it 'is false when the metadata? is not the same' do - this = PathLocation.new(dependency, path: '.') - that = PathLocation.new(dependency, path: '.', metadata: true) + it "is false when the metadata? is not the same" do + this = PathLocation.new(dependency, path: ".") + that = PathLocation.new(dependency, path: ".", metadata: true) expect(this).to_not eq(that) end - it 'is false when the expanded paths are different' do - this = PathLocation.new(dependency, path: '.') - that = PathLocation.new(dependency, path: '..') + it "is false when the expanded paths are different" do + this = PathLocation.new(dependency, path: ".") + that = PathLocation.new(dependency, path: "..") expect(this).to_not eq(that) end - it 'is true when they are the same' do - this = PathLocation.new(dependency, path: '.', metadata: true) - that = PathLocation.new(dependency, path: '.', metadata: true) + it "is true when they are the same" do + this = PathLocation.new(dependency, path: ".", metadata: true) + that = PathLocation.new(dependency, path: ".", metadata: true) expect(this).to eq(that) end end - describe '#to_lock' do - it 'includes the path relative to the Berksfile' do - expect(subject.to_lock).to eq <<-EOH.gsub(/^ {10}/, '') + describe "#to_lock" do + it "includes the path relative to the Berksfile" do + expect(subject.to_lock).to eq <<-EOH.gsub(/^ {10}/, "") path: #{relative_path} EOH end - it 'includes the metadata attribute' do + it "includes the metadata attribute" do subject.stub(:metadata?).and_return(true) - expect(subject.to_lock).to eq <<-EOH.gsub(/^ {10}/, '') + expect(subject.to_lock).to eq <<-EOH.gsub(/^ {10}/, "") path: #{relative_path} metadata: true EOH end end - describe '#lock_data' do - it 'includes the path relative to the Berksfile' do + describe "#lock_data" do + it "includes the path relative to the Berksfile" do expect(subject.lock_data).to eq({ "path" => relative_path.to_s }) end - it 'includes the metadata attribute' do + it "includes the metadata attribute" do subject.stub(:metadata?).and_return(true) expect(subject.lock_data).to eq({ "path" => relative_path.to_s, "metadata" => true }) end end - describe '#to_s' do - it 'uses the relative path' do + describe "#to_s" do + it "uses the relative path" do expect(subject.to_s).to eq("source at #{relative_path}") end end - describe '#inspect' do - it 'includes the right information' do + describe "#inspect" do + it "includes the right information" do subject.stub(:metadata?).and_return(true) expect(subject.inspect).to eq("#") end