<commit_before># frozen_string_literal: true

RSpec.describe "Pages Gem Integration spec" do
  def destination_file(file)
    File.join tmp_dir, file
  end

  def source
    @source ||= fixture_dir
  end

  def destination
    @destination ||= tmp_dir
  end

  def env
    {
      "BUNDLE_GEMFILE" => "#{source}/Gemfile",
      "JEKYLL_ENV"     => "development",
      "DISABLE_WHITELIST" => "", # Do not disable the whitelist.
    }
  end

  let(:file) { "#{self.class.description}.html" }
  let(:path) { destination_file file }
  let(:contents) { File.read path }

  before(:all) do
    FileUtils.rm_rf(destination)
    Dir.chdir(source) do
      bundle_output, status = Open3.capture2e env, %w(bundle install)
      raise StandardError, bundle_output if status.exitstatus != 0
      cmd = %w(bundle exec jekyll build --verbose --trace)
      cmd = cmd.concat ["--source", source, "--destination", destination]
      build_output, status = Open3.capture2e env, *cmd
      raise StandardError, bundle_output + build_output if status.exitstatus != 0
    end
  end
  after(:all) { FileUtils.rm_rf(destination) }

  it "tests all dependencies" do
    contents = File.read(__FILE__)
    contexts = contents.scan(/context \"(.*?)\"/)
    missing = GitHubPages::Dependencies::VERSIONS.keys - contexts.flatten
    missing -= %w(listen activesupport github-pages-health-check)
    msg = "The following dependencies are missing integration tests: #{missing.join(", ")}"
    expect(missing).to be_empty, msg
  end

  context "jekyll" do
    it "builds" do
      expect(path).to be_an_existing_file
    end
  end

  context "jekyll-sass-converter" do
    let(:file) { "jekyll-sass-converter.css" }

    it "Renders SCSS" do
      expect(path).to be_an_existing_file
      expect(contents).to match("body { color: #333; }")
    end
  end

  context "kramdown" do
    it "converts markdown to HTML" do
      expect(contents).to match('<h1 id="test">Test</h1>')
    end
  end

  context "liquid" do
    it "renders liquid templates" do
      expect(contents).to match("Value of foo: bar")
    end
  end

  context "rouge" do
    it "syntax highlights" do
      expected = '<div class="language-ruby highlighter-rouge">'.dup
      expected << '<pre class="highlight"><code><span class="nb">'
      expected << 'puts</span> <span class="s2">"hello world"'
      expect(contents).to match(expected)
    end
  end

  context "jekyll-redirect-from" do<commit_msg>Rouge 2 has a <div> in there.<commit_after># frozen_string_literal: true

RSpec.describe "Pages Gem Integration spec" do
  def destination_file(file)
    File.join tmp_dir, file
  end

  def source
    @source ||= fixture_dir
  end

  def destination
    @destination ||= tmp_dir
  end

  def env
    {
      "BUNDLE_GEMFILE" => "#{source}/Gemfile",
      "JEKYLL_ENV"     => "development",
      "DISABLE_WHITELIST" => "", # Do not disable the whitelist.
    }
  end

  let(:file) { "#{self.class.description}.html" }
  let(:path) { destination_file file }
  let(:contents) { File.read path }

  before(:all) do
    FileUtils.rm_rf(destination)
    Dir.chdir(source) do
      bundle_output, status = Open3.capture2e env, %w(bundle install)
      raise StandardError, bundle_output if status.exitstatus != 0
      cmd = %w(bundle exec jekyll build --verbose --trace)
      cmd = cmd.concat ["--source", source, "--destination", destination]
      build_output, status = Open3.capture2e env, *cmd
      raise StandardError, bundle_output + build_output if status.exitstatus != 0
    end
  end
  after(:all) { FileUtils.rm_rf(destination) }

  it "tests all dependencies" do
    contents = File.read(__FILE__)
    contexts = contents.scan(/context \"(.*?)\"/)
    missing = GitHubPages::Dependencies::VERSIONS.keys - contexts.flatten
    missing -= %w(listen activesupport github-pages-health-check)
    msg = "The following dependencies are missing integration tests: #{missing.join(", ")}"
    expect(missing).to be_empty, msg
  end

  context "jekyll" do
    it "builds" do
      expect(path).to be_an_existing_file
    end
  end

  context "jekyll-sass-converter" do
    let(:file) { "jekyll-sass-converter.css" }

    it "Renders SCSS" do
      expect(path).to be_an_existing_file
      expect(contents).to match("body { color: #333; }")
    end
  end

  context "kramdown" do
    it "converts markdown to HTML" do
      expect(contents).to match('<h1 id="test">Test</h1>')
    end
  end

  context "liquid" do
    it "renders liquid templates" do
      expect(contents).to match("Value of foo: bar")
    end
  end

  context "rouge" do
    it "syntax highlights" do
      expected = '<div class="language-ruby highlighter-rouge">'.dup
      expected << '<div class="highlight"><pre class="highlight">'
      expected << '<code><span class="nb">'
      expected << 'puts</span> <span class="s2">"hello world"'
      expect(contents).to match(expected)
    end
  end

  context "jekyll-redirect-from" do
{'fraction_non_alphanumeric': 0.12808080808080807, 'fraction_numerical': 0.005050505050505051, 'mean_word_length': 3.202886247877759, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 27, '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': '6630505', 'n_tokens_mistral': 1634, 'n_tokens_neox': 1571, 'n_words': 416}