(t *testing.T)
| 995 | } |
| 996 | |
| 997 | func TestValidateFile(t *testing.T) { |
| 998 | tests := []struct { |
| 999 | name string |
| 1000 | setup func(afs afero.Fs) |
| 1001 | file string |
| 1002 | mockPathExists func(fs afero.Fs, path string) (bool, error) |
| 1003 | expectedError error |
| 1004 | }{ |
| 1005 | { |
| 1006 | name: "File is Valid", |
| 1007 | setup: func(afs afero.Fs) { |
| 1008 | require.NoError(t, afero.WriteFile(afs, "/file.txt", []byte("content"), 0644)) |
| 1009 | }, |
| 1010 | file: "/file.txt", |
| 1011 | }, |
| 1012 | { |
| 1013 | name: "File is Empty", |
| 1014 | setup: func(afs afero.Fs) { |
| 1015 | require.NoError(t, afero.WriteFile(afs, "/emptyfile.txt", []byte(""), 0644)) |
| 1016 | }, |
| 1017 | file: "/emptyfile.txt", |
| 1018 | expectedError: ErrFileIsEmtpy, |
| 1019 | }, |
| 1020 | { |
| 1021 | name: "File Does Not Exist", |
| 1022 | setup: func(_ afero.Fs) {}, |
| 1023 | file: "/nonexistent", |
| 1024 | expectedError: ErrFileDoesNotExist, |
| 1025 | }, |
| 1026 | { |
| 1027 | name: "Path is a Directory", |
| 1028 | setup: func(afs afero.Fs) { |
| 1029 | require.NoError(t, afs.Mkdir("/directory", 0755)) |
| 1030 | }, |
| 1031 | file: "/directory", |
| 1032 | expectedError: ErrPathIsDir, |
| 1033 | }, |
| 1034 | { |
| 1035 | name: "Validate Path Error", |
| 1036 | setup: func(afs afero.Fs) {}, |
| 1037 | file: "/some/path", |
| 1038 | mockPathExists: func(fs afero.Fs, path string) (bool, error) { |
| 1039 | return false, fmt.Errorf("forced existence check error") |
| 1040 | }, |
| 1041 | expectedError: fmt.Errorf("forced existence check error"), |
| 1042 | }, |
| 1043 | } |
| 1044 | |
| 1045 | for _, test := range tests { |
| 1046 | t.Run(test.name, func(t *testing.T) { |
| 1047 | // restore the original functions after the test |
| 1048 | origPathExists := pathExists |
| 1049 | defer func() { pathExists = origPathExists }() |
| 1050 | |
| 1051 | // mock functions if needed |
| 1052 | if test.mockPathExists != nil { |
| 1053 | pathExists = test.mockPathExists |
| 1054 | } |
nothing calls this directly
no test coverage detected