summaryrefslogtreecommitdiff
path: root/spec/unit/core/path_traversal_spec.rb
blob: e9da697597f7a138e983c1a3658b9f216464962c (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
# frozen_string_literal: true

RSpec.describe Spandx::Core::PathTraversal do
  let(:result) do
    [].tap do |items|
      subject.each do |item|
        items << item.to_s
      end
    end
  end

  around do |example|
    within_tmp_dir do |directory|
      directory.join('./00/01/02/03/04').mkpath
      directory.join('./00/01/02/03/04/.04').write('04')
      directory.join('./00/01/02/03/04/file.04').write('04')
      directory.join('./00/01/02/03/file.03').write('03')
      directory.join('./00/01/02/file.02').write('02')
      directory.join('./00/01/file.01').write('01')
      directory.join('./00/file.00').write('00')
      directory.join('./file').write('.')

      example.run
    end
  end

  describe '#each' do
    context 'when traversing a directory non-recursively' do
      subject { described_class.new(Pathname.pwd, recursive: false) }

      specify do
        expect(result.map { |x| Pathname.new(x).basename.to_s }).to match_array(['file'])
      end
    end

    context 'when traversing a directory recursively' do
      subject { described_class.new(Pathname.pwd, recursive: true) }

      specify do
        expect(result.map { |x| Pathname.new(x).basename.to_s }).to match_array([
          'file', 'file.00', 'file.01', 'file.02', 'file.03', 'file.04', '.04'
        ])
      end
    end

    context 'when traversing a file non-recursively' do
      subject { described_class.new(path, recursive: false) }

      let(:path) { Pathname.pwd.join('./file') }

      specify do
        expect(result.map { |x| Pathname.new(x).basename.to_s }).to match_array(['file'])
      end
    end

    context 'when traversing a file recursively' do
      subject { described_class.new(path, recursive: true) }

      let(:path) { Pathname.pwd.join('./file') }

      specify do
        expect(result.map { |x| Pathname.new(x).basename.to_s }).to match_array(['file'])
      end
    end
  end
end