|
17 | 17 | package util |
18 | 18 |
|
19 | 19 | import ( |
| 20 | + "fmt" |
20 | 21 | "os" |
21 | | - "path" |
| 22 | + "os/exec" |
| 23 | + "path/filepath" |
| 24 | + "runtime" |
| 25 | + "strings" |
22 | 26 | "testing" |
23 | 27 |
|
24 | 28 | "github.com/stretchr/testify/assert" |
| 29 | + "github.com/stretchr/testify/require" |
25 | 30 |
|
26 | 31 | "github.com/algorand/go-algorand/test/partitiontest" |
27 | 32 | ) |
28 | 33 |
|
29 | 34 | func TestIsEmpty(t *testing.T) { |
30 | 35 | partitiontest.PartitionTest(t) |
| 36 | + t.Parallel() |
31 | 37 |
|
32 | | - testPath := path.Join(os.TempDir(), "this", "is", "a", "long", "path") |
| 38 | + testPath := filepath.Join(t.TempDir(), "this", "is", "a", "long", "path") |
33 | 39 | err := os.MkdirAll(testPath, os.ModePerm) |
34 | 40 | assert.NoError(t, err) |
35 | 41 | defer os.RemoveAll(testPath) |
36 | 42 | assert.True(t, IsEmpty(testPath)) |
37 | 43 |
|
38 | | - _, err = os.Create(path.Join(testPath, "file.txt")) |
| 44 | + _, err = os.Create(filepath.Join(testPath, "file.txt")) |
39 | 45 | assert.NoError(t, err) |
40 | 46 | assert.False(t, IsEmpty(testPath)) |
41 | 47 | } |
| 48 | + |
| 49 | +func testMoveFileSimple(t *testing.T, src, dst string) { |
| 50 | + t.Helper() |
| 51 | + |
| 52 | + require.NoFileExists(t, src) |
| 53 | + require.NoFileExists(t, dst) |
| 54 | + |
| 55 | + defer os.Remove(src) |
| 56 | + defer os.Remove(dst) |
| 57 | + |
| 58 | + f, err := os.Create(src) |
| 59 | + require.NoError(t, err) |
| 60 | + |
| 61 | + _, err = f.WriteString("test file contents") |
| 62 | + require.NoError(t, err) |
| 63 | + require.NoError(t, f.Close()) |
| 64 | + |
| 65 | + err = MoveFile(src, dst) |
| 66 | + require.NoError(t, err) |
| 67 | + |
| 68 | + require.FileExists(t, dst) |
| 69 | + require.NoFileExists(t, src) |
| 70 | + |
| 71 | + dstContents, err := os.ReadFile(dst) |
| 72 | + require.NoError(t, err) |
| 73 | + assert.Equal(t, "test file contents", string(dstContents)) |
| 74 | +} |
| 75 | + |
| 76 | +func TestMoveFile(t *testing.T) { |
| 77 | + partitiontest.PartitionTest(t) |
| 78 | + t.Parallel() |
| 79 | + |
| 80 | + tmpDir := t.TempDir() |
| 81 | + |
| 82 | + src := filepath.Join(tmpDir, "src.txt") |
| 83 | + dst := filepath.Join(tmpDir, "dst.txt") |
| 84 | + testMoveFileSimple(t, src, dst) |
| 85 | +} |
| 86 | + |
| 87 | +func execCommand(t *testing.T, cmdAndArsg ...string) { |
| 88 | + t.Helper() |
| 89 | + |
| 90 | + cmd := exec.Command(cmdAndArsg[0], cmdAndArsg[1:]...) |
| 91 | + var errOutput strings.Builder |
| 92 | + cmd.Stderr = &errOutput |
| 93 | + err := cmd.Run() |
| 94 | + require.NoError(t, err, errOutput.String()) |
| 95 | +} |
| 96 | + |
| 97 | +func TestMoveFileAcrossFilesystems(t *testing.T) { |
| 98 | + partitiontest.PartitionTest(t) |
| 99 | + |
| 100 | + isLinux := strings.HasPrefix(runtime.GOOS, "linux") |
| 101 | + |
| 102 | + // Skip unless CIRCLECI or TEST_MOUNT_TMPFS is set, and we are on a linux system |
| 103 | + if !isLinux || (os.Getenv("CIRCLECI") == "" && os.Getenv("TEST_MOUNT_TMPFS") == "") { |
| 104 | + t.Skip("This test must be run on a linux system with administrator privileges") |
| 105 | + } |
| 106 | + |
| 107 | + mountDir := t.TempDir() |
| 108 | + execCommand(t, "sudo", "mount", "-t", "tmpfs", "-o", "size=1K", "tmpfs", mountDir) |
| 109 | + |
| 110 | + defer execCommand(t, "sudo", "umount", mountDir) |
| 111 | + |
| 112 | + src := filepath.Join(t.TempDir(), "src.txt") |
| 113 | + dst := filepath.Join(mountDir, "dst.txt") |
| 114 | + |
| 115 | + testMoveFileSimple(t, src, dst) |
| 116 | +} |
| 117 | + |
| 118 | +func TestMoveFileSourceDoesNotExist(t *testing.T) { |
| 119 | + partitiontest.PartitionTest(t) |
| 120 | + t.Parallel() |
| 121 | + |
| 122 | + tmpDir := t.TempDir() |
| 123 | + |
| 124 | + src := filepath.Join(tmpDir, "src.txt") |
| 125 | + dst := filepath.Join(tmpDir, "dst.txt") |
| 126 | + |
| 127 | + err := MoveFile(src, dst) |
| 128 | + var pathError *os.PathError |
| 129 | + require.ErrorAs(t, err, &pathError) |
| 130 | + require.Equal(t, "lstat", pathError.Op) |
| 131 | + require.Equal(t, src, pathError.Path) |
| 132 | +} |
| 133 | + |
| 134 | +func TestMoveFileSourceIsASymlink(t *testing.T) { |
| 135 | + partitiontest.PartitionTest(t) |
| 136 | + t.Parallel() |
| 137 | + |
| 138 | + tmpDir := t.TempDir() |
| 139 | + |
| 140 | + root := filepath.Join(tmpDir, "root.txt") |
| 141 | + src := filepath.Join(tmpDir, "src.txt") |
| 142 | + dst := filepath.Join(tmpDir, "dst.txt") |
| 143 | + |
| 144 | + _, err := os.Create(root) |
| 145 | + require.NoError(t, err) |
| 146 | + |
| 147 | + err = os.Symlink(root, src) |
| 148 | + require.NoError(t, err) |
| 149 | + |
| 150 | + // os.Rename should work in this case |
| 151 | + err = MoveFile(src, dst) |
| 152 | + require.NoError(t, err) |
| 153 | + |
| 154 | + // Undo the move |
| 155 | + require.NoError(t, MoveFile(dst, src)) |
| 156 | + |
| 157 | + // But our moveFileByCopying should fail, since we haven't implemented this case |
| 158 | + err = moveFileByCopying(src, dst) |
| 159 | + require.ErrorContains(t, err, fmt.Sprintf("cannot move source file '%s': it is not a regular file", src)) |
| 160 | +} |
| 161 | + |
| 162 | +func TestMoveFileSourceAndDestinationAreSame(t *testing.T) { |
| 163 | + partitiontest.PartitionTest(t) |
| 164 | + t.Parallel() |
| 165 | + |
| 166 | + tmpDir := t.TempDir() |
| 167 | + require.NoError(t, os.Mkdir(filepath.Join(tmpDir, "folder"), os.ModePerm)) |
| 168 | + |
| 169 | + src := filepath.Join(tmpDir, "src.txt") |
| 170 | + dst := src[:len(src)-len("src.txt")] + "folder/../src.txt" |
| 171 | + |
| 172 | + // dst refers to the same file as src, but with a different path |
| 173 | + require.NotEqual(t, src, dst) |
| 174 | + require.Equal(t, src, filepath.Clean(dst)) |
| 175 | + |
| 176 | + _, err := os.Create(src) |
| 177 | + require.NoError(t, err) |
| 178 | + |
| 179 | + // os.Rename can handle this case, but our moveFileByCopying should fail |
| 180 | + err = moveFileByCopying(src, dst) |
| 181 | + require.ErrorContains(t, err, fmt.Sprintf("cannot move source file '%s' to destination '%s': source and destination are the same file", src, dst)) |
| 182 | +} |
| 183 | + |
| 184 | +func TestMoveFileDestinationIsADirectory(t *testing.T) { |
| 185 | + partitiontest.PartitionTest(t) |
| 186 | + t.Parallel() |
| 187 | + |
| 188 | + tmpDir := t.TempDir() |
| 189 | + |
| 190 | + src := filepath.Join(tmpDir, "src.txt") |
| 191 | + dst := filepath.Join(tmpDir, "dst.txt") |
| 192 | + |
| 193 | + _, err := os.Create(src) |
| 194 | + require.NoError(t, err) |
| 195 | + |
| 196 | + err = os.Mkdir(dst, os.ModePerm) |
| 197 | + require.NoError(t, err) |
| 198 | + |
| 199 | + err = MoveFile(src, dst) |
| 200 | + require.ErrorContains(t, err, fmt.Sprintf("cannot move source file '%s' to destination '%s': destination is a directory", src, dst)) |
| 201 | +} |
0 commit comments