<commit_before>require 'spec_helper'

describe Morph::DockerRunner do
  # Tests that involve docker are marked as 'docker: true'. This stops
  # them from running on travis ci which doesn't have access to a docker
  # server

  # These are integration tests with the whole docker server and the
  # docker images that are used. Also, the tests are very slow!
  describe '.compile_and_run', docker: true do
    before(:each) do
      @dir = Dir.mktmpdir
      @container_count = Morph::DockerUtils.stopped_containers.count
    end

    after(:each) { FileUtils.remove_entry @dir }

    it "should let me know that it can't select a buildpack" do
      logs = []
      c, _i3 = Morph::DockerRunner.compile_and_start_run(@dir, {}, {}) do |s, c|
        logs << [s, c]
      end

      expect(c).to be_nil
      expect(logs).to eq [
        [:internalout, "Injecting configuration and compiling...\n"],
        [:internalout, "\e[1G-----> Unable to select a buildpack\n"]
      ]
      expect(Morph::DockerUtils.stopped_containers.count)
        .to eq @container_count
    end

    it 'should be able to run hello world' do
      copy_test_scraper('hello_world_js')

      c, _i3 = Morph::DockerRunner.compile_and_start_run(@dir, {}, {}) {}
      logs = []
      result = Morph::DockerRunner.attach_to_run_and_finish(c, []) do |timestamp, s, c|
        logs << [s, c]
      end
      expect(result.status_code).to eq 0
      expect(logs).to eq [[:stdout, "Hello world!\n"]]
    end

    it 'should be able to run hello world from a sub-directory' do
      copy_test_scraper('hello_world_subdirectory_js')

      c, _i3 = Morph::DockerRunner.compile_and_start_run(@dir, {}, {}) {}
      logs = []
      result = Morph::DockerRunner.attach_to_run_and_finish(c, []) do |timestamp, s, c|
        logs << [s, c]
      end
      expect(result.status_code).to eq 0
      expect(logs).to eq [[:stdout, "Hello world!\n"]]
    end

    it 'should cache the compile stage' do
      copy_test_scraper('hello_world_js')

      # Do the compile once to make sure the cache is primed
      Morph::DockerRunner.compile_and_start_run(@dir, {}, {}) {}
      logs = []
      Morph::DockerRunner.compile_and_start_run(@dir, {}, {}) do |s, c|
        logs << [s, c]
      end
      expect(logs).to eq [
        [:internalout, "Injecting configuration and compiling...\n"],
        [:internalout, "Injecting scraper and running...\n"]
      ]
    end

    it 'should be able to run hello world of course' do
      copy_test_scraper('hello_world_ruby')

      c, _i3 = Morph::DockerRunner.compile_and_start_run(@dir, {}, {}) {}
      logs = []
      result = Morph::DockerRunner.attach_to_run_and_finish(c, []) do |timestamp, s, c|
        logs << [s, c]
      end
      expect(result.status_code).to eq 0
      expect(logs).to eq [
        [:stdout,      "Hello world!\n"]
      ]
      expect(Morph::DockerUtils.stopped_containers.count)
        .to eq @container_count
    end

    it 'should be able to grab a file resulting from running the scraper' do
      copy_test_scraper('write_to_file_ruby')

      c, _i3 = Morph::DockerRunner.compile_and_start_run(@dir, {}, {}) {}
      result = Morph::DockerRunner.attach_to_run_and_finish(
        c, ['foo.txt', 'bar']) {}
      expect(result.status_code).to eq 0
      expect(result.files).to eq('foo.txt' => 'Hello World!', 'bar' => nil)
      expect(Morph::DockerUtils.stopped_containers.count)<commit_msg>Ensure running containers are cleaned up. Should make tests more reliable<commit_after>require 'spec_helper'

