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

forget: Error when invalid unit is given in duration policy

This commit is contained in:
Leo R. Lundgren 2022-08-10 00:38:53 +02:00
parent f0bb4f8708
commit 6f517858e8
3 changed files with 25 additions and 11 deletions

View File

@ -0,0 +1,8 @@
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

View File

@ -106,6 +106,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,23 +63,27 @@ 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 err != nil && !test.err {
t.Fatal(err) t.Fatal(err)
} }