MCPcopy Index your code
hub / github.com/crawshaw/jsonfile / TestFileError

Function TestFileError

jsonfile_test.go:97–129  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

95}
96
97func TestFileError(t *testing.T) {
98 t.Parallel()
99 type DB struct{ Val int }
100
101 path := filepath.Join(t.TempDir(), "tstdir", "testfserr.json")
102 os.MkdirAll(filepath.Dir(path), 0777)
103 db, err := New[DB](path)
104 if err != nil {
105 t.Fatal(err)
106 }
107
108 mustWrite(t, db, func(db *DB) { db.Val = 1 })
109
110 if err := os.Chmod(filepath.Dir(path), 0500); err != nil {
111 t.Fatal(err)
112 }
113 t.Cleanup(func() {
114 os.Chmod(filepath.Dir(path), 0700)
115 })
116
117 if err := db.Write(func(db *DB) error {
118 db.Val = 2
119 return nil
120 }); err == nil || !errors.Is(err, os.ErrPermission) {
121 t.Fatalf("Write err=%v, want %v", err, os.ErrPermission)
122 }
123
124 db.Read(func(db *DB) {
125 if db.Val != 1 {
126 t.Fatalf("Val = %d after rollback, want 1", db.Val)
127 }
128 })
129}
130
131func TestNoAlias(t *testing.T) {
132 t.Parallel()

Callers

nothing calls this directly

Calls 3

mustWriteFunction · 0.85
WriteMethod · 0.80
ReadMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…