Browse Source

parser: reject descriptors if not configured to accept them

Rob Figueiredo 6 years ago
parent
commit
46e5be361c
2 changed files with 12 additions and 1 deletions
  1. 4 1
      parser.go
  2. 8 0
      parser_test.go

+ 4 - 1
parser.go

@@ -101,8 +101,11 @@ func (p Parser) Parse(spec string) (Schedule, error) {
 		spec = strings.TrimSpace(spec[i:])
 	}
 
-	// Handle named schedules (descriptors)
+	// Handle named schedules (descriptors), if configured
 	if strings.HasPrefix(spec, "@") {
+		if p.options&Descriptor == 0 {
+			return nil, fmt.Errorf("parser does not accept descriptors: %v", spec)
+		}
 		return parseDescriptor(spec, loc)
 	}
 

+ 8 - 0
parser_test.go

@@ -350,6 +350,14 @@ func TestStandardSpecSchedule(t *testing.T) {
 	}
 }
 
+func TestNoDescriptorParser(t *testing.T) {
+	parser := NewParser(Minute | Hour)
+	_, err := parser.Parse("@every 1m")
+	if err == nil {
+		t.Error("expected an error, got none")
+	}
+}
+
 func every5min(loc *time.Location) *SpecSchedule {
 	return &SpecSchedule{1 << 0, 1 << 5, all(hours), all(dom), all(months), all(dow), loc}
 }