Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support empty yaml array #217

Merged
merged 2 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions app/models/hiera_data/yaml_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ def content_for_key(key)
return value if [String, Integer, Float].include?(value.class)

value_string = value.to_yaml
value_string.gsub!("---\n", '').gsub!(/^$\n/, '')
value_string
value_string.sub(/\A---(\n| )/, '').gsub(/^$\n/, '')
rwaffen marked this conversation as resolved.
Show resolved Hide resolved
end

def write_key(key, value)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
noop_mode: true
classes: []
foobar::enable_firstrun: true
foobar::firstrun::linux_classes:
hostname: hostname
Expand Down
6 changes: 6 additions & 0 deletions test/models/hiera_data/yaml_file_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class HieraData::YamlFileTest < ActiveSupport::TestCase
test "#keys returns the top level keys for the file" do
expected_keys = [
"noop_mode",
"classes",
"foobar::enable_firstrun",
"foobar::firstrun::linux_classes",
"foobar::time::servers",
Expand Down Expand Up @@ -71,6 +72,11 @@ class HieraData::YamlFileTest < ActiveSupport::TestCase
assert_equal 0.1, file.content_for_key('hdm::float')
end

test "#content_for_key returns empty array as array" do
file = HieraData::YamlFile.new(path: config_dir.join("nodes/testhost.yaml"))
assert_equal "[]\n", file.content_for_key('classes')
end

test "#write_key goes fine" do
path = Rails.root.join('test', 'fixtures', 'files', 'puppet', 'environments', 'development', 'data', 'nodes', 'writehost.yaml')

Expand Down
2 changes: 1 addition & 1 deletion test/models/hiera_data_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class HieraDataTest < ActiveSupport::TestCase
test "#all_keys return all keys" do
hiera = HieraData.new('development')
expected_result = [
"foobar::enable_firstrun", "foobar::firstrun::linux_classes", "foobar::postfix::tp::resources_hash", "foobar::time::servers", "foobar::timezone", "hdm::float", "hdm::integer", "noop_mode"
"classes", "foobar::enable_firstrun", "foobar::firstrun::linux_classes", "foobar::postfix::tp::resources_hash", "foobar::time::servers", "foobar::timezone", "hdm::float", "hdm::integer", "noop_mode"
]

node = Node.new(hostname: "testhost", environment: "development")
Expand Down