diff_spec.rb 10.7 KB
Newer Older
Robert Speicher's avatar
Robert Speicher committed
1 2 3
require "spec_helper"

describe Gitlab::Git::Diff, seed_helper: true do
4
  let(:repository) { Gitlab::Git::Repository.new('default', TEST_REPO_PATH, '') }
Robert Speicher's avatar
Robert Speicher committed
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

  before do
    @raw_diff_hash = {
      diff: <<EOT.gsub(/^ {8}/, "").sub(/\n$/, ""),
        --- a/.gitmodules
        +++ b/.gitmodules
        @@ -4,3 +4,6 @@
         [submodule "gitlab-shell"]
         \tpath = gitlab-shell
         \turl = https://github.com/gitlabhq/gitlab-shell.git
        +[submodule "gitlab-grack"]
        +	path = gitlab-grack
        +	url = https://gitlab.com/gitlab-org/gitlab-grack.git

EOT
      new_path: ".gitmodules",
      old_path: ".gitmodules",
      a_mode: '100644',
      b_mode: '100644',
      new_file: false,
      renamed_file: false,
      deleted_file: false,
      too_large: false
    }

    @rugged_diff = repository.rugged.diff("5937ac0a7beb003549fc5fd26fc247adbce4a52e^", "5937ac0a7beb003549fc5fd26fc247adbce4a52e", paths:
                                          [".gitmodules"]).patches.first
  end

34 35 36
  describe 'size limit feature toggles' do
    context 'when the feature gitlab_git_diff_size_limit_increase is enabled' do
      before do
37
        stub_feature_flags(gitlab_git_diff_size_limit_increase: true)
38 39 40 41 42 43 44 45 46 47 48 49 50
      end

      it 'returns 200 KB for size_limit' do
        expect(described_class.size_limit).to eq(200.kilobytes)
      end

      it 'returns 100 KB for collapse_limit' do
        expect(described_class.collapse_limit).to eq(100.kilobytes)
      end
    end

    context 'when the feature gitlab_git_diff_size_limit_increase is disabled' do
      before do
51
        stub_feature_flags(gitlab_git_diff_size_limit_increase: false)
52 53 54 55 56 57 58 59 60 61 62 63
      end

      it 'returns 100 KB for size_limit' do
        expect(described_class.size_limit).to eq(100.kilobytes)
      end

      it 'returns 10 KB for collapse_limit' do
        expect(described_class.collapse_limit).to eq(10.kilobytes)
      end
    end
  end

Robert Speicher's avatar
Robert Speicher committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
  describe '.new' do
    context 'using a Hash' do
      context 'with a small diff' do
        let(:diff) { described_class.new(@raw_diff_hash) }

        it 'initializes the diff' do
          expect(diff.to_hash).to eq(@raw_diff_hash)
        end

        it 'does not prune the diff' do
          expect(diff).not_to be_too_large
        end
      end

      context 'using a diff that is too large' do
        it 'prunes the diff' do
80
          diff = described_class.new(diff: 'a' * (described_class.size_limit + 1))
Robert Speicher's avatar
Robert Speicher committed
81 82 83 84 85 86 87 88 89 90 91 92

          expect(diff.diff).to be_empty
          expect(diff).to be_too_large
        end
      end
    end

    context 'using a Rugged::Patch' do
      context 'with a small diff' do
        let(:diff) { described_class.new(@rugged_diff) }

        it 'initializes the diff' do
93
          expect(diff.to_hash).to eq(@raw_diff_hash)
Robert Speicher's avatar
Robert Speicher committed
94 95 96 97 98 99 100 101 102
        end

        it 'does not prune the diff' do
          expect(diff).not_to be_too_large
        end
      end

      context 'using a diff that is too large' do
        it 'prunes the diff' do
103 104
          expect_any_instance_of(String).to receive(:bytesize)
            .and_return(1024 * 1024 * 1024)
Robert Speicher's avatar
Robert Speicher committed
105 106 107 108 109 110 111 112 113 114 115 116 117

          diff = described_class.new(@rugged_diff)

          expect(diff.diff).to be_empty
          expect(diff).to be_too_large
        end
      end

      context 'using a collapsable diff that is too large' do
        before do
          # The patch total size is 200, with lines between 21 and 54.
          # This is a quick-and-dirty way to test this. Ideally, a new patch is
          # added to the test repo with a size that falls between the real limits.
118 119
          allow(Gitlab::Git::Diff).to receive(:size_limit).and_return(150)
          allow(Gitlab::Git::Diff).to receive(:collapse_limit).and_return(100)
