summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/chat/output_spec.rb
blob: b179f9e9d0a705f51512d40ae6b1003617271d03 (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
# frozen_string_literal: true

require 'spec_helper'

describe Gitlab::Chat::Output do
  let(:build) do
    create(:ci_build, pipeline: create(:ci_pipeline, source: :chat))
  end

  let(:output) { described_class.new(build) }

  describe '#to_s' do
    it 'returns the build output as a String' do
      trace = Gitlab::Ci::Trace.new(build)

      trace.set("echo hello\nhello")

      allow(build)
        .to receive(:trace)
        .and_return(trace)

      allow(output)
        .to receive(:read_offset_and_length)
        .and_return([0, 13])

      expect(output.to_s).to eq('he')
    end
  end

  describe '#read_offset_and_length' do
    context 'without the chat_reply trace section' do
      it 'falls back to using the build_script trace section' do
        expect(output)
          .to receive(:find_build_trace_section)
          .with('chat_reply')
          .and_return(nil)

        expect(output)
          .to receive(:find_build_trace_section)
          .with('build_script')
          .and_return({ name: 'build_script', byte_start: 1, byte_end: 4 })

        expect(output.read_offset_and_length).to eq([1, 3])
      end
    end

    context 'without the build_script trace section' do
      it 'raises MissingBuildSectionError' do
        expect { output.read_offset_and_length }
          .to raise_error(described_class::MissingBuildSectionError)
      end
    end

    context 'with the chat_reply trace section' do
      it 'returns the read offset and length as an Array' do
        trace = Gitlab::Ci::Trace.new(build)

        allow(build)
          .to receive(:trace)
          .and_return(trace)

        allow(trace)
          .to receive(:extract_sections)
          .and_return([{ name: 'chat_reply', byte_start: 1, byte_end: 4 }])

        expect(output.read_offset_and_length).to eq([1, 3])
      end
    end
  end

  describe '#without_executed_command_line' do
    it 'returns the input without the first line' do
      expect(output.without_executed_command_line("hello\nworld"))
        .to eq('world')
    end

    it 'returns an empty String when the input is empty' do
      expect(output.without_executed_command_line('')).to eq('')
    end

    it 'returns an empty String when the input consits of a single newline' do
      expect(output.without_executed_command_line("\n")).to eq('')
    end
  end

  describe '#find_build_trace_section' do
    it 'returns nil when no section could be found' do
      expect(output.find_build_trace_section('foo')).to be_nil
    end

    it 'returns the trace section when it could be found' do
      section = { name: 'chat_reply', byte_start: 1, byte_end: 4 }

      allow(output)
        .to receive(:trace_sections)
        .and_return([section])

      expect(output.find_build_trace_section('chat_reply')).to eq(section)
    end
  end
end