2014-09-08 13:42:12 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2015-12-09 05:55:36 -05:00
|
|
|
describe Gitlab::Diff::Parser, lib: true do
|
2014-09-08 13:42:12 -04:00
|
|
|
include RepoHelpers
|
|
|
|
|
|
|
|
let(:project) { create(:project) }
|
2015-04-21 09:13:40 -04:00
|
|
|
let(:commit) { project.commit(sample_commit.id) }
|
2014-09-08 13:42:12 -04:00
|
|
|
let(:diff) { commit.diffs.first }
|
|
|
|
let(:parser) { Gitlab::Diff::Parser.new }
|
|
|
|
|
|
|
|
describe :parse do
|
|
|
|
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
|
2016-03-03 12:38:44 -05:00
|
|
|
@lines = parser.parse(diff.lines).to_a
|
2014-09-08 13:42:12 -04:00
|
|
|
end
|
|
|
|
|
2015-02-12 13:17:35 -05:00
|
|
|
it { expect(@lines.size).to eq(30) }
|
2014-09-08 13:42:12 -04:00
|
|
|
|
|
|
|
describe 'lines' do
|
|
|
|
describe 'first line' do
|
|
|
|
let(:line) { @lines.first }
|
|
|
|
|
2015-02-12 13:17:35 -05:00
|
|
|
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') }
|
2014-09-08 13:42:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'removal line' do
|
|
|
|
let(:line) { @lines[10] }
|
|
|
|
|
2015-02-12 13:17:35 -05:00
|
|
|
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 }') }
|
2014-09-08 13:42:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'addition line' do
|
|
|
|
let(:line) { @lines[16] }
|
|
|
|
|
2015-02-12 13:17:35 -05:00
|
|
|
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 = {') }
|
2014-09-08 13:42:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'unchanged line' do
|
|
|
|
let(:line) { @lines.last }
|
|
|
|
|
2015-02-12 13:17:35 -05:00
|
|
|
it { expect(line.type).to eq(nil) }
|
|
|
|
it { expect(line.old_pos).to eq(24) }
|
|
|
|
it { expect(line.new_pos).to eq(31) }
|
2016-01-20 09:26:44 -05:00
|
|
|
it { expect(line.text).to eq(' @cmd_output << stderr.read') }
|
2014-09-08 13:42:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-03-15 20:51:27 -04:00
|
|
|
|
|
|
|
context 'when lines is empty' do
|
|
|
|
it { expect(parser.parse([])).to eq([]) }
|
|
|
|
it { expect(parser.parse(nil)).to eq([]) }
|
|
|
|
end
|
2014-09-08 13:42:12 -04:00
|
|
|
end
|