b2: fix versions and .files with no extension - fixes #5244

This commit is contained in:
Nick Craig-Wood 2021-04-19 16:48:51 +01:00
parent 82f1f7d2c4
commit 06f27384dd
2 changed files with 18 additions and 4 deletions

View file

@ -13,10 +13,21 @@ const versionFormat = "-v2006-01-02-150405.000"
var versionRegexp = regexp.MustCompile("-v\\d{4}-\\d{2}-\\d{2}-\\d{6}-\\d{3}")
// Split fileName into base and extension so that base + ext == fileName
func splitExt(fileName string) (base, ext string) {
ext = path.Ext(fileName)
base = fileName[:len(fileName)-len(ext)]
// .file splits to base == "", ext == ".file"
// so swap ext and base in this case
if ext != "" && base == "" {
base, ext = ext, base
}
return base, ext
}
// Add returns fileName modified to include t as the version
func Add(fileName string, t time.Time) string {
ext := path.Ext(fileName)
base := fileName[:len(fileName)-len(ext)]
base, ext := splitExt(fileName)
s := t.Format(versionFormat)
// Replace the '.' with a '-'
s = strings.Replace(s, ".", "-", -1)
@ -27,8 +38,7 @@ func Add(fileName string, t time.Time) string {
// If the fileName did not have a version then time.Time{} is returned along with an unmodified fileName
func Remove(fileName string) (t time.Time, fileNameWithoutVersion string) {
fileNameWithoutVersion = fileName
ext := path.Ext(fileName)
base := fileName[:len(fileName)-len(ext)]
base, ext := splitExt(fileName)
if len(base) < len(versionFormat) {
return
}

View file

@ -26,6 +26,8 @@ func TestVersionAdd(t *testing.T) {
{t0, "potato-v2001-02-03-040506-123.txt", "potato-v2001-02-03-040506-123-v1970-01-01-010101-123.txt"},
{t0, "123.!!lipps", "123-v1970-01-01-010101-123.!!lipps"},
{t1, "potato", "potato-v2001-02-03-040506-123"},
{t1, ".potato", ".potato-v2001-02-03-040506-123"},
{t1, ".potato.conf", ".potato-v2001-02-03-040506-123.conf"},
{t1, "", "-v2001-02-03-040506-123"},
} {
actual := version.Add(test.in, test.t)
@ -43,6 +45,8 @@ func TestVersionRemove(t *testing.T) {
{"potato-v1970-01-01-010101-123.txt", t0r, "potato.txt"},
{"potato-v2001-02-03-040506-123-v1970-01-01-010101-123.txt", t0r, "potato-v2001-02-03-040506-123.txt"},
{"potato-v2001-02-03-040506-123", t1, "potato"},
{".potato-v2001-02-03-040506-123", t1, ".potato"},
{".potato-v2001-02-03-040506-123.conf", t1, ".potato.conf"},
{"-v2001-02-03-040506-123", t1, ""},
{"potato-v2A01-02-03-040506-123", emptyT, "potato-v2A01-02-03-040506-123"},
{"potato-v2001-02-03-040506=123", emptyT, "potato-v2001-02-03-040506=123"},