test chunk name parser
(t *testing.T, f *Fs)
| 66 | |
| 67 | // test chunk name parser |
| 68 | func testChunkNameFormat(t *testing.T, f *Fs) { |
| 69 | saveOpt := f.opt |
| 70 | defer func() { |
| 71 | // restore original settings (f is pointer, f.opt is struct) |
| 72 | f.opt = saveOpt |
| 73 | _ = f.setChunkNameFormat(f.opt.NameFormat) |
| 74 | }() |
| 75 | |
| 76 | assertFormat := func(pattern, wantDataFormat, wantCtrlFormat, wantNameRegexp string) { |
| 77 | err := f.setChunkNameFormat(pattern) |
| 78 | assert.NoError(t, err) |
| 79 | assert.Equal(t, wantDataFormat, f.dataNameFmt) |
| 80 | assert.Equal(t, wantCtrlFormat, f.ctrlNameFmt) |
| 81 | assert.Equal(t, wantNameRegexp, f.nameRegexp.String()) |
| 82 | } |
| 83 | |
| 84 | assertFormatValid := func(pattern string) { |
| 85 | err := f.setChunkNameFormat(pattern) |
| 86 | assert.NoError(t, err) |
| 87 | } |
| 88 | |
| 89 | assertFormatInvalid := func(pattern string) { |
| 90 | err := f.setChunkNameFormat(pattern) |
| 91 | assert.Error(t, err) |
| 92 | } |
| 93 | |
| 94 | assertMakeName := func(wantChunkName, mainName string, chunkNo int, ctrlType, xactID string) { |
| 95 | gotChunkName := "" |
| 96 | assert.NotPanics(t, func() { |
| 97 | gotChunkName = f.makeChunkName(mainName, chunkNo, ctrlType, xactID) |
| 98 | }, "makeChunkName(%q,%d,%q,%q) must not panic", mainName, chunkNo, ctrlType, xactID) |
| 99 | if gotChunkName != "" { |
| 100 | assert.Equal(t, wantChunkName, gotChunkName) |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | assertMakeNamePanics := func(mainName string, chunkNo int, ctrlType, xactID string) { |
| 105 | assert.Panics(t, func() { |
| 106 | _ = f.makeChunkName(mainName, chunkNo, ctrlType, xactID) |
| 107 | }, "makeChunkName(%q,%d,%q,%q) should panic", mainName, chunkNo, ctrlType, xactID) |
| 108 | } |
| 109 | |
| 110 | assertParseName := func(fileName, wantMainName string, wantChunkNo int, wantCtrlType, wantXactID string) { |
| 111 | gotMainName, gotChunkNo, gotCtrlType, gotXactID := f.parseChunkName(fileName) |
| 112 | assert.Equal(t, wantMainName, gotMainName) |
| 113 | assert.Equal(t, wantChunkNo, gotChunkNo) |
| 114 | assert.Equal(t, wantCtrlType, gotCtrlType) |
| 115 | assert.Equal(t, wantXactID, gotXactID) |
| 116 | } |
| 117 | |
| 118 | const newFormatSupported = false // support for patterns not starting with base name (*) |
| 119 | |
| 120 | // valid formats |
| 121 | assertFormat(`*.rclone_chunk.###`, `%s.rclone_chunk.%03d`, `%s.rclone_chunk._%s`, `^(.+?)\.rclone_chunk\.(?:([0-9]{3,})|_([a-z][a-z0-9]{2,6}))(?:_([0-9a-z]{4,9})|\.\.tmp_([0-9]{10,13}))?$`) |
| 122 | assertFormat(`*.rclone_chunk.#`, `%s.rclone_chunk.%d`, `%s.rclone_chunk._%s`, `^(.+?)\.rclone_chunk\.(?:([0-9]+)|_([a-z][a-z0-9]{2,6}))(?:_([0-9a-z]{4,9})|\.\.tmp_([0-9]{10,13}))?$`) |
| 123 | assertFormat(`*_chunk_#####`, `%s_chunk_%05d`, `%s_chunk__%s`, `^(.+?)_chunk_(?:([0-9]{5,})|_([a-z][a-z0-9]{2,6}))(?:_([0-9a-z]{4,9})|\.\.tmp_([0-9]{10,13}))?$`) |
| 124 | assertFormat(`*-chunk-#`, `%s-chunk-%d`, `%s-chunk-_%s`, `^(.+?)-chunk-(?:([0-9]+)|_([a-z][a-z0-9]{2,6}))(?:_([0-9a-z]{4,9})|\.\.tmp_([0-9]{10,13}))?$`) |
| 125 | assertFormat(`*-chunk-#-%^$()[]{}.+-!?:\`, `%s-chunk-%d-%%^$()[]{}.+-!?:\`, `%s-chunk-_%s-%%^$()[]{}.+-!?:\`, `^(.+?)-chunk-(?:([0-9]+)|_([a-z][a-z0-9]{2,6}))-%\^\$\(\)\[\]\{\}\.\+-!\?:\\(?:_([0-9a-z]{4,9})|\.\.tmp_([0-9]{10,13}))?$`) |
no test coverage detected
searching dependent graphs…