Sha256: c97bec7f53d2879fa04ef313a1cba94ac4df07aec410531a11e481930817bea5

Contents?: true

Size: 1.18 KB

Versions: 6

Compression:

Stored size: 1.18 KB

Contents

require 'spec_helper'

require 'puppet/util/yaml'

describe Puppet::Util::Yaml do
  include PuppetSpec::Files

  let(:filename) { tmpfile("yaml") }

  it "reads a YAML file from disk" do
    write_file(filename, YAML.dump({ "my" => "data" }))

    expect(Puppet::Util::Yaml.load_file(filename)).to eq({ "my" => "data" })
  end

  it "writes data formatted as YAML to disk" do
    Puppet::Util::Yaml.dump({ "my" => "data" }, filename)

    expect(Puppet::Util::Yaml.load_file(filename)).to eq({ "my" => "data" })
  end

  it "raises an error when the file is invalid YAML" do
    write_file(filename, "{ invalid")

    expect { Puppet::Util::Yaml.load_file(filename) }.to raise_error(Puppet::Util::Yaml::YamlLoadError)
  end

  it "raises an error when the file does not exist" do
    expect { Puppet::Util::Yaml.load_file("no") }.to raise_error(Puppet::Util::Yaml::YamlLoadError, /No such file or directory/)
  end

  it "raises an error when the filename is illegal" do
    expect { Puppet::Util::Yaml.load_file("not\0allowed") }.to raise_error(Puppet::Util::Yaml::YamlLoadError, /null byte/)
  end

  def write_file(name, contents)
    File.open(name, "w") do |fh|
      fh.write(contents)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
puppet-3.4.0.rc1 spec/unit/util/yaml_spec.rb
puppet-3.3.2 spec/unit/util/yaml_spec.rb
puppet-3.3.1 spec/unit/util/yaml_spec.rb
puppet-3.3.1.rc3 spec/unit/util/yaml_spec.rb
puppet-3.3.1.rc2 spec/unit/util/yaml_spec.rb
puppet-3.3.1.rc1 spec/unit/util/yaml_spec.rb