summaryrefslogtreecommitdiff
path: root/spec/unit/util/watched_file_spec.rb
blob: f9e2510620980af1c1a95ce46d494cb7d174d320 (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
require 'spec_helper'
require 'puppet/util/watched_file'
require 'puppet/util/watcher'

describe Puppet::Util::WatchedFile do
  let(:an_absurdly_long_timeout) { Puppet::Util::Watcher::Timer.new(100000) }
  let(:an_immediate_timeout) { Puppet::Util::Watcher::Timer.new(0) }

  it "acts like a string so that it can be used as a filename" do
    watched = Puppet::Util::WatchedFile.new("foo")

    expect(watched.to_str).to eq("foo")
  end

  it "considers the file to be unchanged before the timeout expires" do
    watched = Puppet::Util::WatchedFile.new(a_file_that_doesnt_exist, an_absurdly_long_timeout)

    expect(watched).to_not be_changed
  end

  it "considers a file that is created to be changed" do
    watched_filename = a_file_that_doesnt_exist
    watched = Puppet::Util::WatchedFile.new(watched_filename, an_immediate_timeout)

    create_file(watched_filename)

    expect(watched).to be_changed
  end

  it "considers a missing file to remain unchanged" do
    watched = Puppet::Util::WatchedFile.new(a_file_that_doesnt_exist, an_immediate_timeout)

    expect(watched).to_not be_changed
  end

  it "considers a file that has changed but the timeout is not expired to still be unchanged" do
    watched_filename = a_file_that_doesnt_exist
    watched = Puppet::Util::WatchedFile.new(watched_filename, an_absurdly_long_timeout)

    create_file(watched_filename)

    expect(watched).to_not be_changed
  end

  def create_file(name)
    File.open(name, "wb") { |file| file.puts("contents") }
  end

  def a_file_that_doesnt_exist
    PuppetSpec::Files.tmpfile("watched_file")
  end
end