summaryrefslogtreecommitdiff
path: root/spec/scripts/lib/glfm/update_specification_spec.rb
blob: 92434b375151f920796117dcaecdcf503e3f2536 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# frozen_string_literal: true

require 'fast_spec_helper'
require_relative '../../../../scripts/lib/glfm/update_specification'
require_relative '../../../support/helpers/next_instance_of'

# IMPORTANT NOTE: See https://docs.gitlab.com/ee/development/gitlab_flavored_markdown/specification_guide/#update-specificationrb-script
# for details on the implementation and usage of the `update_specification.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.
#
# Note that this test is not structured in a traditional way, with multiple examples
# to cover all different scenarios. Instead, the content of the stubbed test fixture
# files are crafted to cover multiple scenarios with in a single example run.
#
# This is because the invocation of the full script is slow, because it executes
# a subshell for processing, which runs a full Rails environment.
# This results in each full run of the script taking between 30-60 seconds.
# The majority of this is spent loading the Rails environment.
#
# However, only the `with generation of spec.html` context is used
# to test this slow sub-process, and it only contains one example.
#
# All other tests currently in the file pass the `skip_spec_html_generation: true`
# flag to `#process`, which skips the slow sub-process. All of these other tests
# should run in sub-second time when the Spring pre-loader is used. This allows
# logic which is not directly related to the slow sub-processes to be TDD'd with a
# very rapid feedback cycle.
RSpec.describe Glfm::UpdateSpecification, '#process', feature_category: :team_planning do
  include NextInstanceOf

  subject { described_class.new }

  let(:ghfm_spec_txt_uri) { described_class::GHFM_SPEC_TXT_URI }
  let(:ghfm_spec_txt_uri_parsed) { instance_double(URI::HTTPS, :ghfm_spec_txt_uri_parsed) }
  let(:ghfm_spec_txt_uri_io) { StringIO.new(ghfm_spec_txt_contents) }
  let(:ghfm_spec_md_path) { described_class::GHFM_SPEC_MD_PATH }
  let(:ghfm_spec_txt_local_io) { StringIO.new(ghfm_spec_txt_contents) }

  let(:glfm_official_specification_md_path) { described_class::GLFM_OFFICIAL_SPECIFICATION_MD_PATH }
  let(:glfm_official_specification_md_io) { StringIO.new(glfm_official_specification_md_contents) }
  let(:glfm_internal_extensions_md_path) { described_class::GLFM_INTERNAL_EXTENSIONS_MD_PATH }
  let(:glfm_internal_extensions_md_io) { StringIO.new(glfm_internal_extensions_md_contents) }
  let(:glfm_spec_txt_path) { described_class::GLFM_SPEC_TXT_PATH }
  let(:glfm_spec_txt_io) { StringIO.new }
  let(:glfm_spec_html_path) { described_class::GLFM_SPEC_HTML_PATH }
  let(:glfm_spec_html_io) { StringIO.new }
  let(:es_snapshot_spec_md_path) { described_class::ES_SNAPSHOT_SPEC_MD_PATH }
  let(:es_snapshot_spec_md_io) { StringIO.new }
  let(:es_snapshot_spec_html_path) { described_class::ES_SNAPSHOT_SPEC_HTML_PATH }
  let(:es_snapshot_spec_html_io) { StringIO.new }
  let(:markdown_tempfile_io) { StringIO.new }

  let(:ghfm_spec_txt_examples) do
    <<~MARKDOWN
      # Section with examples

      ## Emphasis and strong

      ```````````````````````````````` example
      _EMPHASIS LINE 1_
      _EMPHASIS LINE 2_
      .
      <p><em>EMPHASIS LINE 1</em>
      <em>EMPHASIS LINE 2</em></p>
      ````````````````````````````````

      ```````````````````````````````` example
      __STRONG!__
      .
      <p><strong>STRONG!</strong></p>
      ````````````````````````````````

      End of last GitHub examples section.
    MARKDOWN
  end

  let(:ghfm_spec_txt_contents) do
    <<~MARKDOWN
      ---
      title: GitHub Flavored Markdown Spec
      version: 0.29
      date: '2019-04-06'
      license: '[CC-BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/)'
      ...

      # Introduction

      GHFM Intro.

      #{ghfm_spec_txt_examples}
      <!-- END TESTS -->

      # Appendix

      Appendix text.
    MARKDOWN
  end

  let(:glfm_official_specification_md_examples) do
    <<~MARKDOWN
      # Official Specification Section with Examples

      ```````````````````````````````` example
      official example
      .
      <p>official example</p>
      ````````````````````````````````

    MARKDOWN
  end

  let(:glfm_official_specification_md_contents) do
    <<~MARKDOWN
      GLFM official text before examples

      #{described_class::BEGIN_TESTS_COMMENT_LINE_TEXT}
      #{glfm_official_specification_md_examples}
      #{described_class::END_TESTS_COMMENT_LINE_TEXT}

      GLFM official text after examples
    MARKDOWN
  end

  let(:glfm_internal_extensions_md_examples) do
    <<~MARKDOWN
      # Internal Extension Section with Examples

      ```````````````````````````````` example
      internal example
      .
      <p>internal extension example</p>
      ````````````````````````````````
    MARKDOWN
  end

  let(:glfm_internal_extensions_md_contents) do
    <<~MARKDOWN
      #{described_class::BEGIN_TESTS_COMMENT_LINE_TEXT}
      #{glfm_internal_extensions_md_examples}
      #{described_class::END_TESTS_COMMENT_LINE_TEXT}
    MARKDOWN
  end

  before do
    # Mock default ENV var values
    stub_env('UPDATE_GHFM_SPEC_MD')

    # We mock out the URI and local file IO objects with real StringIO, instead of just mock
    # objects. This gives better and more realistic coverage, while still avoiding
    # actual network and filesystem I/O during the spec run.

    # input files
    allow(URI).to receive(:parse).with(ghfm_spec_txt_uri).and_return(ghfm_spec_txt_uri_parsed)
    allow(ghfm_spec_txt_uri_parsed).to receive(:open).and_return(ghfm_spec_txt_uri_io)
    allow(File).to receive(:open).with(ghfm_spec_md_path) { ghfm_spec_txt_local_io }
    allow(File).to receive(:open).with(glfm_official_specification_md_path) do
      glfm_official_specification_md_io
    end
    allow(File).to receive(:open).with(glfm_internal_extensions_md_path) do
      glfm_internal_extensions_md_io
    end

    # output files
    allow(File).to receive(:open).with(glfm_spec_txt_path, 'w') { glfm_spec_txt_io }
    allow(File).to receive(:open).with(glfm_spec_html_path, 'w') { glfm_spec_html_io }
    allow(File).to receive(:open).with(es_snapshot_spec_md_path, 'w') { es_snapshot_spec_md_io }
    allow(File).to receive(:open).with(es_snapshot_spec_html_path, 'w') { es_snapshot_spec_html_io }

    # Allow normal opening of Tempfile files created during script execution.
    tempfile_basenames = [
      described_class::MARKDOWN_TEMPFILE_BASENAME[0],
      described_class::STATIC_HTML_TEMPFILE_BASENAME[0]
    ].join('|')
    # NOTE: This approach with a single regex seems to be the only way this can work. If you
    # attempt to have multiple `allow...and_call_original` with `any_args`, the mocked
    # parameter matching will fail to match the second one.
    tempfiles_regex = /(#{tempfile_basenames})/
    allow(File).to receive(:open).with(tempfiles_regex, any_args).and_call_original

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

  describe 'retrieving latest GHFM spec.txt' do
    context 'when UPDATE_GHFM_SPEC_MD is not true (default)' do
      it 'does not download' do
        expect(URI).not_to receive(:parse).with(ghfm_spec_txt_uri)

        subject.process(skip_spec_html_generation: true)

        expect(reread_io(ghfm_spec_txt_local_io)).to eq(ghfm_spec_txt_contents)
      end
    end

    context 'when UPDATE_GHFM_SPEC_MD is true' do
      let(:ghfm_spec_txt_local_io) { StringIO.new }

      before do
        stub_env('UPDATE_GHFM_SPEC_MD', 'true')
        allow(File).to receive(:open).with(ghfm_spec_md_path, 'w') { ghfm_spec_txt_local_io }
      end

      context 'with success' do
        it 'downloads and saves' do
          subject.process(skip_spec_html_generation: true)

          expect(reread_io(ghfm_spec_txt_local_io)).to eq(ghfm_spec_txt_contents)
        end
      end

      context 'with error handling' do
        context 'with a version mismatch' do
          let(:ghfm_spec_txt_contents) do
            <<~MARKDOWN
              ---
              title: GitHub Flavored Markdown Spec
              version: 0.30
              ...
            MARKDOWN
          end

          it 'raises an error' do
            expect do
              subject.process(skip_spec_html_generation: true)
            end.to raise_error /version mismatch.*expected.*29.*got.*30/i
          end
        end

        context 'with a failed read of file lines' do
          let(:ghfm_spec_txt_contents) { '' }

          it 'raises an error if lines cannot be read' do
            expect { subject.process(skip_spec_html_generation: true) }.to raise_error /unable to read lines/i
          end
        end

        context 'with a failed re-read of file string' do
          before do
            allow(ghfm_spec_txt_uri_io).to receive(:read).and_return(nil)
          end

          it 'raises an error if file is blank' do
            expect { subject.process(skip_spec_html_generation: true) }.to raise_error /unable to read string/i
          end
        end
      end
    end
  end

  describe 'writing output_spec/spec.txt' do
    let(:glfm_spec_txt_contents) { reread_io(glfm_spec_txt_io) }

    before do
      subject.process(skip_spec_html_generation: true)
    end

    it 'includes only the header and official examples' do
      expected = described_class::GLFM_SPEC_TXT_HEADER + glfm_official_specification_md_contents
      expect(glfm_spec_txt_contents).to eq(expected)
    end
  end

  describe 'writing output_example_snapshots/snapshot_spec.md' do
    let(:es_snapshot_spec_md_contents) { reread_io(es_snapshot_spec_md_io) }

    context 'with valid glfm_internal_extensions.md' do
      before do
        subject.process(skip_spec_html_generation: true)
      end

      it 'replaces the header text with the GitLab version' do
        expect(es_snapshot_spec_md_contents).not_to match(/GitHub Flavored Markdown Spec/m)
        expect(es_snapshot_spec_md_contents).not_to match(/^version: \d\.\d/m)
        expect(es_snapshot_spec_md_contents).not_to match(/^date: /m)

        expect(es_snapshot_spec_md_contents).to match(/#{Regexp.escape(described_class::ES_SNAPSHOT_SPEC_MD_HEADER)}/mo)
      end

      it 'includes header and all examples', :unlimited_max_formatted_output_length do
        # rubocop:disable Style/StringConcatenation (string contatenation is more readable)
        expected = described_class::ES_SNAPSHOT_SPEC_MD_HEADER +
          ghfm_spec_txt_examples +
          "\n" +
          glfm_official_specification_md_examples +
          "\n\n" + # NOTE: We want a blank line between the official and internal examples
          glfm_internal_extensions_md_examples +
          "\n"
        # rubocop:enable Style/StringConcatenation
        expect(es_snapshot_spec_md_contents).to eq(expected)
      end
    end

    context 'with invalid non-example content in glfm_internal_extensions.md' do
      let(:glfm_internal_extensions_md_contents) do
        <<~MARKDOWN
          THIS TEXT IS NOT ALLOWED IN THIS FILE, ONLY EXAMPLES IN BEGIN/END TEST BLOCK ARE ALLOWED
          #{described_class::BEGIN_TESTS_COMMENT_LINE_TEXT}
          #{glfm_internal_extensions_md_examples}
          #{described_class::END_TESTS_COMMENT_LINE_TEXT}
        MARKDOWN
      end

      it 'raises an error' do
        expect { subject.process(skip_spec_html_generation: true) }.to raise_error /no content is allowed outside/i
      end
    end
  end

  # rubocop:disable RSpec/MultipleMemoizedHelpers
  describe 'writing output html files' do
    let(:spec_html_contents) { reread_io(glfm_spec_html_io) }
    let(:snapshot_spec_html_contents) { reread_io(es_snapshot_spec_html_io) }

    before do
      subject.process
    end

    it 'renders expected HTML', :unlimited_max_formatted_output_length do
      # NOTE: We do all assertions for both output HTML files in this same `it` example block,
      #       because calling a full `subject.process` without `skip_spec_html_generation: true`
      #       is very slow, and want to avoid doing it multiple times
      #
      #       We also do fairly loose and minimal assertions around the basic structure of the files.
      #       Otherwise, if we asserted the complete exact structure of the HTML, this would be a
      #       brittle test which would breaks every time that something minor changed around the
      #       GLFM rendering. E.g. classes, ids, attribute ordering, etc. All of this behavior
      #       should be thoroughly covered elsewhere by other testing. If there are regressions
      #       in the update specification logic in the future which are not caught by this example,
      #       additional test coverage can be added as necessary.

      # --------------------
      # spec.html assertions
      # --------------------

      # correct title should in a header
      expect(spec_html_contents).to match(%r{<h1.*#{described_class::GLFM_SPEC_TXT_TITLE}</h1>}o)

      # correct text should be included with correct ordering
      expect(spec_html_contents)
        .to match(%r{official text before.*official example.*official text after}m)

      # -----------------------------
      # snapshot_spec.html assertions
      # -----------------------------

      # correct title should in a header
      expect(snapshot_spec_html_contents).to match(%r{<h1.*#{described_class::ES_SNAPSHOT_SPEC_TITLE}</h1>}o)

      # correct example text should be included
      expect(snapshot_spec_html_contents)
        .to match(%r{internal example}m)

      # -----------------------------
      # general formatting assertions
      # -----------------------------

      md = '_EMPHASIS LINE 1_'
      html = '&lt;em&gt;EMPHASIS LINE 1&lt;/em&gt;'

      # examples should have markdown and HTML in separate pre+code blocks
      expected_example_1_regex = "<pre.*<code.*#{md}.*</code></pre>.*<pre.*<code.*#{html}.*</code></pre>"
      expect(snapshot_spec_html_contents).to match(%r{#{expected_example_1_regex}}m)

      # examples should have proper divs prepended for numbering, links, and styling
      empty_div_for_example_class = '<div>'
      examplenum_div = '<div><a href="#example-1">Example 1</a></div>'
      expect(snapshot_spec_html_contents)
        .to match(%r{#{empty_div_for_example_class}\n#{examplenum_div}.*#{expected_example_1_regex}.*}m)
    end
  end
  # rubocop:enable RSpec/MultipleMemoizedHelpers

  def reread_io(io)
    # Reset the io StringIO to the beginning position of the buffer
    io.seek(0)
    io.read
  end
end