Skip to content

Commit

Permalink
Merge pull request #1 from tampakrap/content_not_empty
Browse files Browse the repository at this point in the history
Make :content mandatory
  • Loading branch information
vikraman committed Feb 1, 2015
2 parents dc03523 + 05ad391 commit 4d3b777
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ This will create a file `/etc/facter/facts.d/env.txt` containing:

environment=production

If the `$target` is not specified, it defaults to the name of the resource.
* `content is mandatory. It can not be empty string or whitespace.
* `$target` is optional. It defaults to the name of the resource.
* `ensure` is optional. It defaults to `present`.
3 changes: 2 additions & 1 deletion lib/puppet/type/fact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@

newproperty(:content) do
desc "The content of the fact"
defaultto ''
validate do |value|
raise ArgumentError, "Content cannot be whitespace" if value.match(/^\s+$/)
fail("Content cannot be empty or whitespace") if value.match(/^\s*$/)
end
end

Expand Down
12 changes: 10 additions & 2 deletions spec/unit/type/fact_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@
end

context "when validating content" do
it "should be mandatory" do
expect { described_class.new(:name => "environment") }.to raise_error
end

it "should reject empty string" do
expect { described_class.new(:name => "environment", :content => "") }.to raise_error
end

it "should reject only whitespace" do
expect { described_class.new(:name => "environment", :content => " ") }.to raise_error
end
Expand All @@ -30,7 +38,7 @@
end

context "when validating target" do
resource = described_class.new(:name => "environment")
resource = described_class.new(:name => "environment", :content => "production")

it "can be optional" do
expect { resource }.to_not raise_error
Expand All @@ -41,7 +49,7 @@
end

it "should accept specified values" do
described_class.new(:name => "environment", :target => "env").should(:target).should == "env"
described_class.new(:name => "environment", :content => "production", :target => "env").should(:target).should == "env"
end
end

Expand Down

0 comments on commit 4d3b777

Please sign in to comment.