Welcome to mirror list, hosted at ThFree Co, Russian Federation.

verify_all_generated_files_are_up_to_date_spec.rb « glfm « lib « scripts « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fca037c9ff32e9f93026a1fb4ea72bee9935de74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# frozen_string_literal: true
require 'fast_spec_helper'
require_relative '../../../../scripts/lib/glfm/verify_all_generated_files_are_up_to_date'

# IMPORTANT NOTE: See https://docs.gitlab.com/ee/development/gitlab_flavored_markdown/specification_guide/#verify-all-generated-files-are-up-to-daterb-script
# for details on the implementation and usage of the `verify_all_generated_files_are_up_to_date.rb` script being tested.
# This developers guide contains diagrams and documentation of the script,
# including explanations and examples of all files it reads and writes.
RSpec.describe Glfm::VerifyAllGeneratedFilesAreUpToDate, '#process' do
  subject { described_class.new }

  let(:output_path) { described_class::GLFM_SPEC_OUTPUT_PATH }
  let(:snapshots_path) { described_class::EXAMPLE_SNAPSHOTS_PATH }
  let(:verify_cmd) { "git status --porcelain #{output_path} #{snapshots_path}" }

  before do
    # Prevent console output when running tests
    allow(subject).to receive(:output)
  end

  context 'when repo is dirty' do
    before do
      # Simulate a dirty repo
      allow(subject).to receive(:run_external_cmd).with(verify_cmd).and_return(" M #{output_path}")
    end

    it 'raises an error', :unlimited_max_formatted_output_length do
      expect { subject.process }.to raise_error(/Cannot run.*uncommitted changes.*#{output_path}/m)
    end
  end

  context 'when repo is clean' do
    before do
      # Mock out all yarn install and script execution
      allow(subject).to receive(:run_external_cmd).with('yarn install --frozen-lockfile')
      allow(subject).to receive(:run_external_cmd).with(/update-specification.rb/)
      allow(subject).to receive(:run_external_cmd).with(/update-example-snapshots.rb/)
    end

    context 'when all generated files are up to date' do
      before do
        # Simulate a clean repo, then simulate no changes to generated files
        allow(subject).to receive(:run_external_cmd).twice.with(verify_cmd).and_return('', '')
      end

      it 'does not raise an error', :unlimited_max_formatted_output_length do
        expect { subject.process }.not_to raise_error
      end
    end

    context 'when generated file(s) are not up to date' do
      before do
        # Simulate a clean repo, then simulate changes to generated files
        allow(subject).to receive(:run_external_cmd).twice.with(verify_cmd).and_return('', "M #{snapshots_path}")
      end

      it 'raises an error', :unlimited_max_formatted_output_length do
        expect { subject.process }.to raise_error(/following files were modified.*#{snapshots_path}/m)
      end
    end
  end
end