summaryrefslogtreecommitdiff
path: root/test/lib/dslkeywords/file_test.rb
blob: 9390aedabb983c23c37a53c73021123f9920423f (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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
require 'minitest/autorun'
require 'fileutils'

require_relative '../../../lib/dsl'

class RCMFileTest < Minitest::Test
  FILE_PATH = './.file_test.rcmtmp'.freeze
  DIR_PATH = './.dir_test.rcmtmp'.freeze

  Minitest.after_run do
    File.unlink(FILE_PATH) if File.file?(FILE_PATH)
    FileUtils.rm_r(DIR_PATH) if File.directory?(DIR_PATH)
  end

  def test_create_file_from_string
    text = 'Hello World!'
    configure_from_scratch do
      file FILE_PATH do
        text
      end
    end
    assert_equal text, File.read(FILE_PATH)
  end

  def test_create_file_from_array
    arr = %w[Hello World and Hello Universe]
    configure_from_scratch do
      file FILE_PATH do
        arr
      end
    end
    assert_equal arr.join("\n"), File.read(FILE_PATH)
  end

  def test_file_absent
    configure_from_scratch do
      file create do
        path FILE_PATH
        is present
        the text
      end

      file delete do
        path FILE_PATH
        is absent
      end
    end

    refute File.file?(FILE_PATH)
  end

  def test_file_absent_with_empty_directory
    file_path = "#{DIR_PATH}/test_file_absent_with_empty_directory/bar/baz/foo.txt"

    configure_from_scratch do
      file create File empty directory do
        path file_path
        manage directory
        the text
      end

      file delete File empty directory do
        path file_path
        is absent
        manage directory and without backup
        requires file create File empty directory
      end
    end

    refute File.file?(file_path)
    refute File.directory?(File.dirname(file_path))
    refute File.directory?(File.dirname(File.dirname(file_path)))
  end

  def test_create_file_from_sourcefile
    text = 'Hello World!'
    source_path = "#{FILE_PATH}.source.rcmtmp"
    File.write(source_path, text)

    configure_from_scratch do
      file FILE_PATH do
        from sourcefile
        source_path
      end
    end
    assert_equal File.read(source_path), File.read(FILE_PATH)
  ensure
    File.unlink(source_path) if File.file?(source_path)
  end

  def test_create_file_from_template
    configure_from_scratch do
      file FILE_PATH do
        from template
        'One plus two is <%= 1 + 2 %>!'
      end
    end
    assert_equal 'One plus two is 3!', File.read(FILE_PATH)
  end

  def test_line
    File.write(FILE_PATH, "Hey there\n")
    configure_from_scratch { file(FILE_PATH) { line 'Whats up?' } }
    assert_equal "Hey there\nWhats up?\n", File.read(FILE_PATH)
  end

  def test_line_absent
    File.write(FILE_PATH, "Hey there\nWhats up?")
    configure_from_scratch do
      file FILE_PATH do
        line 'Whats up?'
        is absent
      end
    end
    assert_equal 'Hey there', File.read(FILE_PATH)

    File.write(FILE_PATH, "Hey there\nWhats up?")
    configure_from_scratch do
      file FILE_PATH do
        line 'Hey there'
        is absent
      end
    end
    assert_equal 'Whats up?', File.read(FILE_PATH)
  end

  def test_manage_directory
    file_path = "#{DIR_PATH}/foo/bar/baz/foo.txt"
    configure_from_scratch do
      file file_path do
        manage directory
        :content
      end
    end
    assert File.directory?(File.dirname(file_path))
    assert File.exist?(file_path)
    assert_equal :content, File.read(file_path).to_sym
  end

  def test_backup
    file_path = "#{DIR_PATH}/foo/backup-me.txt"
    original_content = 'original_content'
    backup_path = "#{DIR_PATH}/foo/.rcm/backup-me.txt.d4c3af73588ce06c32ed04d1b79801286109ea265712a2bd3fdc3ed01c82bb86"

    configure_from_scratch do
      file original do
        path file_path
        manage directory
        original_content
      end

      file new do
        path file_path
        manage directory
        requires file original
        :new_content
      end
    end

    assert File.file?(backup_path)
    assert_equal original_content, File.read(backup_path)
  end

  def test_create_symlink
    symlink_path = "#{DIR_PATH}/the_symlink"
    symlink_target = "#{DIR_PATH}/the_symlink_target"

    configure_from_scratch do
      symlink symlink_path do
        manage directory
        symlink_target
      end
    end

    assert File.symlink?(symlink_path)
    assert_equal symlink_target, File.readlink(symlink_path)
  end

  def test_change_symlink
    symlink_path = "#{DIR_PATH}/the_symlink"
    symlink_target1 = "#{DIR_PATH}/the_symlink_target1"
    symlink_target2 = "#{DIR_PATH}/the_symlink_target2"

    configure_from_scratch do
      symlink original do
        path symlink_path
        manage directory
        symlink_target1
      end

      symlink changed do
        path symlink_path
        requires symlink original
        symlink_target2
      end
    end

    assert File.symlink?(symlink_path)
    assert_equal symlink_target2, File.readlink(symlink_path)
  end
end