Skip to content

Commit

Permalink
creating a new slice in several job options because appending modifie…
Browse files Browse the repository at this point in the history
…s original (#809)

* creating a new slice in several job options because appending modifies original

* add tests
  • Loading branch information
JohnRoesler authored Jan 3, 2025
1 parent c180381 commit bf75107
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 deletions.
9 changes: 3 additions & 6 deletions job.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,7 @@ type Weekdays func() []time.Weekday
// NewWeekdays provide the days of the week the job should run.
func NewWeekdays(weekday time.Weekday, weekdays ...time.Weekday) Weekdays {
return func() []time.Weekday {
weekdays = append(weekdays, weekday)
return weekdays
return append([]time.Weekday{weekday}, weekdays...)
}
}

Expand Down Expand Up @@ -400,8 +399,7 @@ type DaysOfTheMonth func() days
// -5 == 5 days before the end of the month.
func NewDaysOfTheMonth(day int, moreDays ...int) DaysOfTheMonth {
return func() days {
moreDays = append(moreDays, day)
return moreDays
return append([]int{day}, moreDays...)
}
}

Expand Down Expand Up @@ -439,8 +437,7 @@ type AtTimes func() []AtTime
// the job should be run
func NewAtTimes(atTime AtTime, atTimes ...AtTime) AtTimes {
return func() []AtTime {
atTimes = append(atTimes, atTime)
return atTimes
return append([]AtTime{atTime}, atTimes...)
}
}

Expand Down
56 changes: 56 additions & 0 deletions job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -724,3 +724,59 @@ func TestTimeFromAtTime(t *testing.T) {
})
}
}

func TestNewAtTimes(t *testing.T) {
at := NewAtTimes(
NewAtTime(1, 1, 1),
NewAtTime(2, 2, 2),
)

var times []string
for _, att := range at() {
timeStr := TimeFromAtTime(att, time.UTC).Format("15:04")
times = append(times, timeStr)
}

var timesAgain []string
for _, att := range at() {
timeStr := TimeFromAtTime(att, time.UTC).Format("15:04")
timesAgain = append(timesAgain, timeStr)
}

assert.Equal(t, times, timesAgain)
}

func TestNewWeekdays(t *testing.T) {
wd := NewWeekdays(
time.Monday,
time.Tuesday,
)

var dayStrings []string
for _, w := range wd() {
dayStrings = append(dayStrings, w.String())
}

var dayStringsAgain []string
for _, w := range wd() {
dayStringsAgain = append(dayStringsAgain, w.String())
}

assert.Equal(t, dayStrings, dayStringsAgain)
}

func TestNewDaysOfTheMonth(t *testing.T) {
dom := NewDaysOfTheMonth(1, 2, 3)

var domInts []int
for _, d := range dom() {
domInts = append(domInts, d)
}

var domIntsAgain []int
for _, d := range dom() {
domIntsAgain = append(domIntsAgain, d)
}

assert.Equal(t, domInts, domIntsAgain)
}

0 comments on commit bf75107

Please sign in to comment.