diff --git a/spec/test_helpers.rb b/spec/test_helpers.rb index c7f803d..1e9d8bc 100644 --- a/spec/test_helpers.rb +++ b/spec/test_helpers.rb @@ -11,19 +11,36 @@ def fixtures_path File.expand_path(File.dirname(__FILE__) + "/unit/fixtures/") end + def cookbook_path + File.expand_path('cookbooks', tempdir) + end + + def environment_path + File.expand_path('environments', tempdir) + end + def stdout stdout_io.string end def set_chef_config knife.config[:chef_repo_path] = tempdir - knife.config[:cookbook_path] = File.join(fixtures_path, 'cookbooks') - knife.config[:environment_path] = File.join(fixtures_path, 'environments') + knife.config[:cookbook_path] = File.join(tempdir, 'cookbooks') + knife.config[:environment_path] = File.join(tempdir, 'environments') knife.config[:chef_server_url] = "http://localhost:4000" - knife.config[:client_key] = File.join(fixtures_path, 'test_client.pem') + knife.config[:client_key] = File.join(tempdir, 'test_client.pem') knife.config[:client_name] = "test-client" knife.config[:node_name] = "test-node" knife.config[:cache_type] = 'BasicFile' - knife.config[:cache_options] = {:path => File.join(fixtures_path, 'checksums')} + knife.config[:cache_options] = {:path => File.join(tempdir, 'checksums')} + end + + + def copy_test_data + FileUtils.cp_r "#{fixtures_path}/.", tempdir + end + + def cleanup_test_data + FileUtils.rm_r Dir.glob("#{tempdir}/*") end end \ No newline at end of file diff --git a/spec/unit/fixtures/environments/example.json b/spec/unit/fixtures/environments/example.json index db83c31..231b22d 100644 --- a/spec/unit/fixtures/environments/example.json +++ b/spec/unit/fixtures/environments/example.json @@ -2,7 +2,6 @@ "name": "example", "description": "This is an example environment", "cookbook_versions": { - "example": ">= 1.0.0" }, "json_class": "Chef::Environment", "chef_type": "environment", diff --git a/spec/unit/spork_bump_spec.rb b/spec/unit/spork_bump_spec.rb index c9fd93d..0f9dfff 100644 --- a/spec/unit/spork_bump_spec.rb +++ b/spec/unit/spork_bump_spec.rb @@ -18,15 +18,13 @@ require 'spec_helper' require 'chef/knife/spork-bump' require 'knife-spork/runner' +require 'fileutils' module KnifeSpork describe SporkBump do let(:stdout_io) { StringIO.new } let(:stderr_io) { StringIO.new } - let(:default_cookbook_path) do - File.expand_path('cookbooks', fixtures_path) - end subject(:knife) do SporkBump.new(argv).tap do |c| @@ -34,8 +32,18 @@ module KnifeSpork end end + before(:all) do + copy_test_data + end + + after(:all) do + cleanup_test_data + end + let(:argv) { ["example"] } + let(:metadata_file) { "#{cookbook_path}/example/metadata.rb" } + describe '#run' do before(:each) { set_chef_config } it 'calls bump method' do @@ -43,5 +51,42 @@ module KnifeSpork knife.run end end + + describe '#bump' do + before(:each) { set_chef_config } + it 'automatically bumps patch level' do + knife.instance_variable_set(:@cookbook, knife.load_cookbook(argv.first)) + knife.send(:bump) + File.read(metadata_file).should include "version \"0.0.2\"" + end + + it 'manually bumps patch version level' do + knife.instance_variable_set(:@cookbook, knife.load_cookbook(argv.first)) + knife.instance_variable_set(:@name_args, ["example","patch"]) + knife.send(:bump) + File.read(metadata_file).should include "version \"0.0.3\"" + end + + it 'manually bumps minor version level' do + knife.instance_variable_set(:@cookbook, knife.load_cookbook(argv.first)) + knife.instance_variable_set(:@name_args, ["example","minor"]) + knife.send(:bump) + File.read(metadata_file).should include "version \"0.1.0\"" + end + + it 'manually bumps major version level' do + knife.instance_variable_set(:@cookbook, knife.load_cookbook(argv.first)) + knife.instance_variable_set(:@name_args, ["example","major"]) + knife.send(:bump) + File.read(metadata_file).should include "version \"1.0.0\"" + end + + it 'manually sets version to 0.0.1' do + knife.instance_variable_set(:@cookbook, knife.load_cookbook(argv.first)) + knife.instance_variable_set(:@name_args, ["example", "manual", "0.0.1"]) + knife.send(:bump) + File.read(metadata_file).should include "version \"0.0.1\"" + end + end end end \ No newline at end of file diff --git a/spec/unit/spork_promote_spec.rb b/spec/unit/spork_promote_spec.rb index e7a8590..bb98dc9 100644 --- a/spec/unit/spork_promote_spec.rb +++ b/spec/unit/spork_promote_spec.rb @@ -24,8 +24,13 @@ module KnifeSpork let(:stdout_io) { StringIO.new } let(:stderr_io) { StringIO.new } - let(:default_cookbook_path) do - File.expand_path('cookbooks', fixtures_path) + + before(:all) do + copy_test_data + end + + after(:all) do + cleanup_test_data end subject(:knife) do @@ -34,6 +39,8 @@ module KnifeSpork end end + let(:environment_file) { "#{environment_path}/example.json" } + let(:argv) { ["example", "example"] } describe '#run' do @@ -42,6 +49,46 @@ module KnifeSpork expect(knife).to receive(:promote) knife.run end + + it 'calls save_environment_changes method by default' do + expect(knife).to receive(:save_environment_changes) + knife.run + end + + it 'calls save_environment_changes_remote method when --remote is specified' do + knife.config[:remote] = true + expect(knife).to receive(:save_environment_changes_remote) + knife.run + end + end + + describe '#promote' do + before(:each) { set_chef_config } + it 'updates version constraint for cookbook' do + knife.instance_variable_set(:@environments, ["example"]) + knife.instance_variable_set(:@cookbook, "example") + expect(knife).to receive(:update_version_constraints) + knife.send(:promote, knife.load_environment_from_file("example"), "example") + end + end + + describe '#save_environment_changes' do + before(:each) { set_chef_config } + it 'updates the constraint in the environment file' do + knife.instance_variable_set(:@environments, ["example"]) + knife.instance_variable_set(:@cookbook, "example") + knife.send(:save_environment_changes, "example", knife.pretty_print_json(knife.load_environment_from_file("example").to_hash)) + File.read(environment_file).should include "\"example\": \"= 0.0.1\"" + end + end + + describe '#save_environment_changes_remote' do + before(:each) { set_chef_config } + it 'saves the environment change to the server' do + knife.instance_variable_set(:@environments, ["example"]) + knife.instance_variable_set(:@cookbook, "example") + knife.send(:save_environment_changes_remote, "example") + end end end end \ No newline at end of file diff --git a/spec/unit/spork_upload_spec.rb b/spec/unit/spork_upload_spec.rb index b299d49..5e17b4d 100644 --- a/spec/unit/spork_upload_spec.rb +++ b/spec/unit/spork_upload_spec.rb @@ -25,8 +25,13 @@ module KnifeSpork let(:stdout_io) { StringIO.new } let(:stderr_io) { StringIO.new } - let(:default_cookbook_path) do - File.expand_path('cookbooks', fixtures_path) + + before(:all) do + copy_test_data + end + + after(:all) do + cleanup_test_data end subject(:knife) do