| 845 | } |
| 846 | |
| 847 | func TestFileMapperWithDefaultNonExistentFile(t *testing.T) { |
| 848 | type CLI struct { |
| 849 | File *os.File `default:"testdata/missing-default.txt"` |
| 850 | } |
| 851 | var cli CLI |
| 852 | p := mustNew(t, &cli) |
| 853 | |
| 854 | // Test 1: With CLI arg pointing to existing file - should succeed |
| 855 | _, err := p.Parse([]string{"--file", "testdata/file.txt"}) |
| 856 | assert.NoError(t, err) |
| 857 | assert.NotZero(t, cli.File) |
| 858 | assert.Contains(t, cli.File.Name(), "file.txt") |
| 859 | _ = cli.File.Close() |
| 860 | |
| 861 | // Test 2: Without CLI arg - should fail because default file doesn't exist |
| 862 | p = mustNew(t, &cli) |
| 863 | _, err = p.Parse([]string{}) |
| 864 | assert.Error(t, err) |
| 865 | assert.Contains(t, err.Error(), "missing-default.txt") |
| 866 | assert.IsError(t, err, os.ErrNotExist) |
| 867 | } |