<commit_before>require 'spec_helper'

describe Dockly::BashBuilder do
  describe "#normalize_for_dockly" do
    it "sets up log and fatal and makes /opt/dockly" do
      output = subject.normalize_for_dockly
      expect(output).to include("function log")
      expect(output).to include("function fatal")
      expect(output).to include("mkdir -p /opt/dockly")
    end
  end

  describe "#get_from_s3" do
    let(:s3_url) { "s3://url-for-s3/file.tar.gz" }
    context "uses the default output" do
      it "polls from s3 and sets the s3_path" do
        output = subject.get_from_s3(s3_url)
        expect(output).to include("s3cmd -f get $s3_path $output_path")
        expect(output).to include("s3_path=\"#{s3_url}\"")
        expect(output).to include("output_path=\"-\"")
      end
    end
    context "uses a specific output directory" do
      let(:output_dir) { "test" }
      it "polls from s3 and sets the s3_path and output_path" do
        output = subject.get_from_s3(s3_url, output_dir)
        expect(output).to include("s3cmd -f get $s3_path $output_path")
        expect(output).to include("s3_path=\"#{s3_url}\"")
        expect(output).to include("output_path=\"#{output_dir}\"")
      end
    end
  end

  describe "#install_package" do
    let(:path) { "/opt/dockly/deb.deb" }
    it "installs from the given path" do
      output = subject.install_package(path)
      expect(output.strip).to eq("dpkg -i \"#{path}\"")
    end
  end

  describe "#get_and_install_deb" do
    let(:s3_url) { "s3://bucket/path-to-deb.deb" }
    let(:deb_path) { "/opt/dockly/deb_path.deb" }
    it "gets from s3 and installs the package" do
      expect(subject).to receive(:get_from_s3).with(s3_url, deb_path)
      expect(subject).to receive(:install_package).with(deb_path)
      subject.get_and_install_deb(s3_url, deb_path)
    end
  end

  describe "#docker_import" do
    context "when not given a repo" do
      it "imports with no tagging" do
        output = subject.docker_import
        expect(output).to include("docker import -")
      end
    end

    context "when given a repo" do
      let(:repo) { "aRepo" }
      it "imports with repo and latest" do
        output = subject.docker_import(repo)
        expect(output).to include("docker import - #{repo}:latest")
      end

      context "and a non-default tag" do
        let(:tag) { "aTag" }
        it "imports with repo and the tag" do
          output = subject.docker_import(repo, tag)
          expect(output).to include("docker import - #{repo}:#{tag}")
        end
      end
    end
  end

  describe "#docker_tag" do
    context "when there is no tag" do
      it "does not mark a tag" do
        output = subject.docker_tag("test_repo")
        expect(output).to_not include("docker tag")
      end
    end

    context "when there is a tag" do
      it "tags the repo:tag as repo:latest" do
        output = subject.docker_tag("test_repo", "a_tag")
        expect(output).to include("docker tag test_repo:a_tag test_repo:latest")
      end
    end
  end

  describe "#file_docker_import" do
    let(:path) { "/opt/dockly/file.tar.gz" }
    it "cats, gunzips and passes to docker import" do
      expect(subject).to receive(:docker_import)
      output = subject.file_docker_import(path)
      expect(output).to include("cat")
      expect(output).to include("gunzip -c")
    end
  end

  describe "#file_diff_docker_import" do
    let(:base_image) { "s3://bucket/base_image.tar.gz" }
    let(:diff_image) { "/opt/dockly/diff_image.tar.gz" }
    it "gets the base file from S3 and cats that with the diff image and imports to docker" do
      expect(subject).to receive(:get_from_s3).with(base_image)
      expect(subject).to receive(:docker_import)
      expect(subject.file_diff_docker_import(base_image, diff_image)).to include("cat \"#{diff_image}\"")
    end
  end

  describe "#s3_docker_import" do
    let(:s3_url) { "s3://bucket/image.tar.gz" }
    it "pulls, gunzips and passes to docker import" do
      expect(subject).to receive(:get_from_s3)
      expect(subject).to receive(:docker_import)
      output = subject.s3_docker_import(s3_url)<commit_msg>Fix spec<commit_after>require 'spec_helper'

