|
1 | 1 | package rotatelogs_test
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "fmt" |
4 | 5 | "io"
|
5 | 6 | "io/ioutil"
|
6 | 7 | "log"
|
@@ -358,3 +359,56 @@ func TestRotationGenerationalNames(t *testing.T) {
|
358 | 359 | defer rl.Close()
|
359 | 360 | })
|
360 | 361 | }
|
| 362 | + |
| 363 | +type ClockFunc func() time.Time |
| 364 | + |
| 365 | +func (f ClockFunc) Now() time.Time { |
| 366 | + return f() |
| 367 | +} |
| 368 | + |
| 369 | +func TestGHIssue23(t *testing.T) { |
| 370 | + dir, err := ioutil.TempDir("", "file-rotatelogs-generational") |
| 371 | + if !assert.NoError(t, err, `creating temporary directory should succeed`) { |
| 372 | + return |
| 373 | + } |
| 374 | + defer os.RemoveAll(dir) |
| 375 | + |
| 376 | + for _, locName := range []string{"Asia/Tokyo", "Pacific/Honolulu"} { |
| 377 | + loc, _ := time.LoadLocation(locName) |
| 378 | + tests := []struct { |
| 379 | + Expected string |
| 380 | + Clock rotatelogs.Clock |
| 381 | + }{ |
| 382 | + { |
| 383 | + Expected: filepath.Join(dir, strings.ToLower(strings.Replace(locName, "/", "_", -1)) + ".201806010000.log"), |
| 384 | + Clock: ClockFunc(func() time.Time { |
| 385 | + return time.Date(2018, 6, 1, 3, 18, 0, 0, loc) |
| 386 | + }), |
| 387 | + }, |
| 388 | + { |
| 389 | + Expected: filepath.Join(dir, strings.ToLower(strings.Replace(locName, "/", "_", -1)) + ".201712310000.log"), |
| 390 | + Clock: ClockFunc(func() time.Time { |
| 391 | + return time.Date(2017, 12, 31, 23, 52, 0, 0, loc) |
| 392 | + }), |
| 393 | + }, |
| 394 | + } |
| 395 | + for _, test := range tests { |
| 396 | + t.Run(fmt.Sprintf("location = %s, time = %s", locName, test.Clock.Now().Format(time.RFC3339)), func(t *testing.T) { |
| 397 | + template := strings.ToLower(strings.Replace(locName, "/", "_", -1)) + ".%Y%m%d%H%M.log" |
| 398 | + rl, err := rotatelogs.New( |
| 399 | + filepath.Join(dir, template), |
| 400 | + rotatelogs.WithClock(test.Clock), // we're not using WithLocation, but it's the same thing |
| 401 | + ) |
| 402 | + if !assert.NoError(t, err, "rotatelogs.New should succeed") { |
| 403 | + return |
| 404 | + } |
| 405 | + |
| 406 | + t.Logf("expected %s", test.Expected) |
| 407 | + rl.Rotate() |
| 408 | + if !assert.Equal(t, test.Expected, rl.CurrentFileName(), "file names should match") { |
| 409 | + return |
| 410 | + } |
| 411 | + }) |
| 412 | + } |
| 413 | + } |
| 414 | +} |
0 commit comments