<commit_before>require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')

describe "Copy command" do
  
  before(:all) do
    @kvs = storage_connection
  end
  
  context "copying local file to bucket" do
    
    before(:all) { purge_bucket('my_bucket') }
    
    context "when local file does not exist" do
      it "should exit with file not found" do
        response = capture(:stderr){ HPCloud::CLI.start(['copy', 'foo.txt', ':my_bucket']) }
        response.should eql("File not found at 'foo.txt'.\n")
      end
    end
    
    context "when bucket does not exist" do
      it "should exit with bucket not found" do
        response = capture(:stderr){ HPCloud::CLI.start(['copy', 'spec/fixtures/files/foo.txt', ':my_bucket']) }
        response.should eql("You don't have a bucket 'my_bucket'.\n")
      end
    end
    
    context "when file and bucket exist" do
      before(:all) do
        @kvs.put_bucket('my_bucket')
        @response = capture(:stdout){ HPCloud::CLI.start(['copy', 'spec/fixtures/files/foo.txt', ':my_bucket']) }
        @get = @kvs.get_object('my_bucket', 'foo.txt')
      end
      
      it "should report success" do
        @response.should eql("Copied spec/fixtures/files/foo.txt => :my_bucket/foo.txt\n")
      end
      
      it "should copy file to bucket" do
        @get.status.should eql(200)
      end
      
      it "should preserve content-type" do
        @get.headers["content-type"].should eql('text/plain')
      end
      
      after(:all) { purge_bucket('my_bucket') }
    end
    
  end
  
  context "copying remote object to local filesystem" do
    
    before(:all) { create_bucket_with_files('copy_remote_to_local', 'foo.txt') }
    
    context "when bucket does not exist" do
      it "should exit with bucket not found" do
        response = capture(:stderr){ HPCloud::CLI.start(['copy', ':copy_blah/foo.txt', '/tmp/foo.txt']) }
        response.should eql("You don't have a bucket 'copy_blah'.\n")
      end
    end
    
    context "when object does not exist" do
      it "should exit with object not found" do
        response = capture(:stderr){ HPCloud::CLI.start(['copy', ':copy_remote_to_local/foo2.txt', '/tmp/foo.txt']) }
        response.should eql("The specified object does not exist.\n")
      end 
    end
    
    context "when local directory structure does not exist" do
      it "should exit with directory not present" do
        response = capture(:stderr){ HPCloud::CLI.start(['copy', ':copy_remote_to_local/foo.txt', '/blah/foo.txt']) }
        response.should eql("No directory exists at '/blah'.\n")
      end
    end
    
    context "when local directory and object exist" do
      before(:all) do
        @response = capture(:stdout){ HPCloud::CLI.start(['copy', ':copy_remote_to_local/foo.txt', 'spec/tmp/foo.txt']) }
      end
      
      it "should describe copy" do
        @response.should eql("Copied :copy_remote_to_local/foo.txt => spec/tmp/foo.txt\n")
      end
      
      it "should create local file" do
        should satisfy { File.exists?('spec/tmp/foo.txt') }
      end
      
      it "should have same body as object" do
        get = @kvs.get_object('copy_remote_to_local', 'foo.txt')
        File.read('spec/tmp/foo.txt').should eql(get.body)
      end
    end
    
    pending 'when cannot write file'
    
    after(:all) { purge_bucket('copy_remote_to_local') }
    
  end
  
  context "copying remote object within a bucket" do
    
    before(:all) do
      #create_bucket_with_files('copy_inside_bucket', 'foo.txt')
      @kvs.put_bucket('copy_inside_bucket')
      @kvs.put_object('copy_inside_bucket', 'foo.txt', read_file('foo.txt'), {'Content-Type' => 'text/plain'})
    end
    
    context "when bucket does not exist" do
      it "should exit with bucket not found" do
        response = capture(:stderr){ HPCloud::CLI.start(['copy', ':missing_bucket/foo.txt', ':missing_bucket/tmp/foo.txt']) }
        response.should eql("You don't have a bucket 'missing_bucket'.\n")
      end
    end
    
    context "when object does not exist" do<commit_msg>Make sure to clean up local filesystem after copy test.<commit_after>require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')

