File: check_symlinks_spec.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 (162 lines) | stat: -rw-r--r-- 3,747 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# frozen_string_literal: true

require 'spec_helper'

describe 'rake check:symlinks', type: :task do
  before do
    test_files.each do |f|
      FileUtils.mkdir_p(File.dirname(f))
      FileUtils.touch(f)
    end

    symlinks.each do |link, target|
      FileUtils.mkdir_p(File.dirname(link))
      FileUtils.ln_s(target, link)
    end
  end

  let(:test_files) { [] }
  let(:symlinks) { {} }
  let(:expected_output) do
    symlinks.map { |link, target| "Symlink found: #{link} => #{target}" }.join("\n")
  end

  context 'when there are no files' do
    it 'runs without raising an error' do
      expect { task.execute }.not_to raise_error
    end
  end

  context 'when there are regular files' do
    let(:test_files) do
      [
        File.join(Dir.pwd, 'files', 'a_file.pp'),
        File.join(Dir.pwd, 'files', 'another_file.pp'),
      ]
    end

    it 'runs without raising an error' do
      expect { task.execute }.not_to raise_error
    end
  end

  context 'when there is a symlink present' do
    let(:test_files) do
      [
        File.join(Dir.pwd, 'files', 'a_file.pp'),
      ]
    end

    let(:symlinks) do
      {
        File.join(Dir.pwd, 'files', 'a_symlink.pp') => File.join(Dir.pwd, 'files', 'a_file.pp'),
      }
    end

    it 'raises an error' do
      expect { task.execute }
        .to raise_error(/symlink\(s\) exist/i)
        .and output(a_string_including(expected_output)).to_stdout
    end
  end

  context 'when there are symlinks under .git/' do
    let(:test_files) do
      [
        File.join(Dir.pwd, 'files', 'a_file.pp'),
      ]
    end

    let(:symlinks) do
      {
        File.join(Dir.pwd, '.git', 'a_symlink.pp') => File.join(Dir.pwd, 'files', 'a_file.pp'),
      }
    end

    it 'runs without raising an error' do
      expect { task.execute }.not_to raise_error
    end
  end

  context 'when there are symlinks under .bundle/' do
    let(:test_files) do
      [
        File.join(Dir.pwd, 'files', 'a_file.pp'),
      ]
    end

    let(:symlinks) do
      {
        File.join(Dir.pwd, '.bundle', 'a_symlink.pp') => File.join(Dir.pwd, 'files', 'a_file.pp'),
      }
    end

    it 'runs without raising an error' do
      expect { task.execute }.not_to raise_error
    end
  end

  context 'when there are symlinks under vendor/' do
    let(:test_files) do
      [
        File.join(Dir.pwd, 'files', 'a_file.pp'),
      ]
    end

    let(:symlinks) do
      {
        File.join(Dir.pwd, 'vendor', 'a_symlink.pp') => File.join(Dir.pwd, 'files', 'a_file.pp'),
      }
    end

    it 'runs without raising an error' do
      expect { task.execute }.not_to raise_error
    end
  end

  context 'when there are symlinks under a directory listed in .gitignore' do
    before do
      File.write(File.join(Dir.pwd, '.gitignore'), "a_directory/\n")
    end

    let(:test_files) do
      [
        File.join(Dir.pwd, 'files', 'a_file.pp'),
      ]
    end

    let(:symlinks) do
      {
        File.join(Dir.pwd, 'a_directory', 'a_symlink.pp') => File.join(Dir.pwd, 'files', 'a_file.pp'),
      }
    end

    it 'runs without raising an error' do
      expect { task.execute }.not_to raise_error
    end
  end

  context 'when there are symlinks under a directory listed in .pdkignore' do
    before do
      File.open(File.join(Dir.pwd, '.pdkignore'), 'w') do |f|
        f.puts '/another_directory/'
      end
    end

    let(:test_files) do
      [
        File.join(Dir.pwd, 'files', 'a_file.pp'),
      ]
    end

    let(:symlinks) do
      {
        File.join(Dir.pwd, 'another_directory', 'a_symlink.pp') => File.join(Dir.pwd, 'files', 'a_file.pp'),
      }
    end

    it 'runs without raising an error' do
      expect { task.execute }.not_to raise_error
    end
  end
end