Robert Speicher's avatar
Robert Speicher committed
120 121 122
        end

        it 'prunes the diff as a large diff instead of as a collapsed diff' do
Douwe Maan's avatar
Douwe Maan committed
123
          diff = described_class.new(@rugged_diff, expanded: false)
Robert Speicher's avatar
Robert Speicher committed
124 125 126 127 128 129 130 131 132

          expect(diff.diff).to be_empty
          expect(diff).to be_too_large
          expect(diff).not_to be_collapsed
        end
      end

      context 'using a large binary diff' do
        it 'does not prune the diff' do
133 134
          expect_any_instance_of(Rugged::Diff::Delta).to receive(:binary?)
            .and_return(true)
Robert Speicher's avatar
Robert Speicher committed
135 136 137 138 139 140 141

          diff = described_class.new(@rugged_diff)

          expect(diff.diff).not_to be_empty
        end
      end
    end
142

143
    context 'using a GitalyClient::Diff' do
144 145
      let(:diff) do
        described_class.new(
146
          Gitlab::GitalyClient::Diff.new(
147 148 149 150 151 152
            to_path: ".gitmodules",
            from_path: ".gitmodules",
            old_mode: 0100644,
            new_mode: 0100644,
            from_id: '357406f3075a57708d0163752905cc1576fceacc',
            to_id: '8e5177d718c561d36efde08bad36b43687ee6bf0',
153
            patch: raw_patch
154 155 156 157 158
          )
        )
      end

      context 'with a small diff' do
159
        let(:raw_patch) { @raw_diff_hash[:diff] }
160 161 162 163 164 165 166 167 168 169 170

        it 'initializes the diff' do
          expect(diff.to_hash).to eq(@raw_diff_hash)
        end

        it 'does not prune the diff' do
          expect(diff).not_to be_too_large
        end
      end

      context 'using a diff that is too large' do
171
        let(:raw_patch) { 'a' * 204800 }
172 173 174 175 176 177

        it 'prunes the diff' do
          expect(diff.diff).to be_empty
          expect(diff).to be_too_large
        end
      end
178 179 180 181 182

      context 'when the patch passed is not UTF-8-encoded' do
        let(:raw_patch) { @raw_diff_hash[:diff].encode(Encoding::ASCII_8BIT) }

        it 'encodes diff patch to UTF-8' do
Jacob Vosmaer's avatar
Jacob Vosmaer committed
183
          expect(diff.diff).to be_utf8
184 185
        end
      end
186
    end
Robert Speicher's avatar
Robert Speicher committed
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
  end

  describe 'straight diffs' do
    let(:options) { { straight: true } }
    let(:diffs) { described_class.between(repository, 'feature', 'master', options) }

    it 'has the correct size' do
      expect(diffs.size).to eq(24)
    end

    context 'diff' do
      it 'is an instance of Diff' do
        expect(diffs.first).to be_kind_of(described_class)
      end

      it 'has the correct new_path' do
        expect(diffs.first.new_path).to eq('.DS_Store')
      end

      it 'has the correct diff' do
        expect(diffs.first.diff).to include('Binary files /dev/null and b/.DS_Store differ')
      end
    end
  end

  describe '.between' do
    let(:diffs) { described_class.between(repository, 'feature', 'master') }
    subject { diffs }

    it { is_expected.to be_kind_of Gitlab::Git::DiffCollection }

    describe '#size' do
      subject { super().size }

      it { is_expected.to eq(1) }
    end

    context 'diff' do
      subject { diffs.first }

      it { is_expected.to be_kind_of described_class }

      describe '#new_path' do
        subject { super().new_path }

        it { is_expected.to eq('files/ruby/feature.rb') }
      end

      describe '#diff' do
        subject { super().diff }

        it { is_expected.to include '+class Feature' }
      end
    end
  end

  describe '.filter_diff_options' do
244
    let(:options) { { max_files: 100, invalid_opt: true } }
Robert Speicher's avatar
Robert Speicher committed
245 246 247 248 249 250 251 252 253 254 255

    context "without default options" do
      let(:filtered_options) { described_class.filter_diff_options(options) }

      it "should filter invalid options" do
        expect(filtered_options).not_to have_key(:invalid_opt)
      end
    end

    context "with default options" do
      let(:filtered_options) do
256
        default_options = { max_files: 5, bad_opt: 1, ignore_whitespace_change: true }
