github.com/rclone/rclone@v1.66.1-0.20240517100346-7b89735ae726/lib/version/version_test.go (about) 1 package version_test 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/rclone/rclone/fstest" 8 "github.com/rclone/rclone/lib/version" 9 "github.com/stretchr/testify/assert" 10 ) 11 12 var ( 13 emptyT time.Time 14 t0 = fstest.Time("1970-01-01T01:01:01.123456789Z") 15 t0r = fstest.Time("1970-01-01T01:01:01.123000000Z") 16 t1 = fstest.Time("2001-02-03T04:05:06.123000000Z") 17 ) 18 19 func TestVersionAdd(t *testing.T) { 20 for _, test := range []struct { 21 t time.Time 22 in string 23 expected string 24 }{ 25 {t0, "potato.txt", "potato-v1970-01-01-010101-123.txt"}, 26 {t0, "potato-v2001-02-03-040506-123.txt", "potato-v2001-02-03-040506-123-v1970-01-01-010101-123.txt"}, 27 {t0, "123.!!lipps", "123-v1970-01-01-010101-123.!!lipps"}, 28 {t1, "potato", "potato-v2001-02-03-040506-123"}, 29 {t1, ".potato", ".potato-v2001-02-03-040506-123"}, 30 {t1, ".potato.conf", ".potato-v2001-02-03-040506-123.conf"}, 31 {t1, "", "-v2001-02-03-040506-123"}, 32 } { 33 actual := version.Add(test.in, test.t) 34 assert.Equal(t, test.expected, actual, test.in) 35 } 36 } 37 38 func TestVersionRemove(t *testing.T) { 39 for _, test := range []struct { 40 in string 41 expectedT time.Time 42 expectedRemote string 43 }{ 44 {"potato.txt", emptyT, "potato.txt"}, 45 {"potato-v1970-01-01-010101-123.txt", t0r, "potato.txt"}, 46 {"potato-v2001-02-03-040506-123-v1970-01-01-010101-123.txt", t0r, "potato-v2001-02-03-040506-123.txt"}, 47 {"potato-v2001-02-03-040506-123", t1, "potato"}, 48 {".potato-v2001-02-03-040506-123", t1, ".potato"}, 49 {".potato-v2001-02-03-040506-123.conf", t1, ".potato.conf"}, 50 {"-v2001-02-03-040506-123", t1, ""}, 51 {"potato-v2A01-02-03-040506-123", emptyT, "potato-v2A01-02-03-040506-123"}, 52 {"potato-v2001-02-03-040506=123", emptyT, "potato-v2001-02-03-040506=123"}, 53 } { 54 actualT, actualRemote := version.Remove(test.in) 55 assert.Equal(t, test.expectedT, actualT, test.in) 56 assert.Equal(t, test.expectedRemote, actualRemote, test.in) 57 } 58 } 59 60 func TestVersionMatch(t *testing.T) { 61 for _, test := range []struct { 62 in string 63 expected bool 64 }{ 65 {"potato.txt", false}, 66 {"potato", false}, 67 {"", false}, 68 {"potato-v1970-01-01-010101-123.txt", true}, 69 {"potato-v2001-02-03-040506-123-v1970-01-01-010101-123.txt", true}, 70 {"potato-v2001-02-03-040506-123", true}, 71 {"-v2001-02-03-040506-123", true}, 72 {"-v9999-99-99-999999-999", true}, 73 } { 74 actual := version.Match(test.in) 75 assert.Equal(t, test.expected, actual, test.in) 76 } 77 }