| 12 | ) |
| 13 | |
| 14 | func TestRootCreate(t *testing.T) { |
| 15 | var preservedMode os.FileMode |
| 16 | |
| 17 | tests := []struct { |
| 18 | name string |
| 19 | path string |
| 20 | replace bool |
| 21 | setup func(t *testing.T, root, outside string) |
| 22 | wantErr string |
| 23 | verify func(t *testing.T, root, outside string) |
| 24 | }{ |
| 25 | { |
| 26 | name: "creates nested file", |
| 27 | path: "nested/file.txt", |
| 28 | verify: func(t *testing.T, root, _ string) { |
| 29 | t.Helper() |
| 30 | content, err := os.ReadFile(filepath.Join(root, "nested", "file.txt")) |
| 31 | require.NoError(t, err) |
| 32 | assert.Equal(t, "new", string(content)) |
| 33 | }, |
| 34 | }, |
| 35 | { |
| 36 | name: "rejects root directory as file", |
| 37 | path: ".", |
| 38 | wantErr: "root directory", |
| 39 | }, |
| 40 | { |
| 41 | name: "rejects lexical escape", |
| 42 | path: "../file.txt", |
| 43 | wantErr: "not a local child path", |
| 44 | }, |
| 45 | { |
| 46 | name: "rejects trailing separator", |
| 47 | path: "nested" + string(os.PathSeparator), |
| 48 | wantErr: "ends in a separator", |
| 49 | verify: func(t *testing.T, root, _ string) { |
| 50 | t.Helper() |
| 51 | _, err := os.Lstat(filepath.Join(root, "nested")) |
| 52 | require.True(t, os.IsNotExist(err)) |
| 53 | assert.NoFileExists(t, filepath.Join(root, "nested", "nested")) |
| 54 | }, |
| 55 | }, |
| 56 | { |
| 57 | name: "rejects ancestor symlink", |
| 58 | path: "nested/file.txt", |
| 59 | setup: func(t *testing.T, root, outside string) { |
| 60 | t.Helper() |
| 61 | require.NoError(t, os.Symlink(outside, filepath.Join(root, "nested"))) |
| 62 | }, |
| 63 | wantErr: "symbolic link", |
| 64 | verify: func(t *testing.T, _, outside string) { |
| 65 | t.Helper() |
| 66 | assert.NoFileExists(t, filepath.Join(outside, "file.txt")) |
| 67 | }, |
| 68 | }, |
| 69 | { |
| 70 | name: "no clobber rejects dangling final symlink", |
| 71 | path: "file.txt", |