github.com/v2pro/plz@v0.0.0-20221028024117-e5f9aec5b631/countlog/output/rotation/name_by_time_test.go (about)

     1  package rotation
     2  
     3  import (
     4  	"testing"
     5  	"github.com/v2pro/plz/test"
     6  	"github.com/v2pro/plz/countlog"
     7  	"github.com/v2pro/plz/test/must"
     8  	"os"
     9  	"io/ioutil"
    10  	"time"
    11  	"github.com/v2pro/plz/clock"
    12  )
    13  
    14  func Test_NameByTime(t *testing.T) {
    15  	t.Run("next file", test.Case(func(ctx *countlog.Context) {
    16  		namer := &NameByTime{
    17  			Directory: "/tmp",
    18  			Pattern: "test-2006.log",
    19  		}
    20  		defer clock.ResetNow()
    21  		clock.Now = func() time.Time {
    22  			return time.Unix(0, 0)
    23  		}
    24  		newPath := must.Call(namer.NextFile)[0]
    25  		must.Equal("/tmp/test-1970.log", newPath)
    26  	}))
    27  	t.Run("list files", test.Case(func(ctx *countlog.Context) {
    28  		os.RemoveAll("/tmp/testlog")
    29  		os.MkdirAll("/tmp/testlog", 0755)
    30  		ioutil.WriteFile("/tmp/testlog/test-2016.log", []byte{}, 0644)
    31  		ioutil.WriteFile("/tmp/testlog/test-2015.log", []byte{}, 0644)
    32  		ioutil.WriteFile("/tmp/testlog/test-abc.log", []byte{}, 0644)
    33  		namer := &NameByTime{
    34  			Directory: "/tmp/testlog",
    35  			Pattern: "test-2006.log",
    36  		}
    37  		files := must.Call(namer.ListFiles)[0]
    38  		must.Equal([]string{
    39  			"/tmp/testlog/test-2015.log",
    40  			"/tmp/testlog/test-2016.log",
    41  		}, files)
    42  	}))
    43  }