describe Dockly::BashBuilder do
  describe "#normalize_for_dockly" do
    it "sets up log and fatal and makes /opt/dockly" do
      output = subject.normalize_for_dockly
      expect(output).to include("function log")
      expect(output).to include("function fatal")
      expect(output).to include("mkdir -p /opt/dockly")
    end
  end

  describe "#get_from_s3" do
    let(:s3_url) { "s3://url-for-s3/file.tar.gz" }
    context "uses the default output" do
      it "polls from s3 and sets the s3_path" do
        output = subject.get_from_s3(s3_url)
        expect(output).to include("s3cmd -f get $s3_path $output_path")
        expect(output).to include("s3_path=\"#{s3_url}\"")
        expect(output).to include("output_path=\"-\"")
      end
    end
    context "uses a specific output directory" do
      let(:output_dir) { "test" }
      it "polls from s3 and sets the s3_path and output_path" do
        output = subject.get_from_s3(s3_url, output_dir)
        expect(output).to include("s3cmd -f get $s3_path $output_path")
        expect(output).to include("s3_path=\"#{s3_url}\"")
        expect(output).to include("output_path=\"#{output_dir}\"")
      end
    end
  end

  describe "#install_package" do
    let(:path) { "/opt/dockly/deb.deb" }
    it "installs from the given path" do
      output = subject.install_package(path)
      expect(output.strip).to eq("dpkg -i \"#{path}\"")
    end
  end

  describe "#get_and_install_deb" do
    let(:s3_url) { "s3://bucket/path-to-deb.deb" }
    let(:deb_path) { "/opt/dockly/deb_path.deb" }
    it "gets from s3 and installs the package" do
      expect(subject).to receive(:get_from_s3).with(s3_url, deb_path)
      expect(subject).to receive(:install_package).with(deb_path)
      subject.get_and_install_deb(s3_url, deb_path)
    end
  end

  describe "#docker_import" do
    context "when not given a repo" do
      it "imports with no tagging" do
        output = subject.docker_import
        expect(output).to include("docker import -")
      end
    end

    context "when given a repo" do
      let(:repo) { "aRepo" }
      it "imports with repo and latest" do
        output = subject.docker_import(repo)
        expect(output).to include("docker import - #{repo}:latest")
      end

      context "and a non-default tag" do
        let(:tag) { "aTag" }
        it "imports with repo and the tag" do
          output = subject.docker_import(repo, tag)
          expect(output).to include("docker import - #{repo}:#{tag}")
        end
      end
    end
  end

  describe "#docker_tag_latest" do
    context "when there is no tag" do
      it "does not mark a tag" do
        output = subject.docker_tag_latest("test_repo")
        expect(output).to_not include("docker tag")
      end
    end

    context "when there is a tag" do
      it "tags the repo:tag as repo:latest" do
        output = subject.docker_tag_latest("test_repo", "a_tag")
        expect(output).to include("docker tag test_repo:a_tag test_repo:latest")
      end
    end
  end

  describe "#file_docker_import" do
    let(:path) { "/opt/dockly/file.tar.gz" }
    it "cats, gunzips and passes to docker import" do
      expect(subject).to receive(:docker_import)
      output = subject.file_docker_import(path)
      expect(output).to include("cat")
      expect(output).to include("gunzip -c")
    end
  end

  describe "#file_diff_docker_import" do
    let(:base_image) { "s3://bucket/base_image.tar.gz" }
    let(:diff_image) { "/opt/dockly/diff_image.tar.gz" }
    it "gets the base file from S3 and cats that with the diff image and imports to docker" do
      expect(subject).to receive(:get_from_s3).with(base_image)
      expect(subject).to receive(:docker_import)
      expect(subject.file_diff_docker_import(base_image, diff_image)).to include("cat \"#{diff_image}\"")
    end
  end

  describe "#s3_docker_import" do
    let(:s3_url) { "s3://bucket/image.tar.gz" }
    it "pulls, gunzips and passes to docker import" do
      expect(subject).to receive(:get_from_s3)
      expect(subject).to receive(:docker_import)
      output = subject.s3_docker_import(s3_url)
{'fraction_non_alphanumeric': 0.12274368231046931, 'fraction_numerical': 0.00842358604091456, 'mean_word_length': 2.9727533460803057, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4418576', 'n_tokens_mistral': 2832, 'n_tokens_neox': 2730, 'n_words': 698}