TestAtomicWriteFile tests the happy path of AtomicWriteFile function. It verifies that the function correctly writes content to a file with the specified mode.
(t *testing.T)
| 27 | // TestAtomicWriteFile tests the happy path of AtomicWriteFile function. |
| 28 | // It verifies that the function correctly writes content to a file with the specified mode. |
| 29 | func TestAtomicWriteFile(t *testing.T) { |
| 30 | dir := t.TempDir() |
| 31 | |
| 32 | testpath := filepath.Join(dir, "test") |
| 33 | stringContent := "Test content" |
| 34 | reader := bytes.NewReader([]byte(stringContent)) |
| 35 | mode := os.FileMode(0644) |
| 36 | |
| 37 | err := AtomicWriteFile(testpath, reader, mode) |
| 38 | if err != nil { |
| 39 | t.Errorf("AtomicWriteFile error: %s", err) |
| 40 | } |
| 41 | |
| 42 | got, err := os.ReadFile(testpath) |
| 43 | if err != nil { |
| 44 | t.Fatal(err) |
| 45 | } |
| 46 | |
| 47 | if stringContent != string(got) { |
| 48 | t.Fatalf("expected: %s, got: %s", stringContent, string(got)) |
| 49 | } |
| 50 | |
| 51 | gotinfo, err := os.Stat(testpath) |
| 52 | if err != nil { |
| 53 | t.Fatal(err) |
| 54 | } |
| 55 | |
| 56 | if mode != gotinfo.Mode() { |
| 57 | t.Fatalf("expected %s: to be the same mode as %s", |
| 58 | mode, gotinfo.Mode()) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | // TestAtomicWriteFile_CreateTempError tests the error path when os.CreateTemp fails |
| 63 | func TestAtomicWriteFile_CreateTempError(t *testing.T) { |
nothing calls this directly
no test coverage detected
searching dependent graphs…