2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-22 10:58:55 +00:00

Merge pull request #3862 from restic/3861-forget-invalid-policy

forget: Error when invalid unit is given in duration policy
This commit is contained in:
MichaelEischer 2022-08-18 20:45:26 +02:00 committed by GitHub
commit 4cccffab58
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 14 deletions

View File

@ -0,0 +1,9 @@
Bugfix: Yield error on invalid policy to `forget`
The `forget` command previously silently ignored invalid/unsupported
units in the duration options, such as e.g. `--keep-within-daily 2w`.
Specifying an invalid/unsupported duration unit now results in an error.
https://github.com/restic/restic/issues/3861
https://github.com/restic/restic/pull/3862

View File

@ -76,8 +76,7 @@ func nextNumber(input string) (num int, rest string, err error) {
return num, rest, nil return num, rest, nil
} }
// ParseDuration parses a duration from a string. The format is: // ParseDuration parses a duration from a string. The format is `6y5m234d37h`
// 6y5m234d37h
func ParseDuration(s string) (Duration, error) { func ParseDuration(s string) (Duration, error) {
var ( var (
d Duration d Duration
@ -106,6 +105,8 @@ func ParseDuration(s string) (Duration, error) {
d.Days = num d.Days = num
case 'h': case 'h':
d.Hours = num d.Hours = num
default:
return Duration{}, errors.Errorf("invalid unit %q found after number %d", s[0], num)
} }
s = s[1:] s = s[1:]

View File

@ -63,24 +63,34 @@ func TestParseDuration(t *testing.T) {
input string input string
d Duration d Duration
output string output string
err bool
}{ }{
{"9h", Duration{Hours: 9}, "9h"}, {input: "9h", d: Duration{Hours: 9}, output: "9h"},
{"3d", Duration{Days: 3}, "3d"}, {input: "3d", d: Duration{Days: 3}, output: "3d"},
{"4d2h", Duration{Days: 4, Hours: 2}, "4d2h"}, {input: "4d2h", d: Duration{Days: 4, Hours: 2}, output: "4d2h"},
{"7m5d", Duration{Months: 7, Days: 5}, "7m5d"}, {input: "7m5d", d: Duration{Months: 7, Days: 5}, output: "7m5d"},
{"6m4d8h", Duration{Months: 6, Days: 4, Hours: 8}, "6m4d8h"}, {input: "6m4d8h", d: Duration{Months: 6, Days: 4, Hours: 8}, output: "6m4d8h"},
{"5d7m", Duration{Months: 7, Days: 5}, "7m5d"}, {input: "5d7m", d: Duration{Months: 7, Days: 5}, output: "7m5d"},
{"4h3d9m", Duration{Months: 9, Days: 3, Hours: 4}, "9m3d4h"}, {input: "4h3d9m", d: Duration{Months: 9, Days: 3, Hours: 4}, output: "9m3d4h"},
{"-7m5d", Duration{Months: -7, Days: 5}, "-7m5d"}, {input: "-7m5d", d: Duration{Months: -7, Days: 5}, output: "-7m5d"},
{"1y4m-5d-3h", Duration{Years: 1, Months: 4, Days: -5, Hours: -3}, "1y4m-5d-3h"}, {input: "1y4m-5d-3h", d: Duration{Years: 1, Months: 4, Days: -5, Hours: -3}, output: "1y4m-5d-3h"},
{"2y7m-5d", Duration{Years: 2, Months: 7, Days: -5}, "2y7m-5d"}, {input: "2y7m-5d", d: Duration{Years: 2, Months: 7, Days: -5}, output: "2y7m-5d"},
{input: "2w", err: true},
{input: "1y4m3w1d", err: true},
{input: "s", err: true},
} }
for _, test := range tests { for _, test := range tests {
t.Run("", func(t *testing.T) { t.Run("", func(t *testing.T) {
d, err := ParseDuration(test.input) d, err := ParseDuration(test.input)
if err != nil { if test.err {
t.Fatal(err) if err == nil {
t.Fatalf("Missing error for %v", test.input)
}
} else {
if err != nil {
t.Fatal(err)
}
} }
if !cmp.Equal(d, test.d) { if !cmp.Equal(d, test.d) {