From 9cba8ec0eb36cef648bdee23e517dabec9e4dc20 Mon Sep 17 00:00:00 2001
From: Gregory Duchatelet <greg@easyflirt.com>
Date: Mon, 25 Jul 2016 17:31:50 +0200
Subject: [PATCH] Comments in variable assignment is not allowed

---
 chkcrontab_lib.py          | 8 +++++++-
 tests/test_crontab.disable | 7 ++++++-
 2 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/chkcrontab_lib.py b/chkcrontab_lib.py
index 12778cd..54cc703 100755
--- a/chkcrontab_lib.py
+++ b/chkcrontab_lib.py
@@ -681,6 +681,11 @@ class CronLineAssignment(object):
                    'Variable assignments in crontabs are not like shell.'
                    '  $VAR is not expanded.')
 
+    if re.match('".+" ?#', self.variable) or re.match('[^"].*#', self.variable):
+      log.LineError(log.MSG_COMMENT,
+                   'Variable assignments in crontabs are not like shell.'
+                   '  # comment is not allowed.')
+
 
 class CronLineTimeAction(object):
   """Checks cron lines that specify a time and an action.
@@ -881,7 +886,8 @@ class LogCounter(object):
                     'QUOTE_VALUES',
                     'SHELL_VAR',
                     'USER_NOT_FOUND',
-                    'HOURS_NOT_MINUTES'))
+                    'HOURS_NOT_MINUTES',
+                    'COMMENT'))
 
   def __init__(self):
     """Inits LogCounter."""
diff --git a/tests/test_crontab.disable b/tests/test_crontab.disable
index 6fac9b1..9808b33 100644
--- a/tests/test_crontab.disable
+++ b/tests/test_crontab.disable
@@ -24,6 +24,11 @@ OK_EMPTY=""
 BAD_SPACE= 
 OK_SPACE=" "
 
+# disable fail 1 for comment in variable assignment.
+NO_COMMENT="something" # comment
+NO_COMMENT=something # comment
+NO_COMMENT="something # comment"
+
 # disable warn 1 for bad time spec.
 * 3 * * * root Warn for hours not minutes
 # disable fail 1 for bad time spec.
@@ -74,7 +79,7 @@ OK_SPACE=" "
 1,4,6,*/5 */3,2,7 * * mOn-Fri root Good Day Range
 1,4,6,*/5 */3,2,7 * * mOn-Fri/2 root Good Day Range Step
 # chkcrontab: enable-msg=FIELD_VALUE_ERROR
-# FAIL 1 for bad time spec.
+# FAIL 3 for bad time spec.
 1,4,6,*/5 */3,2,7 * * mOn/2 root Bad Day Step
 1,4,6,*/5 */3,2,7 * * mOn-Fri/8 root Good Day Range Step
 # disable warn 1 for probable missing user.
-- 
GitLab