BigW Consortium Gitlab

parser_spec.rb 2.47 KB
Newer Older
1 2
require 'spec_helper'

Douwe Maan committed
3
describe Gitlab::Diff::Parser, lib: true do
4 5 6
  include RepoHelpers

  let(:project) { create(:project) }
7
  let(:commit) { project.commit(sample_commit.id) }
8
  let(:diff) { commit.raw_diffs.first }
9 10
  let(:parser) { Gitlab::Diff::Parser.new }

11
  describe '#parse' do
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
    let(:diff) do
      <<eos
--- a/files/ruby/popen.rb
+++ b/files/ruby/popen.rb
@@ -6,12 +6,18 @@ module Popen

   def popen(cmd, path=nil)
     unless cmd.is_a?(Array)
-      raise "System commands must be given as an array of strings"
+      raise RuntimeError, "System commands must be given as an array of strings"
     end

     path ||= Dir.pwd
-    vars = { "PWD" => path }
-    options = { chdir: path }
+
+    vars = {
+      "PWD" => path
+    }
+
+    options = {
+      chdir: path
+    }

     unless File.directory?(path)
       FileUtils.mkdir_p(path)
@@ -19,6 +25,7 @@ module Popen

     @cmd_output = ""
     @cmd_status = 0
+
     Open3.popen3(vars, *cmd, options) do |stdin, stdout, stderr, wait_thr|
       @cmd_output << stdout.read
       @cmd_output << stderr.read
eos
    end

    before do
50
      @lines = parser.parse(diff.lines).to_a
51 52
    end

53
    it { expect(@lines.size).to eq(30) }
54 55 56 57 58

    describe 'lines' do
      describe 'first line' do
        let(:line) { @lines.first }

59 60 61 62
        it { expect(line.type).to eq('match') }
        it { expect(line.old_pos).to eq(6) }
        it { expect(line.new_pos).to eq(6) }
        it { expect(line.text).to eq('@@ -6,12 +6,18 @@ module Popen') }
63 64 65 66 67
      end

      describe 'removal line' do
        let(:line) { @lines[10] }

68 69 70 71
        it { expect(line.type).to eq('old') }
        it { expect(line.old_pos).to eq(14) }
        it { expect(line.new_pos).to eq(13) }
        it { expect(line.text).to eq('-    options = { chdir: path }') }
72 73 74 75 76
      end

      describe 'addition line' do
        let(:line) { @lines[16] }

77 78 79 80
        it { expect(line.type).to eq('new') }
        it { expect(line.old_pos).to eq(15) }
        it { expect(line.new_pos).to eq(18) }
        it { expect(line.text).to eq('+    options = {') }
81 82 83 84 85
      end

      describe 'unchanged line' do
        let(:line) { @lines.last }

86 87 88
        it { expect(line.type).to eq(nil) }
        it { expect(line.old_pos).to eq(24) }
        it { expect(line.new_pos).to eq(31) }
89
        it { expect(line.text).to eq('       @cmd_output << stderr.read') }
90 91 92
      end
    end
  end
93 94 95 96 97

  context 'when lines is empty' do
    it { expect(parser.parse([])).to eq([]) }
    it { expect(parser.parse(nil)).to eq([]) }
  end
98
end