MCPcopy
hub / github.com/cli/cli / TestTryLock

Function TestTryLock

internal/flock/flock_test.go:13–99  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

11)
12
13func TestTryLock(t *testing.T) {
14 tests := []struct {
15 name string
16 setup func(t *testing.T) string // returns lock path
17 wantErr error
18 verify func(t *testing.T, f *os.File)
19 }{
20 {
21 name: "acquires lock and returns writable file handle",
22 setup: func(t *testing.T) string {
23 return filepath.Join(t.TempDir(), "test.lock")
24 },
25 verify: func(t *testing.T, f *os.File) {
26 t.Helper()
27 _, err := f.WriteString("hello")
28 require.NoError(t, err)
29 _, err = f.Seek(0, 0)
30 require.NoError(t, err)
31 buf := make([]byte, 5)
32 n, err := f.Read(buf)
33 assert.NoError(t, err)
34 assert.Equal(t, "hello", string(buf[:n]))
35 },
36 },
37 {
38 name: "creates lock file if it does not exist",
39 setup: func(t *testing.T) string {
40 dir := filepath.Join(t.TempDir(), "subdir")
41 require.NoError(t, os.MkdirAll(dir, 0o755))
42 return filepath.Join(dir, "new.lock")
43 },
44 verify: func(t *testing.T, f *os.File) {
45 t.Helper()
46 _, err := os.Stat(f.Name())
47 assert.NoError(t, err)
48 },
49 },
50 {
51 name: "second lock on same path returns ErrLocked",
52 setup: func(t *testing.T) string {
53 lockPath := filepath.Join(t.TempDir(), "contended.lock")
54 _, unlock, err := flock.TryLock(lockPath)
55 require.NoError(t, err)
56 t.Cleanup(unlock)
57 return lockPath
58 },
59 wantErr: flock.ErrLocked,
60 },
61 {
62 name: "lock succeeds after unlock",
63 setup: func(t *testing.T) string {
64 lockPath := filepath.Join(t.TempDir(), "reuse.lock")
65 _, unlock, err := flock.TryLock(lockPath)
66 require.NoError(t, err)
67 unlock()
68 return lockPath
69 },
70 },

Callers

nothing calls this directly

Calls 8

TryLockFunction · 0.92
JoinMethod · 0.80
EqualMethod · 0.80
verifyMethod · 0.80
HelperMethod · 0.65
NameMethod · 0.65
RunMethod · 0.65
ReadMethod · 0.45

Tested by

no test coverage detected