Merged the "freebsd_special" pattern into the other crontab records,
since its definition was incomplete.
This unfortunately violates some of the assumptions in FileParsing of
how files are constructed, and the workaround here is not very clean.

Added quite a few unit tests. Some of the expected results for these
tests include some strangeness from FileParsing about when to return
:absent versus when to return nil; the tests document the current
behaviour, even though there shouldn't be semantic differences between

Signed-off-by: Jesse Wolfe <[email protected]>
---
 lib/puppet/provider/cron/crontab.rb |   33 ++++--
 spec/unit/provider/cron/crontab.rb  |  259 +++++++++++++++++++++++++++++++++++
 2 files changed, 283 insertions(+), 9 deletions(-)
 create mode 100644 spec/unit/provider/cron/crontab.rb

diff --git a/lib/puppet/provider/cron/crontab.rb 
b/lib/puppet/provider/cron/crontab.rb
index 6dee2e5..4777b18 100755
--- a/lib/puppet/provider/cron/crontab.rb
+++ b/lib/puppet/provider/cron/crontab.rb
@@ -27,18 +27,13 @@ Puppet::Type.type(:cron).provide(:crontab,
 
     text_line :environment, :match => %r{^\w+=}
 
-    record_line :freebsd_special, :fields => %w{special command},
-        :match => %r{^@(\w+)\s+(.+)$}, :pre_gen => proc { |record|
-            record[:special] = "@" + record[:special]
-        }
-
-    crontab = record_line :crontab, :fields => %w{minute hour monthday month 
weekday command},
-        :match => %r{^\s*(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.+)$},
-        :optional => %w{minute hour weekday month monthday}, :absent => "*"
+    crontab = record_line :crontab, :fields => %w{special minute hour monthday 
month weekday command},
+        :match => 
%r{^\s*(?:@(\w+)|(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+))\s+(.+)$},
+        :optional => %w{special minute hour weekday month monthday}, :absent 
=> "*"
 
     class << crontab
         def numeric_fields
-            fields - [:command]
+            fields - [:command, :special]
         end
         # Do some post-processing of the parsed record.  Basically just
         # split the numeric fields on ','.
@@ -79,6 +74,26 @@ Puppet::Type.type(:cron).provide(:crontab,
             end
             str
         end
+
+        # Overriding the default implementation, which unfortunately wants to 
output details[:special]
+        def join(details)
+            joinchar = self.joiner
+
+            fields.collect { |field|
+                next if field == :special
+                # If the field is marked absent, use the appropriate 
replacement
+                if details[field] == :absent or details[field] == [:absent] or 
details[field].nil?
+                    if self.optional.include?(field)
+                        self.absent
+                    else
+                        raise ArgumentError, "Field '%s' is required" % field
+                    end
+                else
+                    details[field].to_s
+                end
+            }.reject { |c| c.nil?}.join(joinchar)
+        end
+
     end
 
 
diff --git a/spec/unit/provider/cron/crontab.rb 
b/spec/unit/provider/cron/crontab.rb
new file mode 100644
index 0000000..54dfb88
--- /dev/null
+++ b/spec/unit/provider/cron/crontab.rb
@@ -0,0 +1,259 @@
+#!/usr/bin/env ruby
+
+Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? 
require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") }
+
+describe Puppet::Type.type(:cron).provider(:crontab) do
+    before :each do
+        @cron_type = Puppet::Type.type(:cron)
+        @provider = @cron_type.provider(:crontab)
+    end
+
+    it "should round-trip the name as a comment for @special events" do
+        parse = @provider.parse <<-CRON
+# Puppet Name: test
+...@reboot /bin/echo > /tmp/puppet.txt
+        CRON
+        prefetch = @provider.prefetch_hook(parse)
+
+        @provider.to_line(prefetch[0]).should =~ /Puppet Name: test/
+    end
+
+    it "should round-trip the name as a comment for scheduled events" do
+        parse = @provider.parse <<-CRON
+# Puppet Name: test
+17 *   * * *   /bin/echo > /tmp/puppet.txt
+        CRON
+        prefetch = @provider.prefetch_hook(parse)
+
+        @provider.to_line(prefetch[0]).should =~ /Puppet Name: test/
+    end
+
+    it "should round-trip the schedule for scheduled events" do
+        parse = @provider.parse <<-CRON
+# Puppet Name: test
+17 *   * * *   /bin/echo > /tmp/puppet.txt
+        CRON
+        prefetch = @provider.prefetch_hook(parse)
+
+        @provider.to_line(prefetch[0]).should be_include("17 * * * * /bin/echo 
> /tmp/puppet.txt")
+    end
+
+    it "should set all of the time fields for scheduled events" do
+        resource = {
+            :record_type => :crontab,
+            :minute      => 1,
+            :hour        => 2,
+            :monthday    => 3,
+            :month       => 4,
+            :weekday     => 5,
+            :command     => "/bin/true",
+        }
+        @provider.to_line(resource).should == "1 2 3 4 5 /bin/true"
+    end
+
+    it "should output * for absent time fields" do
+        resource = {
+            :record_type => :crontab,
+            :minute      => 1,
+            :hour        => :absent,
+            :monthday    => '*',
+            # skipping month
+            :weekday     => nil,
+            :command     => "/bin/true",
+        }
+        @provider.to_line(resource).should == "1 * * * * /bin/true"
+    end
+
+    it "should output environments for scheduled events" do
+        resource = {
+            :record_type => :crontab,
+            :minute      => 1,
+            :hour        => :absent,
+            :monthday    => '*',
+            # skipping month
+            :weekday     => nil,
+            :command     => "/bin/true",
+            :environment => "ENV=1",
+            :name        => "name",
+        }
+        @provider.to_line(resource).should == "# Puppet Name: name\nENV=1\n1 * 
* * * /bin/true"
+    end
+
+    it "should output special events correctly" do
+        resource = {
+            :record_type => :crontab,
+            :special     => "reboot",
+            :minute      => 1,
+            :hour        => 2,
+            :monthday    => 3,
+            :month       => 4,
+            :weekday     => 5,
+            :command     => "/bin/true",
+        }
+        @provider.to_line(resource).should == "@reboot /bin/true"
+    end
+
+    it "should output environments for special events" do
+        resource = {
+            :record_type => :crontab,
+            :special     => "reboot",
+            :command     => "/bin/true",
+            :environment => "ENV=1",
+            :name        => "name",
+        }
+        @provider.to_line(resource).should == "# Puppet Name: 
name\nenv=...@reboot /bin/true"
+    end
+
+    it "should parse named @special events" do
+        parse = @provider.parse <<-CRON
+# Puppet Name: test
+...@reboot /bin/echo > /tmp/puppet.txt
+        CRON
+        prefetch = @provider.prefetch_hook(parse)
+
+        prefetch.should == [{
+            :record_type => :crontab,
+            :special     => "reboot",
+
+            :minute      => nil,
+            :hour        => nil,
+            :monthday    => nil,
+            :month       => nil,
+            :weekday     => nil,
+
+            :environment => :absent,
+            :name        => 'test',
+
+            :command     => "/bin/echo > /tmp/puppet.txt"
+        }]
+    end
+
+    it "should parse named @special events with an environment" do
+        parse = @provider.parse <<-CRON
+# Puppet Name: test
+VAR=environment
+...@reboot /bin/echo > /tmp/puppet.txt
+        CRON
+        prefetch = @provider.prefetch_hook(parse)
+
+        prefetch.should == [{
+            :record_type => :crontab,
+            :special     => "reboot",
+
+            :minute      => nil,
+            :hour        => nil,
+            :monthday    => nil,
+            :month       => nil,
+            :weekday     => nil,
+
+            :environment => ["VAR=environment"],
+            :name        => 'test',
+
+            :command     => "/bin/echo > /tmp/puppet.txt"
+        }]
+    end
+
+    it "should parse named scheduled events" do
+        parse = @provider.parse <<-CRON
+# Puppet Name: test
+1 2    3 4 5   /bin/echo > /tmp/puppet.txt
+        CRON
+        prefetch = @provider.prefetch_hook(parse)
+
+        prefetch.should == [{
+            :record_type => :crontab,
+            :special     => nil,
+
+            :minute      => ["1"],
+            :hour        => ["2"],
+            :monthday    => ["3"],
+            :month       => ["4"],
+            :weekday     => ["5"],
+
+            :environment => :absent,
+            :name        => 'test',
+
+            :command     => "/bin/echo > /tmp/puppet.txt"
+        }]
+    end
+
+    it "should parse named scheduled events with an environment" do
+        parse = @provider.parse <<-CRON
+# Puppet Name: test
+VAR=environment
+1 2    3 4 5   /bin/echo > /tmp/puppet.txt
+        CRON
+        prefetch = @provider.prefetch_hook(parse)
+
+        prefetch.should == [{
+            :record_type => :crontab,
+            :special     => nil,
+
+            :minute      => ["1"],
+            :hour        => ["2"],
+            :monthday    => ["3"],
+            :month       => ["4"],
+            :weekday     => ["5"],
+
+            :environment => ["VAR=environment"],
+            :name        => 'test',
+
+            :command     => "/bin/echo > /tmp/puppet.txt"
+        }]
+    end
+
+    it "should parse anonymous @special events" do
+        parse = @provider.parse <<-CRON
+...@reboot /bin/echo > /tmp/puppet.txt
+        CRON
+        prefetch = @provider.prefetch_hook(parse)
+
+        prefetch.should == [{
+            :record_type => :crontab,
+            :special     => "reboot",
+
+            :minute      => nil,
+            :hour        => nil,
+            :monthday    => nil,
+            :month       => nil,
+            :weekday     => nil,
+
+            :environment => :absent,
+
+            :command     => "/bin/echo > /tmp/puppet.txt"
+        }]
+    end
+
+    it "should parse anonymous scheduled events" do
+        parse = @provider.parse <<-CRON
+1 *    2 * 3   /bin/echo > /tmp/puppet.txt
+        CRON
+        prefetch = @provider.prefetch_hook(parse)
+
+        prefetch.should == [{
+            :record_type => :crontab,
+            :special     => nil,
+
+            :minute      => ["1"],
+            :hour        => :absent,
+            :monthday    => ["2"],
+            :month       => :absent,
+            :weekday     => ["3"],
+
+            :environment => :absent,
+
+            :command     => "/bin/echo > /tmp/puppet.txt"
+        }]
+
+    end
+
+    it "should parse non-name comments as comments" do
+        parse = @provider.parse <<-CRON
+# This is a comment
+        CRON
+        prefetch = @provider.prefetch_hook(parse)
+
+        prefetch.should == [{:line=>"# This is a comment", 
:record_type=>:comment}]
+    end
+
+end
-- 
1.6.5

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/puppet-dev?hl=en.


Reply via email to