Sha256: fba4be1d4d30b4872c750b742abe553f23ac4688d78adace25b27eee589830b4

Contents?: true

Size: 1.53 KB

Versions: 57

Compression:

Stored size: 1.53 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

  context "when the file is empty" do
    it "returns false" do
      Puppet::FileSystem.touch(filename)

      expect(Puppet::Util::Yaml.load_file(filename)).to be_false
    end

    it "allows return value to be overridden" do
      Puppet::FileSystem.touch(filename)

      expect(Puppet::Util::Yaml.load_file(filename, {})).to eq({})
    end
  end

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

Version data entries

57 entries across 57 versions & 1 rubygems

Version Path
puppet-3.8.7 spec/unit/util/yaml_spec.rb
puppet-3.8.7-x86-mingw32 spec/unit/util/yaml_spec.rb
puppet-3.8.7-x64-mingw32 spec/unit/util/yaml_spec.rb
puppet-3.8.6 spec/unit/util/yaml_spec.rb
puppet-3.8.6-x86-mingw32 spec/unit/util/yaml_spec.rb
puppet-3.8.6-x64-mingw32 spec/unit/util/yaml_spec.rb
puppet-3.8.5 spec/unit/util/yaml_spec.rb
puppet-3.8.5-x86-mingw32 spec/unit/util/yaml_spec.rb
puppet-3.8.5-x64-mingw32 spec/unit/util/yaml_spec.rb
puppet-3.8.4 spec/unit/util/yaml_spec.rb
puppet-3.8.4-x86-mingw32 spec/unit/util/yaml_spec.rb
puppet-3.8.4-x64-mingw32 spec/unit/util/yaml_spec.rb
puppet-3.8.3 spec/unit/util/yaml_spec.rb
puppet-3.8.3-x86-mingw32 spec/unit/util/yaml_spec.rb
puppet-3.8.3-x64-mingw32 spec/unit/util/yaml_spec.rb
puppet-3.8.2 spec/unit/util/yaml_spec.rb
puppet-3.8.2-x86-mingw32 spec/unit/util/yaml_spec.rb
puppet-3.8.2-x64-mingw32 spec/unit/util/yaml_spec.rb
puppet-3.8.1 spec/unit/util/yaml_spec.rb
puppet-3.8.1-x86-mingw32 spec/unit/util/yaml_spec.rb