describe Morph::DockerRunner do
  # Tests that involve docker are marked as 'docker: true'. This stops
  # them from running on travis ci which doesn't have access to a docker
  # server

  # These are integration tests with the whole docker server and the
  # docker images that are used. Also, the tests are very slow!
  describe '.compile_and_run', docker: true do
    before(:each) do
      @dir = Dir.mktmpdir
      @container_count = Morph::DockerUtils.stopped_containers.count
    end

    after(:each) { FileUtils.remove_entry @dir }

    it "should let me know that it can't select a buildpack" do
      logs = []
      c, _i3 = Morph::DockerRunner.compile_and_start_run(@dir, {}, {}) do |s, c|
        logs << [s, c]
      end

      expect(c).to be_nil
      expect(logs).to eq [
        [:internalout, "Injecting configuration and compiling...\n"],
        [:internalout, "\e[1G-----> Unable to select a buildpack\n"]
      ]
      expect(Morph::DockerUtils.stopped_containers.count)
        .to eq @container_count
    end

    it 'should be able to run hello world' do
      copy_test_scraper('hello_world_js')

      c, _i3 = Morph::DockerRunner.compile_and_start_run(@dir, {}, {}) {}
      logs = []
      result = Morph::DockerRunner.attach_to_run_and_finish(c, []) do |timestamp, s, c|
        logs << [s, c]
      end
      expect(result.status_code).to eq 0
      expect(logs).to eq [[:stdout, "Hello world!\n"]]
    end

    it 'should be able to run hello world from a sub-directory' do
      copy_test_scraper('hello_world_subdirectory_js')

      c, _i3 = Morph::DockerRunner.compile_and_start_run(@dir, {}, {}) {}
      logs = []
      result = Morph::DockerRunner.attach_to_run_and_finish(c, []) do |timestamp, s, c|
        logs << [s, c]
      end
      expect(result.status_code).to eq 0
      expect(logs).to eq [[:stdout, "Hello world!\n"]]
    end

    it 'should cache the compile stage' do
      copy_test_scraper('hello_world_js')

      # Do the compile once to make sure the cache is primed
      c, _i3 = Morph::DockerRunner.compile_and_start_run(@dir, {}, {}) {}
      logs = []
      # Clean up container because we're not calling attach_to_run_and_finish
      # which normally does the cleanup
      c.kill
      c.delete

      c, _i3 = Morph::DockerRunner.compile_and_start_run(@dir, {}, {}) do |s, c|
        logs << [s, c]
      end
      c.kill
      c.delete
      expect(logs).to eq [
        [:internalout, "Injecting configuration and compiling...\n"],
        [:internalout, "Injecting scraper and running...\n"]
      ]
    end

    it 'should be able to run hello world of course' do
      copy_test_scraper('hello_world_ruby')

      c, _i3 = Morph::DockerRunner.compile_and_start_run(@dir, {}, {}) {}
      logs = []
      result = Morph::DockerRunner.attach_to_run_and_finish(c, []) do |timestamp, s, c|
        logs << [s, c]
      end
      expect(result.status_code).to eq 0
      expect(logs).to eq [
        [:stdout,      "Hello world!\n"]
      ]
      expect(Morph::DockerUtils.stopped_containers.count)
        .to eq @container_count
    end

    it 'should be able to grab a file resulting from running the scraper' do
      copy_test_scraper('write_to_file_ruby')

      c, _i3 = Morph::DockerRunner.compile_and_start_run(@dir, {}, {}) {}
      result = Morph::DockerRunner.attach_to_run_and_finish(
        c, ['foo.txt', 'bar']) {}
      expect(result.status_code).to eq 0
      expect(result.files).to eq('foo.txt' => 'Hello World!', 'bar' => nil)
      expect(Morph::DockerUtils.stopped_containers.count)
{'fraction_non_alphanumeric': 0.12554416514534475, 'fraction_numerical': 0.0030894537284089315, 'mean_word_length': 2.820815450643777, 'pattern_counts': {'":': 0, '<': 23, '<?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': '349578', 'n_tokens_mistral': 2371, 'n_tokens_neox': 2278, 'n_words': 618}