|
| 1 | +/* |
| 2 | + * Copyright 2026 Han Li and contributors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package util |
| 18 | + |
| 19 | +import ( |
| 20 | + "errors" |
| 21 | + "os" |
| 22 | + "path/filepath" |
| 23 | + "syscall" |
| 24 | + "testing" |
| 25 | +) |
| 26 | + |
| 27 | +func TestMovePathFallsBackOnCrossDeviceRename(t *testing.T) { |
| 28 | + oldRenamePath := renamePath |
| 29 | + renamePath = func(oldPath, newPath string) error { |
| 30 | + return &os.LinkError{Op: "rename", Old: oldPath, New: newPath, Err: syscall.EXDEV} |
| 31 | + } |
| 32 | + t.Cleanup(func() { |
| 33 | + renamePath = oldRenamePath |
| 34 | + }) |
| 35 | + |
| 36 | + srcDir := filepath.Join(t.TempDir(), "src") |
| 37 | + dstDir := filepath.Join(t.TempDir(), "dst") |
| 38 | + nestedDir := filepath.Join(srcDir, "nested") |
| 39 | + |
| 40 | + if err := os.MkdirAll(nestedDir, 0755); err != nil { |
| 41 | + t.Fatalf("failed to create source directory: %v", err) |
| 42 | + } |
| 43 | + if err := os.WriteFile(filepath.Join(srcDir, "main.lua"), []byte("print('hello')\n"), 0644); err != nil { |
| 44 | + t.Fatalf("failed to write root file: %v", err) |
| 45 | + } |
| 46 | + if err := os.WriteFile(filepath.Join(nestedDir, "config.txt"), []byte("ok"), 0600); err != nil { |
| 47 | + t.Fatalf("failed to write nested file: %v", err) |
| 48 | + } |
| 49 | + |
| 50 | + if err := MovePath(srcDir, dstDir); err != nil { |
| 51 | + t.Fatalf("MovePath returned error: %v", err) |
| 52 | + } |
| 53 | + |
| 54 | + if _, err := os.Stat(srcDir); !errors.Is(err, os.ErrNotExist) { |
| 55 | + t.Fatalf("expected source to be removed, got err=%v", err) |
| 56 | + } |
| 57 | + |
| 58 | + content, err := os.ReadFile(filepath.Join(dstDir, "main.lua")) |
| 59 | + if err != nil { |
| 60 | + t.Fatalf("failed to read moved root file: %v", err) |
| 61 | + } |
| 62 | + if string(content) != "print('hello')\n" { |
| 63 | + t.Fatalf("unexpected root file content: %q", content) |
| 64 | + } |
| 65 | + |
| 66 | + info, err := os.Stat(filepath.Join(dstDir, "nested", "config.txt")) |
| 67 | + if err != nil { |
| 68 | + t.Fatalf("failed to stat moved nested file: %v", err) |
| 69 | + } |
| 70 | + if info.Mode().Perm() != 0600 { |
| 71 | + t.Fatalf("expected nested file mode 0600, got %o", info.Mode().Perm()) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func TestMovePathReturnsNonCrossDeviceErrors(t *testing.T) { |
| 76 | + oldRenamePath := renamePath |
| 77 | + renamePath = func(oldPath, newPath string) error { |
| 78 | + return &os.LinkError{Op: "rename", Old: oldPath, New: newPath, Err: os.ErrPermission} |
| 79 | + } |
| 80 | + t.Cleanup(func() { |
| 81 | + renamePath = oldRenamePath |
| 82 | + }) |
| 83 | + |
| 84 | + srcFile := filepath.Join(t.TempDir(), "plugin.lua") |
| 85 | + dstFile := filepath.Join(t.TempDir(), "plugin-copy.lua") |
| 86 | + |
| 87 | + if err := os.WriteFile(srcFile, []byte("print('hello')\n"), 0644); err != nil { |
| 88 | + t.Fatalf("failed to write source file: %v", err) |
| 89 | + } |
| 90 | + |
| 91 | + err := MovePath(srcFile, dstFile) |
| 92 | + if !errors.Is(err, os.ErrPermission) { |
| 93 | + t.Fatalf("expected permission error, got %v", err) |
| 94 | + } |
| 95 | + |
| 96 | + if _, statErr := os.Stat(srcFile); statErr != nil { |
| 97 | + t.Fatalf("expected source file to remain in place, got %v", statErr) |
| 98 | + } |
| 99 | + if _, statErr := os.Stat(dstFile); !errors.Is(statErr, os.ErrNotExist) { |
| 100 | + t.Fatalf("expected destination file to be absent, got %v", statErr) |
| 101 | + } |
| 102 | +} |
0 commit comments