Robert Speicher's avatar
Robert Speicher committed
257 258 259 260 261 262 263 264 265
        described_class.filter_diff_options(options, default_options)
      end

      it "should filter invalid options" do
        expect(filtered_options).not_to have_key(:invalid_opt)
        expect(filtered_options).not_to have_key(:bad_opt)
      end

      it "should merge with default options" do
266
        expect(filtered_options).to have_key(:ignore_whitespace_change)
Robert Speicher's avatar
Robert Speicher committed
267 268 269
      end

      it "should override default options" do
270 271
        expect(filtered_options).to have_key(:max_files)
        expect(filtered_options[:max_files]).to eq(100)
Robert Speicher's avatar
Robert Speicher committed
272 273 274 275
      end
    end
  end

276
  describe '#json_safe_diff' do
micael.bergeron's avatar
micael.bergeron committed
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
    let(:project) { create(:project, :repository) }

    it 'fake binary message when it detects binary' do
      # Rugged will not detect this as binary, but we can fake it
      diff_message = "Binary files files/images/icn-time-tracking.pdf and files/images/icn-time-tracking.pdf differ\n"
      binary_diff = described_class.between(project.repository, 'add-pdf-text-binary', 'add-pdf-text-binary^').first

      expect(binary_diff.diff).not_to be_empty
      expect(binary_diff.json_safe_diff).to eq(diff_message)
    end

    it 'leave non-binary diffs as-is' do
      diff = described_class.new(@rugged_diff)

      expect(diff.json_safe_diff).to eq(diff.diff)
    end
  end

Robert Speicher's avatar
Robert Speicher committed
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
  describe '#submodule?' do
    before do
      commit = repository.lookup('5937ac0a7beb003549fc5fd26fc247adbce4a52e')
      @diffs = commit.parents[0].diff(commit).patches
    end

    it { expect(described_class.new(@diffs[0]).submodule?).to eq(false) }
    it { expect(described_class.new(@diffs[1]).submodule?).to eq(true) }
  end

  describe '#line_count' do
    it 'returns the correct number of lines' do
      diff = described_class.new(@rugged_diff)

      expect(diff.line_count).to eq(9)
    end
  end

  describe '#too_large?' do
    it 'returns true for a diff that is too large' do
      diff = described_class.new(diff: 'a' * 204800)

      expect(diff.too_large?).to eq(true)
    end

    it 'returns false for a diff that is small enough' do
      diff = described_class.new(diff: 'a')

      expect(diff.too_large?).to eq(false)
    end

    it 'returns true for a diff that was explicitly marked as being too large' do
      diff = described_class.new(diff: 'a')

329
      diff.too_large!
Robert Speicher's avatar
Robert Speicher committed
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350

      expect(diff.too_large?).to eq(true)
    end
  end

  describe '#collapsed?' do
    it 'returns false by default even on quite big diff' do
      diff = described_class.new(diff: 'a' * 20480)

      expect(diff).not_to be_collapsed
    end

    it 'returns false by default for a diff that is small enough' do
      diff = described_class.new(diff: 'a')

      expect(diff).not_to be_collapsed
    end

    it 'returns true for a diff that was explicitly marked as being collapsed' do
      diff = described_class.new(diff: 'a')

351
      diff.collapse!
Robert Speicher's avatar
Robert Speicher committed
352 353 354 355 356

      expect(diff).to be_collapsed
    end
  end

357
  describe '#collapsed?' do
Robert Speicher's avatar
Robert Speicher committed
358
    it 'returns true for a diff that is quite large' do
359
      diff = described_class.new({ diff: 'a' * (described_class.collapse_limit + 1) }, expanded: false)
Robert Speicher's avatar
Robert Speicher committed
360

Douwe Maan's avatar
Douwe Maan committed
361
      expect(diff).to be_collapsed
Robert Speicher's avatar
Robert Speicher committed
362 363 364
    end

    it 'returns false for a diff that is small enough' do
Douwe Maan's avatar
Douwe Maan committed
365
      diff = described_class.new({ diff: 'a' }, expanded: false)
Robert Speicher's avatar
Robert Speicher committed
366

Douwe Maan's avatar
Douwe Maan committed
367
      expect(diff).not_to be_collapsed
Robert Speicher's avatar
Robert Speicher committed
368 369 370
    end
  end

371
  describe '#collapse!' do
Robert Speicher's avatar
Robert Speicher committed
372 373 374
    it 'prunes the diff' do
      diff = described_class.new(diff: "foo\nbar")

375
      diff.collapse!
Robert Speicher's avatar
Robert Speicher committed
376 377 378 379 380 381

      expect(diff.diff).to eq('')
      expect(diff.line_count).to eq(0)
    end
  end
end