describe "Copy command" do
  
  before(:all) do
    @kvs = storage_connection
  end
  
  context "copying local file to bucket" do
    
    before(:all) { purge_bucket('my_bucket') }
    
    context "when local file does not exist" do
      it "should exit with file not found" do
        response = capture(:stderr){ HPCloud::CLI.start(['copy', 'foo.txt', ':my_bucket']) }
        response.should eql("File not found at 'foo.txt'.\n")
      end
    end
    
    context "when bucket does not exist" do
      it "should exit with bucket not found" do
        response = capture(:stderr){ HPCloud::CLI.start(['copy', 'spec/fixtures/files/foo.txt', ':my_bucket']) }
        response.should eql("You don't have a bucket 'my_bucket'.\n")
      end
    end
    
    context "when file and bucket exist" do
      before(:all) do
        @kvs.put_bucket('my_bucket')
        @response = capture(:stdout){ HPCloud::CLI.start(['copy', 'spec/fixtures/files/foo.txt', ':my_bucket']) }
        @get = @kvs.get_object('my_bucket', 'foo.txt')
      end
      
      it "should report success" do
        @response.should eql("Copied spec/fixtures/files/foo.txt => :my_bucket/foo.txt\n")
      end
      
      it "should copy file to bucket" do
        @get.status.should eql(200)
      end
      
      it "should preserve content-type" do
        @get.headers["content-type"].should eql('text/plain')
      end
      
      after(:all) { purge_bucket('my_bucket') }
    end
    
  end
  
  context "copying remote object to local filesystem" do
    
    before(:all) { create_bucket_with_files('copy_remote_to_local', 'foo.txt') }
    
    context "when bucket does not exist" do
      it "should exit with bucket not found" do
        response = capture(:stderr){ HPCloud::CLI.start(['copy', ':copy_blah/foo.txt', '/tmp/foo.txt']) }
        response.should eql("You don't have a bucket 'copy_blah'.\n")
      end
    end
    
    context "when object does not exist" do
      it "should exit with object not found" do
        response = capture(:stderr){ HPCloud::CLI.start(['copy', ':copy_remote_to_local/foo2.txt', '/tmp/foo.txt']) }
        response.should eql("The specified object does not exist.\n")
      end 
    end
    
    context "when local directory structure does not exist" do
      it "should exit with directory not present" do
        response = capture(:stderr){ HPCloud::CLI.start(['copy', ':copy_remote_to_local/foo.txt', '/blah/foo.txt']) }
        response.should eql("No directory exists at '/blah'.\n")
      end
    end
    
    context "when local directory and object exist" do
      before(:all) do
        @response = capture(:stdout){ HPCloud::CLI.start(['copy', ':copy_remote_to_local/foo.txt', 'spec/tmp/foo.txt']) }
      end
      
      it "should describe copy" do
        @response.should eql("Copied :copy_remote_to_local/foo.txt => spec/tmp/foo.txt\n")
      end
      
      it "should create local file" do
        File.exists?('spec/tmp/foo.txt').should be_true
      end
      
      it "should have same body as object" do
        get = @kvs.get_object('copy_remote_to_local', 'foo.txt')
        File.read('spec/tmp/foo.txt').should eql(get.body)
      end
    end
    
    pending 'when cannot write file'
    
    after(:all) do
      purge_bucket('copy_remote_to_local')
      File.unlink('spec/tmp/foo.txt')
    end
    
  end
  
  context "copying remote object within a bucket" do
    
    before(:all) do
      #create_bucket_with_files('copy_inside_bucket', 'foo.txt')
      @kvs.put_bucket('copy_inside_bucket')
      @kvs.put_object('copy_inside_bucket', 'foo.txt', read_file('foo.txt'), {'Content-Type' => 'text/plain'})
    end
    
    context "when bucket does not exist" do
      it "should exit with bucket not found" do
        response = capture(:stderr){ HPCloud::CLI.start(['copy', ':missing_bucket/foo.txt', ':missing_bucket/tmp/foo.txt']) }
        response.should eql("You don't have a bucket 'missing_bucket'.\n")
      end
    end
    
    context "when object does not exist" do
{'fraction_non_alphanumeric': 0.13099697885196374, 'fraction_numerical': 0.0009667673716012085, 'mean_word_length': 2.6458149779735685, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2218676', 'n_tokens_mistral': 2620, 'n_tokens_neox': 2573, 'n_words': 698}