File: check_symlinks.rb

package info (click to toggle)
ruby-puppetlabs-spec-helper 8.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 344 kB
  • sloc: ruby: 1,526; sh: 8; makefile: 6
file content (54 lines) | stat: -rw-r--r-- 1,233 bytes parent folder | download
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
# frozen_string_literal: true

require 'pathspec'

module PuppetlabsSpecHelper
  module Tasks
    # Helpers for validating symlinks.
    class CheckSymlinks
      DEFAULT_IGNORED = [
        '/.git/',
        '/.bundle/',
        '/vendor/',
      ].freeze

      IGNORE_LIST_FILES = [
        '.pdkignore',
        '.gitignore',
      ].freeze

      def check(dir = Dir.pwd)
        dir = Pathname.new(dir) unless dir.is_a?(Pathname)
        results = []

        dir.each_child(true) do |child|
          next if ignored?(child.to_s)

          if child.symlink?
            results << child
          elsif child.directory? && child.basename.to_s !~ /^(\.git|\.?bundle)$/
            results.concat(check(child))
          end
        end

        results
      end

      def ignored?(path)
        path = "#{path}/" if File.directory?(path)

        !ignore_pathspec.match_paths([path], Dir.pwd).empty?
      end

      def ignore_pathspec
        @ignore_pathspec ||= PathSpec.new(DEFAULT_IGNORED).tap do |pathspec|
          IGNORE_LIST_FILES.each do |f|
            next unless File.file?(f) && File.readable?(f)

            File.open(f, 'r') { |fd| pathspec.add(fd) }
          end
        end
      end
    end
  end
end