File: content_spec.rb

package info (click to toggle)
puppet 5.5.22-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 21,316 kB
  • sloc: ruby: 254,925; sh: 1,608; xml: 219; makefile: 153; sql: 103
file content (98 lines) | stat: -rw-r--r-- 3,603 bytes parent folder | download | duplicates (3)
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
require 'spec_helper'

require 'puppet/file_serving/content'

describe Puppet::FileServing::Content do
  let(:path) { File.expand_path('/path') }

  it "should be a subclass of Base" do
    expect(Puppet::FileServing::Content.superclass).to equal(Puppet::FileServing::Base)
  end

  it "should indirect file_content" do
    expect(Puppet::FileServing::Content.indirection.name).to eq(:file_content)
  end

  it "should only support the binary format" do
    expect(Puppet::FileServing::Content.supported_formats).to eq([:binary])
  end

  it "should have a method for collecting its attributes" do
    expect(Puppet::FileServing::Content.new(path)).to respond_to(:collect)
  end

  it "should not retrieve and store its contents when its attributes are collected" do
    content = Puppet::FileServing::Content.new(path)

    expect(File).not_to receive(:read).with(path)
    content.collect

    expect(content.instance_variable_get("@content")).to be_nil
  end

  it "should have a method for setting its content" do
    content = Puppet::FileServing::Content.new(path)
    expect(content).to respond_to(:content=)
  end

  it "should make content available when set externally" do
    content = Puppet::FileServing::Content.new(path)
    content.content = "foo/bar"
    expect(content.content).to eq("foo/bar")
  end

  it "should be able to create a content instance from binary file contents" do
    expect(Puppet::FileServing::Content).to respond_to(:from_binary)
  end

  it "should create an instance with a fake file name and correct content when converting from binary" do
    instance = double('instance')
    expect(Puppet::FileServing::Content).to receive(:new).with("/this/is/a/fake/path").and_return(instance)

    expect(instance).to receive(:content=).with("foo/bar")

    expect(Puppet::FileServing::Content.from_binary("foo/bar")).to equal(instance)
  end

  it "should return an opened File when converted to binary" do
    content = Puppet::FileServing::Content.new(path)

    expect(File).to receive(:new).with(path, "rb").and_return(:file)

    expect(content.to_binary).to eq(:file)
  end
end

describe Puppet::FileServing::Content, "when returning the contents" do
  let(:path) { File.expand_path('/my/path') }
  let(:content) { Puppet::FileServing::Content.new(path, :links => :follow) }

  it "should fail if the file is a symlink and links are set to :manage" do
    content.links = :manage
    expect(Puppet::FileSystem).to receive(:lstat).with(path).and_return(double("stat", :ftype => "symlink"))
    expect { content.content }.to raise_error(ArgumentError)
  end

  it "should fail if a path is not set" do
    expect { content.content }.to raise_error(Errno::ENOENT)
  end

  it "should raise Errno::ENOENT if the file is absent" do
    content.path = File.expand_path("/there/is/absolutely/no/chance/that/this/path/exists")
    expect { content.content }.to raise_error(Errno::ENOENT)
  end

  it "should return the contents of the path if the file exists" do
    expect(Puppet::FileSystem).to receive(:stat).with(path).and_return(double('stat', :ftype => 'file'))
    expect(Puppet::FileSystem).to receive(:binread).with(path).and_return(:mycontent)
    expect(content.content).to eq(:mycontent)
  end

  it "should cache the returned contents" do
    expect(Puppet::FileSystem).to receive(:stat).with(path).and_return(double('stat', :ftype => 'file'))
    expect(Puppet::FileSystem).to receive(:binread).with(path).and_return(:mycontent)
    content.content
    # The second run would throw a failure if the content weren't being cached.
    content.content
  end
end