Skip to content

Commit bf75107

Browse files
authored
creating a new slice in several job options because appending modifies original (#809)
* creating a new slice in several job options because appending modifies original * add tests
1 parent c180381 commit bf75107

File tree

2 files changed

+59
-6
lines changed

2 files changed

+59
-6
lines changed

Diff for: job.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,7 @@ type Weekdays func() []time.Weekday
311311
// NewWeekdays provide the days of the week the job should run.
312312
func NewWeekdays(weekday time.Weekday, weekdays ...time.Weekday) Weekdays {
313313
return func() []time.Weekday {
314-
weekdays = append(weekdays, weekday)
315-
return weekdays
314+
return append([]time.Weekday{weekday}, weekdays...)
316315
}
317316
}
318317

@@ -400,8 +399,7 @@ type DaysOfTheMonth func() days
400399
// -5 == 5 days before the end of the month.
401400
func NewDaysOfTheMonth(day int, moreDays ...int) DaysOfTheMonth {
402401
return func() days {
403-
moreDays = append(moreDays, day)
404-
return moreDays
402+
return append([]int{day}, moreDays...)
405403
}
406404
}
407405

@@ -439,8 +437,7 @@ type AtTimes func() []AtTime
439437
// the job should be run
440438
func NewAtTimes(atTime AtTime, atTimes ...AtTime) AtTimes {
441439
return func() []AtTime {
442-
atTimes = append(atTimes, atTime)
443-
return atTimes
440+
return append([]AtTime{atTime}, atTimes...)
444441
}
445442
}
446443

Diff for: job_test.go

+56
Original file line numberDiff line numberDiff line change
@@ -724,3 +724,59 @@ func TestTimeFromAtTime(t *testing.T) {
724724
})
725725
}
726726
}
727+
728+
func TestNewAtTimes(t *testing.T) {
729+
at := NewAtTimes(
730+
NewAtTime(1, 1, 1),
731+
NewAtTime(2, 2, 2),
732+
)
733+
734+
var times []string
735+
for _, att := range at() {
736+
timeStr := TimeFromAtTime(att, time.UTC).Format("15:04")
737+
times = append(times, timeStr)
738+
}
739+
740+
var timesAgain []string
741+
for _, att := range at() {
742+
timeStr := TimeFromAtTime(att, time.UTC).Format("15:04")
743+
timesAgain = append(timesAgain, timeStr)
744+
}
745+
746+
assert.Equal(t, times, timesAgain)
747+
}
748+
749+
func TestNewWeekdays(t *testing.T) {
750+
wd := NewWeekdays(
751+
time.Monday,
752+
time.Tuesday,
753+
)
754+
755+
var dayStrings []string
756+
for _, w := range wd() {
757+
dayStrings = append(dayStrings, w.String())
758+
}
759+
760+
var dayStringsAgain []string
761+
for _, w := range wd() {
762+
dayStringsAgain = append(dayStringsAgain, w.String())
763+
}
764+
765+
assert.Equal(t, dayStrings, dayStringsAgain)
766+
}
767+
768+
func TestNewDaysOfTheMonth(t *testing.T) {
769+
dom := NewDaysOfTheMonth(1, 2, 3)
770+
771+
var domInts []int
772+
for _, d := range dom() {
773+
domInts = append(domInts, d)
774+
}
775+
776+
var domIntsAgain []int
777+
for _, d := range dom() {
778+
domIntsAgain = append(domIntsAgain, d)
779+
}
780+
781+
assert.Equal(t, domInts, domIntsAgain)
782+
}

0 commit comments

Comments
 (0)