@@ -86,6 +86,14 @@ func TestDeleteLine(t *testing.T) {
8686 {"#\n #\n #\n 00,30 * * * * cd /workdir && /home/resticprofile --no-ansi --config \" config.yaml\" run-schedule backup@profile\n " , true },
8787 {"#\n #\n #\n 00,30 * * * * user cd /workdir && /home/resticprofile --no-ansi --config config.yaml run-schedule backup@profile\n " , true },
8888 {"#\n #\n #\n 00,30 * * * * user cd /workdir && /home/resticprofile --no-ansi --config \" config.yaml\" run-schedule backup@profile\n " , true },
89+ {"#\n #\n #\n # 00,30 * * * * test $(date '+\\ %w') -eq 2 || test $(date '+\\ %w') -eq 3 && /home/resticprofile --no-ansi --config \" config.yaml\" run-schedule backup@profile\n " , false },
90+ {"#\n #\n #\n # 00,30 * * * * test $(date '+\\ %w') -eq 2 || test $(date '+\\ %w') -eq 3 && cd /workdir && /home/resticprofile --no-ansi --config \" config.yaml\" run-schedule backup@profile\n " , false },
91+ {"#\n #\n #\n 00,30 * * * * test $(date '+\\ %w') -eq 2 || test $(date '+\\ %w') -eq 3 && cd /workdir && /home/resticprofile --no-ansi --config \" config.yaml\" run-schedule backup@profile\n " , true },
92+ {"#\n #\n #\n 00,30 * * * * user test $(date '+\\ %w') -eq 2 || test $(date '+\\ %w') -eq 3 && cd /workdir && /home/resticprofile --no-ansi --config config.yaml run-schedule backup@profile\n " , true },
93+ {"#\n #\n #\n # 00,30 * * * * test $(date '+\\ %w') -eq 0 && /home/resticprofile --no-ansi --config \" config.yaml\" run-schedule backup@profile\n " , false },
94+ {"#\n #\n #\n # 00,30 * * * * test $(date '+\\ %w') -eq 0 && cd /workdir && /home/resticprofile --no-ansi --config \" config.yaml\" run-schedule backup@profile\n " , false },
95+ {"#\n #\n #\n 00,30 * * * * test $(date '+\\ %w') -eq 0 && cd /workdir && /home/resticprofile --no-ansi --config \" config.yaml\" run-schedule backup@profile\n " , true },
96+ {"#\n #\n #\n 00,30 * * * * user test $(date '+\\ %w') -eq 0 && cd /workdir && /home/resticprofile --no-ansi --config config.yaml run-schedule backup@profile\n " , true },
8997 }
9098
9199 for _ , testRun := range testData {
@@ -373,6 +381,20 @@ func TestParseEntry(t *testing.T) {
373381 _ = e .Minute .AddValue (0 )
374382 _ = e .Minute .AddValue (30 )
375383 })
384+ scheduledEventOnMondays := calendar .NewEvent (func (e * calendar.Event ) {
385+ _ = e .Second .AddValue (0 )
386+ _ = e .Minute .AddValue (0 )
387+ _ = e .Minute .AddValue (30 )
388+ _ = e .WeekDay .AddValue (1 )
389+ })
390+
391+ scheduledEventOnSundaysAndWednesdays := calendar .NewEvent (func (e * calendar.Event ) {
392+ _ = e .Second .AddValue (0 )
393+ _ = e .Minute .AddValue (0 )
394+ _ = e .Minute .AddValue (30 )
395+ _ = e .WeekDay .AddValue (0 )
396+ _ = e .WeekDay .AddValue (3 )
397+ })
376398 testData := []struct {
377399 source string
378400 expectEntry * Entry
@@ -417,6 +439,23 @@ func TestParseEntry(t *testing.T) {
417439 source : "00,30 * * * * user cd /workdir && /home/resticprofile --no-ansi --config \" config file.yaml\" run-schedule backup@profile" ,
418440 expectEntry : & Entry {configFile : "config file.yaml" , profileName : "profile" , commandName : "backup" , user : "user" , workDir : "/workdir" , event : scheduledEvent , commandLine : "/home/resticprofile --no-ansi --config \" config file.yaml\" run-schedule backup@profile" },
419441 },
442+ // with day of week extra
443+ {
444+ source : "00,30 * * * * test $(date '+\\ %w') -eq 1 && /home/resticprofile --no-ansi --config \" config file.yaml\" run-schedule backup@profile" ,
445+ expectEntry : & Entry {configFile : "config file.yaml" , profileName : "profile" , commandName : "backup" , user : "" , event : scheduledEventOnMondays , commandLine : "/home/resticprofile --no-ansi --config \" config file.yaml\" run-schedule backup@profile" },
446+ },
447+ {
448+ source : "00,30 * * * * user test $(date '+\\ %w') -eq 0 || test $(date '+\\ %w') -eq 3 && /home/resticprofile --no-ansi --config \" config file.yaml\" run-schedule backup@profile" ,
449+ expectEntry : & Entry {configFile : "config file.yaml" , profileName : "profile" , commandName : "backup" , user : "user" , event : scheduledEventOnSundaysAndWednesdays , commandLine : "/home/resticprofile --no-ansi --config \" config file.yaml\" run-schedule backup@profile" },
450+ },
451+ {
452+ source : "00,30 * * * * test $(date '+\\ %w') -eq 0 || test $(date '+\\ %w') -eq 3 && cd /workdir && /home/resticprofile --no-ansi --config config.yaml run-schedule backup@profile" ,
453+ expectEntry : & Entry {configFile : "config.yaml" , profileName : "profile" , commandName : "backup" , workDir : "/workdir" , event : scheduledEventOnSundaysAndWednesdays , commandLine : "/home/resticprofile --no-ansi --config config.yaml run-schedule backup@profile" },
454+ },
455+ {
456+ source : "00,30 * * * * user test $(date '+\\ %w') -eq 1 && cd /workdir && /home/resticprofile --no-ansi --config \" config file.yaml\" run-schedule backup@profile" ,
457+ expectEntry : & Entry {configFile : "config file.yaml" , profileName : "profile" , commandName : "backup" , user : "user" , workDir : "/workdir" , event : scheduledEventOnMondays , commandLine : "/home/resticprofile --no-ansi --config \" config file.yaml\" run-schedule backup@profile" },
458+ },
420459 }
421460
422461 for _ , testRun := range testData {
@@ -475,3 +514,78 @@ func TestGetEntries(t *testing.T) {
475514 assert .Equal (t , "/some/bin/resticprofile --no-ansi --config config.yaml run-schedule backup@profile" , entries [0 ].commandLine )
476515 })
477516}
517+
518+ func TestAddWeekDays (t * testing.T ) {
519+ tests := []struct {
520+ name string
521+ input string
522+ expectError bool
523+ expectedDays []int
524+ }{
525+ {
526+ name : "empty string" ,
527+ input : "" ,
528+ expectError : false ,
529+ expectedDays : nil ,
530+ },
531+ {
532+ name : "single day with trailing &&" ,
533+ input : `test $(date '+\%w') -eq 2 &&` ,
534+ expectError : false ,
535+ expectedDays : []int {2 },
536+ },
537+ {
538+ name : "single day with trailing && and spaces" ,
539+ input : `test $(date '+\%w') -eq 5 && ` ,
540+ expectError : false ,
541+ expectedDays : []int {5 },
542+ },
543+ {
544+ name : "two days with trailing &&" ,
545+ input : `test $(date '+\%w') -eq 2 || test $(date '+\%w') -eq 3 &&` ,
546+ expectError : false ,
547+ expectedDays : []int {2 , 3 },
548+ },
549+ {
550+ name : "three days with trailing &&" ,
551+ input : `test $(date '+\%w') -eq 1 || test $(date '+\%w') -eq 3 || test $(date '+\%w') -eq 5 &&` ,
552+ expectError : false ,
553+ expectedDays : []int {1 , 3 , 5 },
554+ },
555+ {
556+ name : "sunday (day 0)" ,
557+ input : `test $(date '+\%w') -eq 0 &&` ,
558+ expectError : false ,
559+ expectedDays : []int {0 },
560+ },
561+ {
562+ name : "saturday (day 6)" ,
563+ input : `test $(date '+\%w') -eq 6 &&` ,
564+ expectError : false ,
565+ expectedDays : []int {6 },
566+ },
567+ {
568+ name : "invalid day out of range" ,
569+ input : `test $(date '+\%w') -eq 9 &&` ,
570+ expectError : true ,
571+ },
572+ }
573+
574+ for _ , tt := range tests {
575+ t .Run (tt .name , func (t * testing.T ) {
576+ event := calendar .NewEvent ()
577+ err := addWeekDays (tt .input , event )
578+ if tt .expectError {
579+ assert .Error (t , err )
580+ return
581+ }
582+ require .NoError (t , err )
583+ if len (tt .expectedDays ) == 0 {
584+ assert .False (t , event .WeekDay .HasValue ())
585+ } else {
586+ assert .True (t , event .WeekDay .HasValue ())
587+ assert .Equal (t , tt .expectedDays , event .WeekDay .GetRangeValues ())
588+ }
589+ })
590+ }
591+ }
0 commit comments