Skip to content

Commit

Permalink
make filter options explicitly named
Browse files Browse the repository at this point in the history
  • Loading branch information
wr0ngway committed Sep 16, 2021
1 parent a61671c commit 9fef257
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
6 changes: 4 additions & 2 deletions lib/kubetruth/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,11 @@ def stringify(str)
str.to_s.to_json
end

def to_yaml(str, no_header=false)
def to_yaml(str, options = {})
options = {} unless options.is_a?(Hash)
p options
result = str.to_yaml
result = result[4..-1] if no_header
result = result[4..-1] if options['no_header']
result
end

Expand Down
10 changes: 8 additions & 2 deletions spec/kubetruth/template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,17 @@ module Kubetruth

it "produces a yaml string" do
expect(to_yaml([1, 2])).to eq("---\n- 1\n- 2\n")
# also check how liquid handles named parameters
expect(described_class.new("{{ var | to_yaml }}").render(var: [1, 2])).to eq("---\n- 1\n- 2\n")
end

it "produces header free yaml" do
expect(to_yaml([1, 2], true)).to eq("- 1\n- 2\n")
expect(to_yaml({"foo" => "bar"}, true)).to eq("foo: bar\n")
expect(to_yaml([1, 2], "no_header" => false)).to eq("---\n- 1\n- 2\n")
expect(to_yaml([1, 2], "no_header" => true)).to eq("- 1\n- 2\n")
expect(to_yaml({"foo" => "bar"}, "no_header" => true)).to eq("foo: bar\n")
# also verify that liquid handles named parameters
expect(described_class.new("{{ var | to_yaml: no_header: true}}").render(var: [1, 2])).to eq("- 1\n- 2\n")
expect(described_class.new("{{ var | to_yaml: no_header: false}}").render(var: [1, 2])).to eq("---\n- 1\n- 2\n")
end

end
Expand Down

0 comments on commit 9fef257

Please sign in to comment.