(t *testing.T)
| 14 | ) |
| 15 | |
| 16 | func TestNewSaveCommandErrors(t *testing.T) { |
| 17 | testCases := []struct { |
| 18 | name string |
| 19 | args []string |
| 20 | isTerminal bool |
| 21 | expectedError string |
| 22 | imageSaveFunc func(images []string, options ...client.ImageSaveOption) (client.ImageSaveResult, error) |
| 23 | }{ |
| 24 | { |
| 25 | name: "wrong args", |
| 26 | args: []string{}, |
| 27 | expectedError: "requires at least 1 argument", |
| 28 | }, |
| 29 | { |
| 30 | name: "output to terminal", |
| 31 | args: []string{"output", "file", "arg1"}, |
| 32 | isTerminal: true, |
| 33 | expectedError: "cowardly refusing to save to a terminal. Use the -o flag or redirect", |
| 34 | }, |
| 35 | { |
| 36 | name: "ImageSave fail", |
| 37 | args: []string{"arg1"}, |
| 38 | isTerminal: false, |
| 39 | expectedError: "error saving image", |
| 40 | imageSaveFunc: func(images []string, options ...client.ImageSaveOption) (client.ImageSaveResult, error) { |
| 41 | return nil, errors.New("error saving image") |
| 42 | }, |
| 43 | }, |
| 44 | { |
| 45 | name: "output directory does not exist", |
| 46 | args: []string{"-o", "fake-dir/out.tar", "arg1"}, |
| 47 | expectedError: `failed to save image: invalid output path: stat fake-dir: no such file or directory`, |
| 48 | }, |
| 49 | { |
| 50 | name: "output file is irregular", |
| 51 | args: []string{"-o", "/dev/null", "arg1"}, |
| 52 | expectedError: `failed to save image: cannot write to a character device file`, |
| 53 | }, |
| 54 | { |
| 55 | name: "invalid platform", |
| 56 | args: []string{"--platform", "<invalid>", "arg1"}, |
| 57 | expectedError: `invalid platform`, |
| 58 | }, |
| 59 | } |
| 60 | for _, tc := range testCases { |
| 61 | t.Run(tc.name, func(t *testing.T) { |
| 62 | cli := test.NewFakeCli(&fakeClient{imageSaveFunc: tc.imageSaveFunc}) |
| 63 | cli.Out().SetIsTerminal(tc.isTerminal) |
| 64 | cmd := newSaveCommand(cli) |
| 65 | cmd.SetOut(io.Discard) |
| 66 | cmd.SetErr(io.Discard) |
| 67 | cmd.SetArgs(tc.args) |
| 68 | assert.ErrorContains(t, cmd.Execute(), tc.expectedError) |
| 69 | }) |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | func TestNewSaveCommandSuccess(t *testing.T) { |
nothing calls this directly
no test coverage detected
searching dependent graphs…