|
| 1 | +package guard |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/donislawdev/TestingFilesGenerator/internal/core" |
| 10 | +) |
| 11 | + |
| 12 | +// Replacing somebody's file keeps the mode it had. |
| 13 | +// |
| 14 | +// The defect this closes was in the tool before the window existed, and it is |
| 15 | +// the reason the whole write path got looked at. "tfg recipe fmt -w" replaced |
| 16 | +// a recipe through a rename, and a rename moves the file it renames - mode and |
| 17 | +// all - so what survived was the temporary file's mode rather than the |
| 18 | +// original's. Measured on Linux with tools/probes/atomic-replace: a recipe |
| 19 | +// somebody had made private at 0600 came back at 0644, readable by everyone |
| 20 | +// on the machine. |
| 21 | +// |
| 22 | +// It skips on Windows and says so, rather than passing there. Windows has no |
| 23 | +// permission bits, so the question this asks cannot be put to it - and a guard |
| 24 | +// that reports success for a question it did not ask is worse than one that |
| 25 | +// stays away. |
| 26 | +func TestReplacingAFileKeepsTheModeItHad(t *testing.T) { |
| 27 | + if os.Getenv("GOOS") == "windows" || filepath.Separator == '\\' { |
| 28 | + t.Skip("Windows has no permission bits, so there is no mode to keep - measured on Linux instead") |
| 29 | + } |
| 30 | + |
| 31 | + path := filepath.Join(t.TempDir(), "recipe.yaml") |
| 32 | + if err := os.WriteFile(path, []byte("version: 1\n"), 0o600); err != nil { |
| 33 | + t.Fatal(err) |
| 34 | + } |
| 35 | + |
| 36 | + if err := core.ReplaceFile(path, []byte("version: 2\n")); err != nil { |
| 37 | + t.Fatalf("replacing a writable file failed: %v", err) |
| 38 | + } |
| 39 | + |
| 40 | + info, err := os.Stat(path) |
| 41 | + if err != nil { |
| 42 | + t.Fatal(err) |
| 43 | + } |
| 44 | + if got := info.Mode().Perm(); got != 0o600 { |
| 45 | + t.Errorf("the file was %v before and is %v after, so a private recipe came back readable by others", |
| 46 | + os.FileMode(0o600), got) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +// A file marked read only is left alone, on every system. |
| 51 | +// |
| 52 | +// Measured on both, because they disagree: a rename asks for permission on the |
| 53 | +// DIRECTORY rather than on the file, so read only stops the replacement on |
| 54 | +// Windows and does not stop it on Linux. Refusing everywhere is the rule this |
| 55 | +// project already applies to file names - a recipe that behaves differently on |
| 56 | +// a colleague's machine is worse than one refused on all of them. |
| 57 | +// |
| 58 | +// Both halves are checked, because refusing while having already written is |
| 59 | +// the failure that looks like success: the error is reported, and the file is |
| 60 | +// gone anyway. |
| 61 | +func TestAReadOnlyFileIsLeftAlone(t *testing.T) { |
| 62 | + dir := t.TempDir() |
| 63 | + path := filepath.Join(dir, "recipe.yaml") |
| 64 | + const was = "version: 1\n" |
| 65 | + if err := os.WriteFile(path, []byte(was), 0o444); err != nil { |
| 66 | + t.Fatal(err) |
| 67 | + } |
| 68 | + defer os.Chmod(path, 0o644) |
| 69 | + |
| 70 | + err := core.ReplaceFile(path, []byte("version: 2\n")) |
| 71 | + if err == nil { |
| 72 | + t.Fatal("a read only file was replaced without complaint") |
| 73 | + } |
| 74 | + |
| 75 | + // The refusal says what to do about it, which is D6 rather than politeness. |
| 76 | + for _, want := range []string{"read only", "writable"} { |
| 77 | + if !strings.Contains(err.Error(), want) { |
| 78 | + t.Errorf("the refusal does not mention %q. It says: %s", want, err) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + now, readErr := os.ReadFile(path) |
| 83 | + if readErr != nil { |
| 84 | + t.Fatal(readErr) |
| 85 | + } |
| 86 | + if string(now) != was { |
| 87 | + t.Errorf("the file was refused and changed anyway: %q", string(now)) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +// Nothing is left beside the file, whatever happened. |
| 92 | +// |
| 93 | +// The half written copy sits next to the target rather than in the system |
| 94 | +// temporary directory, because a rename across volumes is not one operation. |
| 95 | +// That puts it in somebody's repository, so it has to go - on the way out of |
| 96 | +// every path, not only the happy one. The version this replaced cleaned up |
| 97 | +// after a failed rename and not after a failed write. |
| 98 | +func TestReplacingLeavesNoHalfWrittenCopyBehind(t *testing.T) { |
| 99 | + for _, c := range []struct { |
| 100 | + what string |
| 101 | + build func(t *testing.T, dir string) string |
| 102 | + }{ |
| 103 | + {"a replacement that worked", func(t *testing.T, dir string) string { |
| 104 | + path := filepath.Join(dir, "fine.yaml") |
| 105 | + if err := os.WriteFile(path, []byte("version: 1\n"), 0o644); err != nil { |
| 106 | + t.Fatal(err) |
| 107 | + } |
| 108 | + return path |
| 109 | + }}, |
| 110 | + {"one refused before anything was created", func(t *testing.T, dir string) string { |
| 111 | + path := filepath.Join(dir, "locked.yaml") |
| 112 | + if err := os.WriteFile(path, []byte("version: 1\n"), 0o444); err != nil { |
| 113 | + t.Fatal(err) |
| 114 | + } |
| 115 | + t.Cleanup(func() { os.Chmod(path, 0o644) }) |
| 116 | + return path |
| 117 | + }}, |
| 118 | + // The one that matters, and the one the first version of this guard |
| 119 | + // did not have: the copy is written and THEN the rename fails. Without |
| 120 | + // a case that gets that far, this test passed on a writer that never |
| 121 | + // cleaned up, because the only failure it tried refuses before there |
| 122 | + // is anything to clean. |
| 123 | + // |
| 124 | + // A directory with something in it is the portable way to make a |
| 125 | + // rename fail. Holding the target open does it on Windows and not on |
| 126 | + // Linux, and a full disk cannot be arranged in a test. |
| 127 | + {"one where the rename failed after the copy was written", func(t *testing.T, dir string) string { |
| 128 | + path := filepath.Join(dir, "occupied") |
| 129 | + if err := os.MkdirAll(filepath.Join(path, "child"), 0o755); err != nil { |
| 130 | + t.Fatal(err) |
| 131 | + } |
| 132 | + return path |
| 133 | + }}, |
| 134 | + } { |
| 135 | + t.Run(c.what, func(t *testing.T) { |
| 136 | + dir := t.TempDir() |
| 137 | + path := c.build(t, dir) |
| 138 | + |
| 139 | + _ = core.ReplaceFile(path, []byte("version: 2\n")) |
| 140 | + |
| 141 | + leftovers, err := filepath.Glob(filepath.Join(dir, "*"+".tfg-writing")) |
| 142 | + if err != nil { |
| 143 | + t.Fatal(err) |
| 144 | + } |
| 145 | + if len(leftovers) != 0 { |
| 146 | + t.Errorf("after %s there is still %v beside the file", c.what, leftovers) |
| 147 | + } |
| 148 | + }) |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +// The case above is only worth having if the middle of it is really reached. |
| 153 | +// |
| 154 | +// A test that arranges a failure it never triggers reports a clean directory |
| 155 | +// because nothing ever happened in it - which is the same shape as a guard |
| 156 | +// that passes without reaching the code. So this asks the arrangement itself: |
| 157 | +// does replacing onto a non empty directory actually fail. |
| 158 | +func TestTheRenameFailureThatGuardIsBuiltOnReallyHappens(t *testing.T) { |
| 159 | + dir := t.TempDir() |
| 160 | + path := filepath.Join(dir, "occupied") |
| 161 | + if err := os.MkdirAll(filepath.Join(path, "child"), 0o755); err != nil { |
| 162 | + t.Fatal(err) |
| 163 | + } |
| 164 | + |
| 165 | + if err := core.ReplaceFile(path, []byte("version: 2\n")); err == nil { |
| 166 | + t.Fatal("replacing onto a non empty directory succeeded, so the guard above never reaches the cleanup it exists for") |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +// The content that arrives is the content that was asked for. |
| 171 | +// |
| 172 | +// The plain case, and it is here because everything else in this file is about |
| 173 | +// a refusal - a writer that refuses correctly and writes the wrong bytes would |
| 174 | +// pass all of them. |
| 175 | +func TestReplacingPutsTheContentThatWasAskedFor(t *testing.T) { |
| 176 | + path := filepath.Join(t.TempDir(), "recipe.yaml") |
| 177 | + if err := os.WriteFile(path, []byte("version: 1\n"), 0o644); err != nil { |
| 178 | + t.Fatal(err) |
| 179 | + } |
| 180 | + |
| 181 | + const want = "version: 2\ntargets: []\n" |
| 182 | + if err := core.ReplaceFile(path, []byte(want)); err != nil { |
| 183 | + t.Fatalf("replacing failed: %v", err) |
| 184 | + } |
| 185 | + |
| 186 | + got, err := os.ReadFile(path) |
| 187 | + if err != nil { |
| 188 | + t.Fatal(err) |
| 189 | + } |
| 190 | + if string(got) != want { |
| 191 | + t.Errorf("the file holds %q and should hold %q", string(got), want) |
| 192 | + } |
| 193 | +} |
0 commit comments