Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
Loading