<commit_before>#
# Author:: <NAME> (<<EMAIL>>)
# Copyright:: Copyright (c) 2010 Opscode, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'spec_helper'

require 'chef/cookbook_uploader'
require 'chef/cookbook_site_streaming_uploader'

describe Chef::Knife::CookbookSiteShare do

  before(:each) do
    @knife = Chef::Knife::CookbookSiteShare.new
    @knife.name_args = ['cookbook_name', 'AwesomeSausage']

    @cookbook = Chef::CookbookVersion.new('cookbook_name')

    @cookbook_loader = double('Chef::CookbookLoader')
    @cookbook_loader.stub(:cookbook_exists?).and_return(true)
    @cookbook_loader.stub(:[]).and_return(@cookbook)
    Chef::CookbookLoader.stub(:new).and_return(@cookbook_loader)

    @cookbook_uploader = Chef::CookbookUploader.new('herpderp', File.join(CHEF_SPEC_DATA, 'cookbooks'), :rest => "norest")
    Chef::CookbookUploader.stub(:new).and_return(@cookbook_uploader)
    @cookbook_uploader.stub(:validate_cookbooks).and_return(true)
    Chef::CookbookSiteStreamingUploader.stub(:create_build_dir).and_return(Dir.mktmpdir)

    Chef::Mixin::ShellOut.stub(:shell_out!).and_return(true)
    @stdout = StringIO.new
    @knife.ui.stub(:stdout).and_return(@stdout)
  end

  describe 'run' do

    before(:each) do
      @knife.stub(:do_upload).and_return(true)
    end

    it 'should should print usage and exit when given no arguments' do
      @knife.name_args = []
      @knife.should_receive(:show_usage)
      @knife.ui.should_receive(:fatal)
      lambda { @knife.run }.should raise_error(SystemExit)
    end

    it 'should print usage and exit when given only 1 argument' do
      @knife.name_args = ['cookbook_name']
      @knife.should_receive(:show_usage)
      @knife.ui.should_receive(:fatal)
      lambda { @knife.run }.should raise_error(SystemExit)
    end

    it 'should check if the cookbook exists' do
      @cookbook_loader.should_receive(:cookbook_exists?)
      @knife.run
    end

    it "should exit and log to error if the cookbook doesn't exist" do
      @cookbook_loader.stub(:cookbook_exists?).and_return(false)
      @knife.ui.should_receive(:error)
      lambda { @knife.run }.should raise_error(SystemExit)
    end

    it 'should make a tarball of the cookbook' do
      Chef::Mixin::ShellOut.should_receive(:shell_out!) do |args|
        args.should match /tar -czf/
      end
      @knife.run
    end

    it 'should exit and log to error when the tarball creation fails' do
      Chef::Mixin::ShellOut.stub(:shell_out!).and_raise(Chef::Exceptions::Exec)
      @knife.ui.should_receive(:error)
      lambda { @knife.run }.should raise_error(SystemExit)
    end

    it 'should upload the cookbook and clean up the tarball' do<commit_msg>Hack around system differences for travis test<commit_after>#
# Author:: <NAME> (<<EMAIL>>)
# Copyright:: Copyright (c) 2010 Opscode, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'spec_helper'

require 'chef/cookbook_uploader'
require 'chef/cookbook_site_streaming_uploader'

describe Chef::Knife::CookbookSiteShare do

  before(:each) do
    @knife = Chef::Knife::CookbookSiteShare.new
    @knife.name_args = ['cookbook_name', 'AwesomeSausage']

    @cookbook = Chef::CookbookVersion.new('cookbook_name')

    @cookbook_loader = double('Chef::CookbookLoader')
    @cookbook_loader.stub(:cookbook_exists?).and_return(true)
    @cookbook_loader.stub(:[]).and_return(@cookbook)
    Chef::CookbookLoader.stub(:new).and_return(@cookbook_loader)

    @cookbook_uploader = Chef::CookbookUploader.new('herpderp', File.join(CHEF_SPEC_DATA, 'cookbooks'), :rest => "norest")
    Chef::CookbookUploader.stub(:new).and_return(@cookbook_uploader)
    @cookbook_uploader.stub(:validate_cookbooks).and_return(true)
    Chef::CookbookSiteStreamingUploader.stub(:create_build_dir).and_return(Dir.mktmpdir)

    Chef::Mixin::ShellOut.stub(:shell_out!).and_return(true)
    @stdout = StringIO.new
    @knife.ui.stub(:stdout).and_return(@stdout)
  end

  describe 'run' do

    before(:each) do
      @knife.stub(:do_upload).and_return(true)
    end

    it 'should should print usage and exit when given no arguments' do
      @knife.name_args = []
      @knife.should_receive(:show_usage)
      @knife.ui.should_receive(:fatal)
      lambda { @knife.run }.should raise_error(SystemExit)
    end

    it 'should print usage and exit when given only 1 argument' do
      @knife.name_args = ['cookbook_name']
      @knife.should_receive(:show_usage)
      @knife.ui.should_receive(:fatal)
      lambda { @knife.run }.should raise_error(SystemExit)
    end

    it 'should check if the cookbook exists' do
      @cookbook_loader.should_receive(:cookbook_exists?)
      @knife.run
    end

    it "should exit and log to error if the cookbook doesn't exist" do
      @cookbook_loader.stub(:cookbook_exists?).and_return(false)
      @knife.ui.should_receive(:error)
      lambda { @knife.run }.should raise_error(SystemExit)
    end

    it 'should make a tarball of the cookbook' do
      Chef::Mixin::ShellOut.should_receive(:shell_out!) do |args|
        args.to_s.should match /tar -czf/
      end
      @knife.run
    end

    it 'should exit and log to error when the tarball creation fails' do
      Chef::Mixin::ShellOut.stub(:shell_out!).and_raise(Chef::Exceptions::Exec)
      @knife.ui.should_receive(:error)
      lambda { @knife.run }.should raise_error(SystemExit)
    end

    it 'should upload the cookbook and clean up the tarball' do
{'fraction_non_alphanumeric': 0.11347626339969372, 'fraction_numerical': 0.0033690658499234303, 'mean_word_length': 4.126373626373627, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6636195', 'n_tokens_mistral': 2173, 'n_tokens_neox': 2086, 'n_words': 523}